注释:在LCD上实现简易电子钟.功能1:整点报时(到整点时候,LED闪一分钟,蜂鸣器响一分钟);功能二:按键暂停;功能三:按键重置时间:功能四:按键实现调整时间
#include<reg52.h> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义
#include<intrins.h>
unsigned char sec = 0;
unsigned char min = 0;
unsigned char hour = 0;
unsigned char count = 0;
unsigned char flag = 0;
sbit RS = P2^4; //定义端口
sbit RW = P2^5;
sbit EN = P2^6;
sbit key1 = P1^0;
sbit key2 = P1^1;
sbit key3 = P1^2;
sbit key4 = P1^3;
sbit key5 = P1^4;
sbit key6 = P1^5;
sbit key7 = P1^6;
sbit key8 = P1^7;
sbit buzz = P2^2;
#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 P0
/*------------------------------------------------
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 push_button()
{
if(!key5)
{
DelayMs(10);
if(!key5)
{
while(!key5);
EA = 0;
}
}
if(!key1) //1
{
DelayMs(10);
if(!key1)
{
while(!key1);
sec++;
if(sec == 60)
{
sec = 0;
min++;
}
}
}
if(!key2) //2
{
DelayMs(10);
if(!key2)
{
while(!key2);
sec--;
if(sec == 255)
{
sec = 59;
min--;
}
}
}
if (!key3)//3
{
DelayMs(10);
if(!key3)
{
while(!key3);
min++;
if(min == 60)
{
min = 0;
hour++;
}
}
}
if(!key4)//4
{
DelayMs(10);
if(!key4)
{
while(!key4);
min--;
if(min == 255)
{
min = 59;
if((hour-1) == 255)
{
hour = 23;
}
}
}
}
if(!key7) //7
{
EA = 1;
}
if(!key6)//6
{
sec = 0;
min = 0;
hour = 0;
EA = 1;
}
if(!key8)//8
{
DelayMs(10);
if(!key8)
{
while(!key8);
hour++;
if(hour == 24)
{
hour = 0;
}
}
}
}
/*------------------------------------------------
写入命令函数
------------------------------------------------*/
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 main(void)
{
//unsigned char i;
//unsigned char *p;
LCD_Init();
TMOD = 0x01;
TL0 = 0xb0;
TH0 = 0x3c;
EA = 1;
ET0 = 1;
TR0 = 1;
while(1)
{
LCD_Write_Char(7,0,0X30 + sec%10);
LCD_Write_Char(6,0,0X30 + sec/10);
LCD_Write_Char(5,0,0x3a);
LCD_Write_Char(4,0,0X30 + min%10);
LCD_Write_Char(3,0,0X30 + min/10);
LCD_Write_Char(2,0,0x3a);
LCD_Write_Char(1,0,0X30 + hour%10);
LCD_Write_Char(0,0,0X30 + hour/10);
push_button();
}
}
void time0_isr(void) interrupt 1
{
count++;
EA = 0;
TH0 = 0x3C;
TL0 = 0xb0;
if(count == 20)
{
count = 0;
sec++;
if(sec == 60)//用于显示0~9
{
sec = 0;
min++;
flag = 0;
}
if(min == 60)
{
min = 0;
hour++;
flag = 1;
}
if(hour == 24)
{
hour = 0;
}
}
EA = 1;
if(flag)
{ buzz = ~buzz;
P3 = 0x00;
DelayMs(10);
P3 = 0xff;
DelayMs(10);
}
}