熱門(mén): 51單片機(jī) | 24小時(shí)必答區(qū) | 單片機(jī)教程 | 單片機(jī)DIY制作 | STM32 | Cortex M3 | 模數(shù)電子 | 電子DIY制作 | 音響/功放 | 拆機(jī)樂(lè)園 | Arduino | 嵌入式OS | 程序設(shè)計(jì)
![]() |
發(fā)布時(shí)間: 2018-12-2 15:54
正文摘要:初學(xué)單片機(jī),老師比較傾向讓學(xué)生自學(xué),所以很多東西要在網(wǎng)上自己學(xué)習(xí),針對(duì)這個(gè)設(shè)計(jì)感覺(jué)無(wú)從下手,求大神們幫忙,基于我們這種板子幫我出個(gè)程序我學(xué)習(xí)一下,謝謝! |
網(wǎng)上自己隨便找都有的,ds18b20主要去學(xué)它的單串口通信線 |
這個(gè)程序要自己把管腳改了 |
這種百度一下就很多的,我是百度里學(xué)會(huì)的C51 |
#include"reg51.h" #include"intrins.h" #define uchar unsigned char #define uint unsigned int sbit DSPORT=P3^7; sbit LSA=P2^2; sbit LSB=P2^3; sbit LSC=P2^4; uchar code smgduan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //uchar DisplayData[8]; void delay(uint z) { while(z--); } void delayus(uint x,uchar y) { uchar i,j; for(i=x;i>0;i--) for(j=y;j>0;j--); } void Ds18b20Init() { uchar st=1; DSPORT = 1; _nop_();_nop_(); while(st) { DSPORT = 0; //時(shí)序很重要,一定要準(zhǔn)確; delayus(149,1); //延時(shí)750us DSPORT = 1; delayus(4,4); //延時(shí)15~60 us; if(DSPORT == 0) //當(dāng) DSPORT=0時(shí),初始化才是正確的; st = 1; else st = 0; delayus(99,1); //延時(shí)500us } } void Ds18b20WriteByte(uchar dat) //寫(xiě)是判斷數(shù)據(jù)再寫(xiě)入, { uchar i,temp; DSPORT = 1; _nop_();_nop_(); for(i=0;i<8;i++) { DSPORT = 0; delayus(0,0); //延時(shí)20us temp=dat&0x01; // delayus(9,1); if(temp == 1) //判斷,然后把數(shù)據(jù)置給總線(把數(shù)據(jù)送走,好判斷) DSPORT = 1; delayus(2,2); //延時(shí)45us dat>>=1; DSPORT = 1; } } uchar Ds18b20ReadByte() //讀是有了數(shù)據(jù)再給數(shù)據(jù) { uchar i,dat; // static bit j; for(i=0;i<8;i++) { dat>>=1; DSPORT=1; _nop_();_nop_(); DSPORT=0; _nop_();_nop_(); _nop_();_nop_(); _nop_();_nop_(); DSPORT=1; //當(dāng)至高時(shí),傳送數(shù)據(jù); _nop_();_nop_();_nop_();_nop_(); // j=DSPORT; if(DSPORT) //判斷總線,高位傳送數(shù)據(jù); dat=dat|0x80; delayus(1,1); } return dat; } void Ds18b20ChangTemp() { Ds18b20Init(); delayus(1,100); Ds18b20WriteByte(0xcc); //跳過(guò)ROM Ds18b20WriteByte(0x44); } void Ds18b20ReadTempCom() { Ds18b20Init(); delayus(1,100); Ds18b20WriteByte(0xcc); Ds18b20WriteByte(0xbe); } int Ds18b20ReadTemp() { int temp=0; uchar tmh,tml; Ds18b20ChangTemp(); Ds18b20ReadTempCom(); tml=Ds18b20ReadByte(); tmh=Ds18b20ReadByte(); temp=(tmh*256+tml)*0.0625*100+0.5; //乘100使其小數(shù)點(diǎn)前兩位,加0.5使其四舍五入; return temp; } void display(int temp) { uchar i; /***在此不能這樣轉(zhuǎn)換,因?yàn)樯厦娴膖emp已經(jīng)轉(zhuǎn)換為一個(gè)數(shù)值,取反出來(lái)有錯(cuò), 這樣只是數(shù)碼管上的數(shù)值取反了,而不是DS18B20公式取反! ****/ /* int tp; // float 不能取位; if(temp<0) { DisplayData[0]=0x40; //if小于0,則前面加一個(gè) '-' ; temp=temp-1; temp=~temp; tp=temp; } else //大于0,則前面的不顯示 ; { DisplayData[0]=0x00; tp=temp; } */ for(i=0;i<4;i++) { switch(i) { // case 0: LSA=0;LSB=0;LSC=1; P0=DisplayData[0]; break; case 0: LSA=1;LSB=1;LSC=0; P0=smgduan[temp/1000]; break; case 1: LSA=0;LSB=1;LSC=0; P0=smgduan[temp%1000/100]|0x80; break; case 2: LSA=1;LSB=0;LSC=0; P0=smgduan[temp%100/10]; break; case 3: LSA=0;LSB=0;LSC=0; P0=smgduan[temp%10]; break; } delay(100); //延時(shí)必須寫(xiě)在清屏前面, P0=0x00; } } void main() { while(1) { display(Ds18b20ReadTemp()); } } 拿去吧,這個(gè)是c51的板子程序,管腳改了,數(shù)碼管程序按照你的板子寫(xiě)就可以顯示了,只能幫你到這里了,別的靠你自己加油。 |
Powered by 單片機(jī)教程網(wǎng)