<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>

新聞中心

EEPW首頁(yè) > 嵌入式系統 > 設計應用 > 51單片機18B20溫度與串口實(shí)驗

51單片機18B20溫度與串口實(shí)驗

作者: 時(shí)間:2016-11-30 來(lái)源:網(wǎng)絡(luò ) 收藏
void send_number(uchar *p,uint k) //通過(guò)串口發(fā)送溫度值函數
{
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() //按鍵發(fā)送函數
{
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 ");
while(1)
{
count++;
temp=read_18b20();
show_temp(temp);
if(count>500) //約沒(méi)3秒發(fā)送一次溫度值給串口
{
send_number("The temperature is ",temp);
count=0;
}
else
{
}
}
}
//-----------------------18B20----庫函數----------------
sbit dq=P3^4; //18B20數據口
void send_1() //發(fā)送1位1函數
{
dq=0;
dq=0;
dq=0;
dq=1;
delay(2);
}
void send_0()//發(fā)送1位0函數
{
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 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 start_18b20()//啟動(dòng)18b20實(shí)質(zhì)上判斷有誤應答信號
{
static uint cnt;
dq=0;
delay(62);
dq=1;
while(dq==1) //等待拉低
{
cnt++;
if(cnt>50000)
{
cnt=0;
return 1;//等待超時(shí)判斷說(shuō)明器件壞了
}
}
while(dq==0)
{
cnt++;
if(cnt>50000)
{
cnt=0;
return 1;
}
}
return 0; //正常啟動(dòng)返回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;
}
上一頁(yè) 1 2 下一頁(yè)

關(guān)鍵詞: 51單片機18B20溫度與串

評論


技術(shù)專(zhuān)區

關(guān)閉
国产精品自在自线亚洲|国产精品无圣光一区二区|国产日产欧洲无码视频|久久久一本精品99久久K精品66|欧美人与动牲交片免费播放
<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>