单片机源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "key.h"
- #include "sys.h"
- #include "lcd.h"
- #include "usart.h"
- #include "hc05.h"
- #include "usart3.h"
- #include "string.h"
- #include "usmart.h"
- #include "beep.h"
- #include "adc.h"
- #include "tsensor.h"
-
- //显示ATK-HC05模块的主从状态
- void HC05_Role_Show(void)
- {
- if(HC05_Get_Role()==1)LCD_ShowString(30,140,200,16,16,"ROLE:Master"); //主机
- else LCD_ShowString(30,140,200,16,16,"ROLE:Slave "); //从机
- }
- //显示ATK-HC05模块的连接状态
- void HC05_Sta_Show(void)
- {
- if(HC05_LED)LCD_ShowString(120,140,120,16,16,"STA:Connected "); //连接成功
- else LCD_ShowString(120,140,120,16,16,"STA:Disconnect"); //未连接
- }
- int main(void)
- {
- short temp;
- u8 key;
- u8 sendmask=0;
- //u8 sendcnt=0;
- u8 sendbuf[20];
- u8 reclen=0;
- delay_init(); //延时函数初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
- uart_init(115200); //串口初始化为9600
- LED_Init(); //初始化与LED连接的硬件接口
- KEY_Init(); //初始化按键
- LCD_Init(); //初始化LCD
- usmart_dev.init(72); //初始化USMART
-
-
- BEEP_Init();
- T_Adc_Init(); //ADC初始化
-
-
- POINT_COLOR=BLUE;//设置字体为蓝色
- LCD_ShowString(30,280,200,16,16,"TEMPERATE: 00.00C");
- LCD_ShowString(30,310,200,16,16,"SMOKE: ");
-
-
-
- delay_ms(1000); //等待蓝牙模块上电稳定
- while(HC05_Init()) //初始化ATK-HC05模块
- {
- LCD_ShowString(30,90,200,16,16,"ATK-HC05 Error!");
- delay_ms(500);
- LCD_ShowString(30,90,200,16,16,"Please Check!!!");
- delay_ms(100);
- }
- LCD_ShowString(30,110,200,16,16,"ATK-HC05 Standby!");
- LCD_ShowString(30,160,200,16,16,"Send:");
- LCD_ShowString(30,180,200,16,16,"Receive:");
- POINT_COLOR=BLUE;
- HC05_Role_Show();
- delay_ms(100);
- USART3_RX_STA=0;
- while(1)
- {
- HC05_Sta_Show();
- temp=Get_Temprate(); //得到温度值
- //if(temp>70)
- //{LCD_ShowString(30+10*8,310,16,16,16,"DANGER");
- //LED0=1;
- //BEEP=0;}
- if(temp<0)
- {
- temp=-temp;
- LCD_ShowString(30+10*8,280,200,16,16,"-"); //显示负号
- }
- else
- LCD_ShowString(30+10*8,280,16,16,16," "); //无符号
- LCD_ShowxNum(30+11*8,280,temp/100,2,16,0); //显示整数部分
- LCD_ShowxNum(30+14*8,280,temp%100,2,16, 0X80); //显示小数部分
- LED1=1;
-
- if(PBin(13))
- {LED0=1;BEEP=0;LCD_ShowString(30+6*8,310,200,16,16," SAFE! ");LED1=0;
- }
- else if(PBin(13)==0||temp>70)
-
- {LED0=0;
- BEEP=1;
- LCD_ShowString(30+6*8,310,200,16,16,"DANGER!");
- LED1=1;}
- delay_ms(250);
-
-
- key=KEY_Scan(0);
- if(key==KEY1_PRES) //切换模块主从设置
- {
- key=HC05_Get_Role();
- if(key!=0XFF)
- {
- key=!key; //状态取反
- if(key==0)HC05_Set_Cmd("AT+ROLE=0");
- else HC05_Set_Cmd("AT+ROLE=1");
- HC05_Role_Show();
- HC05_Set_Cmd("AT+RESET"); //复位ATK-HC05模块
- delay_ms(200);
- }
- }
-
- else if(BEEP==1)
- {
- sendmask=1; //发送/停止发送
-
- }
- else if
- (BEEP==0)
- {LCD_Fill(30+40,160,240,160+16,WHITE);
- sendmask=0;}//清除显示
-
- delay_ms(10);
- if(sendmask) //定时发送
- {
- sprintf((char*)sendbuf,"Danger\r\n");
- LCD_ShowString(30+40,160,200,16,16,sendbuf); //显示发送数据
- u3_printf("Danger\r\n"); //发送到蓝牙模块
- }
- HC05_Sta_Show();
-
- delay_ms(10);
-
-
- if(USART3_RX_STA&0X8000) //接收到一次数据了
- {
- LCD_Fill(30,200,64,64,WHITE); //清除显示
- reclen=USART3_RX_STA&0X7FFF; //得到数据长度
- USART3_RX_BUF[reclen]=0; //加入结束符
- if(reclen==9||reclen==8) //控制DS1检测
- {
- if(strcmp((const char*)USART3_RX_BUF,"+LED0 ON")==0)LED0=0; //打开LED1
- if(strcmp((const char*)USART3_RX_BUF,"+LED0 OFF")==0)LED0=1;//关闭LED1
- if(strcmp((const char*)USART3_RX_BUF,"+BEEP ON")==0) BEEP=1;
- if(strcmp((const char*)USART3_RX_BUF,"+BEEP OFF")==0) BEEP=0;
- }
- LCD_ShowString(30,200,209,16,16,USART3_RX_BUF);//显示接收到的数据
- USART3_RX_STA=0;
- }
- }
- }
复制代码
所有资料51hei提供下载:
1.7z
(254.9 KB, 下载次数: 127)
|