#include<reg52.h> #define uchar unsigned char #define uint unsigned int uint out; uchar hou,min,scn,day,mon,week,year; uchar code num[15]={'0','1','2','3','4','5','6','7','8','9','-',':',0xdf,'.','C'}; uchar data date[11]={2,0,1,1,10,1,1,10,1,1,1}; uchar data time[8]={1,1,11,1,1,11,1,1}; uchar data temp[6]={1,1,13,1,12,14}; uchar code weeks[7][3]= { {'M','O','N'}, {'T','U','E'}, {'W','E','D'}, {'T','H','U'}, {'F','R','I'}, {'S','A','T'}, {'S','U','N'}, }; sbit led=P2^0; sbit IO=P1^6; sbit CE=P1^7; sbit SLK=P1^5; sbit SCK=P1^2; sbit RCK=P1^1; sbit EN=P1^3; sbit SOU=P3^7; sbit si=P3^6; sbit RS=P1^0; sbit RW=P1^1; sbit LCDEN=P1^2; sbit POWER=P1^4; void delay18(uint x) { uchar i,j; for(i=x;i>0;i--) for(j=11;j>0;j--); } void init18(void) //溫度模塊初始化 { uint i; si=1; i++; si=0; delay18(8); si=1; i=2200; while(si==1|(i<0))i--; delay18(5); si=1; } void writebyte18(uchar dat) //溫度模塊寫字節(jié) { uchar i=0,j; for(i=8;i>0;i--) { si=0; j++;j++; si=dat&0x01; j=5; while(j--); //等待18b20采集信號 si=1; dat>>=1; } si=1; } uchar readbyte18(void) //溫度模塊讀字節(jié) { uchar i,j,k,value; for(i=8;i>0;i--) { value=value>>1; si=1; j++; si=0; j++;j++; si=1; j++;j++; if(si) value|=0x80; j=3;while(j--); //50微秒內(nèi)釋放總線 } return(value); }
void start(void) //溫度采集開始 { init18(); delay18(10); writebyte18(0xcc); writebyte18(0x44); } uint gettemp(void) //獲取溫度數(shù)據(jù) { float ftemp; uint temp,t; uchar a,b; delay18(10); init18(); writebyte18(0xcc); writebyte18(0xbe); a=readbyte18(); b=readbyte18(); temp=b; temp=temp<<8; temp|=a; t=temp; ftemp=t*0.0625; t=ftemp*100+(t>0?0.5:-0.5); return t; } void init() { SLK=0; IO=0; CE=0; } void writebyte(uchar com,uchar dat) //DS1302寫字節(jié) { uint i,j; SLK=0; CE=1; for(i=0;i<8;i++) { SLK=0; IO=com&0X01; j++; SLK=1; com=com>>1; } for(i=0;i<8;i++) { SLK=0; IO=dat&0X01; j++; SLK=1; dat=dat>>1; } CE=0; } uchar readbyte(uchar com) //DS1302讀字節(jié) { uchar i,date; CE=0; // SLK=0; CE=1; for(i=0;i<8;i++) { //SLK=0; IO=com&0x01; //j++; SLK=0; SLK=1; com=com>>1; } for(i=0;i<8;i++) //筆記:注意讀取數(shù)據(jù)的順序排列,左移位及右移位。下降沿的制造以及數(shù)據(jù)的讀取~ { date>>=1; SLK=0; if(IO==1) date=date|0x80; SLK=1; } CE=0; return date; } void delay(x) { uint i,j; for(i=x;i>0;i--) for(j=110;j>0;j--); } void alarm() //鬧鐘 { uint i,j,k; for(k=60;k>0;k--) { for(j=4;j>0;j--) { for(i=100;i>0;i--) { SOU=0; delay(1); SOU=1; } delay(15); } delay(300); } }
void delay1( ms) { uint i,j; for(i=ms;i>0;i--) for(j=55;j>0;j--); }
void wtcom(uchar com) //LCD寫命令 { RS=0; RW=0; P0=com; delay(5); LCDEN=1; delay(5); LCDEN=0; }
void wtdate(uchar date) //LCD寫數(shù)據(jù) { RS=1; P0=date; delay1(1); LCDEN=1; delay1(1); LCDEN=0; } void initlcd() //初始化LCD { LCDEN=0; wtcom(0x38); wtcom(0x0c); wtcom(0x06); wtcom(0x01); } void writelcd() { uchar i; initlcd(); wtcom(0x80); for(i=0;i<10;i++) { wtdate(num[date]); } wtcom(0x80+0x43); for(i=0;i<5;i++) { wtdate(num[time]); } wtcom(0x8d); for(i=0;i<3;i++) { wtdate(weeks[(week-1)]); } wtcom(0x80+0x4a); for(i=0;i<6;i++) { wtdate(num[temp]); } } void displaydate() { date[2]=year>>4; date[3]=year&0x0f; date[5]=mon>>4; date[6]=mon&0x0f; date[8]=day>>4; date[9]=day&0x0f; } void displaytime() { time[0]=hou>>4; time[1]=hou&0x0f; time[3]=min>>4; time[4]=min&0x0f; time[6]=scn>>4; time[7]=scn&0x0f; } void displaytemp() { temp[0]=gettemp()/1000; temp[1]=gettemp()/100%10; temp[3]=gettemp()/10%10; } void main() { EN=1; init(); //writebyte(0x8c,0x12); // writebyte(0x8a,0x01); //writebyte(0x88,0x03); // writebyte(0x86,0x31); // writebyte(0x84,0x10); //寫小時 //writebyte(0x82,0x28); //寫分鐘 // writebyte(0x80,0x30); //寫秒鐘 // writelcd(); P2=0Xff; while(1) { uchar change1,change2; uint t1,t2; start(); scn=readbyte(0x81); min=readbyte(0x83); hou=readbyte(0x85); day=readbyte(0x87); mon=readbyte(0x89); week=readbyte(0x8b); year=readbyte(0x8d); change1=min; displaytemp(); t1=temp[3]; displaydate(); displaytime(); if(change1!=change2) writelcd(); change2=min; if(t1!=t2) writelcd(); t2=temp[3]; delay1(1000); if(hou==0X10) if(min==0x40) POWER=0; } } |