基于msp430溫度數(shù)碼管顯示_第1頁(yè)
基于msp430溫度數(shù)碼管顯示_第2頁(yè)
基于msp430溫度數(shù)碼管顯示_第3頁(yè)
基于msp430溫度數(shù)碼管顯示_第4頁(yè)
基于msp430溫度數(shù)碼管顯示_第5頁(yè)
已閱讀5頁(yè),還剩5頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、./*程序功能:用ds18b20 測(cè)量室溫并在數(shù)碼管上顯示。-測(cè)試說明:觀察顯示溫度數(shù)值。*/#include #include ds18b20.h#include ds18b20.c#define wei_h p5out|= bit5#define wei_l p5out&= bit5#define duan_lp6out &= bit6#define duan_hp6out |= bit6/要顯示的6 位溫度數(shù)字uchar dn6;/數(shù)碼管七段碼;0-fuchar scandata16 = 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07, 0x7f,0x6f

2、,0x77,0x7c,0x39,0x5e,0x79,0x71;/數(shù)碼管位選變量uchar cnt = 0;void disp_numb(uint temper);/*主函數(shù) */void main(void)/* 下面六行程序關(guān)閉所有的io 口 */p5dir = 0xff;p5out = 0xff;p1dir = 0xff;p1out = 0xff;p2dir = 0xff;p2out = 0xff;p3dir = 0xff;p3out = 0xff;p4dir = 0xff;p4out = 0xff;p5dir = 0xff;p5out = 0xff;p6dir = 0xff;p6out

3、= 0xff;uchar i;wdtctl=wdtpw+wdthold;/*- 選擇系統(tǒng)主時(shí)鐘為8mhz-*/bcsctl1 &= xt2off;/ 打開 xt2 高頻晶體振蕩器doifg1 &= ofifg;/ 清除晶振失敗標(biāo)志.for (i = 0xff; i 0; i-);/等待 8mhz 晶體起振while (ifg1 & ofifg);/晶振失效標(biāo)志仍然存在?bcsctl2 |= selm_2 + sels;/mclk和 smclk選擇高頻晶振p6dir |= bit6;p6out |= bit6;/ 關(guān)閉電平轉(zhuǎn)換p5dir |= bit5;p5out |= bit5;/關(guān)閉電平轉(zhuǎn)換

4、p6dir |= bit7;p6out |= bit7; /關(guān)閉蜂鳴器/ 設(shè)置看門狗定時(shí)器,初始化控制數(shù)碼管的iowdtctl = wdt_adly_1_9;ie1 |= wdtie;/ p4dir = 0xff;/ p5dir = 0xff;/ p5out = 0xff; /p4out = 0xff; /p6dir = 0xff; /p6out = 0xff;/計(jì)數(shù)時(shí)鐘選擇smlk=8mhz , 1/8 分頻后為1mhztactl |= tassel_2 + id_3;/打開全局中斷_eint();/循環(huán)讀數(shù)顯示while(1)disp_numb(do1convert();/ 不停地轉(zhuǎn)換顯示

5、/*函數(shù)名稱: watchdog_timer功能:看門狗定時(shí)器中斷服務(wù)函數(shù),進(jìn)行數(shù)碼管動(dòng)態(tài)掃描參數(shù):無返回值:無*/#pragma vector = wdt_vector_interrupt void watchdog_timer(void)/ p4out = 0xff;/ wei_h;/ wei_l;p4out = scandatadn5-cnt;.if(cnt=1) p4out |= bit7;/ 在第二位顯示小數(shù)點(diǎn)duan_h;duan_l;p4out = (1cnt);wei_h;wei_l;cnt+;if(cnt = 6) cnt = 0;/*函數(shù)名稱: disp_numb功能:將從

6、ds18b20 讀取的 11bit 溫度數(shù)據(jù)轉(zhuǎn)換成數(shù)碼管顯示的溫度數(shù)字參數(shù): temper-11bit 溫度數(shù)據(jù)返回值:無*/void disp_numb(uint temper)uchar i;for(i = 0;i = 10)dn2 -= 10;.dn3 += 1;if(temper&bit3)dn3 += 5;if(temper & bit4)dn4 += 1;if(temper & bit5)dn4 += 2;if(temper & bit6)dn4 += 4;if(temper & bit7)dn4 += 8;if(dn4 = 10)dn4 -= 10;dn5 += 1;if(tem

7、per & bit8)dn4 += 6;dn5 += 1;if(dn4 = 10)dn4 -= 10;dn5 += 1;if(temper & bit9)dn4 += 2;dn5 += 3;if(dn4 = 10).dn4 -= 10;dn5 += 1;if(temper & bita)dn4 += 4;dn5 += 6;if(dn4 = 10)dn4 -= 10;dn5 += 1;if(dn5 = 10)dn5 -= 10;/ /*18b20.c*/ #include typedef unsigned char uchar;typedef unsigned intuint;#define d

8、q1 p1out |= bit6#define dq0 p1out &= bit6#define dq_inp1dir &= bit6#define dq_outp1dir |= bit6#define dq_val(p1in & bit6)/*函數(shù)名稱: delaynus功能:實(shí)現(xiàn) n 個(gè)微秒的延時(shí)參數(shù): n- 延時(shí)長(zhǎng)度返回值:無說明:定時(shí)器 a 的計(jì)數(shù)時(shí)鐘是1mhz ,cpu 主頻 8mhz所以通過定時(shí)器延時(shí)能夠得到極為精確的us 級(jí)延時(shí)*/void delaynus(uint n)ccr0 = n;.tactl |= mc_1;/ 增計(jì)數(shù)到ccr0while(!(tactl & bit0

9、);/ 等待tactl &= mc_1;/ 停止計(jì)數(shù)tactl &= bit0;/ 清除中斷標(biāo)志/*函數(shù)名稱: init_18b20功能:對(duì) ds18b20 進(jìn)行復(fù)位操作參數(shù):無返回值:初始化狀態(tài)標(biāo)志:1- 失敗, 0-成功*/uchar init_18b20(void)uchar error;dq_out;/ 拉高_(dá)dint();/ 關(guān)閉中斷dq0;/ 拉低delaynus(500);dq1;/ 拉高delaynus(55);dq_in;/ 方向設(shè)置輸入_nop();if(dq_val)/輸入值為1error = 1;/初始化失敗else/輸入值為0error = 0;/初始化成功dq_ou

