基于51单片机的温度测试系统+有VB上位机_第1页
基于51单片机的温度测试系统+有VB上位机_第2页
基于51单片机的温度测试系统+有VB上位机_第3页
基于51单片机的温度测试系统+有VB上位机_第4页
基于51单片机的温度测试系统+有VB上位机_第5页
已阅读5页,还剩29页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、简介:这是我学完51单片机的第一个作品,单片机型号为89c52,时钟芯片用的是ds1302,温度传感器用的是ds18b20,usb供电,掉电后时钟芯片继续工作,且已经设置好的温度上下限和闹钟数据不会丢失。上位机是用vb语言写的,只是一个模型,从上位机上可以观察当前温度值以及温度上下限,以后会更加完善其功能。下面是下位机程序和上位机程序,程序比较长,而且下位机程序的键盘部分写得有点麻烦。/*作者:江苏科技大学 电子信息科学与技术二班 吴亚迪注:此程序可以实现万年历闹钟温度测定 温度超过上下限发出警报断电后时钟不停,且已设好的温度上下限和闹钟数据不会丢失*/#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;/闹钟标志位bit stop_alarm=0;/停止闹钟标志位uchar i,j;uchar th,tl; /温度上下限uint th1,th2,tl1,tl2;uchar min,hour,sec; /闹钟时分秒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指定地址读数据 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); /关闭写保护write_ds(0x80,0x22); /初始化秒write_ds(0x82,0x59); /分write_ds(0x84,0x23); /时write_ds(0x86,0x21); /日write_ds(0x88,0x05); /月write_ds(0x8a,0x06); /星期write_ds(0x8c,0x11); /年write_ds(0x8e,0x80); /开写

6、保护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)/写数据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()/开始函数sda=1;delay1();scl=1;delay1();sda=0;delay1();void s

9、top()/停止函数sda=0;delay1();scl=1;delay1();sda=1;delay1();void respons()/应答uchar i;sda=1;delay1();scl=1;while(sda=1&i250) i+;scl=0;delay1();void write_byterom(uchar date)/向eeprom中写一字节数据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中读一字节数据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)/指定地址写数据start();write_byterom(0xa0);respons();write_byterom(add);respons();write_byterom(date);respons();stop();uchar read_add(uchar add)/指定地址读数据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中写一字节uchar i;for(i=0;i=1;delay2(4);uchar read_byte()/从ds18b20中读一字节uchar i,dat=0;for(i=0;i=1;dq=1;if(dq)dat=dat|0x80;delay2(4);return dat;uchar read_temp() /读温度函数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()/读上限函数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()/读下限函数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);/禁止写保护while(temp=0x0e)/松手检测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)/松手检测temp=p2;temp&=0x0f;if(num1=1)write_com(0x80+0x40+0x08);write_com(0x0f);/光标闪烁temp=p2;temp&=0x0f;if(temp=

16、0x0d)delay(5);if(temp=0x0d)while(temp=0x0d)/松手检测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、松手检测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)/松手检测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)/松手检测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、/松手检测temp=p2;temp&=0x0f;h=read_ds(0x85);/从ds1302中读时 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中写入时else if(temp=0x0b)delay(5);if(temp=0x0b)while(temp=0x0b)/松手检测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)/松手检测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)/松手检测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)/松手检测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)/松手检测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、/松手检测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)/松手检测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)/松手检测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)/松手检测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. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论