51單片機18B20溫度與串口實驗
void send_number(uchar *p,uint k) //通過串口發送溫度值函數
{
while(*p)
{
send_byte(*p);
p++;
}
send_byte( );
send_byte( );
send_byte(k/100+48);
send_byte(k%100/10+48);
send_byte(.);
send_byte(k%10+48);
send_byte();
send_byte();
}
/*
void key8() //按鍵發送函數
{
static uchar i,j;
if(k8==0)
{
if(i==0)
{
j++;
if(j>3)
{
i=1;j=0;
send_number("the temperature is ",temp);
}
}
}
else
{
i=0;
j=0;
}
}
*/
{
while(*p)
{
send_byte(*p);
p++;
}
send_byte( );
send_byte( );
send_byte(k/100+48);
send_byte(k%100/10+48);
send_byte(.);
send_byte(k%10+48);
send_byte();
send_byte();
}
/*
void key8() //按鍵發送函數
{
static uchar i,j;
if(k8==0)
{
if(i==0)
{
j++;
if(j>3)
{
i=1;j=0;
send_number("the temperature is ",temp);
}
}
}
else
{
i=0;
j=0;
}
}
*/
void main()
{
{
serial_init();//串口初始化
send_byte(12); //超級終端清屏
send_str("serial_init is ok "); //初始化好壞標示字符串
str_init(); //串口中斷初始化
send_str("str_init is ok ");
send_byte(12); //超級終端清屏
send_str("serial_init is ok "); //初始化好壞標示字符串
str_init(); //串口中斷初始化
send_str("str_init is ok ");
while(1)
{
count++;
temp=read_18b20();
show_temp(temp);
if(count>500) //約沒3秒發送一次溫度值給串口
{
send_number("The temperature is ",temp);
count=0;
}
else
{
}
}
}
{
count++;
temp=read_18b20();
show_temp(temp);
if(count>500) //約沒3秒發送一次溫度值給串口
{
send_number("The temperature is ",temp);
count=0;
}
else
{
}
}
}
//-----------------------18B20----庫函數----------------
sbit dq=P3^4; //18B20數據口
void send_1() //發送1位1函數
{
dq=0;
dq=0;
dq=0;
dq=1;
delay(2);
}
{
dq=0;
dq=0;
dq=0;
dq=1;
delay(2);
}
void send_0()//發送1位0函數
{
dq=0;
delay(2);
dq=1;
}
{
dq=0;
delay(2);
dq=1;
}
void Tsend_byte(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
if(dat&0x01)send_1();
else send_0();
dat>>=1;
}
}
{
uchar i;
for(i=0;i<8;i++)
{
if(dat&0x01)send_1();
else send_0();
dat>>=1;
}
}
uchar read_byte() //18B20讀取數據函數
{
uchar i,byte;
for(i=0;i<8;i++)
{
dq=0;
dq=0;
dq=1;
dq=1;
byte>>=1;
if(dq==1)
{
byte=byte+0x80; //如果采樣為1則最高位為1
}
delay(3);
}
return byte;
}
{
uchar i,byte;
for(i=0;i<8;i++)
{
dq=0;
dq=0;
dq=1;
dq=1;
byte>>=1;
if(dq==1)
{
byte=byte+0x80; //如果采樣為1則最高位為1
}
delay(3);
}
return byte;
}
uchar start_18b20()//啟動18b20實質上判斷有誤應答信號
{
static uint cnt;
dq=0;
delay(62);
dq=1;
while(dq==1) //等待拉低
{
cnt++;
if(cnt>50000)
{
cnt=0;
return 1;//等待超時判斷說明器件壞了
}
}
while(dq==0)
{
cnt++;
if(cnt>50000)
{
cnt=0;
return 1;
}
}
return 0; //正常啟動返回0值
}
{
static uint cnt;
dq=0;
delay(62);
dq=1;
while(dq==1) //等待拉低
{
cnt++;
if(cnt>50000)
{
cnt=0;
return 1;//等待超時判斷說明器件壞了
}
}
while(dq==0)
{
cnt++;
if(cnt>50000)
{
cnt=0;
return 1;
}
}
return 0; //正常啟動返回0值
}
uint read_18b20()//讀取溫度值函數
{
uchar TH,TL;
uint temp;
start_18b20();
Tsend_byte(0xcc);
Tsend_byte(0x44);
;;
start_18b20();
Tsend_byte(0xcc);
Tsend_byte(0xbe);
TL=read_byte();
TH=read_byte();
temp=TH;
temp<<=8;
temp=temp+TL;
/*if(temp&0xf800) //如果最高位為1則是溫度為負值 .可以設置溫度負值符號
{
temp=~temp+1;
}
*/
temp=temp*0.0625*10; //顯示小數部分
return temp;
主站蜘蛛池模板:
曲靖市|
台南县|
昭平县|
万荣县|
山丹县|
将乐县|
广安市|
闸北区|
新郑市|
八宿县|
河东区|
西藏|
普兰店市|
伊川县|
莆田市|
奉化市|
连江县|
突泉县|
和龙市|
镇宁|
谷城县|
长宁区|
阿克陶县|
宁南县|
横峰县|
介休市|
庆城县|
都昌县|
安西县|
苏尼特右旗|
西青区|
湘乡市|
秦安县|
江源县|
英德市|
合江县|
天全县|
米林县|
师宗县|
榆中县|
台北市|
{
uchar TH,TL;
uint temp;
start_18b20();
Tsend_byte(0xcc);
Tsend_byte(0x44);
;;
start_18b20();
Tsend_byte(0xcc);
Tsend_byte(0xbe);
TL=read_byte();
TH=read_byte();
temp=TH;
temp<<=8;
temp=temp+TL;
/*if(temp&0xf800) //如果最高位為1則是溫度為負值 .可以設置溫度負值符號
{
temp=~temp+1;
}
*/
temp=temp*0.0625*10; //顯示小數部分
return temp;
關鍵詞:
51單片機18B20溫度與串
評論