1、 ); 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中指定的参数
2、初始化外设NVIC寄存器 TIM_Cmd(TIM3, ENABLE); /使能TIMx外设 u8 flag=0;int main(void) u8 t = 0; u8 shuzu20; u8 keyvalue=0; u8 gaibianshui=0; short temperature; delay_init(); /延时函数初始化 uart_init(9600); /串口初始化为9600 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);/ 设置中断优先级分组2 LED_Init(); /初始化与LED连接的硬件接口 TIM3_Int_Init(9
3、999,7199);/10Khz的计数频率,计数到5000为500ms LCD_Init(); POINT_COLOR = RED; /设置字体为红色 LCD_ShowString(0, 50, 200, 16, 16, STM32 test); LCD_ShowString(0, 70, 200, 16, 16, name: zhang qi qi /LCD_ShowString(0,90,200,16,16,Max 32 C Min 26 C memset(shuzu, 0, 20); sprintf(shuzu, Max is %2d C,Min is %2d C, temp_high,
4、 temp_low); LCD_ShowString(0, 90, 200, 16, 16, shuzu); while (DS18B20_Init() /DS18B20初始化 LCD_ShowString(0, 130, 200, 16, 16, DS18B20 Error delay_ms(200); LCD_Fill(0, 130, 239, 130 + 16, WHITE); POINT_COLOR = BLUE; /设置字体为蓝色 LCD_ShowString(0, 110, 260, 16, 16, The temperature is: . C now LCD_ShowChar(
5、0+25*8,110,16,0); LCD_ShowChar(0+29*8,110,! while (1) POINT_COLOR = BLUE; if (t % 10 = 0) /每100ms读取一次 t = 0; temperature = DS18B20_Get_Temp(); if (temperature 0) LCD_ShowChar(0 + 40, 150, -, 16, 0); /显示负号 temperature = -temperature; /转为正数 else /去掉负号 memset(shuzu, 0, 20); sprintf(shuzu, %3d.%1dC, tem
6、perature / 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_hign) if(temperaturezanshi_hign) flag=2;chaoshangxian else flag=0; LED0=1; /正常情况灯不亮 LED1=1; /获取温度时间控制t t+; /浏览开关 KEY_Init(); delay_
7、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, LCD_ShowString(0, 90, 200, 16, 16, shuzu); if(keyvalue=1) /key0按下 temp_high-; if(gaibianshui=2) /改变上限 temp_low+;
8、 temp_low-; keyvalue=0;void TIM3_IRQHandler(void) /TIM3中断 if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) /检查指定的TIM中断发生与否:TIM 中断源 TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); /清除TIMx的中断待处理位: /温度不正常处理灯 if(flag=1) LED0 = !LED0; if(flag=2) LED1 = !LED1; LED 函数led.cvoid LED_Init(void) GPIO_InitTypeDe
9、f GPIO_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口速度为50MHz GPIO_Init(GPIO
10、A, &GPIO_InitStructure); /根据设定参数初始化GPIOA.8 GPIO_SetBits(GPIOA,GPIO_Pin_8); /PA.8 输出高 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; /LED1-PD.2 端口配置, 推挽输出 GPIO_Init(GPIOD, & /推挽输出 ,IO口速度为50MHz GPIO_SetBits(GPIOD,GPIO_Pin_2); /PD.2 输出高 温度代码 s18b20.cvoid DS18B20_Rst(void) DS18B20_IO_OUT(); /SET PA0 OUTPUT D
11、S18B20_DQ_OUT=0; /拉低DQ delay_us(750); /拉低750us DS18B20_DQ_OUT=1; /DQ=1 delay_us(15); /15US/等待DS18B20的回应/返回1:未检测到DS18B20的存在/返回0:存在u8 DS18B20_Check(void) u8 retry=0; DS18B20_IO_IN();/SET PA0 INPUT while (DS18B20_DQ_IN&retry=200)return 1; else retry=0; while (!DS18B20_DQ_IN&240)=240)return 1; return 0;
12、/从DS18B20读取一个位/返回值:1/0u8 DS18B20_Read_Bit(void) / read one bit u8 data;/SET PA0 OUTPUT delay_us(2);/SET PA0 INPUT delay_us(12); if(DS18B20_DQ_IN)data=1; else data=0; delay_us(50); return data;/从DS18B20读取一个字节读到的数据u8 DS18B20_Read_Byte(void) / read one byte u8 i,j,dat; dat=0; for (i=1;i=8;i+) j=DS18B20
13、_Read_Bit(); dat=(j1); return dat;/写一个字节到DS18B20/dat:要写入的字节void DS18B20_Write_Byte(u8 dat) u8 j; u8 testb;/SET PA0 OUTPUT; for (j=1;j1; if (testb) / Write 1 delay_us(60); else / Write 0/开始温度转换void DS18B20_Start(void)/ ds1820 start convert DS18B20_Rst(); DS18B20_Check(); DS18B20_Write_Byte(0xcc);/ sk
14、ip rom DS18B20_Write_Byte(0x44);/ convert /初始化DS18B20的IO口 DQ 同时检测DS的存在不存在存在 u8 DS18B20_Init(void) GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); /使能PORTA口时钟 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; /PORTA0 推挽输出 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out
15、_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, & GPIO_SetBits(GPIOA,GPIO_Pin_2); /输出1 return DS18B20_Check();/从ds18b20得到温度值/精度:0.1C温度值 (-5501250) short DS18B20_Get_Temp(void) u8 temp; u8 TL,TH; short tem; DS18B20_Start (); / ds1820 start convert DS18B20_Write_Byte(0xbe);/ co
16、nvert TL=DS18B20_Read_Byte(); / LSB TH=DS18B20_Read_Byte(); / MSB if(TH7) TH=TH; TL=TL; temp=0;/温度为负 else temp=1;/温度为正 tem=TH; /获得高八位 temKEY1WK_UP!u8 KEY_Scan(u8 mode) static u8 key_up=1;/按键按松开标志 if(mode)key_up=1; /支持连按 if(key_up&(KEY0=0|KEY1=0|WK_UP=1) delay_ms(10);/去抖动 key_up=0; if(KEY0=0)return KEY0_PRES; else if(KEY1=0)return KEY1_PRES; else if(WK_UP=1)return WKUP_PRES; else if(KEY0=1&KEY1=1&WK_UP=0)key_up=1;/ 无按键按下
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1