原创 c51电子钟

2008-3-28 20:25 1422 0 分类: MCU/ 嵌入式
xiao.gifda.gif


#include<reg51.h>
#define uchar  unsigned char
#define  XS  P2  
sbit  RS = P1^0 ;
sbit  RW = P1^1 ;
sbit  EN = P1^2 ;
unsigned  char   buff="0",second=0 ,minute=0,hour=0,day=0,xinqi=0,month=0,year=0;
void  t0()   interrupt   1 using 0 
{
      //TF0=0;
   //TR0=1;
   TH0=0x15;
      TL0=0x0a0;
   buff++;
   if(buff>=20)
   {buff=0; 
      second++;}                          //秒加1
   if(second==60)                      //如果秒到60
    {
     second="0";                         //秒清0
     minute++;                         //分加1
     if(minute==60)                    //如果分到60
      {
       minute="0";                       //分清0
       hour++;                         //小时加1
       if(hour==24)                    //如果小时到24
        {
         hour="0";                       //小时清零
         day++;                        //天加1
         if(day>month)         //如果天大于本月最后一天
          {
           day="1";                      //天置1
           month++;                    //月加1
           if(month>12)                //如果月大于12
            {
             month="1";                  //月置1
             year++;                   //年加1
             if(year>=100)             //如果年大于100
              {
               year="0";                 //年清零
              }
            }
          }
        }
      }
    }
 
}
 
void LCD_delay()  reentrant
{   uchar i,j;
   for(i=0;i<10;i++)
    for(j=0;j<10;j++)
  ;
}


void LCD_command(uchar command)  //reentrant   using  3
{            
    XS="command";
  RS=0;
  RW=0;
  EN=0;
 
  LCD_delay();
  EN=1;
}
void LCD_data(uchar dat) //reentrant
{
   XS="dat"; 
  RS=1;
  RW=0;
  EN=0;
  
  LCD_delay();
    EN="1";
}
void LCD_write_char(uchar dat)   //  reentrant
{
  LCD_data(dat);
}
void initlcd()
{
  LCD_command(0x01);  
  LCD_command(0x38);    
  LCD_command(0x0c);  
  LCD_command(0x06);
  LCD_command(0x01);   
}
void time_menu()
{      
   LCD_command(0x01);
    LCD_command(0x80); 
  LCD_write_char('D');
  LCD_write_char('A');
  LCD_write_char('T');
    LCD_write_char('E');
    LCD_command(0xC0); 
  LCD_write_char('T');
  LCD_write_char('I');
  LCD_write_char('M');
    LCD_write_char('E');      
}
void showtime(void)
{
                      LCD_command(0x85);
                      //LCD_write_char('X');
                      //LCD_write_char('-');
                      //LCD_data(xinqi%10+0x30);
                      //LCD_write_char(' ');
                     LCD_write_char('Y');
                      LCD_write_char('-');
                      LCD_data(year/10+0x30);
                      LCD_data(year%10+0x30);
                      LCD_write_char(' ');
                      LCD_write_char('M');
                      LCD_write_char('-');
                      LCD_data(month/10+0x30);
                      LCD_data(month%10+0x30);
                      LCD_write_char(' ');
                      LCD_write_char('D');
                      LCD_write_char('-');
                      LCD_data(day/10+0x30);
                      LCD_data(day%10+0x30);
                    
                      LCD_command(0xc5);
                      LCD_data(hour/10+0x30);
                      LCD_data(hour%10+0x30);
                      LCD_write_char(':');
                      LCD_data(minute/10+0x30);
                      LCD_data(minute%10+0x30);
                      LCD_write_char(':');
                      LCD_data(second/10+0x30);
                      LCD_data(second%10+0x30);
}
void main()
{
TMOD=0x21;                 //定时器工作在模式1                 
 TR0=1;                     //启动定时器0
 EA=1;              //开总中断 
  ET0=1;                       //开定时器0中断
 year=05;
 month=06;
 day=01;          
  
   initlcd();
   time_menu();
   while(1)
   {
   showtime();
   }
}

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
我要评论
0
0
关闭 站长推荐上一条 /1 下一条