Key_Value = Key_Read();//¶ÁÈ¡°´¼üϵıàºÅ Key_Down = Key_Value & (Key_Old ^ Key_Value); Key_Old = Key_Value£» 是什么意思
时间: 2024-06-13 14:09:55 浏览: 9
这段代码是在读取按键状态的过程中使用的。其中,`Key_Read()`函数用于读取当前按键状态,`Key_Value`变量存储了读取到的按键状态值。接下来,`Key_Down`变量通过对当前按键状态值和上一次按键状态值进行异或运算得到,用于表示当前按下的按键。最后,将当前按键状态值存储到`Key_Old`变量中,以备下一次读取使用。
相关问题
void main() { u8 i=0; int temp_value; u8 temp_buf[5]; ds18b20_init();//³õʼ»¯DS18B20 while(1) { i++; if(i%50==0)//¼ä¸ôÒ»¶Îʱ¼ä¶ÁȡζÈÖµ£¬¼ä¸ôʱ¼äÒª´óÓÚζȴ«¸ÐÆ÷ת»»Î¶Èʱ¼ä temp_value=ds18b20_read_temperture()*10;//±£ÁôζÈֵСÊýºóһλ if(temp_value<0)//¸ºÎÂ¶È { temp_value=-temp_value; temp_buf[0]=0x40;//ÏÔʾ¸ººÅ } else temp_buf[0]=0x00;//²»ÏÔʾ temp_buf[1]=gsmg_code[temp_value/1000];//°Ùλ temp_buf[2]=gsmg_code[temp_value%1000/100];//ʮλ temp_buf[3]=gsmg_code[temp_value%1000%100/10]|0x80;//¸öλ+СÊýµã temp_buf[4]=gsmg_code[temp_value%1000%100%10];//СÊýµãºóһλ smg_display(temp_buf,4); } }在代码中加入蜂鸣器报警
好的,你可以在代码中加入以下代码段,就可以实现蜂鸣器报警功能了:
```
if(temp_value >= 300) // 如果温度超过 30 度
{
BEEP = 0; // 蜂鸣器响
}
else
{
BEEP = 1; // 蜂鸣器不响
}
```
完整代码如下:
```
void main()
{
u8 i=0;
int temp_value;
u8 temp_buf[5];
ds18b20_init();//³õʼ»¯DS18B20
while(1)
{
i++;
if(i%50==0)//¼ä¸ôÒ»¶Îʱ¼ä¶ÁȡζÈÖµ£¬¼ä¸ôʱ¼äÒª´óÓÚζȴ«¸ÐÆ÷ת»»Î¶Èʱ¼ä
{
temp_value=ds18b20_read_temperture()*10;//±£ÁôζÈֵСÊýºóһλ
if(temp_value<0)//¸ºÎ¶È
{
temp_value=-temp_value;
temp_buf[0]=0x40;//ÏÔʾ¸ººÅ
}
else
temp_buf[0]=0x00;//²»ÏÔʾ
temp_buf[1]=gsmg_code[temp_value/1000];//°Ùλ
temp_buf[2]=gsmg_code[temp_value%1000/100];//ʮλ
temp_buf[3]=gsmg_code[temp_value%1000%100/10]|0x80;//¸öλ+СÊýµã
temp_buf[4]=gsmg_code[temp_value%1000%100%10];//СÊýµãºóһλ
smg_display(temp_buf,4);
if(temp_value >= 300) // 如果温度超过 30 度
{
BEEP = 0; // 蜂鸣器响
}
else
{
BEEP = 1; // 蜂鸣器不响
}
}
}
}
```
void main() { u8 i=0; int temp_value; u8 temp_buf[5]; ds18b20_init();//³õʼ»¯DS18B20 while(1) { i++; if(i%50==0)//¼ä¸ôÒ»¶Îʱ¼ä¶ÁȡζÈÖµ£¬¼ä¸ôʱ¼äÒª´óÓÚζȴ«¸ÐÆ÷ת»»Î¶Èʱ¼ä temp_value=ds18b20_read_temperture()*10;//±£ÁôζÈֵСÊýºóһλ if(temp_value<0)//¸ºÎÂ¶È { temp_value=-temp_value; temp_buf[0]=0x40;//ÏÔʾ¸ººÅ } else temp_buf[0]=0x00;//²»ÏÔʾ temp_buf[1]=gsmg_code[temp_value/1000];//°Ùλ temp_buf[2]=gsmg_code[temp_value%1000/100];//ʮλ temp_buf[3]=gsmg_code[temp_value%1000%100/10]|0x80;//¸öλ+СÊýµã temp_buf[4]=gsmg_code[temp_value%1000%100%10];//СÊýµãºóһλ smg_display(temp_buf,4); } }优化使在32度使蜂鸣器响
您可以参考下面的代码实现在温度达到32度时使蜂鸣器响起,并将温度显示在数码管上。
```c
#include <REG52.H>
#include <DS18B20.h>
#define uchar unsigned char
#define uint unsigned int
sbit DSPORT = P3^7; // DS18B20数据线
sbit beep = P1^7; // 蜂鸣器
uchar code smgduan[] = {
// 0~9的段码
0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f,
// A~F的段码
0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71
};
uchar code gsmg_code[] = {
// 0~9的码表,共10个
0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f
};
void delay(uint z) {
uint x, y;
for(x = z; x > 0; x--) {
for(y = 114; y > 0; y--);
}
}
void smg_display(uchar *dat, uchar num) {
uchar i;
for(i = 0; i < num; i++) {
P0 = dat[i];
P2 = 0x7f ^ (1 << i);
delay(1);
}
}
void main() {
uchar TL, TH, i = 0;
int temp_value;
uchar temp_buf[5];
ds18b20_init();
while(1) {
i++;
if(i % 50 == 0) {
ds18b20_init();
delay(1);
DS18B20WriteByte(0xcc);
DS18B20WriteByte(0x44);
delay(1);
ds18b20_init();
delay(1);
DS18B20WriteByte(0xcc);
DS18B20WriteByte(0xbe);
TL = DS18B20ReadByte();
TH = DS18B20ReadByte();
temp_value = TH;
temp_value <<= 8;
temp_value |= TL;
temp_value = temp_value / 16;
if(temp_value >= 32) {
beep = 0;
} else {
beep = 1;
}
temp_buf[0] = gsmg_code[temp_value / 1000];
temp_buf[1] = gsmg_code[temp_value % 1000 / 100];
temp_buf[2] = gsmg_code[temp_value % 1000 % 100 / 10] | 0x80;
temp_buf[3] = gsmg_code[temp_value % 1000 % 100 % 10];
smg_display(temp_buf, 4);
}
}
}
```
需要注意的是,上述代码中的`DS18B20.h`文件中需要包含DS18B20的相关函数实现,您需要根据自己的实际情况进行修改。另外,代码中使用了数码管和蜂鸣器,请根据自己实际的电路连接进行修改。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)