|
溫度已經(jīng)能正常顯示了,但是蜂鳴器和led燈一直不合適,這個邏輯應(yīng)該怎么改
1.png (50.51 KB, 下載次數(shù): 25)
下載附件
2021-11-4 16:05 上傳
以下是主程序的代碼,根據(jù)開發(fā)板的程序修改出來的,顯示完全沒問題,就是判定等級的邏輯不合適,我覺得我寫的邏輯應(yīng)該沒啥大問題,但是根據(jù)讀取的溫度來判定等級就是不合適,判定邏輯應(yīng)該沒什么大問題,請大佬幫我看看
單片機源程序如下:
#include<common.h>
#include<l1602.h>
#include<temp.h>
sbit led1=P3^0;
sbit dz=P3^3;//蜂鳴器
uchar buf[20];
u8 s;
void LcdDisplay(int temp);
void level(int temp);
void Delay500ms() [url=]//@12.000MHz[/url]
{
unsigned char i, j, k;
i = 23;
j = 205;
k = 120;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main()
{
int x1;
float x;
led1=1;dz=1;
LCD1602_Init();//初始化液晶
delay(10);
// LCD_Clear();//清屏
// Delay500ms();
SetRowCol(0,0);
DispString("Temp: C");
SetRowCol(0,0);
DispString("Temp: C");
while(1)
{
dz=1;
LcdDisplay(Ds18b20ReadTemp());
Delay1ms(1000);//1秒一次
x1=Ds18b20ReadTemp();
x=(Ds18b20ReadTemp())*0.0625*100+0.5;
// x=22;
/***************************等級判定***************************/
if(x>=20&&x<=30)
{
SetRowCol(1,14);
DispString((u8*)"L");
//LCD_Write_Char(14,1,'L');
Delay1ms(1000);
led1=~led1;
Delay1ms(1000);
dz=0;
}
else if(x>30&&x<=40)
{
// LCD_Write_Char(14,1,'N');
// Delay1ms(1000);
led1=~led1;
Delay1ms(1000);
dz=~dz;
}
else if(x>40&&x<=50)
{
// LCD_Write_Char(14,1,'H');
// Delay1ms(1000);
led1=~led1;
Delay1ms(1000);
dz=~dz;
}
else
{
// LCD_Write_String(0,1," ");
led1=led1;
dz=dz;
}
}
/******************************************************************/
}
void LcdDisplay( int temp) //lcd顯示
{
float tp;
unsigned char datas[] = {0, 0, 0, 0, 0}; //定義數(shù)組
if(temp< 0) //當溫度值為負數(shù)
{
LCD_Write_Char(5,0,'-');//因為讀取的溫度是實際溫度的補碼,所以減1,再取反求出原碼
temp=temp-1;
temp=~temp;
tp=temp;
temp=tp*0.0625*100;
}
else
{
LCD_Write_Char(5,0,'+');
tp=temp;
temp=tp*0.0625*100;
}
datas[0] = temp / 10000;
datas[1] = temp % 10000 / 1000;
datas[2] = temp % 1000 / 100;
datas[3] = temp % 100 / 10;
datas[4] = temp % 10;
LCD_Write_Char(6,0,datas[0]+0x30);
LCD_Write_Char(7,0,datas[1]+0x30);
LCD_Write_Char(8,0,datas[2]+0x30);
LCD_Write_Char(9,0,'.');
LCD_Write_Char(10,0,datas[3]+0x30);
LCD_Write_Char(11,0,datas[4]+0x30);
}
以上是主程序,以下是18b20最后的讀取溫度程序
int Ds18b20ReadTemp()
{
int temp=0;
unsigned char tmh,tml;
Ds18b20ChangTemp(); //先寫入轉(zhuǎn)換命令
Ds18b20ReadTempCom(); //然后等待轉(zhuǎn)換完后發(fā)送讀取溫度命令
tml=Ds18b20ReadByte(); //讀取溫度值共16位,先讀低字節(jié)
tmh=Ds18b20ReadByte(); //再讀高字節(jié)
temp=tmh;
temp<<=8;
temp|=tml;
return temp;
}
|
-
-
wendu.zip
2021-11-4 15:41 上傳
點擊文件名下載附件
96.65 KB, 下載次數(shù): 1
|