熱門: 51單片機(jī) | 24小時(shí)必答區(qū) | 單片機(jī)教程 | 單片機(jī)DIY制作 | STM32 | Cortex M3 | 模數(shù)電子 | 電子DIY制作 | 音響/功放 | 拆機(jī)樂園 | Arduino | 嵌入式OS | 程序設(shè)計(jì)
|
發(fā)布時(shí)間: 2016-6-17 18:51
正文摘要:小白制作個(gè)數(shù)字溫度計(jì),4位共陽數(shù)碼管,位選p0.1 2 3 4,段選p2,ds18b20測(cè)溫,接p3.7,用下面程序運(yùn)行后第二三四位數(shù)碼管顯示正確,就是第一位數(shù)碼管顯示不對(duì),求大神修改#include"reg51.h" #define uchar uns ... |
|
按下面試一試(省略號(hào)的內(nèi)容自己加上): P2=table[temp/100]; //顯示百位 delay1(5); wx1=1; wx2=0; P2=table[temp%100/10]; //顯示十位 ...... ...... P2=table[temp%10]; //顯示個(gè)位 ...... ...... P2=table[temp/10]+0x80; /加上0x80就顯示小數(shù)點(diǎn)了。 |
461534727 發(fā)表于 2016-6-18 15:10 我的只有接電阻,芯片為STC12C5204,設(shè)置為推挽輸出 |
geminiguy_07 發(fā)表于 2016-6-18 14:06 我的電路沒用鎖存,就用了pnp驅(qū)動(dòng)三極管而已 |
461534727 發(fā)表于 2016-6-18 13:24 那可能是我倆的硬件電路不一樣 我的是單片機(jī)直接接數(shù)碼管,不用連鎖存器 |
geminiguy_07 發(fā)表于 2016-6-18 10:47 void wenduxianshi(unsigned int temp) { unsigned char buf[4]; unsigned char i; for(i=0;i<4;i++) //i=0 個(gè)位 { buf = temp % 10; temp /= 10; } if(buf[3] == 0) { if(buf[2] == 0) { if(buf[1] == 0) { wx1=0; P2=table[buf[0]]; delay1(8); } else { wx2=0; P2=table[buf[1]]; delay1(4); wx1=0; P2=table[buf[0]]; delay1(4); } } else { wx3=0; P2=table[buf[2]]; delay1(2); wx2=0; P2=table[buf[1]]; delay1(4); wx1=0; P2=table[buf[0]]; delay1(4); } } else { wx4=0; P2=table[buf[3]]; delay1(2); wx3=0; P2=table[buf[2]]; delay1(2); wx2=0; P2=table[buf[1]]; delay1(4); wx1=0; P2=table[buf[0]]; delay1(4); } } 改成這樣后,顯示全都不正確了 |
ahshmj 發(fā)表于 2016-6-18 11:40 那顯示函數(shù)應(yīng)該怎么更改呀 |
|
樓主給出的程序一共就三位,十位個(gè)位和小數(shù)。就沒有百位。 另外,你的小數(shù)的顯示為什么要取余?“P2=table[xs%10]; //顯示個(gè)位” 而不是除以10?(這樣,其實(shí)顯示的是小數(shù)的百分位,不是十分位,更不是個(gè)位) 其他的沒有仔細(xì)看。估計(jì)也有些小問題。 |
|
顯示程序試一試換成這樣 void wenduxianshi(unsigned int data) { unsigned char buf[4]; unsigned char i; for(i=0;i<4;i++) //i=0 個(gè)位 { buf[i] = data % 10; data /= 10; } if(buf[3] == 0) { if(buf[2] == 0) { if(buf[1] == 0) { wx1=1; P2=table[buf[0]]; delay1ms(8); } else { wx2=1; P2=table[buf[1]]; delay1ms(4); wx1=1; P2=table[buf[0]]; delay1ms(4); } } else { wx3=1; P2=table[buf[2]]; delay1ms(2); wx2=1; P2=table[buf[1]]; delay1ms(4); wx1=1; P2=table[buf[0]]; delay1ms(4); } } else { wx4=1 P2=table[buf[3]]; delay1ms(2); wx3=1; P2=table[buf[2]]; delay1ms(2); wx2=1; P2=table[buf[1]]; delay1ms(4); wx1=1; P2=table[buf[0]]; delay1ms(4); } } 注:根據(jù)需要適當(dāng)修改一下 |
| 該案例程序包括2部分:測(cè)溫和顯示。顯示電路不一定采用上述電路。讀別人的程序用于參考,吸收自己有用的部分,然后改寫。這樣才會(huì)有幫助和提高。 |
zl2168 發(fā)表于 2016-6-17 19:41 可是我的原理圖沒用鎖存芯片,就用pnp驅(qū)動(dòng)數(shù)碼管而已 |
|
本帖最后由 zl2168 于 2016-6-17 19:42 編輯 DS18B20 測(cè)溫 先Proteus仿真一下,確認(rèn)有效。
實(shí)例97 DS18B20測(cè)溫.rar
(51.78 KB, 下載次數(shù): 11)
以上摘自張志良編著《單片機(jī)實(shí)驗(yàn)實(shí)訓(xùn)100例》 ISBN 978-7-5124-1603-1,北航社出版 書中電路和程序設(shè)計(jì)有詳細(xì)說明,程序語句條條有注解。 |
Powered by 單片機(jī)教程網(wǎng)