#include<reg52.h> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义#include<intrins.h>#include "i2c.h"sbit RS = P2^6; //定义端口 sbit RW = P2^5;sbit EN = P2^7;sbit Beep = P3^0;/*sbit KEY1 = P1^0; //暂停sbit KEY2 = P1^1; //开始sbit KEY3 = P1^2; //选择哪一位操作sbit KEY4 = P1^3; //++sbit KEY5 = P1^4; //--sbit KEY6 = P1^5;*/#define GPIO_KEY P1#define RS_CLR RS=0 #define RS_SET RS=1#define RW_CLR RW=0 #define RW_SET RW=1 #define EN_CLR EN=0#define EN_SET EN=1#define DataPort P0char hour,min,sec;unsigned int kkk;unsigned char flag = 1;unsigned char num = 0;unsigned char local = 0;unsigned char weizhi;unsigned char KeyValue;char clhour,clmin,clsec,clock = 0;//unsigned char hour1[] = "change hour";//unsigned char min1[] = "change min";//unsigned char sec1[] = "change sec";unsigned char KeyScan(void);/*------------------------------------------------ uS延时函数,含有输入参数 unsigned char t,无返回值 unsigned char 是定义无符号字符变量,其值的范围是 0~255 这里使用晶振12M,精确延时请使用汇编,大致延时 长度如下 T=tx2+5 uS ------------------------------------------------*/void DelayUs2x(unsigned char t){ while(--t);}/*------------------------------------------------ mS延时函数,含有输入参数 unsigned char t,无返回值 unsigned char 是定义无符号字符变量,其值的范围是 0~255 这里使用晶振12M,精确延时请使用汇编------------------------------------------------*/void DelayMs(unsigned char t){ while(t--) { //大致延时1mS DelayUs2x(245); DelayUs2x(245); }}/*------------------------------------------------ 判忙函数------------------------------------------------*/ bit LCD_Check_Busy(void) { DataPort= 0xFF; RS_CLR; RW_SET; EN_CLR; _nop_(); EN_SET; return (bit)(DataPort & 0x80); }/*------------------------------------------------ 写入命令函数------------------------------------------------*/ void LCD_Write_Com(unsigned char com) { // while(LCD_Check_Busy()); //忙则等待 DelayMs(5); RS_CLR; RW_CLR; EN_SET; DataPort= com; _nop_(); EN_CLR; }/*------------------------------------------------ 写入数据函数------------------------------------------------*/ void LCD_Write_Data(unsigned char Data) { //while(LCD_Check_Busy()); //忙则等待 DelayMs(5); RS_SET; RW_CLR; EN_SET; DataPort= Data; _nop_(); EN_CLR; }/*------------------------------------------------ 清屏函数------------------------------------------------*/ void LCD_Clear(void) { LCD_Write_Com(0x01); DelayMs(5); }/*------------------------------------------------ 写入字符串函数------------------------------------------------*/ void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s) { if (y == 0) { LCD_Write_Com(0x80 + x); } else { LCD_Write_Com(0xC0 + x); } while (*s) { LCD_Write_Data( *s); s ++; } }/*------------------------------------------------ 写入字符函数------------------------------------------------*/ void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data) { if (y == 0) { LCD_Write_Com(0x80 + x); } else { LCD_Write_Com(0xC0 + x); } LCD_Write_Data( Data); }/*------------------------------------------------ 初始化函数------------------------------------------------*/ void LCD_Init(void) { LCD_Write_Com(0x38); /*显示模式设置*/ DelayMs(5); LCD_Write_Com(0x38); DelayMs(5); LCD_Write_Com(0x38); DelayMs(5); LCD_Write_Com(0x38); LCD_Write_Com(0x08); /*显示关闭*/ LCD_Write_Com(0x01); /*显示清屏*/ LCD_Write_Com(0x06); /*显示光标移动设置*/ DelayMs(5); LCD_Write_Com(0x0C); /*显示开及光标设置*/ }void Delay(unsigned char i){ while(i--);}/*------------------------------------------------ 主函数------------------------------------------------*/ void main(void) { //unsigned char i; //unsigned char *p; LCD_Init(); /*while (1) { i = 1; p = "1234567890"; LCD_Clear(); LCD_Write_String(2,0,"Welcome to"); DelayMs(250); while (*p) { LCD_Write_Char(i,1,*p); i ++; p ++; DelayMs(250); } DelayMs(250); } */ TMOD = 0x01; //定时器模式,工作方式是十六位定时器 TH0 = 0x3c; TL0 = 0xb0; EA = 1; ET0 = 1; TR0 = 1; hour = 12; min = 59; sec = 55; //Beep= 0; while(1) { num = KeyScan(); if(num == 1) { flag = 0; } if(num == 2) { flag = 1; } if(num == 3) { local++; if(local == 6) { local = 0; } switch(local) { case 5://LCD_Clear(); /*LCD_Write_Com(0x01); LCD_Write_String(2,1,"change sec01");*/ LCD_Write_Char(14,0,' '); DelayMs(50); break; case 4://LCD_Clear(); LCD_Write_Com(0x01); LCD_Write_String(2,1,"change sec10"); break; case 3://LCD_Clear(); LCD_Write_Com(0x01); LCD_Write_String(2,1,"change min01"); break; case 2://LCD_Clear(); LCD_Write_Com(0x01); LCD_Write_String(2,1,"change min10"); break; case 1://LCD_Clear(); LCD_Write_Com(0x01); LCD_Write_String(2,1,"change hou01"); break; case 0://LCD_Clear(); LCD_Write_Com(0x01); LCD_Write_String(2,1,"change hou10"); break; } //LCD_Clear(); } if(num == 4) { if(clock == 0) { switch(local) { case 5:sec++; if(sec >= 60) { min++; sec = 0; }break; case 4:sec += 10; if(sec >= 60) { min++; sec = 0; }break; case 3:min++; if(min >= 60) { hour++; min = 0; }break; case 2:min += 10; if(min >= 60) { hour++; min = 0; }break; case 1:hour++; if(hour >= 24) { sec = 0; min = 0; hour = 0; }break; case 0:hour += 10; if(hour >= 24) { sec = 0; min = 0; hour = 0; }break; } } else if(clock == 1) { switch(local) { case 5:clsec++; if(clsec >= 60) { clmin++; clsec = 0; }break; case 4:clsec += 10; if(clsec >= 60) { clmin++; clsec = 0; }break; case 3:clmin++; if(clmin >= 60) { clhour++; clmin = 0; }break; case 2:clmin += 10; if(clmin >= 60) { clhour++; clmin = 0; }break; case 1:clhour++; if(clhour >= 24) { clsec = 0; clmin = 0; clhour = 0; }break; case 0:clhour += 10; if(clhour >= 24) { clsec = 0; clmin = 0; clhour = 0; }break; } } } if(num == 5) { if(clock == 0) { switch(local) { case 5:sec--; if(sec <= 0) { min--; sec = 0; }break; case 4:sec -= 10; if(sec <= 0) { min--; sec = 0; }break; case 3:min--; if(min <= 0) { hour--; min = 59; }break; case 2:min -= 10; if(min <= 0) { hour--; min = 59; }break; case 1:hour--; if(hour <= 0) { sec = 0; min = 0; hour = 0; }break; case 0:hour -= 10; if(hour <= 0) { sec = 0; min = 0; hour = 0; }break; } } else if(clock == 1) { switch(local) { case 5:clsec--; if(clsec <= 0) { clmin--; clsec = 0; }break; case 4:clsec -= 10; if(clsec <= 0) { clmin--; clsec = 0; }break; case 3:clmin--; if(clmin <= 0) { clhour--; clmin = 59; }break; case 2:clmin -= 10; if(clmin <= 0) { clhour--; clmin = 59; }break; case 1:clhour--; if(clhour <= 0) { clsec = 0; clmin = 0; clhour = 0; }break; case 0:clhour -= 10; if(clhour <= 0) { clsec = 0; clmin = 0; clhour = 0; }break; } } } if(num == 6) { clock = 1; } if(num == 7) { clock = 0; } if(clock == 0) { //LCD_Write_Com(0x01); LCD_Write_String(0,0,"time :"); LCD_Write_Char(7,0,(hour/10)+ '0'); LCD_Write_Char(8,0,(hour%10)+ '0'); LCD_Write_Char(9,0,':'); LCD_Write_Char(10,0,(min/10)+ '0'); LCD_Write_Char(11,0,(min%10)+ '0'); LCD_Write_Char(12,0,':'); LCD_Write_Char(13,0,(sec/10)+ '0'); LCD_Write_Char(14,0,(sec%10)+ '0'); } else { //LCD_Write_Com(0x01); LCD_Write_String(0,0,"clock :"); LCD_Write_Char(7,0,(clhour/10)+ '0'); LCD_Write_Char(8,0,(clhour%10)+ '0'); LCD_Write_Char(9,0,':'); LCD_Write_Char(10,0,(clmin/10)+ '0'); LCD_Write_Char(11,0,(clmin%10)+ '0'); LCD_Write_Char(12,0,':'); LCD_Write_Char(13,0,(clsec/10)+ '0'); LCD_Write_Char(14,0,(clsec%10)+ '0'); } if(sec >= 60) { sec = 0; min++; for(kkk = 0;kkk < 1000;kkk++) { Beep = !Beep; Delay(50); } } if(min >= 60) { min = 0; hour++; /*for(kkk = 0;kkk < 100;kkk++) { Beep = !Beep; Delay(50); }*/ } if(hour >= 24) { hour = 0; min = 0; sec = 0; } if((hour == clhour) && (min == clmin) && (sec == clsec)) { for(kkk == 0;kkk < 500;kkk++) { Beep = !Beep; Delay(50); } } }}void ISR_T0(void) interrupt 1{ unsigned char i; TH0 = 0x3c; TL0 = 0xb0; if(flag == 1) { i++; if(i == 20) { i = 0; sec++; } }}unsigned char KeyScan(void){ char a = 0; GPIO_KEY=0x0f; if(GPIO_KEY!=0x0f)//读取按键是否按下 { DelayMs(10);//延时10ms进行消抖 if(GPIO_KEY!=0x0f)//再次检测键盘是否按下 { //测试列 GPIO_KEY=0X0F; switch(GPIO_KEY) { case(0X07): KeyValue=1;break; case(0X0b): KeyValue=5;break; case(0X0d): KeyValue=9;break; case(0X0e): KeyValue=13;break; } //测试行 GPIO_KEY=0XF0; switch(GPIO_KEY) { case(0X70): KeyValue=KeyValue+3;break; case(0Xb0): KeyValue=KeyValue+2;break; case(0Xd0): KeyValue=KeyValue+1;break; case(0Xe0): KeyValue=KeyValue;break; } while((a<50) && (GPIO_KEY!=0xf0)) //检测按键松手检测 { DelayMs(10); a++; } return KeyValue; } }}