The interface of the 1602 display screen (connection: LCD1602 display interface introduction) and related functions of read and write operations (connection: LCD1602 read-write control and initialization function) were introduced before. Do a simple 1602 application case today – design a stopwatch. Requirements: There are stopwatch start and reset function buttons, and the stopwatch timing time is displayed on LCD1602 in real time.

1. The hardware circuit schematic diagram of the system:

2. System programming

2.1 Declaration of header files, global variables and functions

#include “LCD_1602.h”
        #include
unsigned char Disp_Buffer[10];//This can be set slightly larger, it does not affect the result display, when the display encounters the end of the string, it will end
//Define two bit variables, corresponding to the button start button and stop reset button respectively
        sbit key_set=P3^2;
        sbit key_reset=P3^3;
        unsigned char TIme_count=0;
        unsigned int TIme_S=0;
void TImer_inial();//Timer initialization
void delay(unsigned int i);//Delay function
void start();//Start the stopwatch
void stop();//Stop resetting the stopwatch
void second_cal_show();//Calculation and display of seconds
 

2.2 Main program

The main program mainly completes the initialization of the display screen, the initialization display, and the initialization of the timer. Note that it will not start when the timer is initialized. The start is controlled by the start button, and the timer is not started if the start button is not pressed. After initialization, call start() in the while loop; determine whether to start the timer, call stop(); determine whether to stop the stopwatch and reset it according to the key state; call second_cal_show(); to calculate and display seconds.

        void main()
        {

            InitLcd1602();
            LcdShowStr(0, 0, “Current : 0 S”);
            LcdShowStr(0, 1, “Last    : 0 S”);
            TImer_inial();
        while(1)
        {
             start();
             stop();
             second_cal_show();    
        }
    }
 

2.3 Subroutines

2.3.1 Initial timer: Timer T0 works in mode 1, and the timer time is 0.02 seconds.
 

    void timer_inial()
    {
TMOD = 0X01; //
TH0=0XB8;//Timing 0.02s
TL0=0X00;
    }

2.3.2 Delay function: software delay, mainly using button software to debounce

// delay function
void delay(unsigned int i)
{
        unsigned int k;
      for(k=0;k

2.3.3 Start function: Detect whether the button is pressed, if the button is pressed, the timer T0 is started and starts counting from 0.

void start()
{
    if(0==key_set)
         {
delay(1200);//10ms debounce
if(0==key_set)//Check whether the key is pressed again
             {
while(!key_set);//Wait for the key to be released
                  TR0=1;
                  time_S=0;
                  time_count=0;
                
             }
        
         }

}

2.3.4 Stop reset function: detect whether the stop button is pressed, if the button is pressed, the timer T0 will be stopped, and the information on the LCD display will be updated.

void stop()
{
  if(0==key_reset)
         {
delay(1200);//10ms debounce
if(0==key_reset)//Check whether the key is pressed again
             {
while(!key_reset);//Wait for the key to be released
                  TR0=0;
LcdWriteCmd(0x01); //Clear the screen
                  LcdShowStr(0, 0, “Current : 0 S”);
                  LcdShowStr(0, 1, “Last    : 0 S”);
                  LcdShowStr(10, 1, Disp_Buffer);
             }
        
         }
        
}

2.3.5 Second processing and display function: every time the timer T0 overflows once, the time_count value is increased by 1, and 50 times are added to the second, and the value of the second variable time_S is increased by 1, and then the display information is updated.

void second_cal_show()
{
  if(TF0==1)
        {
            TF0=0;
          TH0=0XB8;
TL0=0X00;
            time_count++;
if(time_count>=50)//50 times a second
            {
              time_count=0;
                time_S++;
                sprintf(Disp_Buffer,”%d S”,time_S);
            LcdShowStr(10, 0, Disp_Buffer);    
            }
            
        }
}

The above is all the code, the compilation is as follows:





Reviewing Editor: Liu Qing

Leave a Reply

Your email address will not be published.