1、(完整版)CT107D_DS1302_DS18B20液晶显示/*DS1302的SPI数据端口P23 时钟端口P17 片选端口P13(高电频有效 低复位)*/*DS18B20的单总线 数据时钟端口P14*/includereg52。hdefine rst573 P2&=0x1fdefine ledY4C P2=0x80define beeY5C P2=0xa0#define smgY6C P2=0xc0#define smgY7C P2|=0xe0sbit P04=P04;sbit P06=P06;sbit P36=P36;/*1602 与12864 可共用子程序 初始化 命令也可统一为 0x3
2、8 0x0e 0x06 0x01/sbit rsLCD=P20;sbit wrLCD=P21;sbit enLCD=P12;sbit ds18b20=P14;/单总线sbit c1302=P17;/SPI时钟sbit d1302=P23;/SPI数据线sbit s1302=P13;/SPI片选 高有效 低复位void delay6us(unsigned char us)/11+6*uswhile(us-);void init107()P36=0;rst573;/close ledP0=0xff;ledY4C;rst573;/close beeP04=0;P06=0;beeY5C;/close
3、smgP0=0x00;smgY6C;rst573;void comLCD(unsigned char com)unsigned char temp;dorsLCD=0;wrLCD=1;enLCD=1;temp=P0;while(temp&0x80);rsLCD=0;wrLCD=0;enLCD=1;P0=com;enLCD=0;void datLCD(unsigned char dat)unsigned char temp;dorsLCD=0;wrLCD=1;enLCD=1;temp=P0;while(temp0x80);rsLCD=1;wrLCD=0;enLCD=1;P0=dat;enLCD=
4、0;delay6us(50);/为了1602void initLCD()comLCD(0x38);/显示模式设置指令 1602固定的第一句指令comLCD(0x0e);/光标打开,不闪烁comLCD(0x06);/写入一个字符后指针地址加1,写一个字符时整屏不移动/comLCD(0x07);/写入一个字符后指针地址加1,写一个字符时整屏移动comLCD(0x01);/清屏显示,数据指针清0void rst18b20()ds18b20=0;delay6us(90);/480ds18b20=1;delay6us(40);/240 至少为20void write18b20(unsigned char
5、 temp)unsigned char i;for(i=8;i;i)ds18b20=0;ds18b20=temp&0x01;delay6us(5);ds18b20=1;/关键的神机!temp=1;unsigned char read18b20()unsigned char i,temp;for(i=8;i;i-)ds18b20=0;temp=1;ds18b20=1;/关键的神机!if(ds18b20) temp=0x80;delay6us(5);return temp;unsigned int temperature()unsigned char temp_L,temp_H;unsigned
6、int temp;rst18b20();write18b20(0xcc);/跳过ROM匹配write18b20(0x44);rst18b20();write18b20(0xcc);/跳过ROM匹配write18b20(0xbe);temp_L=read18b20();temp_H=read18b20();temp=temp_H;temp=1;for(i=8;i;i-)c1302=0;d1302=temp0x01;c1302=1;/上升沿写入temp=1;s1302=0;unsigned char read1302(unsigned char addr)unsigned char i,temp;
7、s1302=0;c1302=0;s1302=1;for(i=8;i;i)c1302=0;d1302=addr&0x01;c1302=1;/上升沿写入addr=1;for(i=8;i;i-)c1302=1;temp=1;c1302=0;/下降沿输出if(d1302) temp|=0x80;return temp;void set1302()write1302(0x8e,0x00);write1302(0x8c,0x11);/年write1302(0x8a,0x03);/周write1302(0x88,0x10);/月write1302(0x86,0x05);/日write1302(0x84,0x
8、20);/时write1302(0x82,0x19);/分write1302(0x80,0x59);/秒write1302(0x8e,0x80);void data1302(unsigned char p)p0=read1302(0x8d);write1302(0x00,0x00);p1=read1302(0x8b);write1302(0x00,0x00);p2=read1302(0x89);write1302(0x00,0x00);p3=read1302(0x87);write1302(0x00,0x00);p4=read1302(0x85);write1302(0x00,0x00);p5
9、=read1302(0x83);write1302(0x00,0x00);p6=read1302(0x81);write1302(0x00,0x00);void main()unsigned int temp,temp1;unsigned char *p,time7;unsigned char row=”一二三四五六日;unsigned char row1=20 年 月 日;unsigned char row2=” - 星期 ”;unsigned char row3= 温度;unsigned char row4=”mm=_=mm;init107();initLCD();set1302();co
10、mLCD(0x80);for(p=row1;p;p+)datLCD(p);/如果是1602 字节间加延时comLCD(0x90);for(p=row2;*p;p+)datLCD(*p);comLCD(0x88);for(p=row3;p;p+)datLCD(*p);comLCD(0x98);for(p=row4;p;p+)datLCD(*p);while(1)data1302(time);comLCD(0x81);datLCD(time0/16+0);datLCD(time0%16+0);comLCD(0x83);datLCD(time2/16+0);datLCD(time2%16+0);co
11、mLCD(0x85);datLCD(time3/16+0);datLCD(time3%16+0);comLCD(0x90);datLCD(time4/16+0);datLCD(time416+0);comLCD(0x92);datLCD(time5/16+0);datLCD(time516+0);comLCD(0x94);datLCD(time6/16+0);datLCD(time6%16+0);comLCD(0x97);if(time1%2) time1=time11;datLCD(rowtime1);datLCD(rowtime1+1);temp=temperature();if(temp!=temp1)temp1=temp;comLCD(0x8c);datLCD(temp/100+0);datLCD(temp%100/10+0);datLCD(。);datLCD(temp10+0);