新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > ATMEGA16與DS18B20數(shù)碼管顯示

ATMEGA16與DS18B20數(shù)碼管顯示

作者: 時(shí)間:2016-12-02 來源:網(wǎng)絡(luò) 收藏
一個(gè)誤差值大的DS18B20把我害得不淺,一直以為是程序錯(cuò),下狠心換了個(gè),成功了!誤了我三天去查程序

/*本程序?yàn)榘宋还碴帢O數(shù)碼管且有兩個(gè)573控制的動(dòng)態(tài)掃描*/
#include
#include
#define uchar unsigned char
#define uint unsigned int
//注code的功能是把后面的數(shù)據(jù)存在程序存貯器中,不用code就放到了隨機(jī)存貯器中.
#pragmadata:code
const table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d, 0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//不帶小數(shù)點(diǎn)
const table2[]={0xbf,0x86,0xdb,0xcf,0xe6,
0xed,0xfd,0x87,0xff,0xdf};//帶小數(shù)點(diǎn)編碼


/*如果用uchar table[]就放到了數(shù)據(jù)存貯器中。決不要這樣用,這樣占用空間多。*/
/*兩個(gè)573,段碼PA3,位碼PA4*/
//把18B20的DQ接到PC2上
#define DQ_IN DDRC&=~BIT(2)
//上一句是把DQ設(shè)為輸入
#define DQ_OUT DDRC|=BIT(2)
//上一句是把DQ設(shè)為輸出
#define DQ_SET PORTC|=BIT(2)
//上一句是把DQ設(shè)為高電平
#define DQ_CLR PORTC&=~BIT(2)
//上一句是把DQ設(shè)為低電平
#define DQ_RD PINC&BIT(2)
//上一句是從PC2中讀出數(shù)據(jù)
uchar disdata[4];
void delay(uint ms)
{
uint i,j;
for(i=ms;i>0;i--)

for(j=220;j>0;j--);

}
void delayus(uint us)
{
while(us--);
}
void show(uchar j,uchar k)
{
DDRA|=BIT(3);
DDRA|=BIT(4);
DDRB=0XFF;
PORTA|=BIT(3);
PORTB=table[j];
PORTA&=~BIT(3);

PORTB=0XFF;
PORTB&=~BIT(k);
PORTA|=BIT(4);
PORTA&=~BIT(4);
delay(5);
}

本文引用地址:http://butianyuan.cn/article/201612/324665.htm

void show_point(uchar j,uchar k)
{
DDRA|=BIT(3);
DDRA|=BIT(4);
DDRB=0XFF;
PORTA|=BIT(3);
PORTB=table2[j];
PORTA&=~BIT(3);

PORTB=0XFF;
PORTB&=~BIT(k);
PORTA|=BIT(4);
PORTA&=~BIT(4);
delay(5);
}
rest_18B20()
{
uchar i ;
DQ_OUT;//把DQ設(shè)為輸出
DQ_SET;//拉高DQ
delayus(5);
DQ_CLR;//拉低DQ
delayus(800);
DQ_SET;//拉高DQ
//delayus(550);
DQ_IN;//把DQ設(shè)為輸入
i=DQ_RD;
delayus(800);
return i;
}
void write_18B20(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
DQ_OUT;//把DQ設(shè)為輸出
DQ_CLR;//拉低DQ
delayus(30);
if(dat&0x01)//判斷當(dāng)前位是不是高,如果是高就執(zhí)行下面語句
{
DQ_SET;//拉高DQ,讓總線采樣到1
}
else
{
DQ_CLR;//拉低DQ
}
delayus(80);
DQ_SET;//釋放總線
dat=dat>>1;
}
}
uchar read_18B20()
{
uchar i,value;
for(i=0;i<8;i++)
{

DQ_OUT;//把DQ設(shè)為輸出
DQ_CLR;//拉低DQ
value=value>>1;
delayus(10);
DQ_SET;//釋放總線 等MCU采樣
DQ_IN;//把DQ設(shè)為輸入才能采樣
if(DQ_RD)
{
value|=0x80;
}

delayus(100);
// DQ_SET;//釋放總線
}
return value;
}

void display(uint tvalue)
{
disdata[0]=tvalue/1000;//百位數(shù)
disdata[1]=tvalue%1000/100;//十位數(shù)
disdata[2]=tvalue%100/10;//個(gè)位數(shù)
disdata[3]=tvalue%10;//小數(shù)位
show(disdata[0],0);
delay(2);
show(disdata[1],1);
delay(2);
show_point(disdata[2],2);
//show(0x80,2);
show(disdata[3],3);
//下面這種顯示方式也可以
/* uint i,temp[4];
for(i=0;i<4;i++)
{
temp[3-i]=dat%10;
dat=dat/10;
}

for(i=0;i<4;i++)
{
show(temp[i],i);
delay(2);*/
/* // 以下三句為關(guān)閉顯示,動(dòng)態(tài)顯示時(shí)要用的
DDRA|=BIT(3);
DDRA|=BIT(4);
DDRB=0XFF;
}*/
}
void main()
{int L,H;
uint tvalue;
while(1)
{
rest_18B20();
write_18B20(0xcc);//跳過讀序列號
write_18B20(0x44);//啟動(dòng)溫度轉(zhuǎn)換
delayus(50);
//讀之前再復(fù)位一次
rest_18B20();
write_18B20(0xcc);//跳過讀序列號
write_18B20(0xbe);//啟動(dòng)開始采樣,讀暫存器
delayus(50);
L=read_18B20();//先讀低八位字節(jié)
H=read_18B20();//后讀高八位
tvalue=H;
tvalue<<=8;
tvalue=tvalue|L;
tvalue=tvalue*0.625;//擴(kuò)大10倍
display(tvalue);
}
}



評論


技術(shù)專區(qū)

關(guān)閉