熱門: 51單片機(jī) | 24小時(shí)必答區(qū) | 單片機(jī)教程 | 單片機(jī)DIY制作 | STM32 | Cortex M3 | 模數(shù)電子 | 電子DIY制作 | 音響/功放 | 拆機(jī)樂園 | Arduino | 嵌入式OS | 程序設(shè)計(jì)
|
發(fā)布時(shí)間: 2017-9-11 12:03
正文摘要:本帖最后由 素還真 于 2017-9-11 12:06 編輯 #include<reg51.h> #include<intrins.h> #define uint unsigned int #define uchar unsigned char sbit DS=P1^6; uint temp; unsigned char code ta ... |
| DS18B20采集溫度的時(shí)候不能有中斷程序 |
| 你好,這個(gè)應(yīng)該是程序出了問題,看數(shù)碼管的代碼有沒問題 |
|
本帖最后由 zl2168 于 2017-9-11 21:33 編輯 給你介紹一個(gè)18b20的案例,自己對照查錯(cuò)吧! 先Proteus仿真一下,確認(rèn)有效。
實(shí)例97 DS18B20測溫.rar
(51.78 KB, 下載次數(shù): 8)
以上摘自張志良編著《80C51單片機(jī)仿真設(shè)計(jì)實(shí)例教程——基于Keil C和Proteus》清華大學(xué)出版社ISBN 978-7-302-41682-1,內(nèi)有常用的單片機(jī)應(yīng)用100案例,用于仿真實(shí)驗(yàn)操作,電路與程序真實(shí)可靠可信可行。仿真電路和Hex文件能在清華出版社網(wǎng)站免費(fèi)下載,程序源代碼只能到書上看了。到圖書館借,或到新華書店翻閱,或到網(wǎng)上書店打折購買。 |
| 另外代碼要寫在return前,不過這個(gè)仿真其實(shí)沒有用到中斷,所以沒什么影響。謝謝大家的幫助。 |
|
我下午仔細(xì)研究了一下,主要是兩個(gè)問題。 void DSrun(void) { bit ask; EA=0; drst(); delay(1); if(ask==0) { write(0xcc); write(0x44); } EA=1; } 進(jìn)行溫度轉(zhuǎn)換之后還要讀取RAM中存儲(chǔ)的數(shù)據(jù)。就是在write(0x04)后再進(jìn)行一次初始化,然后用0xbe指令讀取數(shù)據(jù)。 P0=table[A1]; P2=0x01; delayms(1); P0=table[A2]; P2=0x02; delayms(1); 這個(gè)數(shù)碼管顯示程序中P2口位選使能寫錯(cuò)了,因?yàn)椴皇炀毞傅玫图夊e(cuò)誤。 |
| 如果還是不行,你可以先把數(shù)碼管程序搞定,再調(diào)試18b20.仿真和硬件在時(shí)間上還是有很大差別的 |
|
#include<reg51.h> #include<intrins.h> #define uint unsigned int #define uchar unsigned char sbit DS=P1^6; uint temp; unsigned char code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf}; unsigned char code table1[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10}; void delay(uint count) { uint i; while(count) { i=200; while(i>0) i--; count--; } } void delayms(uchar x) { uint i,j; for(i=x;i>0;i--) for(j=120;j>0;j--); } void delay10us(uchar t) { do { _nop_();_nop_();_nop_();_nop_(); _nop_();_nop_();_nop_();_nop_(); } while(--t); } void Init_Com(void) { TMOD = 0x20; PCON = 0x00; SCON = 0x50; TH1 = 0xFd; TL1 = 0xFd; TR1 = 1; } bit drst(void) { bit ask; EA=0; DS=0; delay10us(65); DS=1; delay10us(6); ask=DS; while(!DS); return ask; EA=1; } bit readbit(void) { bit dat; EA=0; DS=0;_nop_(); DS=1;_nop_();_nop_(); dat=DS; delay10us(6); return (dat); EA=1; } uchar read(void) { uchar i,j,dat; dat=0; EA=0; for(i=1;i<=8;i++) { j=readbit(); dat=(j<<7)|(dat>>1); //讀出的數(shù)據(jù)最低位在最前面,這樣剛好一個(gè)字節(jié)在DAT里 } return(dat); EA=1; } void write(uchar dat) { uchar j; bit testb; EA=0; for(j=1;j<=8;j++) { testb=dat&0x01; dat=dat>>1; if(testb) { DS=0; _nop_();_nop_(); DS=1; delay10us(6); } else { DS=0; //write 0 delay10us(6); DS=1; _nop_();_nop_(); } }EA=1; } void DSrun(void) { bit ask; EA=0; drst(); delay(1); if(ask==0) { write(0xcc); write(0x44); } EA=1; } uint tmp() { float tt; uchar a,b; EA=0; DSrun(); a=read(); b=read(); temp=b; temp<<=8; temp=temp|a; tt=temp*0.0625; temp=tt*10+0.5; return temp; EA=1; } void display(uint temp) { uchar A1,A2,A3,A4,A2s,A3s,ser; ser=temp/10; ser=SBUF; A1=temp/1000; //這里是為了顯示正負(fù),目前沒有實(shí)現(xiàn)。 A2s=temp%1000; A2=A2s/100; A3s=A2s%100; A3=A3s/10; A4=A3s%10; P0=table[A1]; P2=0x01; delayms(1); P0=table[A2]; P2=0x02; delayms(1); P0=table1[A3]; P2=0x04; delayms(1); P0=table[A4]; P2=0x08; delayms(1); } void main() { Init_Com(); while(1) { display(tmp()); } } |
| 參與人數(shù) 1 | 黑幣 +10 | 收起 理由 |
|---|---|---|
|
| + 10 | 回帖助人的獎(jiǎng)勵(lì)! |
Powered by 單片機(jī)教程網(wǎng)