吉安做网站的,郑州网站建设学校,12366纳税服务平台,深圳市网络营销公司功能
//硬件#xff1a;msp430f5529、dht22、LCD1602、蜂鸣器、烟雾传感器、蓝牙模块。 //功能#xff1a;读取温湿度、烟雾浓度显示到屏幕#xff1b; //按键调节三个报警数值#xff1b; //温度、湿度、烟雾浓度#xff0c;任意一个大于报警数值就蜂鸣器报警#xff1…功能
//硬件msp430f5529、dht22、LCD1602、蜂鸣器、烟雾传感器、蓝牙模块。 //功能读取温湿度、烟雾浓度显示到屏幕 //按键调节三个报警数值 //温度、湿度、烟雾浓度任意一个大于报警数值就蜂鸣器报警 //单片机将温度、湿度、烟雾浓度通过蓝牙模块发送到电脑端显示。 //电脑端也可以调节三个报警数值。
引脚 //PCF8574 LCD1602----MSP430F5529
//GND----------------GND
//VCC----------------3.3v
//SDA----------------P2.4
//SCL----------------P2.5//HC05 蓝牙----------MSP430F5529
//GND----------------GND
//VCC----------------3.3v
//RX-----------------P3.3 T
//TX-----------------P3.4 R//PM2烟雾传感器 0~3.3v电压转为ppm单位 20~20000ppm
//DHT22--------------MSP430F5529
//GND----------------GND
//VCC----------------3.3v
//DAT----------------P2.3//烟雾---------------MSP430F5529
//GND----------------GND
//VCC----------------3.3v
//A0-----------------P6.5//蜂鸣器-------------MSP430F5529
//GND----------------GND
//VCC----------------3.3v
//DAT----------------P1.6//按键----------------MSP430F5529
//KEY1----------------P1.2
//KEY2----------------P1.3
//KEY3----------------P1.4
//KEY4----------------P1.5
//GND-----------------GND//板子自己有的按键
//KEY5----------------P1.1
//KEY6----------------P2.1部分程序 unsigned char get_key(void) //声明一个函数返回值类型为 unsigned char函数名为 get_key参数列表为空
{unsigned char key; //声明一个名为 key 的无符号字符变量if ((P1IN BIT2) 0) //如果 P1IN 寄存器的第二位是 0说明按键1被按下{delay_ms(10); //延时 10 毫秒以确保读取的值稳定if ((P1IN BIT2) 0) //再次检测按键1是否被按下{key 1; //将 key 设置为 1表示按下了按键1while ((P1IN BIT2) 0)//等待按键1松开;}}else if ((P1IN BIT3) 0) //如果按键1未被按下检测按键2是否被按下{delay_ms(10); //同样进行延时if ((P1IN BIT3) 0) //检测按键2是否被按下{key 2; //将 key 设置为 2表示按下了按键2while ((P1IN BIT3) 0)//等待按键2松开;}}else if ((P1IN BIT4) 0){delay_ms(10);if ((P1IN BIT4) 0){key 3;while ((P1IN BIT4) 0);}}else if ((P1IN BIT5) 0){delay_ms(10);if ((P1IN BIT5) 0){key 4;while ((P1IN BIT5) 0);}}else if ((P1IN BIT1) 0){delay_ms(10);if ((P1IN BIT1) 0){key 5;while ((P1IN BIT1) 0);}}else if ((P2IN BIT1) 0){delay_ms(10);if ((P2IN BIT1) 0){key 6;while ((P2IN BIT1) 0);}}else{key 0;}return key;
}u8 data[5];
u16 shidu, wendu, yanwu;
u16 shidu_baojing 900, wendu_baojing 400, yanwu_baojing 10000;
u8 disp[20];
u8 count 0;
u8 timecnt 0;
u8 keyvalue 0;void disp_line1(void) // 定义名为disp_line1的函数
{count 0; // 初始化计数器disp[count] wendu % 1000 / 100 0; // 将温度百位数字转换为字符存入显示缓存数组计数器1disp[count] wendu % 100 / 10 0; // 将温度十位数字转换为字符存入显示缓存数组计数器1disp[count] C; // 存入字符C表示摄氏度计数器1disp[count] ; // 存入空格字符计数器1disp[count] shidu % 1000 / 100 0; // 将湿度百位数字转换为字符存入显示缓存数组计数器1disp[count] shidu % 100 / 10 0; // 将湿度十位数字转换为字符存入显示缓存数组计数器1disp[count] %; // 存入字符%表示百分比符号计数器1disp[count] ; // 存入空格字符计数器1disp[count] yanwu % 100000 / 10000 0; // 将烟雾万位数字转换为字符存入显示缓存数组计数器1disp[count] yanwu % 10000 / 1000 0; // 将烟雾千位数字转换为字符存入显示缓存数组计数器1disp[count] yanwu % 1000 / 100 0; // 将烟雾百位数字转换为字符存入显示缓存数组计数器1disp[count] yanwu % 100 / 10 0; // 将烟雾十位数字转换为字符存入显示缓存数组计数器1disp[count] yanwu % 10 0; // 将烟雾个位数字转换为字符存入显示缓存数组计数器1disp[count] p; // 存入字符p表示“ppm”parts per million计数器1disp[count] 0; // 存入字符\0表示字符串结束计数器1LCD_write_str(0, 0, disp); // 在LCD屏幕第0行显示disp数组中的内容
}void send_computer(void)
{count 0;disp[count] wendu % 1000 / 100 0;disp[count] wendu % 100 / 10 0;disp[count] C;disp[count] ;disp[count] shidu % 1000 / 100 0;disp[count] shidu % 100 / 10 0;disp[count] %;disp[count] ;disp[count] yanwu % 100000 / 10000 0;disp[count] yanwu % 10000 / 1000 0;disp[count] yanwu % 1000 / 100 0;disp[count] yanwu % 100 / 10 0;disp[count] yanwu % 10 0;disp[count] p;disp[count] \r;disp[count] \n;disp[count] 0;Print_Str(disp);
}void disp_line2(void)
{count 0; //计数器清零//显示湿度报警值的百位、十位disp[count] shidu_baojing % 1000 / 100 0;disp[count] shidu_baojing % 100 / 10 0;disp[count] C; //显示Cdisp[count] ; //显示空格//显示温度报警值的百位、十位disp[count] wendu_baojing % 1000 / 100 0;disp[count] wendu_baojing % 100 / 10 0;disp[count] %; //显示%disp[count] ; //显示空格//显示烟雾报警值的万位、千位、百位、十位、个位disp[count] yanwu_baojing % 100000 / 10000 0;disp[count] yanwu_baojing % 10000 / 1000 0;disp[count] yanwu_baojing % 1000 / 100 0;disp[count] yanwu_baojing % 100 / 10 0;disp[count] yanwu_baojing % 10 0;disp[count] p; //显示pdisp[count] 0; //末尾字符为0//在LCD的第1行第0列显示字符串dispLCD_write_str(0, 1, disp);
}void deal_key(void)
{// 定义一个函数deal_key没有返回值没有参数if (keyvalue 1){// 如果keyvalue的值为1shidu_baojing 10;// 湿度报警值加10if (shidu_baojing 900){// 如果湿度报警值大于900shidu_baojing 900;// 把湿度报警值设为900}}else if (keyvalue 2){// 否则如果keyvalue的值为2shidu_baojing - 10;// 湿度报警值减10if (shidu_baojing 100){// 如果湿度报警值小于100shidu_baojing 100;// 把湿度报警值设为100}}else if (keyvalue 3){// 否则如果keyvalue的值为3wendu_baojing 10;// 温度报警值加10if (wendu_baojing 900){// 如果温度报警值大于900wendu_baojing 900;// 把温度报警值设为900}}else if (keyvalue 4){// 否则如果keyvalue的值为4wendu_baojing - 10;// 温度报警值减10if (wendu_baojing 100){// 如果温度报警值小于100wendu_baojing 100;// 把温度报警值设为100}}else if (keyvalue 5){// 否则如果keyvalue的值为5yanwu_baojing 100;// 烟雾报警值加100if (yanwu_baojing 20000){// 如果烟雾报警值大于20000yanwu_baojing 20000;// 把烟雾报警值设为20000}}else if (keyvalue 6){// 否则如果keyvalue的值为6yanwu_baojing - 100;// 烟雾报警值减100if (yanwu_baojing 200){// 如果烟雾报警值大于200yanwu_baojing 200;// 把烟雾报警值设为200}}
}void contorl_beep(void) // 定义一个名为 contorl_beep 的函数
{if ((shidu shidu_baojing) || (wendu wendu_baojing) // 如果湿度、温度或烟雾任意一个超过了警戒值|| (yanwu yanwu_baojing)){P1OUT ~BIT6; // 将 P1OUT 寄存器的第 6 位设置为 0表示开启蜂鸣器}else // 如果湿度、温度和烟雾都没有超过警戒值{P1OUT | BIT6; // 将 P1OUT 寄存器的第 6 位设置为 1表示关闭开启蜂鸣器}
}/* 烟雾浓度换算 */
static unsigned int yw_ppm_count(unsigned int x1)
{float a 0 ,b 0,c 0,d 0;float ax 0 ,bx 0,cx 0,dx 0;float y;float x;xx1*3.3/4096;a x * x * x * x * x;b x * x * x * x;c x * x * x;d x * x ;ax 0.0001923 * a;bx 0.006017 * b;cx 0.07267 * c;dx 0.425 * d;y ax - bx cx - dx (1.267 * x) 1.209;y y * 1000;return (unsigned int)y;
}void main(void)
{WDTCTL WDTPW WDTHOLD; // 停止看门狗定时器init_clock(); // 初始化时钟init_uart(); // 初始化UART串口init_adc(); // 初始化ADC模块init_key(); // 初始化按键init_beep(); // 初始化蜂鸣器while (DHT22_Init() 1) // 初始化DHT22温湿度传感器{delay_ms(100); // 延时100毫秒}delay_ms(1000);LCD_Init(); // 初始化液晶屏disp_line2(); // 在液晶屏上显示第二行_EINT(); // 开启总中断while (1) // 无限循环{delay_ms(10); // 延时10毫秒timecnt; // 时间计数器加1if (timecnt 200) // 如果时间计数器超过200{timecnt 0; // 重置时间计数器DHT22_Read_Data(data); // 读取DHT22传感器数据shidu data[0]; // 湿度数据shidu (shidu 8) data[1]; // 拼接数据wendu data[2]; // 温度数据wendu (wendu 8) data[3]; // 拼接数据yanwu get_adc(); // 获取烟雾浓度值//0到4096转为0到20000ppmyanwu yw_ppm_count(yanwu); // 将ADC值转换为烟雾浓度值contorl_beep(); // 控制蜂鸣器disp_line1(); // 在液晶屏上显示第一行disp_line2(); // 在液晶屏上显示第二行send_computer();}keyvalue get_key(); // 获取按键值if (keyvalue) // 如果有按键按下{deal_key(); // 处理按键disp_line2(); // 在液晶屏上显示第二行}}
}unsigned char revchar 0; // 定义一个无符号字符变量初始值为0用于保存接收到的字符
unsigned char revchar_openflag 0; // 定义一个无符号字符变量初始值为0用于标志接收到的字符是否为有效数据
unsigned char revchar_cnt 0; // 定义一个无符号字符变量初始值为0用于计数接收到的有效数据个数
unsigned char revchar_buffer[20]; // 定义一个无符号字符数组长度为20用于存储接收到的有效数据#pragma vectorUSCI_A0_VECTOR // 定义中断函数当 USCI_A0_VECTOR 中断发生时执行以下函数
__interrupt void USCI_A0_ISR(void)
{switch (__even_in_range(UCA0IV, 4))// 获取 USCI_A0_VECTOR 中断的状态{case 0: // Vector 0 - no interrupt无中断状态break;case 2: // Vector 2 - RXIFG接收到数据的中断状态revchar UCA0RXBUF; // 读取 USCI_A0_VECTOR 的接收缓冲区数据到变量 revcharif (revchar_openflag) // 如果接收到的字符是有效数据{//S889912345 // 检查接收到的数据是否符合特定的格式如S889912345if ((revchar 0) (revchar 9)) // 如果接收到的数据是数字字符{revchar_buffer[revchar_cnt] revchar - 0; // 把接收到的数字字符转化为对应的数字存入 revchar_buffer 数组中revchar_cnt; // 计数器加1if (revchar_cnt 9) // 如果接收到的数字字符数量达到特定的数量{shidu_baojing revchar_buffer[0] * 100 revchar_buffer[1] * 10; // 计算湿度报警值wendu_baojing revchar_buffer[2] * 100 revchar_buffer[3] * 10; // 计算温度报警值yanwu_baojing revchar_buffer[4] * 10000 revchar_buffer[5] * 1000 revchar_buffer[6] * 100 revchar_buffer[7] * 10 revchar_buffer[8]; // 计算烟雾报警值revchar_openflag 0; // 清除接收标志revchar_cnt 0; // 清除计数器}}else // 如果接收到的字符不是数字字符{revchar_openflag 0; // 清除接收标志revchar_cnt 0; // 清除计数器}}if (revchar S){revchar_openflag 1;revchar_cnt 0;}break;case 4:break; // Vector 4 - TXIFGdefault:break;}
}
全部程序
https://docs.qq.com/sheet/DUEdqZ2lmbmR6UVdU?tabBB08J2