基于51單片機(jī)的溫度測(cè)試系統(tǒng)+有VB上位機(jī)_第1頁(yè)
基于51單片機(jī)的溫度測(cè)試系統(tǒng)+有VB上位機(jī)_第2頁(yè)
基于51單片機(jī)的溫度測(cè)試系統(tǒng)+有VB上位機(jī)_第3頁(yè)
基于51單片機(jī)的溫度測(cè)試系統(tǒng)+有VB上位機(jī)_第4頁(yè)
基于51單片機(jī)的溫度測(cè)試系統(tǒng)+有VB上位機(jī)_第5頁(yè)
已閱讀5頁(yè),還剩29頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、簡(jiǎn)介:這是我學(xué)完51單片機(jī)的第一個(gè)作品,單片機(jī)型號(hào)為89c52,時(shí)鐘芯片用的是ds1302,溫度傳感器用的是ds18b20,usb供電,掉電后時(shí)鐘芯片繼續(xù)工作,且已經(jīng)設(shè)置好的溫度上下限和鬧鐘數(shù)據(jù)不會(huì)丟失。上位機(jī)是用vb語(yǔ)言寫的,只是一個(gè)模型,從上位機(jī)上可以觀察當(dāng)前溫度值以及溫度上下限,以后會(huì)更加完善其功能。下面是下位機(jī)程序和上位機(jī)程序,程序比較長(zhǎng),而且下位機(jī)程序的鍵盤部分寫得有點(diǎn)麻煩。/*作者:江蘇科技大學(xué) 電子信息科學(xué)與技術(shù)二班 吳亞迪注:此程序可以實(shí)現(xiàn)萬(wàn)年歷鬧鐘溫度測(cè)定 溫度超過(guò)上下限發(fā)出警報(bào)斷電后時(shí)鐘不停,且已設(shè)好的溫度上下限和鬧鐘數(shù)據(jù)不會(huì)丟失*/#include#define ucha

2、r unsigned char#define uint unsigned intuchar code table=20 / / day- ; uchar code table1= : : ;uchar code table2=0123456789;uchar code table3= alarm clock ;sbit ds_clk=p36; sbit ds_io=p37;sbit ds_rst=p14;sbit lcdrs=p10;sbit lcdrw=p11;sbit lcden=p12;sbit scl=p32;sbit sda=p33;sbit acc_7=acc7;sbit p34=

3、p34;bit alarm=0;/鬧鐘標(biāo)志位bit stop_alarm=0;/停止鬧鐘標(biāo)志位uchar i,j;uchar th,tl; /溫度上下限uint th1,th2,tl1,tl2;uchar min,hour,sec; /鬧鐘時(shí)分秒uint min1,min2,hour1,hour2,sec1,sec2;void delay(uint i)uint j;for(;i0;i-)for(j=0;j0; i- ) ds_clk = 0; temp = add; ds_io= temp&0x01; add = 1; ds_clk = 1; for ( i=8; i0; i- ) ds_c

4、lk = 0; temp = date; ds_io =temp&0x01; date = 1; ds_clk = 1; ds_rst= 0; uchar read_ds(uchar add ) /ds1302指定地址讀數(shù)據(jù) unsigned char i,temp,date1; ds_rst=0; ds_clk=0; ds_rst= 1; for ( i=8; i0; i- ) ds_clk = 0; temp = add; ds_io=temp&0x01; add = 1; ds_clk = 1; for ( i=8; i0; i- ) /這里要注意 acc_7=ds_io; ds_clk

5、 = 1; acc=1; ds_clk = 0; ds_rst=0; date1=acc; return date1;void init_ds() /初始化ds1302write_ds(0x8e,0x00); /關(guān)閉寫保護(hù)write_ds(0x80,0x22); /初始化秒write_ds(0x82,0x59); /分write_ds(0x84,0x23); /時(shí)write_ds(0x86,0x21); /日write_ds(0x88,0x05); /月write_ds(0x8a,0x06); /星期write_ds(0x8c,0x11); /年write_ds(0x8e,0x80); /開(kāi)寫

