Electronic clock based on 51 single chip microcomputer timer

Publisher:幸福自在Latest update time:2012-09-14 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. An electronic 1602 LCD electronic clock that can display the day of the week and time.
2. But there is no subroutine to adjust the time. Who knows how to adjust the subroutine? It can be improved.
3. The LCD port can be modified at will according to your own circuit. It is at the top of the program.
4. This program has been successfully tested on the 51hei single-chip development board. All codes and header files
Download address: http://www.51hei.com/ziliao/51hei-5/电子时刻+1602LCD.rar

#include 
#include "51hei.h"
#define uchar unsigned char
sbit RW=P2^7;
sbit RS=P3^5;
sbit E=P3^4;
sbit duan=P2^6;
bit at=0;
//sbit busy_bit=P1^7;
uchar code shen[]={"I love you!!"};
uchar code word[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,
0x39,0x3a,0x20,0x2d,0x00,0x01,0x02,0x03,0x03,0x04,0x05,0x06,0x07};
uchar code ri[]={0x1f,0x19,0x19,0x1f,0x19,0x19,0x1f,0x00}; //Custom symbol: Sunday
uchar code yi[]={0x00,0x00,0x00,0x1f,0x00,0x00,0x00,0x00,0x00};/*Custom symbol: Monday*/
uchar code er[]={0x00,0x00,0x0e,0x00,0x1f,0x00,0x00,0x00,0x00};/*Custom symbol: Tuesday 2*/
uchar code san[]={0x00,0x1f,0x00,0x0e,0x00,0x1f,0x00,0x00};/*Custom symbol: Wednesday 2*/
uchar code si[]={0x00,0x1f,0x1a,0x1a,0x11,0x11,0x00,0x00}; //Custom symbol: Thursday   
uchar code wu[]={0x00,0x1f,0x08,0x1f,0x09,0x1f,0x00,0x00}; //Custom symbol: Friday
uchar code liu[]={0x08,0x04,0x1f,0x0a,0x11,0x10,0x00,0x00}; //Custom symbol: Friday
uchar dispbuf[10],ri1,h,m,s,counter;

 

void delay()
{
  uchar i;
 for(i=0;i<255;i++);
}

 

/*******Write command**********/
void lcd_wmc(uchar i)
{

  P0=i;
  RS=0;
  RW=0;
  E=0;
  delay();
  E=1;
}

 

/*******Write data***********/

void lcd_wmd(uchar i)
{

  P0=i;
  RS=1;
  RW=0;
  E=0;
  delay();
  E=1;
}

 


/*******Initialize LCD*******/
void lcd_init()
{
  uchar i;
  lcd_wmc(0x01);
  lcd_wmc(0x38); //Function setting command-5 Display mode setting: 16×2 display, 5×7 dot matrix,
8-bit data interface
  lcd_wmc(0x0c); // Display switch control instruction 0x0c--display function is on without cursor, that is, the cursor does not blink.
00x0f--Display function is on, cursor is flashing
  lcd_wmc(0x06); //Display mode setting: cursor moves right, characters do not move Input mode setting command -3
                  
  lcd_wmc(0x82); // 0xc9 =0x80 | 0x49 Set DDRAM address instruction--8   
PPt 8 ox82=0x80|0x02
  for(i=0;i<12;i++)
  lcd_wmd(shen[i]); //Display I love you!! string                    
  lcd_wmc(0xc0);
 
}

 

 

/*******Update buffer subroutine*******/
void newbuf()
{
 dispbuf[0]=s%10;
 dispbuf[1]=s/10;
 dispbuf[3]=m%10;
 dispbuf[4]=m/10;
 dispbuf[6]=h%10;
 dispbuf[7]=h/10;
 dispbuf[9]=ri+13;
}

 

