完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
求教各位高手:像这样的程序该如何设计时钟温度界面和闹钟界面切换,刚上电时12864显示时钟温度界面,我曾尝试过用“if(alARM_coun!=0)切换到闹钟界面修改闹钟时分秒和开关闹钟,但修改好了以后无法返回时钟温度界面。以下是程序烦各位高手帮看看。 #include #include "intrins.h" #include "24c02.h" #define uchar unsigned char #define uint unsigned int //寄存器宏定义 #define WRITE_PROTECT 0x8E #define WRITE_SECOND 0X80 //写(秒)地址 #define WRITE_MINUTE 0X82 //写(分)地址 #define WRITE_HOUR 0x84 //写(时)地址 #define WRITE_WEEK 0x8A //写(周)地址 #define WRITE_YEAR 0x8C //写(年)地址 #define WRITE_MONTH 0x88 //写(月)地址 #define WRITE_DAY 0x86 //写(日)地址 #define READ_SECOND 0x81//读(秒)地址 #define READ_MINUTE 0x83//读(分)地址 #define READ_HOUR 0x85//读(时)地址 #define READ_WEEK 0x8B//读(周)地址 #define READ_YEAR 0x8D//读(年)地址 #define READ_MONTH 0x89//读(月)地址 #define READ_DAY 0x87//读(日)地址 ***it LCD_RS = P2 ^ 0; //寄存器选择输入 ***it LCD_RW = P2 ^ 1; //液晶读/写控制 ***it LCD_EN = P2 ^ 2; //液晶使能控制 ***it LCD_PSB = P2 ^ 3; //串/并方式控制 ***it LCD_RST = P2 ^ 5; //液晶复位端口 sfr P1M0 =0X92;//寄存器地址 ***it ACC_7=ACC^7; //设置累加器 ***it SCLK=P1^0 ;// DS1302时钟信号7脚 ***it DIO=P1^1 ;// DS1302数据信号6脚 ***it CE=P1^2 ;// DS1302片选5脚 ***it fm= P2^6; //蜂鸣器 ***it DQ=P2^7; //18B20总线 ***it key1=P3^0;//时钟修改选择键 ***it key2=P3^1; //加键 ***it key3=P3^2;//减键 ***it key4=P3^3;//闹钟修改选择键 uchar nian,yue,ri,shi,fen,miao,zhou,I,count,temm; //时分秒年月日周指针 uchar flag_alarm,alarm_coun,alarm_on; uchar alarm_shi,alarm_fen,alarm_miao; //闹钟时分秒指针 uchar code DIS1[16]={"星期1"}; uchar code DIS2[16]={"2011年08月01日"}; uchar code DIS3[16]={"00: 00: 00"}; uchar code DIS4[16]={"当前温度27℃"}; uchar code DIS5[16]={"闹钟"}; uchar code DIS6[16]={"ON#"}; uchar code DIS7[16]={"OFF"}; uchar code data16[]={0X00,0X01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09, 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19, 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29, 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49, 0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59, 0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69, 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79, 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89, 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,}; void delayus(uchar i) //延时函数 { while(i--); } void delayms(uint ms) //延时函数 { uchar y; while (ms--) { for (y = 0; y < 114; y++); } } void delayNOP() //延时函数 { _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); } void lcd_wcmd(uchar cmd) //往12864里面写指令 { LCD_RS=0; LCD_RW=0; LCD_EN=0; P0=cmd; delayms(5); LCD_EN=1; delayms(5); LCD_EN=0; } void lcd_wdat(uchar dat) //往12864里面写数据 { LCD_RS=1; LCD_RW=0; LCD_EN=0; P0=dat; delayms(5); LCD_EN=1; delayms(5); LCD_EN=0; } void lcd_init()//12864液晶初始化 { LCD_PSB=1; delayms(5); lcd_wcmd(0x30); delayms(5); lcd_wcmd(0x0C); delayms(5); lcd_wcmd(0x06); delayms(5); lcd_wcmd(0x01); delayms(5); } void lcd_pos(uchar X, uchar Y) { uchar pos; if (X==1) { X=0x80; } else if (X==2) { X=0x90; } else if (X==3) { X=0x88; } else if (X==4) { X=0x98; } pos=X+Y; //确定显示地址 lcd_wcmd(pos); //写显示地址 } /*地址、数据发送子程序*/ void Write1302(uchar addr,dat) { uchar i,temp; CE=0;//CE引脚为低,数据传送中止 SCLK=0;//清零时钟总线 CE=1;//CE引脚为高,逻辑控制有效 for(i=8;i>0;i--) //循环8次移位 {SCLK=0; //清零时钟总线 delayNOP(); temp=addr; DIO=(bit)(temp&0x01);//每次传输低字节. (bit) 对计算结果取位。不管计算结果(temp&0x01)是几位,它只取最右边的一位。 addr>>=1;//右移一位 SCLK=1; } /*发送数据*/ for(i=8;i>0;i--) { SCLK=0; delayNOP(); temp=dat; DIO=(bit)(temp&0x01); dat>>=1; SCLK=1; } CE=0; } /*数据读取子程序*/ uchar Read1302(uchar addr) {uchar i,temp,dat1,dat2; CE=0; SCLK=0; delayNOP(); CE=1; /*发送地址*/ for(i=8;i>0;i--) //循环8次移位 {SCLK=0; delayNOP(); temp=addr; DIO=(bit)(temp&0x01);//每次传输低字节 addr>>=1; //右移一位 SCLK=1; } /*读取数据*/ for(i=8;i>0;i--) {ACC_7=DIO; SCLK=1; delayNOP(); ACC>>=1; SCLK=0; } CE=0; dat1=ACC; dat2=dat1/16;//数据进制转换 dat1=dat1%16;//十六进制转十进制 dat1=dat1+dat2*10; return(dat1); } void Initial1302() //1302 初始化 {Write1302(WRITE_PROTECT,0X00);//(地址,数据) Write1302(WRITE_SECOND,0X00);//秒位初始化 Write1302(WRITE_MINUTE,0X58);//分钟初始化 Write1302(WRITE_HOUR,0X23);//小时初始化 Write1302(WRITE_WEEK,0X01); Write1302(WRITE_DAY,0X01); Write1302(WRITE_MONTH,0x08) ; Write1302(WRITE_YEAR,0X11); Write1302(WRITE_PROTECT,0X80);//允许写保护 } //温度 void init_ds18b20() //18b20初始化 { uchar j; DQ=0; delayus(100); DQ=1; delayus(5); j=DQ; delayus(15); } void ds18b20write_byte(uchar dat) //写 { uchar i; for(i=0;i<8;i++) { DQ=0; DQ=dat&0x01; delayus(8); DQ=1; dat>>=1; } delayus(8); } uchar ds18b20read_byte(void)//读 { uchar j,value; for(j=0;j<8;j++) { DQ=0; value>>=1; DQ=1; if(DQ==1) value|=0x80; delayus(6); } return value; } uchar readtemperature() { uchar a,b; init_ds18b20(); ds18b20write_byte(0xcc); //跳过ROM ds18b20write_byte(0x44); //启动温度测量 delayus(30); init_ds18b20(); ds18b20write_byte(0xcc); ds18b20write_byte(0xbe); a=ds18b20read_byte(); b=ds18b20read_byte(); b<<=4; b+=(a&0xf0)>>4; return b; } /*显示*/ void xianshi()//时分秒,年月日,周,显示位置 { uchar a,b,c,d,e,f,g,h,k,l,m,n; a=miao%10; b=miao/10; c=fen%10; d=fen/10; e=shi%10; f=shi/10; g=ri%10; h=ri/10; k=yue%10; l=yue/10; m=nian%10; n=nian/10; lcd_pos(1,4); lcd_wdat(zhou+0x30); lcd_pos(2,1); lcd_wdat(n+0x30); lcd_wdat(m+0x30); lcd_pos(2,3); lcd_wdat(l+0x30); lcd_wdat(k+0x30); lcd_pos(2,5); lcd_wdat(h+0x30); lcd_wdat(g+0x30); lcd_pos(3,0); lcd_wdat(f+0X30); lcd_wdat(e+0x30); lcd_pos(3,2); lcd_wdat(d+0x30); lcd_wdat(c+0x30); lcd_pos(3,4); lcd_wdat(b+0x30); lcd_wdat(a+0x30); } void miaogai() //秒改 { while(1) { if(count==1)// 如果结果为1就执行if里面的语句,如果为0则不执行 { lcd_pos(3,4); lcd_wcmd(0x0f); //显示开,开光标 if(key2==0) { delayms(5); if(key2==0) { miao++; if(miao>=60)miao=0; Write1302(WRITE_PROTECT,0X00); Write1302(WRITE_SECOND,data16[miao]); Write1302(WRITE_PROTECT,0X80); xianshi(); } } if(key3==0){delayms(5); if(key3==0) {if(miao==0)miao=59; else miao--; Write1302(WRITE_PROTECT,0X00); Write1302(WRITE_SECOND,data16[miao]); Write1302(WRITE_PROTECT,0X00); xianshi();} } delayms(5); } break; //即满足条件时,便跳出循环。 } } void fengai() //分修改 { while(1) { if(count==2) { lcd_pos(3,2); lcd_wcmd(0x0f); //显示开,关光标 if(key2==0) { delayms(5); if(key2==0) { fen++; if(fen>=60) fen=0; Write1302(WRITE_PROTECT,0X00); Write1302(WRITE_MINUTE,data16[fen]); Write1302(WRITE_PROTECT,0X80); xianshi(); } } if(key3==0){delayms(5); if(key3==0) {if(fen==0)fen=59; else fen--; Write1302(WRITE_PROTECT,0X00); Write1302(WRITE_MINUTE,data16[fen]); Write1302(WRITE_PROTECT,0X80);xianshi();} } delayms(5);} break; } } void shigai() //时修改 { while(1) { if(count==3) { lcd_pos(3,0); lcd_wcmd(0x0f); //显示开,关光标 if(key2==0) { delayms(5); if(key2==0) { shi++; if(shi>=24)shi=0; Write1302(WRITE_PROTECT,0X00); Write1302(WRITE_HOUR,data16[shi]); Write1302(WRITE_PROTECT,0X80); } xianshi(); delayms(5); } if(key3==0) { delayms(5); if(key3==0) { if(shi==0)shi=23; else shi--; Write1302(WRITE_PROTECT,0X00); Write1302(WRITE_HOUR,data16[shi]); Write1302(WRITE_PROTECT,0X80); } xianshi(); } delayms(5); } break; } } void niangai() // 年修改 { while(1) { if(count==4) { lcd_pos(2,1); lcd_wcmd(0x0f); //显示开,关光标 if(key2==0) { delayms(5); if(key2==0) { nian++; if(nian>=100)nian=0; Write1302(WRITE_PROTECT,0X00); Write1302(WRITE_YEAR,data16[nian]); Write1302(WRITE_PROTECT,0X80); } xianshi(); } if(key3==0){delayms(5); if(key3==0) {if(nian==0)nian=99; else nian--; Write1302(WRITE_PROTECT,0X00); Write1302(WRITE_YEAR,data16[nian]); Write1302(WRITE_PROTECT,0X80); } xianshi(); } delayms(5);} break; } } void yuegai()//月修改 { while(1) { if(count==5) { lcd_pos(2,3); lcd_wcmd(0x0f); //显示开,关光标 if(key2==0) { delayms(5); if(key2==0) { yue++; if(yue>=13)yue=1; Write1302(WRITE_PROTECT,0X00); Write1302(WRITE_MONTH,data16[yue]); Write1302(WRITE_PROTECT,0X80); } xianshi(); } if(key3==0){delayms(5); if(key3==0) {if(yue==1)yue=12; else yue--; Write1302(WRITE_PROTECT,0X00); Write1302(WRITE_MONTH,data16[yue]); Write1302(WRITE_PROTECT,0X80); } xianshi(); } delayms(5);} break; } } void rigai() // 日 改 { while(1) { if(count==6) { lcd_pos(2,5); lcd_wcmd(0x0f); //显示开,关光标 if(key2==0) { delayms(5); if(key2==0) { ri++; if(ri>=32)ri=0; Write1302(WRITE_PROTECT,0X00); Write1302(WRITE_DAY,data16[ri]); Write1302(WRITE_PROTECT,0X80); } xianshi(); } if(key3==0){delayms(5); if(key3==0) { if(ri==1)ri=31; else ri--; Write1302(WRITE_PROTECT,0X00); Write1302(WRITE_DAY,data16[ri]); Write1302(WRITE_PROTECT,0X80); } xianshi(); } delayms(5);} break; } } void zhougai() //周改 { while(1) { if(count==7) { lcd_pos(1,4); lcd_wcmd(0x0f); //显示开,关光标 if(key2==0) { delayms(10); if(key2==0) { zhou++; if(zhou>=8)zhou=1; Write1302(WRITE_PROTECT,0X00); Write1302(WRITE_WEEK,data16[zhou]); Write1302(WRITE_PROTECT,0X80); } xianshi(); } if(key3==0) { delayms(5); if(key3==0) { if(zhou==1)zhou=7; else zhou--; Write1302(WRITE_PROTECT,0X00); Write1302(WRITE_WEEK,data16[zhou]); Write1302(WRITE_PROTECT,0X80); } xianshi(); } delayms(5); } break; } } void wenduweiz() //温度显示位置 { uchar o,p; o=temm%10; p=temm/10; lcd_pos(4,5);//显示第四行 lcd_wdat(p+0x30); lcd_wdat(o+0x30); { lcd_pos(4,4); data16[temm]; } } void gaishijian() //时间修改选择键 { if(key1==0) { delayms(2); if(key1==0)while(!key1); count++; } while(count) { shigai();//时修改 fengai();//分修改 miaogai();//秒修改 niangai();//年修改 yuegai();//月修改 rigai();//日 zhougai(); //周 if(count==8)//count=1选择秒改,以次类推。假如count等于8次那么count结束退出修改 {count=0; lcd_wcmd(0x0c);//显示开关光标 } break; } //跳出循环 delayms(100); } void shizhong() //时钟温度显示界面 { // lcd_init(); //12864初始化 lcd_wcmd(0x01);// 清屏 P0=0xff; P0=0; lcd_pos(1,2); I=0; while(DIS1[I]!=' |