#include <reg52.h>
unsigned char code tab1[16]="  Now time is:  ";
unsigned char  tab2[16]="    00:00:00    ";
unsigned char second = 0,minite=0,hour=0;
单片机printf函数/*设置管脚*/
sbit RS = P1^0;      //sbit RS = P3^5 ;     
sbit RW = P1^1;
sbit E  = P1^2;
sbit Beep = P3^4;
/*LCD1602控制指令*/          // #define  a b  ==>  a=b
#define LCD_Set        0x38  // 0011 1000  显示初始化,16*2显示,5*7点阵,8位数据接口 ;
#define LCD_Clear      0x01  // 0000 0001  清屏LCD ;
#define LCD_Display1  0x0f  // 0000 1111  显示功能设置:开显示,显示光标,光标闪烁 ;
#define LCD_Display2  0x0c  // 0000 1100  显示功能设置:开显示,不显示光标,光标不闪烁 ;
#define LCD_Mode      0x06  // 0000 0110  设置光标状态默认0x06,为读一个字符光标加1 ;
#define LCD_1_Left  0x80          //设置初始化数据指针,指向 左半屏第一行首位 ;
#define LCD_2_Left (0x80+0x40)    //设置初始化数据指针,指向 左半屏第二行首位 ;
/*液晶1602 毫秒延时*/
void delay_ms(unsigned int ms)
{
unsigned int i,j;
for(i=0;i<ms;i++)
    {for(j=0;j<1141;j++){;}}         
/*液晶1602 写控制指令函数*/
void Write_Command(unsigned char Command)
{
  RS = 0 ;                      // 进行指令的操作
  RW = 0 ;                      // 进行写操作
  P2 = Command ;                // 赋值指令
  E = 1 ;                        // 使能端拉高
  delay_ms(1);                  // 等待指令写完
  E = 0 ;                        // 使能端拉低
}
/*液晶1602 写数据函数*/
void Write_Data(unsigned char Data)
{
  RS = 1 ;                      // 进行数据的操作
  RW = 0 ;                      // 进行写操作
  P2 = Data ;                    // 赋值数据
  E = 1 ;                        // 使能端拉高
  delay_ms(1);                  // 等待数据写完
  E = 0 ;                        // 使能端拉低
}
/*液晶1602 使用初始化*/
void LCD1602_Init(void)
{
  Write_Command(LCD_Set);    // 显示初始化,16*2显示,5*7点阵,8位数据接口 ;   
  Write_Command(LCD_Clear);      // 清屏LCD ;
  Write_Command(LCD_Display2);  // 显示功能设置:开显示,不显示光标,光标不闪烁 ;
  Write_Command(LCD_Mode);      // 设置光标状态默认0x06,为读一个字符光标加1 ;
}
void LCD1602Printfc(unsigned char line,unsigned char address,unsigned char Data)
{
    if(line)
    {
        Write_Command(0xc0+address);
        Write_Data(Data);
    }
    else {
        Write_Command(0x80+address);
        Write_Data(Data);
    }   
}
/*液晶1602 显示*/
void LCD1602_Display(unsigned char *table1 , unsigned char *table2)
{
unsigned char i;
Write_Command( LCD_1_Left );    //第一行显示16个字符delay_ms(1);
for(i=0;i<16;i++)
{
  Write_Data(table1[i]);
  delay_ms(20);
}
Write_Command( LCD_2_Left );  //第二行显示16个字符delay_ms(1);
for(i=0;i<16;i++)
{
  Write_Data(table2[i]);
  delay_ms(20);
}
}
void time_view(void)
{
    if(second == 60){minite ++;second=0;}
    if(minite == 60){hour++;minite = 0;}
    if(hour == 24)hour=0;
    LCD1602Printfc(1,0,hour/10+'0');
    LCD1602Printfc(1,1,hour%10+'0');
    LCD1602Printfc(1,2,':');
    LCD1602Printfc(1,3,minite/10+'0');
    LCD1602Printfc(1,4,minite%10+'0');
    LCD1602Printfc(1,5,':');
    LCD1602Printfc(1,6,second/10+'0');
    LCD1602Printfc(1,7,second%10+'0');
}
void Time0Init(void)
{
    TMOD = 0x01;
    TH0  = 0x3c;
    TL0  = 0xb0;
    ET0  = 1;
    TR0  = 1;
    EA  = 1;
}
/*主函数*/
void main(void)
{
    LCD1602_Init();                  //调用1602初始化程序
    Time0Init();
    while(1)
    {
        time_view();
//        LCD1602_Display(tab1,tab2);                //调用显示程序
    }
}
void time0(void)interrupt 2
{
    static unsigned char time = 0;
    TH0 = 0x3c;
    TL0 = 0xb0;
    time ++;
    if(time == 20){time = 0;second ++;}   
}