将采集的温度通过蓝牙模块实时传给手机。我重新回来了 以后经常分享
单片机源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "sys.h"
- #include "usart.h"
- #include "lcd.h"
- #include "key.h"
- #include "adc.h"
- #include "math.h"
- #include "exti.h"
- #include "timer.h"
- #include "hc05.h"
- #include "usart2.h"
- #include "string.h"
- u8 yuzhi=25;
- u8 shangxian=0;
- u8 xiaxian=0;
- u8 wendu_4=0;
- float wendu_1=0,wendu_2=0,wendu_3;
- //显示ATK-HC05模块的主从状态
- void HC05_Role_Show(void)
- {
- if(HC05_Get_Role()==1)LCD_ShowString(0,240,200,16,16,"ROLE:Master"); //主机
- else LCD_ShowString(0,240,200,16,16,"ROLE:Slave "); //从机
- }
- //显示ATK-HC05模块的连接状态
- void HC05_Sta_Show(void)
- {
- if(HC05_LED)LCD_ShowString(90,240,120,16,16,"STA:Connected "); //连接成功
- else LCD_ShowString(90,240,120,16,16,"STA:Disconnect"); //未连接
- }
-
- int main(void)
- {
- u16 adcx;
- u8 sendcnt_1=0;
- u8 sendcnt_2=0;
- u8 sendbuf[40];
- u8 reclen=0;
- float temp;
- delay_init(); //延时函数初始化
- uart_init(9600); //串口初始化为9600
- LED_Init(); //初始化与LED连接的硬件接口
- LCD_Init();
- Adc_Init(); //ADC初始化
- EXTIX_Init(); //外部中断初始化
- KEY_Init(); //初始化与按键连接的硬件接口
- LED0=0; //点亮LED
- POINT_COLOR=RED;//设置字体为红色
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// 设置中断优先级分组
- LCD_ShowString(50,0,200,16,16,"The Eighth Group");
- LCD_ShowString(0,20,200,16,16,"Temperature Control intel");
- LCD_ShowString(60,40,200,16,16,"-ligent System");
- LCD_ShowString(0,60,200,16,16,"Set the threshold:");
- LCD_ShowString(160,60,200,16,16,"'C");
- LCD_ShowString(0,80,200,16,16,"Data:2018/4/28");
- LCD_ShowString(0,100,200,16,16,"Temperature:00.0'C");
- LCD_ShowString(0,120,200,16,16,"ADC_CH1_VAL:");
- LCD_ShowString(0,140,200,16,16,"ADC_CH1_VOL:0.000V");
- LCD_ShowString(0,160,200,16,16,"Current Process:");
- LCD_ShowString(128,160,200,16,16,"KEEP");
- LCD_ShowString(0,180,200,16,16,"ShangXian:00'C");
- LCD_ShowString(0,200,200,16,16,"XiaXian:00'C");
-
-
- POINT_COLOR=MAGENTA;
- //显示提示信息
- LCD_ShowString(0,260,200,16,16,"Send:");
- LCD_ShowString(0,280,200,16,16,"Receive:");
- while(HC05_Init()) //初始化ATK-HC05模块
- {
- LCD_ShowString(0,220,200,16,16,"ATK-HC05 Error!");
- delay_ms(500);
- LCD_ShowString(0,220,200,16,16,"Please Check!!!");
- delay_ms(100);
- }
- LCD_ShowString(0,220,200,16,16,"ATK-HC05 Standby!");
- HC05_Sta_Show();
- HC05_Role_Show();
- POINT_COLOR=BLUE;//设置字体为蓝色
- LCD_ShowxNum(144,60,yuzhi,2,16,0X80);
- while(1)
- {
- TIM3_Int_Init(9999,7199);//10Khz的计数频率,计数到5000为500ms
- LED0=!LED0;
- adcx=Get_Adc_Average(ADC_Channel_1,10);
- POINT_COLOR=BLUE;//设置字体为蓝色
- LCD_ShowxNum(96,120,adcx,4,16,0);//显示ADC的值
- temp=(float)adcx*(3.3/4096);
- wendu_1=temp*10;
- adcx=temp;
- LCD_ShowxNum(96,140,adcx,1,16,0);//显示电压值
- temp-=adcx;
- temp*=1000;
- LCD_ShowxNum(112,140,temp,3,16,0X80);
- LED0=!LED0;
- delay_ms(250);
- wendu_3=(float)(10000*wendu_1+25692)/2107;
- wendu_4=wendu_3;
- sendcnt_1=wendu_3;
-
- LCD_ShowxNum(96,100,wendu_3,2,16,0);//
- wendu_3-=wendu_4;
- wendu_2=wendu_3*10;
- sendcnt_2=wendu_2;
- LCD_ShowxNum(120,100,wendu_2,1,16,0);//
- delay_ms(200);
- POINT_COLOR=MAGENTA;
- sprintf((char*)sendbuf,"Temperature:%d.%d'C\r\n",sendcnt_1,sendcnt_2);
- LCD_ShowString(48,260,200,16,16,sendbuf); //显示发送数据
- u2_printf("Temperature: %d.%d\r\n",sendcnt_1,sendcnt_2); //发送到蓝牙模块
-
- POINT_COLOR=BLUE;
- if(wendu_4>=shangxian)
- {
- LCD_ShowString(128,160,200,16,16,"SNOW");
- GPIO_ResetBits(GPIOC,GPIO_Pin_0);
- GPIO_SetBits(GPIOC,GPIO_Pin_1);
- }
- if(wendu_4<=xiaxian)
- {
- LCD_ShowString(128,160,200,16,16,"HOT ");
- GPIO_ResetBits(GPIOC,GPIO_Pin_1);
- GPIO_SetBits(GPIOC,GPIO_Pin_0);
- }
- if(wendu_4<shangxian&&wendu_4>xiaxian)
- {
- LCD_ShowString(128,160,200,16,16,"KEEP");
- GPIO_ResetBits(GPIOC,GPIO_Pin_0);
- GPIO_ResetBits(GPIOC,GPIO_Pin_1);
- }
- if(USART2_RX_STA&0X8000) //接收到一次数据了
- {
- LCD_Fill(64,280,264,300,WHITE); //清除显示
- reclen=USART2_RX_STA&0X7FFF; //得到数据长度
- USART2_RX_BUF[reclen]=0; //加入结束符
- if(reclen==9||reclen==8) //控制DS1检测
- {
- if(strcmp((const char*)USART2_RX_BUF,"+LED1 ON")==0)LED1=0; //打开LED1
- if(strcmp((const char*)USART2_RX_BUF,"+LED1 OFF")==0)LED1=1;//关闭LED1
- }
- POINT_COLOR=MAGENTA;
- LCD_ShowString(64,280,200,119,16,USART2_RX_BUF);//显示接收到的数据
- USART2_RX_STA=0;
- }
- }
- }
复制代码
所有资料51hei提供下载:
传感器课设程序32版终.rar
(366.14 KB, 下载次数: 232)
|