STM32温度报警传感器代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

主函数、main.c#includeled.h#includedelay.h#includesys.h#includeusart.h#includelcd.h#includeds18b20.h#includekey.h#includestring.h#includestdio.h#includestdlib.hinttemp_low=22;inttemp_high=32;intzanshi_low=0;intzanshi_hign=0;voidTIM3_Int_Init(u16arr,u16psc){TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;NVIC_InitTypeDefNVIC_InitStructure;RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//时钟使能TIM_TimeBaseStructure.TIM_Period=arr;//设置在下一个更新事件装入活动的自动重装载寄存器周期的值计数到5000为500msTIM_TimeBaseStructure.TIM_Prescaler=psc;//设置用来作为TIMx时钟频率除数的预分频值10Khz的计数频率TIM_TimeBaseStructure.TIM_ClockDivision=0;//设置时钟分割:TDTS=Tck_timTIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//TIM向上计数模式TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);//根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位TIM_ITConfig(//使能或者失能指定的TIM中断TIM3,//TIM2TIM_IT_Update,ENABLE//使能);NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn;//TIM3中断NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;//先占优先级0级NVIC_InitStructure.NVIC_IRQChannelSubPriority=3;//从优先级3级NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;//IRQ通道被使能NVIC_Init(&NVIC_InitStructure);//根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器TIM_Cmd(TIM3,ENABLE);//使能TIMx外设}u8flag=0;intmain(void){u8t=0;u8shuzu[20];u8keyvalue=0;u8gaibianshui=0;shorttemperature;delay_init();//延时函数初始化uart_init(9600);//串口初始化为9600NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组2LED_Init();//初始化与LED连接的硬件接口TIM3_Int_Init(9999,7199);//10Khz的计数频率,计数到5000为500msLCD_Init();POINT_COLOR=RED;//设置字体为红色LCD_ShowString(0,50,200,16,16,STM32test);LCD_ShowString(0,70,200,16,16,name:zhangqiqi);//LCD_ShowString(0,90,200,16,16,Max32CMin26C);memset(shuzu,0,20);sprintf(shuzu,Maxis%2dC,Minis%2dC,temp_high,temp_low);LCD_ShowString(0,90,200,16,16,shuzu);while(DS18B20_Init())//DS18B20初始化{LCD_ShowString(0,130,200,16,16,DS18B20Error);delay_ms(200);LCD_Fill(0,130,239,130+16,WHITE);delay_ms(200);}POINT_COLOR=BLUE;//设置字体为蓝色LCD_ShowString(0,110,260,16,16,Thetemperatureis:.Cnow);LCD_ShowChar(0+25*8,110,',',16,0);LCD_ShowChar(0+29*8,110,'!',16,0);while(1){POINT_COLOR=BLUE;//设置字体为蓝色if(t%10==0)//每100ms读取一次{t=0;temperature=DS18B20_Get_Temp();if(temperature0){LCD_ShowChar(0+40,150,'-',16,0);//显示负号temperature=-temperature;//转为正数}elseLCD_ShowChar(0+40,150,'',16,0);//去掉负号memset(shuzu,0,20);sprintf(shuzu,Thetemperatureis:%3d.%1dC,temperature/10,temperature%10);LCD_ShowString(0,110,200,16,16,shuzu);}delay_ms(10);//判断温度zanshi_low=temp_low*10;zanshi_hign=temp_high*10;if((temperaturezanshi_low)||(temperaturezanshi_hign)){if(temperaturezanshi_low){flag=1;printf(chaoxiaxian);}if(temperaturezanshi_hign){flag=2;printf(chaoshangxian);}}else{flag=0;LED0=1;//正常情况灯不亮LED1=1;}//获取温度时间控制tt++;//浏览开关KEY_Init();delay_ms(10);keyvalue=KEY_Scan(0);if(keyvalue!=0){if(keyvalue==3){gaibianshui=(gaibianshui+1)%3;}if(gaibianshui==1)//改变上限{if(keyvalue==2)//key1按下{temp_high++;memset(shuzu,0,20);sprintf(shuzu,Maxis%2dC,Minis%2dC,temp_high,temp_low);LCD_ShowString(0,90,200,16,16,shuzu);}if(keyvalue==1)//key0按下{temp_high--;memset(shuzu,0,20);sprintf(shuzu,Maxis%2dC,Minis%2dC,temp_high,temp_low);LCD_ShowString(0,90,200,16,16,shuzu);}}if(gaibianshui==2)//改变上限{if(keyvalue==2)//key1按下{temp_low++;memset(shuzu,0,20);sprintf(shuzu,Maxis%2dC,Minis%2dC,temp_high,temp_low);LCD_ShowString(0,90,200,16,16,shuzu);}if(keyvalue==1)//key0按下{temp_low--;memset(shuzu,0,20);sprintf(shuzu,Maxis%2dC,Minis%2dC,temp_high,temp_low);LCD_ShowString(0,90,200,16,16,shuzu);}}keyvalue=0;}}}voidTIM3_IRQHandler(void)//TIM3中断{if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET)//检查指定的TIM中断发生与否:TIM中断源{TIM_ClearITPendingBit(TIM3,TIM_IT_Update);//清除TIMx的中断待处理位:TIM中断源//温度不正常处理灯if(flag==1){LED0=!LED0;}if(flag==2){LED1=!LED1;}}}LED函数led.c#includeled.hvoidLED_Init(void){GPIO_InitTypeDefGPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOD,ENABLE);//使能PA,PD端口时钟GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;//LED0--PA.8端口配置GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//推挽输出GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//IO口速度为50MHzGPIO_Init(GPIOA,&GPIO_InitStructure);//根据设定参数初始化GPIOA.8GPIO_SetBits(GPIOA,GPIO_Pin_8);//PA.8输出高GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;//LED1--PD.2端口配置,推挽输出GPIO_Init(GPIOD,&GPIO_InitStructure);//推挽输出,IO口速度为50MHzGPIO_SetBits(GPIOD,GPIO_Pin_2);//PD.2输出高}温度代码s18b20.c#includeds18b20.h#includedelay.hvoidDS18B20_Rst(void){DS18B20_IO_OUT();//SETPA0OUTPUTDS18B20_DQ_OUT=0;//拉低DQdelay_us(750);//拉低750usDS18B20_DQ_OUT=1;//DQ=1delay_us(15);//15US}//等待DS18B20的回应//返回1:未检测到DS18B20的存在//返回0:存在u8DS18B20_Check(void){u8retry=0;DS18B20_IO_IN();//SETPA0INPUTwhile(DS18B20_DQ_IN&&retry200){retry++;delay_us(1);};if(retry=200)return1;elseretry=0;while(!DS18B20_DQ_IN&&retry240){retry++;delay_us(1);};if(retry=240)return1;return0;}//从DS18B20读取一个位//返回值:1/0u8DS18B20_Read_Bit(void)//readonebit{u8data;DS18B20_IO_OUT();//SETPA0OUTPUTDS18B20_DQ_OUT=0;delay_us(2);DS18B20_DQ_OUT=1;DS18B20_IO_IN();//SETPA0INPUTdelay_us(12);if(DS18B20_DQ_IN)data=1;elsedata=0;delay_us(50);returndata;}//从DS18B20读取一个字节//返回值:读到的数据u8DS18B20_Read_Byte(void)//readonebyte

1 / 11
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功