1、完整版)CT107D_DS1302_DS18B20液晶显示 /*DS1302的SPI数据端口P2^3 时钟端口P1^7 片选端口P1^3(高电频有效 低复位)*/ /*DS18B20的单总线 数据时钟端口P1^4*/ #include〈reg52。h〉 #define rst573 P2&=0x1f #define ledY4C P2|=0x80 #define beeY5C P2|=0xa0 #define smgY6C P2|=0xc0 #define smgY7C P2|=0xe0 sbit P04=P0^4; sbit P06=P0^6; sbit
2、P36=P3^6; /*1602 与12864 可共用子程序 初始化 命令也可统一为 0x38 0x0e 0x06 0x01*/ sbit rsLCD=P2^0; sbit wrLCD=P2^1; sbit enLCD=P1^2; sbit ds18b20=P1^4;//单总线 sbit c1302=P1^7;//SPI时钟 sbit d1302=P2^3;//SPI数据线 sbit s1302=P1^3;//SPI片选 高有效 低复位 void delay6us(unsigned char us)//11+6*us { while(us-—);
3、 } void init107() { P36=0; rst573; //close led P0=0xff; ledY4C; rst573; //close bee P04=0; P06=0; beeY5C; //close smg P0=0x00; smgY6C; rst573; } void comLCD(unsigned char com) { unsigned char temp; do { rsLCD=0; wrLCD=1; enLCD=1; temp=P0; }whi
4、le(temp&0x80); rsLCD=0; wrLCD=0; enLCD=1; P0=com; enLCD=0; } void datLCD(unsigned char dat) { unsigned char temp; do { rsLCD=0; wrLCD=1; enLCD=1; temp=P0; }while(temp&0x80); rsLCD=1; wrLCD=0; enLCD=1; P0=dat; enLCD=0; delay6us(50);//为了1602 } void init
5、LCD() { comLCD(0x38);//显示模式设置指令 1602固定的第一句指令 comLCD(0x0e);//光标打开,不闪烁 comLCD(0x06);//写入一个字符后指针地址加1,写一个字符时整屏不移动 // comLCD(0x07);//写入一个字符后指针地址加1,写一个字符时整屏移动 comLCD(0x01);//清屏显示,数据指针清0 } void rst18b20() { ds18b20=0; delay6us(90);//〉480 ds18b20=1; delay6us(40);//〉240 至少为20 } v
6、oid write18b20(unsigned char 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;//关键的神机!!
7、 if(ds18b20) temp|=0x80; delay6us(5); } return temp; } unsigned int temperature() { unsigned char temp_L,temp_H; unsigned int temp; rst18b20(); write18b20(0xcc);//跳过ROM匹配 write18b20(0x44); rst18b20(); write18b20(0xcc);//跳过ROM匹配 write18b20(0xbe); temp_L=read18b20
8、); temp_H=read18b20(); temp=temp_H; temp〈<=8; temp|=temp_L; temp=temp*0。625;//保留一位小数 return temp; } void write1302(unsigned char addr,unsigned char temp) { unsigned char i; s1302=0; c1302=0; s1302=1; for(i=8;i;i——) { c1302=0; d1302=addr&0x01; c1302=1;//上升沿写入
9、 addr>〉=1; } for(i=8;i;i--) { c1302=0; d1302=temp&0x01; c1302=1;//上升沿写入 temp>〉=1; } s1302=0; } unsigned char read1302(unsigned char addr) { unsigned char i,temp; s1302=0; c1302=0; s1302=1; for(i=8;i;i——) { c1302=0; d1302=addr&0x01; c1302=1;//上升沿写入
10、 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,0x2
11、0);//时 write1302(0x82,0x19);//分 write1302(0x80,0x59);//秒 write1302(0x8e,0x80); } void data1302(unsigned char *p) { p[0]=read1302(0x8d); write1302(0x00,0x00); p[1]=read1302(0x8b); write1302(0x00,0x00); p[2]=read1302(0x89); write1302(0x00,0x00); p[3]=read1302(0x87); write1302(
12、0x00,0x00); p[4]=read1302(0x85); write1302(0x00,0x00); p[5]=read1302(0x83); write1302(0x00,0x00); p[6]=read1302(0x81); write1302(0x00,0x00); } void main() { unsigned int temp,temp1; unsigned char *p,time[7]; unsigned char row[]=”一二三四五六日"; unsigned char row1[]="20 年 月 日";
13、 unsigned char row2[]=” —- —— 星期 ”; unsigned char row3[]=" 温度"; unsigned char row4[]=”mm~~~=_=~~~mm"; init107(); initLCD(); set1302(); comLCD(0x80); for(p=row1;*p;p++) datLCD(*p);//如果是1602 字节间加延时 comLCD(0x90); for(p=row2;*p;p++) datLCD(*p); comLCD(0x88); for(p=row
14、3;*p;p++) datLCD(*p); comLCD(0x98); for(p=row4;*p;p++) datLCD(*p); while(1) { data1302(time); comLCD(0x81); datLCD(time[0]/16+'0'); datLCD(time[0]%16+’0’); comLCD(0x83); datLCD(time[2]/16+’0’); datLCD(time[2]%16+'0'); comLCD(0x85); datLCD(time[3]/16+’0');
15、 datLCD(time[3]%16+'0’); comLCD(0x90); datLCD(time[4]/16+’0’); datLCD(time[4]%16+’0’); comLCD(0x92); datLCD(time[5]/16+’0’); datLCD(time[5]%16+’0'); comLCD(0x94); datLCD(time[6]/16+'0’); datLCD(time[6]%16+'0'); comLCD(0x97); if(time[1]%2) time[1]=time[1]—1; datLCD(row[time[1]]); datLCD(row[time[1]+1]); temp=temperature(); if(temp!=temp1) { temp1=temp; comLCD(0x8c); datLCD(temp/100+'0’); datLCD(temp%100/10+'0'); datLCD('。’); datLCD(temp%10+'0'); } } }






