做一个数字时钟程序,只有一个按钮设置,帮忙看看哈
// Program to make a digital clock using LCD#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
#define port P1
#define LCD_DATA P3 // Data port for LCD
sbit LCD_RS=P2^6; //LCD control pins
sbit LCD_RW=P0^5;
sbit LCD_EN=P2^0;
sbit key=port^3;
sbit dig_hr1=port^3;
sbit dig_min1=port^3;
int hr ,hr1=0;
int min,min1=0;
int sec,sec1=0;
uchar delay(uint z) //delay for z ms
{
uint x,y;
for(x=0;x<z;x++)
for(y=0;y<2000;y++);
return 0;
}
void lcd_com(unsigned char com) // Function to send command on LCD
{
LCD_DATA = com;
LCD_RS= 0;
LCD_RW=0;
LCD_EN=1;
delay(1);
LCD_EN=0;
return;
}
void lcd_data(unsigned char da) // Function to send data on LCD
{
LCD_DATA = da;
LCD_RS= 1;
LCD_RW=0;
LCD_EN=1;
delay(1);
LCD_EN=0;
return;
}
void lcd_msg1(char*msg)
{
char*pstr;
pstr=msg;
while(*pstr)
{
lcd_data(*pstr++);
}
}
lcd_data_int(int time_val) // Function to send number on LCD
{
int int_amt;
int_amt=time_val/10;
lcd_data(int_amt+48);
int_amt=time_val%10;
lcd_data(int_amt+48);
}
void init()
{
lcd_com(0x38); //LCD initialized 8-bit interface
lcd_com(0x0c); //display on, cursor off
lcd_com(0x06); //cursor moving direction
lcd_com(0x01); //clear display
lcd_com(0x02); //cursor home
}
void set_hr1() // Function to set hour
{
hr1++;
if(hr1>11)
hr1=0;
lcd_com(0xc0);
lcd_data_int(hr1);
lcd_data(':');
}
void set_min1() // Function to set minute
{
min1++;
if(min1>59)
min1=0;
lcd_com(0xc0);
lcd_data_int(min1);
}
void main()
{
int k;
key=1;
dig_hr1=1;
dig_min1=1;
lcd_com(0x01);
lcd_com(0x80);
lcd_msg1("SET TIME");
lcd_com(0xc0);
lcd_data_int(hr1);
lcd_data(':');
lcd_data_int(min1);
while(key!=0)
{
delay(10);
if(dig_hr1==1)
set_hr1();
if(dig_min1==1)
set_min1();
}
delay(200);
lcd_com(0x01);
while(1)
{
for(k=0;k<2;k++)
{
for(hr=hr1;hr<12;hr++)
{
for(min=min1;min<60;min++)
{
for(sec=0;sec<60;sec++)
{
lcd_com(0x82);
delay(1);
lcd_data_int(hr);
lcd_data(':');
lcd_data_int(min);
lcd_data(':');
lcd_data_int(sec);
}
}
}
}
}
}