最近幾天,一直在用cvavr編寫ds18b20的程序,用protues進行仿真,一直沒成功過,很糾結。讀出來的數據不是ffff,就是37.6,剛剛讀出個-123.9出來了,調ds18b20 的溫度lcd顯示也不變,估計是沒有讀出數據來,求高手指點迷津,下面是程序,麻煩大俠指出問題所在,希望有好心人分享下杰作(最好是通過仿真的)如果是分模塊做的希望把模塊中的子程序也附上,本人剛上手,很多東西都不是很熟,有的話發我郵箱非常感謝! 我是用頭文件的形式把ds要用的函數嵌進主函數的,我用的是lcd顯示,lcd程序也是沒有調用函數,是自己編的,是ok的,現在問題就是讀不出數據來 要說明的幾點就是我在<dspredef.h>中已經定義好了端口: #ifndef _dspredef_h_ #define _dspredef_h_ #include <mega16.h> #include <delay.h> //#define nop() #asm ("nop")#asm ("nop")#asm ("nop")#asm ("nop")#asm ("nop")#asm ("nop")#asm ("nop")#asm ("nop")#asm ("nop")#asm ("nop")#asm ("nop") #define uchar unsigned char #define uint unsigned int
#define openlcd DDRC = 0xc0;DDRB = 0xff #define lcden PORTC.6 #define lcdrs PORTC.7 #define outdsdq DDRA.7 = 1 #define indsdq DDRA.7 = 0 #define ds18dqw PORTA.7 #define ds18dqr PINA.7 #endif
上面的是定義文件 #ifndef _dsfun_h_ #define _dsfun_h_ #include <dspredef.h> uchar numtable[] = "0123456789. -"; uchar tmpcode[6] = {0,0,0,0,0,0}; uchar dsreset() { uchar a; outdsdq; ds18dqw = 0; #asm("cli") delay_us(500); #asm("sei") ds18dqw = 1; #asm("cli") delay_us(100); #asm("sei") indsdq; a = ds18dqr; a = ds18dqr; #asm("cli") delay_us(500); #asm("sei") return (a); } uchar dsread() { uchar i,k = 0,buf; i = 8; while(i--) { outdsdq; ds18dqw = 0; #asm("cli") delay_us(10); #asm("sei") ds18dqw = 1; indsdq; k >>= 1; buf = ds18dqr; buf = ds18dqr; if(buf)k |= 0x80; #asm("cli") delay_us(50); #asm("sei") } return (k); } void dswrite(uchar dat) { uint j; outdsdq; for(j = 1;j <= 8;j++) { ds18dqw = 0; #asm("cli") delay_us(10); #asm("sei") if(dat & 0x01) { ds18dqw =1; // #asm("cli") // delay_us(45); // #asm("sei") } // else // { // ds18dqw = 0; // // #asm("cli") // delay_us(45); // #asm("sei") // // ds18dqw = 1; // // #asm("cli") // delay_us(35); // #asm("sei") // } #asm("cli") delay_us(100); #asm("sei") ds18dqw = 1; dat >>= 1; } } void dschange() { dsreset(); dswrite(0xcc); dswrite(0x44); } uint dstmp() { uchar tmpl,tmph; uint tmp; dsreset(); dswrite(0xcc); dswrite(0xbe); tmpl = dsread(); tmph = dsread(); tmp = tmpl; tmp <<= 8; tmp |= tmph; return (tmp); } void tmpdispose(uint t) { if(t <= 0x07ff) tmpcode[0] = numtable[11]; else { tmpcode[0] = numtable[12]; t = ~t +1; } t *= 0.625; tmpcode[1] = numtable[t / 1000]; tmpcode[2] = numtable[(t % 1000) / 100]; tmpcode[3] = numtable[(t % 100) / 10]; tmpcode[4] = numtable[10]; tmpcode[5] = numtable[t % 10]; } #endif |