/*******Display subroutine**********/
void disp(uchar dispadd)
{
   uchar tmp; //define a uchar variable
   lcd_wmc(dispadd); // Call the write instruction function to write the DDRAM address instruction into

  
   tmp=dispbuf[9]; //week//The same applies to the following
   tmp=word[tmp];
   lcd_wmd(tmp);

   tmp=dispbuf[8];
   tmp=word[tmp];     
   lcd_wmd(tmp);

 

   tmp=dispbuf[7]; //Line 2 assigns the value to tmp
   tmp=word[tmp]; //Give the number to be displayed to tmp
   lcd_wmd(tmp); //Write data function to display the number to be displayed on the display

   tmp=dispbuf[6]; //The same applies to the following
   tmp=word[tmp];
   lcd_wmd(tmp);

   tmp=dispbuf[5];
   tmp=word[tmp];     
   lcd_wmd(tmp);

   tmp=dispbuf[4];
   tmp=word[tmp];
   lcd_wmd(tmp); //minutes

   tmp=dispbuf[3];
   tmp=word[tmp];     
   lcd_wmd(tmp);

   tmp=dispbuf[2]; //The display shows two dots flashing
   tmp=word[tmp];
   lcd_wmd(tmp);

   tmp=dispbuf[1];
   tmp=word[tmp]; //seconds
   lcd_wmd(tmp);

   tmp=dispbuf[0];
   tmp=word[tmp];
   lcd_wmd(tmp);
}
 

 

/*************************Initialization subroutine**********************/
void init()
{

  TMOD=0x01;
  TH0=0x4c;
  TL0=0x00;
  EA=1;
  ET0=1;
  TR0=1;
  counter=0;
  h=23;m=59;s=0;
  ri1=6;
  dispbuf[2]=10;
  dispbuf[5]=10;
  dispbuf[8]=12;
}

 

 

 

/***************************Main program****************************/

void main(void)
{ guanled();
  guandz();
  init();
  lcd_init();

  while(1)
  {
   if(!at)
 {
     //Blink
      if(counter<10)        
      {
       dispbuf[2]=10;
       dispbuf[5]=10;
      }
      else
      {            
       dispbuf[2]=11;
       dispbuf[5]=11;
      }
      //Update the display buffer and call the display program
      if(counter==0)
      {
       newbuf();
       disp(0xc4);
       }
      else if(counter==10)
      disp(0xc4);  
    }
   }
}

 

 

 


/*****************************Timer 0 interrupt**********************/
void Time0() interrupt 1 using 2 //Again, the shorter the execution time of the interrupt subroutine, the better
{
  TH0=(65536-46075)/256;
  TL0=(65536-46075)%256;
  counter++;
  if(counter==20)
  {
    s++;
  counter=0;
  if(s==60)
  {
    m++;
    s=0;
      if(m==60)
    {
      h++;
    m=0;
    if(h==24)
     h=0;
  ri1++;
    if(ri1==7)
       {
     ri1=0;
    }
         }
       }

  }
}
Reference address:Electronic clock based on 51 single chip microcomputer timer

Previous article:Infrared data communication experiment
Next article:4-bit common anode digital tube dynamic scanning display assembly program

Recommended ReadingLatest update time:2024-11-16 22:54

【PIC32MZ】Timer
PIC32MZ has up to 9 timer groups, but not all of them can be used simultaneously at any time, which is mainly a problem when using 32-bit counters. Timer 2, 4, 6, and 8 support 16-bit and 32-bit timers, but when using 32-bit, the ID occupied is not the same as itself, but the next one. Take Timer4 as an example. In
[Microcontroller]
【PIC32MZ】Timer
How to use the timer of s3c2410
2410 has 5 timers in total, timer4 has no pin output, and the rest can be used as PWM. 0 and 1 share a prescaler 2, 3, 4 share a prescaler TCFG0 correspond to these two pre-dividers respectively. Don’t forget to add +1 to the division value, because the division value cannot be 0. TCFG1 corresponds to the fr
[Microcontroller]
STM32 Timer1 TIM1 interrupt
/*Timer 1ms interrupt, dead wait timer delay*/ /*File Timer.c */ #include "stm32f10x.h"     unsigned int TimeDelay = 0; void TIM1_Configuration(void) {   NVIC_InitTypeDef NVIC_InitStructure; TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;   TIM_DeInit(TIM1); /*Reset TIM1 timer*/ RCC_APB2PeriphClockCmd(RCC_APB2Perip
[Microcontroller]
HOLTEK ht67f50 MCU timer0 timing
1. This HOLTEK microcontroller is very good, with basically all the functions, except that it does not have UART, which gives me a headache. However, its other performances are good, especially the timer function, which is fully available. Here is the basic timing of timer0 in my experiment. It is very simple to do it
[Microcontroller]
PIC microcontroller - using the overflow interrupt of Timer2 to realize dynamic scanning of digital tubes
Write a program to make the display sequence of the digital tube be: 0123,1230,2301,3012. Digital tube display is divided into static scanning and dynamic scanning. Dynamic scanning display is generally divided into two ways 1. Select a digital tube position, write a broken code display, delay for a certain period
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号