6、保護(hù)void read_all() /讀s,m,h,date,mon,year,days=read_ds(0x81);s1=s/16;s2=s%16;m=read_ds(0x83);m1=m/16;m2=m%16;h=read_ds(0x85);h1=h/16;h2=h%16;date=read_ds(0x87);date1=date/16;date2=date%16;mon=read_ds(0x89);mon1=mon/16;mon2=mon%16;day=read_ds(0x8b);year=read_ds(0x8d);year1=year/16;year2=year%16;/* 1602

7、部分*/void write_com(uchar com) /寫命令lcdrs=0;lcden=0;p0=com;delay(5);lcden=1;delay(5);lcden=0;void write_date(uchar date)/寫數(shù)據(jù)lcdrs=1;lcden=0;p0=date;delay(5);lcden=1;delay(5);lcden=0;void init_1602() /初始化1602lcdrw=0;write_com(0x01);write_com(0x38);write_com(0x0f);write_com(0x06);for(i=0;i16;i+)write_da

8、te(tablei);delay(10);write_com(0x80+0x40);for(i=0;i16;i+)write_date(table1i);delay(10);write_com(0x80+0x40+0x0e);write_date(0xdf);delay(10);write_date(c);/* eeprom部分*/void delay1() ; ; void init()/初始化總線sda=1;delay1();scl=1;delay1();void start()/開(kāi)始函數(shù)sda=1;delay1();scl=1;delay1();sda=0;delay1();void s

9、top()/停止函數(shù)sda=0;delay1();scl=1;delay1();sda=1;delay1();void respons()/應(yīng)答uchar i;sda=1;delay1();scl=1;while(sda=1&i250) i+;scl=0;delay1();void write_byterom(uchar date)/向eeprom中寫一字節(jié)數(shù)據(jù)uchar i;for(i=0;i8;i+)date=date1;scl=0;delay1();sda=cy;delay1();scl=1;delay1();scl=0;delay1();uchar read_byterom()/從ee

10、prom中讀一字節(jié)數(shù)據(jù)uchar i,temp;scl=0;delay1();for(i=0;i8;i+)scl=1;delay1();temp=1;temp|=sda;scl=0;delay1();return temp;void write_add(uchar add,uchar date)/指定地址寫數(shù)據(jù)start();write_byterom(0xa0);respons();write_byterom(add);respons();write_byterom(date);respons();stop();uchar read_add(uchar add)/指定地址讀數(shù)據(jù)uchar t

11、emp;start();write_byterom(0xa0);respons();write_byterom(add);respons();start();write_byterom(0xa1);respons();temp=read_byterom();stop();return temp;/* ds18b20部分*/sbit dq=p35;void delay2(uchar i)while(i-);void init_ds18b20()/初始化ds18b20uchar i;dq=1;delay2(8);dq=0;delay2(80);dq=1;while(dq=1&i255) i+;de

