详细注释下述代码:#include <reg52.h> #include <onewire.h> unsigned char duanma[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; unsigned char duanma_x[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10}; unsigned int temp=0; unsigned int temp_h=0; unsigned int temp_l=30; void Delay_SMG(unsigned int t) { while(t--); } void SelectHC573(unsigned char n) { switch(n) { case 0 : P2 = (P2 & 0x1f ) | 0x00; break; case 4 : P2 = (P2 & 0x1f ) | 0x80; break; case 5 : P2 = (P2 & 0x1f ) | 0xa0; break; case 6 : P2 = (P2 & 0x1f ) | 0xc0; break; case 7 : P2 = (P2 & 0x1f ) | 0xe0; break; } } void DisplaySMG_Bit(unsigned char pos,unsigned char dat) { P2=0xE0;P0=0xff;//先全部关掉数码管,避免显示不正常 P2=0xC0;P0=0x01<<pos; P2=0xE0;P0=dat; } void DisplaySMG_temp() { DisplaySMG_Bit(1,duanma[temp%10]); Delay_SMG(100); DisplaySMG_Bit(0,duanma[temp/10]); //第0位和第1位显示温度 Delay_SMG(100); DisplaySMG_Bit(5,0xbf); Delay_SMG(100); DisplaySMG_Bit(7,duanma[temp_l%10]); Delay_SMG(100); DisplaySMG_Bit(6,duanma[temp_l/10]); Delay_SMG(100); DisplaySMG_Bit(2,0xbf); Delay_SMG(100); DisplaySMG_Bit(4,duanma[temp_h%10]); Delay_SMG(100); DisplaySMG_Bit(3,duanma[temp_h/10]); Delay_SMG(100); P2=0xC0;P0=0xff; P2=0xE0;P0=0xff; } void Delay(unsigned int t) { while(t--) { DisplaySMG_temp(); } } void Read_DS18B20_temp() { unsigned char LSB,MSB; init_ds18b20(); Write_DS18B20(0xcc); Write_DS18B20(0x44); Delay(1000); init_ds18b20(); Write_DS18B20(0xcc); Write_DS18B20(0xbe); LSB=Read_DS18B20(); MSB=Read_DS18B20(); temp=MSB; temp=(temp<<8)|LSB; temp>>=4; } void open_buzz() { P0|=0x40; SelectHC573(5); SelectHC573(0); } void close_buzz() { P0&=0xbf; SelectHC573(5); SelectHC573(0); } void main() { P2=0x80;P0=0xff; while(1) { Read_DS18B20_temp(); if(temp>temp_h) { temp_h=temp; } if(temp_l>temp) { temp_l=temp; } if(temp>=30) { open_buzz(); }else if(temp<30) { close_buzz(); } DisplaySMG_temp(); } }
时间: 2024-01-18 19:03:03 浏览: 76
这是一段基于单片机的温度控制程序,通过读取DS18B20温度传感器的数据,控制数码管的显示和蜂鸣器的开关。下面对代码进行分析:
1. `#include <reg52.h>`和`#include <onewire.h>`是头文件的引用。
2. `unsigned char duanma[10]`和`unsigned char duanma_x[10]`是两个数码管显示的数组,分别对应常规数字和小数位数字。
3. `unsigned int temp`、`unsigned int temp_h=0`和`unsigned int temp_l=30`是定义温度值的变量,`temp`存储实际温度值,`temp_h`存储最高温度值,`temp_l`存储最低温度值。
4. `DisplaySMG_Bit`函数用于控制数码管的显示,其中`pos`参数表示要显示的数码管位置,`dat`参数表示要显示的数字。
5. `DisplaySMG_temp`函数用于控制整个数码管的显示,包括温度数字和小数点的显示。
6. `Delay_SMG`函数用于控制数码管的闪烁效果。
7. `SelectHC573`函数用于控制74HC573芯片的输出,其中`n`参数表示要输出的芯片编号。
8. `Read_DS18B20_temp`函数用于读取DS18B20温度传感器的数据,并将数据转化为实际温度值。
9. `open_buzz`和`close_buzz`函数用于控制蜂鸣器的开关。
10. `main`函数是程序的入口点,其中通过不断读取温度值,并根据温度值控制数码管的显示和蜂鸣器的开关。同时,还会记录最高温度和最低温度。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-dosexec](https://img-home.csdnimg.cn/images/20210720083343.png)
![text/plain](https://img-home.csdnimg.cn/images/20250102104920.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![cpp](https://img-home.csdnimg.cn/images/20250102104920.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)