1、基于51单片机的温度测试系统+有VB上位机简介:这是我学完51单片机的第一个作品,单片机型号为89c52,时钟芯片用的是DS1302,温度传感器用的是DS18B20,USB供电,掉电后时钟芯片继续工作,且已经设置好的温度上下限和闹钟数据不会丢失。上位机是用VB语言写的,只是一个模型,从上位机上可以观察当前温度值以及温度上下限,以后会更加完善其功能。下面是下位机程序和上位机程序,程序比较长,而且下位机程序的键盘部分写得有点麻烦。/*作者:江苏科技大学 电子信息科学与技术二班 吴亚迪注:此程序可以实现万年历闹钟温度测定 温度超过上下限发出警报断电后时钟不停,且已设好的温度上下限和闹钟数据不会丢失*
2、/#include#define uchar 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
3、 ACC_7=ACC7;sbit P34=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
4、 ( i=8; i0; i- ) ds_clk = 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- ) /这里要注
5、意 ACC_7=ds_io; ds_clk = 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); /年wri
6、te_ds(0x8e,0x80); /开写保护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;
7、year2=year%16;/* 1602部分*/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);fo
8、r(i=0;i16;i+)write_date(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();
9、sda=0;delay1();void stop()/停止函数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();uch
10、ar read_byterom()/从eeprom中读一字节数据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(ucha
11、r add)/指定地址读数据uchar temp;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;w
12、hile(DQ=1&i255) i+;delay2(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);in
13、it_ds18b20();write_byte(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
14、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();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;
15、if(temp=0x0e)num1=1;write_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=P
16、2;temp&=0x0f;if(temp=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=0
17、x0b)while(temp=0x0b)/松手检测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=0x0d)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+=
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1