12、lay2(35);delay2(20);void write_byte(uchar dat)/向ds18b20中寫一字節(jié)uchar i;for(i=0;i=1;delay2(4);uchar read_byte()/從ds18b20中讀一字節(jié)uchar i,dat=0;for(i=0;i=1;dq=1;if(dq)dat=dat|0x80;delay2(4);return dat;uchar read_temp() /讀溫度函數(shù)uchar a,b,c;init_ds18b20();write_byte(0xcc);write_byte(0x44);init_ds18b20();write_by

13、te(0xcc);write_byte(0xbe);a=read_byte();b=read_byte();c=(b4);return c;uchar read_th()/讀上限函數(shù)uchar a,b,c;init_ds18b20();write_byte(0xcc);write_byte(0x44);init_ds18b20();write_byte(0xcc);write_byte(0xbe);a=read_byte();b=read_byte();c=read_byte();return c;uchar read_tl()/讀下限函數(shù)uchar a,b,c;init_ds18b20();

14、write_byte(0xcc);write_byte(0x44);init_ds18b20();write_byte(0xcc);write_byte(0xbe);a=read_byte();b=read_byte();a=read_byte();c=read_byte();return c;/* 鍵盤部分(p20p23)*/void keyscan()uchar temp,num1,num2;temp=p2;temp&=0x0f;if(temp!=0x0f) /消抖delay(5);if(temp!=0x0f)temp=p2;temp&=0x0f;if(temp=0x0e)num1=1;w

15、rite_ds(0x8e,0x00); write_ds(0x80,s|0x80);/禁止寫保護(hù)while(temp=0x0e)/松手檢測(cè)temp=p2;temp&=0x0f;while(1)temp=p2;temp&=0x0f;if(temp=0x0e) delay(5);temp=p2;temp&=0x0f;if(temp=0x0e)num1+;while(temp=0x0e)/松手檢測(cè)temp=p2;temp&=0x0f;if(num1=1)write_com(0x80+0x40+0x08);write_com(0x0f);/光標(biāo)閃爍temp=p2;temp&=0x0f;if(temp=

16、0x0d)delay(5);if(temp=0x0d)while(temp=0x0d)/松手檢測(cè)temp=p2;temp&=0x0f;s=read_ds(0x81);s1=s/16;s2=s%16;s2+=1;if(s2=10)s2=0;s1+=1;if(s1=6) s1=0;write_com(0x80+0x40+0x07);write_date(table2s1);write_date(table2s2);write_ds(0x80,s1*16+s2);/向ds1302中寫入秒else if(temp=0x0b)delay(5);if(temp=0x0b)while(temp=0x0b)/

17、松手檢測(cè)temp=p2;temp&=0x0f;s=read_ds(0x81);/讀ds1302秒 s1=s/16;s2=s%16;s2-=1;if(s2=-1)s2=9;s1-=1;if(s1=-1) s1=5;write_com(0x80+0x40+0x07);write_date(table2s1);write_date(table2s2);write_ds(0x80,s1*16+s2);if(num1=2)write_com(0x80+0x40+0x05);write_com(0x0f);temp=p2;temp&=0x0f;if(temp=0x0d)delay(5);if(temp=0

18、x0d)while(temp=0x0d)/松手檢測(cè)temp=p2;temp&=0x0f;m=read_ds(0x83);/從ds1302中讀分 m1=m/16;m2=m%16;m2+=1;if(m2=10)m2=0;m1+=1;if(m1=6) m1=0;write_com(0x80+0x40+0x04);write_date(table2m1);write_date(table2m2);write_ds(0x82,m1*16+m2);/向ds1302中寫入分else if(temp=0x0b)delay(5);if(temp=0x0b)while(temp=0x0b)/松手檢測(cè)temp=p2

19、;temp&=0x0f;m=read_ds(0x83); m1=m/16;m2=m%16;m2-=1;if(m2=-1)m2=9;m1-=1;if(m1=-1) m1=5;write_com(0x80+0x40+0x04);write_date(table2m1);write_date(table2m2);write_ds(0x82,m1*16+m2);if(num1=3)write_com(0x80+0x40+0x02);write_com(0x0f);temp=p2;temp&=0x0f;if(temp=0x0d)delay(5);if(temp=0x0d)while(temp=0x0d)

20、/松手檢測(cè)temp=p2;temp&=0x0f;h=read_ds(0x85);/從ds1302中讀時(shí) h1=h/16;h2=h%16;h2+=1;if(h2=10)h2=0;h1+=1;if(h2=4&h1=2)h2=0;h1=0;write_com(0x80+0x40+0x01);write_date(table2h1);write_date(table2h2);write_ds(0x84,h1*16+h2);/向ds1302中寫入時(shí)else if(temp=0x0b)delay(5);if(temp=0x0b)while(temp=0x0b)/松手檢測(cè)temp=p2;temp&=0x0f

21、;h=read_ds(0x85); h1=h/16;h2=h%16;h2-=1;if(h2=-1)h2=9;h1-=1;if(h2=9&h1=-1)h2=3;h1=2;write_com(0x80+0x40+0x01);write_date(table2h1);write_date(table2h2);write_ds(0x84,h1*16+h2);if(num1=4)write_com(0x80+0x0f);write_com(0x0f);temp=p2;temp&=0x0f;if(temp=0x0d)delay(5);if(temp=0x0d)while(temp=0x0d)/松手檢測(cè)te

22、mp=p2;temp&=0x0f;day=read_ds(0x8b);/從ds1302中讀星期day+=1;if(day=8)day=1;write_com(0x80+0x0f);write_date(table2day);write_ds(0x8a,day);/向ds1302中寫入星期else if(temp=0x0b)delay(5);if(temp=0x0b)while(temp=0x0b)/松手檢測(cè)temp=p2;temp&=0x0f;day=read_ds(0x8b);day-=1;if(day=0)day=7;write_com(0x80+0x0f);write_date(tabl

23、e2day);write_ds(0x8a,day);if(num1=5)write_com(0x80+0x09);write_com(0x0f);temp=p2;temp&=0x0f;if(temp=0x0d)delay(5);if(temp=0x0d)while(temp=0x0d)/松手檢測(cè)temp=p2;temp&=0x0f;date=read_ds(0x87);/從ds1302中讀日期 date1=date/16;date2=date%16;date2+=1;if(date2=10)date2=0;date1+=1;if(date2=2&date1=3)date2=0;date1=0;

24、if(date1=0&date2=0)date1=0;date2=1;write_com(0x80+0x08);write_date(table2date1);write_date(table2date2);write_ds(0x86,date1*16+date2);/向ds1302中寫入日期else if(temp=0x0b)delay(5);if(temp=0x0b)while(temp=0x0b)/松手檢測(cè)temp=p2;temp&=0x0f;date=read_ds(0x87); date1=date/16;date2=date%16;date2-=1;if(date2=-1)date

25、2=9;date1-=1;if(date2=9&date1=-1)date2=1;date1=3;if(date1=0&date2=0)date1=3;date2=1;write_com(0x80+0x08);write_date(table2date1);write_date(table2date2);write_ds(0x86,date1*16+date2);if(num1=6)write_com(0x80+0x06);write_com(0x0f);temp=p2;temp&=0x0f;if(temp=0x0d)delay(5);if(temp=0x0d)while(temp=0x0d)

26、/松手檢測(cè)temp=p2;temp&=0x0f;mon=read_ds(0x89);/從ds1302中讀月 mon1=mon/16;mon2=mon%16;mon2+=1;if(mon2=10)mon2=0;mon1+=1;if(mon2=3&mon1=1)mon2=0;mon1=0;if(mon1=0&mon2=0)mon1=0;mon2=1;write_com(0x80+0x05);write_date(table2mon1);write_date(table2mon2);write_ds(0x88,mon1*16+mon2);/向ds1302中寫入月else if(temp=0x0b)d

27、elay(5);if(temp=0x0b)while(temp=0x0b)/松手檢測(cè)temp=p2;temp&=0x0f;mon=read_ds(0x89); mon1=mon/16;mon2=mon%16;mon2-=1;if(mon2=-1)mon2=9;mon1-=1;if(mon2=9&mon1=-1)mon2=2;mon1=1;if(mon1=0&mon2=0)mon1=1;mon2=2;write_com(0x80+0x05);write_date(table2mon1);write_date(table2mon2);write_ds(0x88,mon1*16+mon2);if(n

28、um1=7)write_com(0x80+0x03);write_com(0x0f);temp=p2;temp&=0x0f;if(temp=0x0d)delay(5);if(temp=0x0d)while(temp=0x0d)/松手檢測(cè)temp=p2;temp&=0x0f;year=read_ds(0x8d);/從ds1302中讀年 year1=year/16;year2=year%16;year2+=1;if(year2=10)year2=0;year1+=1;if(year1=9)year1=0;write_com(0x80+0x02);write_date(table2year1);wr

29、ite_date(table2year2);write_ds(0x8c,year1*16+year2);/向ds1302中寫入年else if(temp=0x0b)delay(5);if(temp=0x0b)while(temp=0x0b)/松手檢測(cè)temp=p2;temp&=0x0f;year=read_ds(0x8d); year1=year/16;year2=year%16;year2-=1;if(year2=-1)year2=9;year1-=1;if(year1=-1)year1=7;write_com(0x80+0x02);write_date(table2year1);write

30、_date(table2year2);write_ds(0x8c,year1*16+year2);if(num1=8) /修改上限write_com(0x80+0x40+0x0d);write_com(0x0f);delay(10); write_com(0x80+0x40+0x09);write_com(0x0e);write_date(t);write_date(h);write_com(0x80+0x40+0x0d);write_com(0x0f);th=read_th();write_com(0x80+0x40+0x0b);if(th=100)write_date(-);th=th+1;else write_date( );th1=th/10;th2=th%10;write_date(table2th1)

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝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ù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 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)論