|
單片機源程序如下:
- #include <reg52.h>//頭文件
- #include <intrins.h>
- #include <math.h>
- #define uchar unsigned char//宏定義
- #define uint unsigned short int//宏定義
- #define sint short int
- #define nop _nop_()
- void delay(uint z);//延時1MS
- void delay1(uchar x2);//延時7us左右;
- void write_Directive(uchar a);//寫LCD指令
- void write_Data(uchar a);//寫LCD數(shù)據(jù)
- void write_lcd_com_date(uchar addr,uchar date);//實現(xiàn)LCD寫指令及數(shù)據(jù)
- void LCD_init();//LCD初始化
- void start(); //AT24C02開始程序
- void send(uchar send_data);//AT24C02發(fā)送一個字節(jié)
- uchar receive();//AT24C02接收一個程序
- void ack();//AT24C02是否應(yīng)答
- void yack();//AT24C02應(yīng)答
- void nack();//AT24C02無應(yīng)答
- void stop();//AT24C02停止
- void write(uchar xaddress,sint temp);//AT24C02寫入溫度
- uint read(uchar daddress);//AT24C02讀出溫度
- void send_str(sint cc,uchar chflag); // 傳送字串
- void DB18B20_init_1();//DB18B20_1復(fù)位
- void write_DB18B20_1(uchar dat);//寫DB18B20_1
- uchar read_DB18B20_1();//讀DB18B20_1
- void DB18B20_init_1();//DB18B20_2復(fù)位
- void write_DB18B20_1(uchar dat);//寫DB18B20_2
- uchar read_DB18B20_1();//讀DB18B20_2
- void tempzh_1(); //溫度轉(zhuǎn)換1
- void tempzh_2(); //溫度轉(zhuǎn)換2
- void tempread_1(); //讀取溫度1
- void tempread_2(); //讀取溫度2
- void temp(sint TEMPS,uchar t,bit fuflag);//顯示溫度函數(shù)
- void readcc(); //讀取存儲芯片中最高及最低溫度
- void keyscan(); //鍵掃描程序
- void write_ds1302(uchar addr, uchar date);//寫DS1302
- uchar read_ds1302(uchar addr);//讀DS1302
- void init_ds1302();//DS1302初始化
- void time();//時間顯示
- void timekey();//時間部分功能鍵
- void tempkey();//溫度部分功能鍵
- void shuma(uchar addr,uchar xz,bit flag);//1302調(diào)節(jié)函數(shù)及轉(zhuǎn)換
- void flash(uchar bz);//字符閃爍函數(shù)
- void tempbj(); //溫度比較
- uchar bdata ch;//定義可位尋址空間,用于24c02的讀取
- sbit RS=P1^4;//1602數(shù)據(jù)/命令選擇
- sbit RW=P1^5;//1602讀寫命令選擇
- sbit E=P1^6;//1602使能端
- sbit led=P0^1;//發(fā)光二極管
- sbit beep=P0^0;//蜂鳴器
- sbit SDA=P0^2;//24c02數(shù)據(jù)總線
- sbit SCL=P0^3;//24c02時鐘總線
- sbit DQ1=P1^0;//溫度傳感器1
- sbit DQ2=P3^2;//溫度傳感器2
- sbit DSIO=P1^2;//1302輸入輸出口
- sbit DSCLK=P1^1;//1302脈沖
- sbit DSRST=P1^3;//1302復(fù)位
- sbit key1=P3^7;//五個按鍵及其標(biāo)志
- sbit key2=P0^7;
- sbit key3=P0^6;
- sbit key4=P0^5;
- sbit key5=P0^4;
- bit k1flag=0;
- bit k2flag=0;
- bit k3flag=0;
- bit k4flag=0;
- bit k5flag=0;
- sbit a0=ch^0;//定義ch各位
- sbit a1=ch^1;
- sbit a2=ch^2;
- sbit a3=ch^3;
- sbit a4=ch^4;
- sbit a5=ch^5;
- sbit a6=ch^6;
- sbit a7=ch^7;
- bit fuflag1=0;//負溫度標(biāo)志1
- bit fuflag2=0;//負溫度標(biāo)志2
- bit ackflag=0;//24c02應(yīng)答是否標(biāo)志
- bit sendflag=0;//串口發(fā)送標(biāo)志
- sint temps1,temps2,tempd1,tempd2,tempx1,tempx2,uptem1,downtem1,uptem2,downtem2;//溫度參數(shù)
- uchar count=0,tcount=0,scount=0,t=0;
- uchar secondh,secondl,minuteh,minutel,hourh,hourl,second,minute,hour;//時鐘參數(shù)
- uchar monthl,monthh,month,dayl,dayh,day,year,yearl,yearh,week;
- uchar code DispTab_1[12]={'0','1','2','3','4','5','6','7','8','9',' ','-'};//1602:0-9 數(shù)字
- uchar DispBuf[5]; //5字節(jié)的顯示緩沖區(qū)
- uchar code zzz[] = {"Made In Gao J C"};//定義字符串
- uchar code cc1[4] = {"DQ1:"};
- uchar code cc2[4] = {"DQ2:"};
- uchar code cc3[14] = {"DQ1 is so high"};
- uchar code cc4[13] = {"DQ1 is so low"};
- uchar code cc5[14] = {"DQ2 is so high"};
- uchar code cc6[13] = {"DQ2 is so low"};
- void delay(uint z)//1ms延時
- {
- uchar i;
- while(z--)
- for(i = 120;i > 0;i--);
- }
- void delay1(uchar x2)//短延時,由于18b20
- {
- while(x2--);
- }
- void write_Directive(uchar a)//寫LCD指令
- {
- RS = 0;
- RW = 0;
- delay(1);
- E = 0;
- P2 = a;
- delay(1);
- E = 1;
- delay(1);
- E = 0;
- delay(1);
- }
- void write_Data(uchar a)//寫LCD數(shù)據(jù)
- {
- RS = 1;
- RW = 0;
- delay(1);
- E = 0;
- P2 = a;
- delay(1);
- E = 1;
- delay(1);
- E = 0;
- delay(1);
- }
- void write_lcd_com_date(uchar addr,uchar date)//實現(xiàn)LCD寫指令及數(shù)據(jù)
- {
- write_Directive(addr);
- delay(1);
- write_Data(date);
- }
- void LCD_init()//LCD初始化
- {
- uchar i;
- delay(15);
- write_Directive(0x38);
- delay(5);
- write_Directive(0x38);
- delay(5);
- write_Directive(0x38);
- write_Directive(0x01);
- write_Directive(0x02);//光標(biāo)復(fù)位
- write_Directive(0x0c);//不顯示光標(biāo)
- //write_Directive(0x0f);//顯示光標(biāo)
- write_Directive(0x84);//顯示字符串
- for(i = 0;i < 7;i++)
- write_Data(zzz[i]);
- write_Directive(0xc3);
- for(i = 8;i < 15;i++)
- write_Data(zzz[i]);
- }
- void start() //AT24C02開始程序
- {
- SCL = 1;
- SDA = 1;
- nop;
- nop;
- SDA = 0;
- nop;
- nop;
- SCL = 0;
- }
- void send(uchar send_data)//AT24C02發(fā)送一個字節(jié)
- {
- ch = send_data;
- SDA = a7; SCL = 1; nop;nop; SCL = 0;
- SDA = a6; SCL = 1; nop;nop; SCL = 0;
- SDA = a5; SCL = 1; nop;nop; SCL = 0;
- SDA = a4; SCL = 1; nop;nop; SCL = 0;
- SDA = a3; SCL = 1; nop;nop; SCL = 0;
- SDA = a2; SCL = 1; nop;nop; SCL = 0;
- SDA = a1; SCL = 1; nop;nop; SCL = 0;
- SDA = a0; SCL = 1; nop;nop; SCL = 0;
- }
- uchar receive()//AT24C02接收一個程序
- {
- SDA = 1;
- SCL = 1; nop;nop; a7 = SDA; SCL = 0;
- SCL = 1; nop;nop; a6 = SDA; SCL = 0;
- SCL = 1; nop;nop; a5 = SDA; SCL = 0;
- SCL = 1; nop;nop; a4 = SDA; SCL = 0;
- SCL = 1; nop;nop; a3 = SDA; SCL = 0;
- SCL = 1; nop;nop; a2 = SDA; SCL = 0;
- SCL = 1; nop;nop; a1 = SDA; SCL = 0;
- SCL = 1; nop;nop; a0 = SDA; SCL = 0;
- return(ch);
- }
- void ack()//AT24C02是否應(yīng)答
- {
- SDA = 1;
- SCL = 1;
- nop;
- nop;
- if(SDA)
- ackflag = 1;
- else
- ackflag = 0;
- SCL=0;
- }
- void yack()//AT24C02應(yīng)答
- {
- SDA = 0;
- SCL = 1;
- nop;
- nop;
- SCL = 0;
- SDA = 1;
- }
- void nack()//AT24C02無應(yīng)答
- {
- SDA = 1;
- SCL = 1;
- nop;
- nop;
- SCL = 0;
- SDA = 0;
- }
- void stop()//AT24C02停止
- {
- SDA = 0;
- SCL = 1;
- nop;
- nop;
- SDA = 1;
- nop;
- nop;
- SCL = 0;
- SDA = 0;
- }
- void write(uchar xaddress,sint temp)//AT24C02寫入溫度
- {
- uchar a,b;
- do{
- start();
- send(0xa0);
- ack();
- } while(ackflag);
- do{
- send(xaddress);
- ack();
- }while(ackflag);
- a = temp;
- temp >>= 8;
- b = temp;
- do{
- send(a);
- ack();
- }while(ackflag);
- do{
- send(b);
- ack();
- }while(ackflag);
- stop();
- }
- uint read(uchar daddress)//AT24C02讀出溫度
- {
- uchar a,b;
- sint temp;
- do{
- start();
- send(0XA0);
- ack();
- }while(ackflag);
- do{
- send(daddress);
- ack();
- }while(ackflag);
- do{
- start();
- send(0XA1);
- ack();
- }while(ackflag);
- a = receive();
- yack();
- b = receive();
- yack();
- nack();
- stop();
- temp = b;
- temp <<= 8;
- temp |= a;
- return temp;
- }
- void send_str(sint cc,uchar chflag) // 傳送字串
- {
- uchar i,a,b,ch[4];
- ES=0;
- cc = abs(cc);
- a = cc / 100;
- b = cc % 100;
- ch[1] = b;
- ch[2] = a;
- ch[3] = chflag;
- for(i = 3;i > 0;i--){
- SBUF = ch[i];
- while(!TI); // 等特數(shù)據(jù)傳送
- TI = 0; // 清除數(shù)據(jù)傳送標(biāo)志
- ES=1;
- }
- }
- /*void serial() interrupt 4
- {a=SBUF;
- RI=0;
- flag=1;
- }
- */
- void DB18B20_init_1()//DB18B20復(fù)位
- {
- DQ1 = 1;
- delay1(20);
- DQ1 = 0;
- delay1(80);
- DQ1 = 1;
- delay1(15);
- }
- void write_DB18B20_1(uchar dat)//寫DB18B20
- {
- uchar i;
- for(i = 8;i > 0;i--){
- DQ1 = 1;
- delay1(10);
- DQ1 = 0;
- nop;
- DQ1 = dat&0x01;
- delay1(10);
- DQ1 = 1;
- dat >>= 1;
- }
- delay1(7);
- }
- uchar read_DB18B20_1()//讀DB18B20
- {
- uchar i,date;
- for(i = 8;i > 0;i--){
- DQ1 = 0;
- date >>= 1;
- DQ1 = 1;
- if(DQ1)
- date |= 0x80;
- delay(4);
- }
- return date;//返回溫度值
- }
- void DB18B20_init_2()//DB18B20復(fù)位
- {
- DQ2=1;
- delay1(20);
- DQ2=0;
- delay1(80);
- DQ2=1;
- delay1(15);
- }
- void write_DB18B20_2(uchar dat)//寫DB18B20
- {
- uchar i;
- for(i = 8;i > 0;i--){
- DQ2 = 1;
- delay1(10);
- DQ2 = 0;
- nop;
- DQ2 = dat&0x01;
- delay1(10);
- DQ2 = 1;
- dat >>= 1;
- }
- delay1(7);
- }
- uchar read_DB18B20_2()//讀DB18B20
- {
- uchar i,date;
- for(i = 8;i > 0;i--){
- DQ2 = 0;
- date >>= 1;
- DQ2 = 1;
- if(DQ2)
- date |= 0x80;
- delay(4);
- }
- return date;//返回溫度值
- }
- void tempzh_1() //溫度轉(zhuǎn)換
- {
- DB18B20_init_1();//DB18B20復(fù)位
- write_DB18B20_1(0xcc);//寫DB18B20;0xcc 跳過 ROM 指令
- write_DB18B20_1(0x44);//寫DB18B20;0x44 啟動溫度轉(zhuǎn)換指令
- }
- void tempzh_2() //溫度轉(zhuǎn)換
- {
- DB18B20_init_2();//DB18B20復(fù)位
- write_DB18B20_2(0xcc);//寫DB18B20;0xcc 跳過 ROM 指令
- write_DB18B20_2(0x44);//寫DB18B20;0x44 啟動溫度轉(zhuǎn)換指令
- }
- void tempread_1()//溫度讀取
- {
- uint temp;
- uchar a,b;
- fuflag1 = 0;
- DB18B20_init_1();//DB18B20復(fù)位
- write_DB18B20_1(0xcc);//寫DB18B20;0xcc 跳過 ROM 指令
- write_DB18B20_1(0xbe);//寫DB18B20;0xbe 讀溫度指令
- a = read_DB18B20_1(); //a為低字節(jié)8位
- b = read_DB18B20_1(); //b為高字節(jié)8位
- temp = b; //temp為溫度值UINT 16bit
- temp <<= 8; //兩個字節(jié)組合到一起
- temp |= a;//此處可以直接把補碼轉(zhuǎn)換過來,只是關(guān)于負數(shù)標(biāo)志的問題需要考慮
- if(b > 7){ //判斷正負,因為負溫度高五位是1,故而可得
- fuflag1 = 1;
- temps1 = (temp*0.0625-4096)*10-0.5;
- }
- else{
- fuflag1 = 0;
- temps1 = temp*0.0625*10+0.5;
- }
- if(tempd1 < temps1){
- tempd1 = temps1;
- write(0x00,tempd1);
- }
- else if(tempx1 > temps1){
- tempx1 = temps1;
- write(0x04,tempx1);
- }
- if(sendflag){
- t++;
- if(t == 5){
- send_str(temps1,1);
- send_str(tempd1,2);
- send_str(tempx1,3);
- }
- }
- }
- void tempread_2()
- {
- uint temp;
- uchar a,b;
- fuflag2 = 0;
- DB18B20_init_2();//DB18B20復(fù)位
- write_DB18B20_2(0xcc);//寫DB18B20;0xcc 跳過 ROM 指令
- write_DB18B20_2(0xbe);//寫DB18B20;0xbe 讀溫度指令
- a = read_DB18B20_2(); //a為低字節(jié)8位
- b = read_DB18B20_2(); //b為高字節(jié)8位
- temp = b; //temp為溫度值UINT 16bit
- temp <<= 8; //兩個字節(jié)組合到一起
- temp |= a;//此處可以直接把補碼轉(zhuǎn)換過來,只是關(guān)于負數(shù)標(biāo)志的問題需要考慮
- if(b>7){
- fuflag2 = 1;
- temps2=(temp*0.0625-4096)*10-0.5;
- }
- else{
- fuflag2=0;
- temps2=temp*0.0625*10+0.5;
- }
- if(tempd2<temps2){
- tempd2=temps2;
- write(0x08,tempd2);
- }
- else if(tempx2>temps2){
- tempx2=temps2;
- write(0x0c,tempx2);
- }
- if(sendflag){
- if(t == 5){
- t = 0;
- send_str(temps2,4);
- send_str(tempd2,5);
- send_str(tempx2,6);
- }
- }
- }
- void temp(sint temps,uchar t,bit fuflag)//顯示溫度函數(shù)
- {
- uchar i;
- uint mm;
- temps = abs(temps);
- DispBuf[0] = temps / 1000;//顯示百位
- mm = temps % 1000;
- DispBuf[1] = mm / 100;//顯示十位
- mm = mm % 100;
- DispBuf[2] = mm / 10;//顯示個位
- DispBuf[3] = mm % 10;//顯示小數(shù)點:0.1
- i = DispBuf[0];//百位判斷,如果為0,則消隱
- if(i == 0) i = 10;
- if(fuflag)//負溫度判斷,如果為0,則溫度為負的,顯示負號
- i = 11;
- DispBuf[0] = DispTab_1[i];//查表,取字符
- i = DispBuf[1];
- DispBuf[1] = DispTab_1[i];
- i = DispBuf[2];
- DispBuf[2] = DispTab_1[i];
- i = DispBuf[3];
- DispBuf[3] = DispTab_1[i];
- i = DispBuf[4];
- DispBuf[4] = DispTab_1[i];
- write_Directive(0x84+t);//第一行,第五個位置地址
- write_Data(DispBuf[0]);//第一行,第五個位置
- write_Data(DispBuf[1]);//第一行,第六個位置
- write_Data(DispBuf[2]);//第一行,第八個位置
- write_Data('.');//第一行,第七個位置
- write_Data(DispBuf[3]);//第一行,第九個位置
- }
- void readcc()//讀24c02中最高和最低溫度并顯示
- {
- bit flag;
- tempd1 = read(0x00);
- if(tempd1 > 0) flag = 0;
- else flag = 1;
- temp(tempd1,0x00,flag);
- tempx1 = read(0x04);
- if(tempx1 > 0) flag = 0;
- else flag=1;
- temp(tempx1,0x06,flag);
- tempd2 = read(0x08);
- if(tempd2 > 0) flag = 0;
- else flag = 1;
- temp(tempd2,0x40,flag);
- tempx2 = read(0x0c);
- if(tempx2 > 0) flag = 0;
- else flag = 1;
- temp(tempx2,0x46,flag);
- }
- void keyscan() //鍵掃描程序
- {
- bit flag = 0;
- if(!key1){ //按鍵1
- delay(30);
- if(!key1){
- while(!key1);
- k1flag = 1;
- flag = 1;
- }
- }
- if(!key2){ //按鍵2
- delay(30);
- if(!key2){
- while(!key2);
- k2flag = 1;
- flag = 1;
- }
- }
- if(!key3){ //按鍵3
- delay(30);
- if(!key3){
- while(!key3);
- k3flag = 1;
- flag = 1;
- }
- }
- if(!key4){ //按鍵4
- delay(30);
- if(!key4){
- while(!key4);
- k4flag = !k4flag;
- flag = 1;
- }
- }
- if(!key5){ //按鍵5
- delay(30);
- if(!key5){
- while(!key5);
- k5flag=!k5flag;
- flag=1;
- write_Directive(0x01);
- }
- }
- if(flag){
- beep=0;
- delay(100);
- beep=1;
- }
- }
- uchar read_ds1302(uchar addr)//讀DS1302
- {
- uchar i,temp = 0x00;
- DSRST = 0;
- DSIO = 1;
- DSCLK = 0;
- DSRST = 1;
- for(i = 8;i > 0;i--){ //循環(huán)8次 寫入地址數(shù)據(jù)
- DSCLK = 0;
- DSIO = addr&0x01; //每次傳輸?shù)妥止?jié)
- addr >>= 1; //右移一位
- DSCLK = 1;
- }
- delay(1);
- if(DSIO)
- temp |= 0x80; //每次傳輸?shù)妥止?jié)
- DSCLK = 0;
- temp >>= 1;
- for(i = 7;i > 0;i--){ //循環(huán)7次 讀取數(shù)據(jù)此處如果讀8次就會出現(xiàn)亂碼
- DSCLK = 0;
- if(DSIO)
- temp |= 0x80; //每次傳輸?shù)妥止?jié)
- DSCLK = 1;
- temp >>= 1; //右移一位
- }
- DSCLK = 1;
- DSRST = 0;
- DSIO = 0;
- return temp;
- }
- void write_ds1302(uchar addr, uchar date)//寫DS1302
- {
- uchar i;
- DSRST = 0;
- DSCLK = 0;
- DSRST = 1;
- for (i = 8;i > 0;i--){ //循環(huán)8次 寫入地址數(shù)據(jù)
- DSCLK = 0;
- nop;
- DSIO = addr&0x01; //每次傳輸?shù)妥止?jié)
- addr >>= 1; //右移一位
- DSCLK = 1;
- nop;
- }
- for (i = 8;i > 0;i--){ //循環(huán)8次 寫入數(shù)據(jù)
- DSCLK = 0;
- nop;
- DSIO = date&0x01; //每次傳輸?shù)妥止?jié)
- date >>= 1; //右移一位 DSCLK=1
- DSCLK = 1;
- nop;
- }
- DSRST = 0;
- delay(1);
- }
- void init_ds1302()//DS1302初始化
- {
- write_ds1302(0x8e,0x00);
- write_ds1302(0x90,0xa5);
- /*write_ds1302(0x80,0x00); //00秒
- write_ds1302(0x82,0x54); //54分
- write_ds1302(0x84,0x17); //17點
- write_ds1302(0x86,0x13); //13日
- write_ds1302(0x88,0x03); //3月
- write_ds1302(0x8a,0x05); //星期五
- write_ds1302(0x8c,0x12); //2012年*/
- }
- void time()//時間顯示
- {
- write_lcd_com_date(0x81,'2');
- write_lcd_com_date(0x82,'0');
- second = read_ds1302(0x81);
- secondl = second & 0x0f;
- secondh = second >> 4;
-
- minute = read_ds1302(0x83);
- minutel = minute & 0x0f;
- minuteh = minute >> 4;
-
- hour = read_ds1302(0x85);
- hourl = hour & 0x0f;
- hourh = hour >> 4;
- month = read_ds1302(0x89);
- monthl = month & 0x0f;
- monthh = month >> 4;
-
- year = read_ds1302(0x8d);
- yearl = year & 0x0f;
- yearh = year >> 4;
- day = read_ds1302(0x87);
- dayl = day & 0x0f;
- dayh = day >> 4;
- week = read_ds1302(0x8b);
- week = week&0x0f;
- write_lcd_com_date(0xca,0x30 + secondl);
- write_lcd_com_date(0xc9,0x30 + secondh);
- write_lcd_com_date(0xc8,':');
- write_lcd_com_date(0xc7,0x30 + minutel);
- write_lcd_com_date(0xc6,0x30 + minuteh);
- write_lcd_com_date(0xc5,':');
- write_lcd_com_date(0xc4,0x30 + hourl);
- write_lcd_com_date(0xc3,0x30 + hourh);
- write_lcd_com_date(0x84,0x30 + yearl);
- write_lcd_com_date(0x83,0x30 + yearh);
- write_lcd_com_date(0x85,'-');
- write_lcd_com_date(0x87,0x30 + monthl);
- write_lcd_com_date(0x86,0x30 + monthh);
- write_lcd_com_date(0x88,'-');
- write_lcd_com_date(0x8a,0x30 + dayl);
- write_lcd_com_date(0x89,0x30 + dayh);
- write_lcd_com_date(0x8c,0x30 + week);
- }
- void shuma(uchar addr,uchar xz,bit flag)
- {
- char date;
- uchar day=0;
- date = read_ds1302(addr + 1); //讀分寄存器上的數(shù)據(jù)
- date = ((date >> 4) * 10) + (date & 0x0f); //BCD Convert to shi
- if(flag == 0) date++;
- else date--;
- switch(xz)
- {
- case 0:if(date > 59) date=0;if(date < 0) date = 59;break;//分秒
- case 1:if(date > 23) date=0;if(date < 0) date = 23;break;//時
- case 2:if(date > 12) date=0;if(date < 0) date = 12;break;//月
- case 3:if(date > 99) date=0;if(date < 0) date = 99;break;//年
- case 4:if(date > 7) date=1;if(date < 1) date = 7;break;//星期
- case 5:
- day = read_ds1302(0X87);
- day = ((day >> 4) * 10) + (day & 0x0f);
- if(day != 2){
- if(day % 2 == 1){
- if(date > 31)
- date = 0;
- else if(date < 0)
- date = 31;
- }
- else{
- if(date > 30)
- date = 0;
- else if(date < 0)
- date = 30;
- }
- }
- else{
- if(date > 29)
- date = 0;
- else if(date < 0)
- date = 29;
- }
- break;
- default:break;
- }
- date = ((date / 10) << 4) + (date % 10);//Hex Convert to BCD
- write_ds1302(addr,date);//將變量里的數(shù)據(jù)(BCD碼)寫回到分寄存器上
- }
- void flash(uchar bz)//字符閃爍可以采用亮滅的方式
- {
- bit flag = 0;
- uchar sj = 0;
- while(1){
- sj++;
- if(sj == 25){
- sj = 0;
- flag = !flag;
- }
- keyscan();
- if(!flag){
- second = read_ds1302(0x81);
- secondl = second & 0x0f;
- secondh = second >> 4;
- write_lcd_com_date(0xca,0x30 + secondl);
- write_lcd_com_date(0xc9,0x30 + secondh);
- }
- switch(bz)
- {
- case 0:
- if(flag){
- write_lcd_com_date(0xca,' ');
- write_lcd_com_date(0xc9,' ');
- }
- break;
- case 1:
- if(flag){
- write_lcd_com_date(0xc7,' ');
- write_lcd_com_date(0xc6,' ');
- }
- else{
- write_lcd_com_date(0xc7,0x30+minutel);
- write_lcd_com_date(0xc6,0x30+minuteh);
- }
- break;
- case 2:
- if(flag){
- write_lcd_com_date(0xc4,' ');
- write_lcd_com_date(0xc3,' ');
- }
- else{
- write_lcd_com_date(0xc4,0x30+hourl);
- write_lcd_com_date(0xc3,0x30+hourh);
- }
- break;
- case 3:
- if(flag){
- write_lcd_com_date(0x84,' ');
- write_lcd_com_date(0x83,' ');
- }
- else{
- write_lcd_com_date(0x84,0x30+yearl);
- write_lcd_com_date(0x83,0x30+yearh);
- }
- break;
- case 4:
- if(flag){
- write_lcd_com_date(0x87,' ');
- write_lcd_com_date(0x86,' ');
- }
- else{
- write_lcd_com_date(0x87,0x30+monthl);
- write_lcd_com_date(0x86,0x30+monthh);
- }
- break;
- case 5:
- if(flag){
- write_lcd_com_date(0x8a,' ');
- write_lcd_com_date(0x89,' ');
- }
- else{
- write_lcd_com_date(0x8a,0x30+dayl);
- write_lcd_com_date(0x89,0x30+dayh);
- }
- break;
- case 6:
- if(flag){
- write_lcd_com_date(0x8c,' ');
- delay(10);
- }
- else{
- write_lcd_com_date(0x8c,0x30+week);
- delay(10);
- }
- break;
- default:break;
- }
- if(k5flag || !k4flag || k3flag || k2flag || k1flag)
- {
- if(k5flag){
- k3flag = 0;
- k4flag = 0;
- write_Directive(0x01);
- }
- write_Directive(0x0c);
- break;
- }
- }
- }
- void timekey()//時間部分功能鍵
- {
- keyscan();
- if(!k5flag){
- if(k4flag){
- if(k3flag){
- k3flag = 0;
- count++;
- }
- switch(count)
- {
- case 0:
- flash(6);
- if(k2flag){
- shuma(0x8a,4,1);//week
- k2flag = 0;
- }
- else if(k1flag){
- shuma(0x8a,4,0);
- k1flag = 0;
- }
- break;
- case 1:
- flash(5);
- if(k2flag){
- shuma(0x86,5,1);//day
- k2flag = 0;
- }
- else if(k1flag){
- shuma(0x86,5,0);
- k1flag = 0;
- }
- break;
- case 2:
- flash(4);
- if(k2flag){
- shuma(0x88,2,1);//month
- k2flag = 0;
- }
- else if(k1flag){
- shuma(0x88,2,0);
- k1flag = 0;
- }
- break;
- case 3:
- flash(3);
- if(k2flag){
- shuma(0x8c,3,1);//year
- k2flag = 0;
- }
- else if(k1flag){
- shuma(0x8c,3,0);
- k1flag = 0;
- }
- break;
- case 4:
- flash(0);
- if(k2flag){
- shuma(0x80,0,1);//second
- k2flag=0;
- }
- else if(k1flag){
- shuma(0x80,0,0);
- k1flag = 0;
- }
- break;
- case 5:
- flash(1);
- if(k2flag){
- shuma(0x82,0,1);//minute
- k2flag = 0;
- }
- else if(k1flag){
- shuma(0x82,0,0);
- k1flag = 0;
- }
- break;
- case 6:
- flash(2);
- if(k2flag){
- shuma(0x84,1,1);//hour
- k2flag=0;
- }
- else if(k1flag){
- shuma(0x84,1,0);
- k1flag=0;
- }
- break;
- default : break;
- }
- if(count > 6)
- count = 0;
- }
- }
- }
- void tempkey()//溫度部分功能鍵
- {
- uchar i;
- bit flag;
- keyscan();
- if(k5flag){
- write_Directive(0x80);
- for(i = 0;i < 4;i++){
- write_Data(cc1[i]);//顯示字符串
- }
- write_Directive(0xc0);
- for(i = 0;i < 4;i++){
- write_Data(cc2[i]);//顯示字符串
- }
- if(k2flag && k1flag){
- readcc();
- delay(5000);
- write_Directive(0x01);
- k2flag = 0;
- k1flag = 0;
- }
- if(k2flag && k3flag){
- write(0x00,-550);
- write(0x04,1250);
- write(0x08,-550);
- write(0x0c,1250);
- k2flag = 0;
- k3flag = 0;
- }
- if(k4flag){
- temp(uptem1,0x00,flag);
- temp(downtem1,0x06,flag);
- temp(uptem2,0x40,flag);
- temp(downtem2,0x46,flag);
- while(1){
- keyscan();
- if(k3flag){
- k3flag = 0;
- tcount++;
- }
- switch(tcount)
- {
- case 0:
- write_Directive(0x89);
- write_Directive(0x0f);
- if(k1flag){
- uptem1 += 10;
- k1flag = 0;
- }
- else if(k2flag){
- uptem1 -= 10;
- k2flag = 0;
- }
- if(uptem1 > 0)
- flag = 0;
- else
- flag = 1;
- temp(uptem1,0x00,flag);
- write(0x10,uptem1);
- break;
- case 1:
- write_Directive(0x8f);
- write_Directive(0x0f);
- if(k1flag){
- downtem1 += 10;
- k1flag = 0;
- }
- else if(k2flag){
- downtem1 -= 10;
- k2flag=0;
- }
- if(downtem1 > 0)
- flag = 0;
- else
- flag = 1;
- temp(downtem1,0x06,flag);
- write(0x14,downtem1);
- break;
- case 2:
- write_Directive(0xc9);
- write_Directive(0x0f);
- if(k1flag){
- uptem2 += 10;
- k1flag=0;
- }
- if(k2flag){
- uptem2 -= 10;
- k2flag = 0;
- }
- if(uptem2 > 0)
- flag = 0;
- else
- flag = 1;
- temp(uptem2,0x40,flag);
- write(0x18,uptem2);
- break;
- case 3:
- write_Directive(0xcf);
- write_Directive(0x0f);
- if(k1flag){
- downtem2 += 10;
- k1flag=0;
- }
- else if(k2flag){
- downtem2 -= 10;
- k2flag = 0;
- }
- if(downtem2 > 0)
- flag = 0;
- else
- flag = 1;
- temp(downtem2,0x46,flag);
- write(0x1c,downtem2);
- break;
- default: break;
- }
- if(tcount > 3)
- tcount = 0;
- if(!k4flag || !k5flag){
- if(!k5flag)
- k4flag=0;
- write_Directive(0x01);
- write_Directive(0x0c);
- break;
- }
- }
- }
- }
- }
- void tempbj() //溫度比較
- {
- uchar i;
- if(temps1 > uptem1){
- beep = 0;
- led = 0;
- write_Directive(0x01);
- write_Directive(0x81);//第二行第一位地址
- for(i = 0;i < 14;i++){
- write_Data(cc3[i]);//顯示字符串
- }
- delay(3000);
- beep = 1;
- led = 1;
- write_Directive(0x01);
- }
- else if(temps1<downtem1){
- beep=0;
- led=0;
- write_Directive(0x01);
- write_Directive(0x81);//第二行第一位地址
- for(i=0;i<13;i++){
- write_Data(cc4[i]);//顯示字符串
- }
- delay(3000);
- beep=1;
- led=1;
- write_Directive(0x01);
- }
- if(temps2>uptem2){
- beep=0;
- led=0;
- write_Directive(0x01);
- write_Directive(0x81);//第二行第一位地址
- for(i=0;i<14;i++){
- write_Data(cc5[i]);//顯示字符串
- }
- delay(3000);
- beep=1;
- led=1;
- write_Directive(0x01);
- }
- else if(temps2<downtem2){
- beep=0;
- led=0;
- write_Directive(0x01);
- write_Directive(0x81);//第二行第一位地址
- for(i=0;i<13;i++){
- write_Data(cc6[i]);//顯示字符串
- }
- delay(3000);
- beep=1;
- led=1;
- write_Directive(0x01);
- }
- }
- /*void adjust_res2() ///res 分別等于 0x1f, 0x3f, 0x5f 溫度讀數(shù)分辨率分別對應(yīng)
- // 0.5, 0.25, 0.125
- { DB18B20_init_2();
- write_DB18B20_2(0xcc); //跳過Rom
- write_DB18B20_2(0x4e); //寫暫存器
- write_DB18B20_2(0x70); //寫TH
- write_DB18B20_2(0x01); //寫TL
- write_DB18B20_2(0x3f); //寫結(jié)構(gòu)寄存器
- write_DB18B20_2(0xcc); //跳過Rom
- write_DB18B20_2(0x48); //把暫存器內(nèi)容寫到EPRam中
- }
- void adjust_res1() ///res 分別等于 0x1f, 0x3f, 0x5f 溫度讀數(shù)分辨率分別對應(yīng)
- // 0.5, 0.25, 0.125
- { DB18B20_init_1();
- write_DB18B20_1(0xcc); //跳過Rom
- write_DB18B20_1(0x4e); //寫暫存器
- write_DB18B20_1(0x70); //寫TH
- write_DB18B20_1(0x01); //寫TL
- write_DB18B20_1(0x3f); //寫結(jié)構(gòu)寄存器
- write_DB18B20_1(0xcc); //跳過Rom
- write_DB18B20_1(0x48); //把暫存器內(nèi)容寫到EPRam中
- } */
- void main()
- {
- uchar i;
- /*write(0x00,250);
- write(0x04,150);
- write(0x08,250);
- write(0x0c,150);
- write(0x10,500);
- write(0x14,50);
- write(0x18,500);
- write(0x1c,50);*/
- TMOD = 0x20; // 定時器1工作于8位自動重載模式, 用于產(chǎn)生波特率
- TH1 = 0xfd; // 波特率9600
- TL1 = 0xfd;
- EA=1; //開中斷
- ES=1; //允許串口中斷
- SM0=0;
- SM1=1;
- REN=1;
- TR1=1;
- uptem1 = read(0x10);
- downtem1 = read(0x14);
- uptem2 = read(0x18);
- downtem2 = read(0x1c);
- LCD_init();//LCD復(fù)位
- tempzh_1();
- tempzh_2();
- delay(1000);//延時
- tempread_1();
- tempread_2();
- tempzh_1();
- tempzh_2();
- delay(1000);//延時
- tempread_1();
- tempread_2();
- delay(3000);//延時
- readcc();
- write_Directive(0x01);
- init_ds1302();
- delay(100);
- /*adjust_res1();
- adjust_res2();*/
- while(1){
- if(RI){ // 是否有數(shù)據(jù)到來
- RI = 0;
- i = SBUF; // 暫存接收到的數(shù)據(jù)
- if(i){
- sendflag = 1;
- send_str(0x0000,0);
- }
- else{
- sendflag = 0;
- }
- }
- tempzh_1();
- tempzh_2();
- keyscan();
- if(!k5flag){
- time();
- timekey();
- }
- else{
- temp(temps1,0x00,fuflag1);
- temp(temps2,0x40,fuflag2);
- tempkey();
- tempbj();
- }
- delay(35);
- keyscan();
- delay(35);
- keyscan();
- delay(35);
- keyscan();
- delay(35);
- tempread_1();
- tempread_2();
- }
- }
復(fù)制代碼
|
|