10、t;/ 設(shè)為輸出dq1;/拉高_(dá)eint();/ 中斷使能delaynus(400);return error;/*函數(shù)名稱: write_18b20功能:向 ds18b20 寫入一個(gè)字節(jié)的數(shù)據(jù)參數(shù): wdata-寫入的數(shù)據(jù)返回值:無.*/void write_18b20(uchar wdata)uchar i;_dint();for(i = 0; i = 1;delaynus(50);/ 延時(shí) 50usdq1;delaynus(10);/ 延時(shí) 10us_eint();/*函數(shù)名稱: read_18b20功能:從 ds18b20 讀取一個(gè)字節(jié)的數(shù)據(jù)參數(shù):無返回值:讀出的一個(gè)字節(jié)數(shù)據(jù)*/uch

11、ar read_18b20(void)uchar i;uchar temp = 0;_dint();for(i = 0;i = 1;dq0;delaynus(6);/ 延時(shí) 6usdq1;delaynus(8);/ 延時(shí) 9usdq_in;_nop();if(dq_val)temp |= 0x80;delaynus(45);/ 延時(shí) 45usdq_out;dq1;.delaynus(10);/ 延時(shí) 10us_eint();returntemp;/讀到的一個(gè)字節(jié)/*函數(shù)名稱: skip功能:發(fā)送跳過讀取產(chǎn)品id 號(hào)命令參數(shù):無返回值:無*/void skip(void)write_18b20(

12、0xcc);/*函數(shù)名稱: convert功能:發(fā)送溫度轉(zhuǎn)換命令參數(shù):無返回值:無*/void convert(void)write_18b20(0x44);/*函數(shù)名稱: read_sp功能:發(fā)送讀scratchpad命令參數(shù):無返回值:無*/void read_sp(void)write_18b20(0xbe);/*函數(shù)名稱: readtemp功能:從 ds18b20 的 scratchpad 讀取溫度轉(zhuǎn)換結(jié)果參數(shù):無返回值:讀取的溫度數(shù)值*/uint readtemp(void).uchar temp_low;uinttemp;temp_low = read_18b20();/ 讀低位temp = read_18b20();/ 讀高位temp = (temp 0;i-)delaynus(60000); / 延時(shí) 800ms 以上doi = init_18b20();while(i);skip();read_sp();/ 發(fā)送讀 scratchpad 命令return readtemp();/ 讀取的溫度數(shù)值/*/*18b20.h*/void delaynus(unsigned int n);unsigned char

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論