Based on 51 single-chip timing, digital tube display time

Based on 51 single-chip timing, digital tube display time

If you want the digital tube to display the time, you have to let the digital tube display, and then you need to make the displayed numbers carry in seconds, minutes, and hours. Digital tube display can read my previous article:
Based on 51 single chip microcomputer digital tube display

The timer interrupt of the
single-chip microcomputer 51 has two 16-bit programmable timer T0/counter T1 inside the single-chip microcomputer, and an additional T2 timer/counter in the 52 single-chip microcomputer;

The essence of the timer/counter is to add 1 counter (16 bits), which is composed of 2 registers of high 8 bits and low 8 bits. TMOD is the working mode register of the timer/counter to determine the working mode and function: TCON is the control register, which controls the start and stop of TO and TI and sets the overflow flag.
The count pulse input by the plus 1 counter has two sources, one is sent by the system's clock oscillator output pulse after dividing by 12; the other is an external pulse source input from the T0 or T1 pin, and each pulse counter adds 1. When the counter is full I, another pulse is input to make the counter return to zero, and the overflow of the counter sets TFO or TF1 in the TCON register to 1, and sends an interrupt request to the CPU (when timer/counter interrupts are enabled). If the timer/counter is working in the timing mode, it means that the timing time is up: if it is working in the counting mode, it means that the count value is full.
It can be seen from the above that the count value of the counter is subtracted from the value of the overflowed counter.
When set to timer mode, the increment counter is to count the internal machine cycles (1 machine cycle is equal to 12 oscillation cycles, that is, the counting frequency is 1/12 of the crystal oscillator frequency). The count value N multiplied by the machine cycle Tey is the timing time t.
Timer initial value calculation

Timing time T=(Nth power of 2-X) 12/Single-chip crystal oscillator frequency (when mode 0, N=13 when mode 1, N=16 when mode 2, N=8)

For example, the timing is 2ms, when the crystal oscillator is 12M.
Method 1
Timing time 2ms=(2's 16th power-time constant X) 12/f i.e. crystal oscillator is 12M f=12 1000000
2ms=(2's 16th power-time constant X) 12/(12
1000000)
Method 2
12MHz Divide 12 to 1MHz, which means that one second = 1,000,000 machine cycles. 2ms=50,000 machine cycles.

Time constant X=2000
TH0=(65536-2000)/256; it is converted to hexadecimal and then divided by 256 to take the integer, that is, the high 8 bits are sent to the high 8-bit counter of the time constant register
TL0=(65536-2000)%256 ; Is converted to hexadecimal and then divided by 256 to take the remainder, that is, the lower 8 bits are sent to the lower 8-bit counter of the time constant register

When the one-chip computer uses the timer or counter function, usually need to set up two registers related to the timer: timer/counter working mode register TMOD and timer/counter control register TCON.
The
definition of each bit of timer/counter working mode register TMOD TMOD is as follows:

Bit number D7 D6 D5 D4 D3 D2 D1 D0
Bit symbol GATE C/T M1 M0 GATE C/T M1 M0

D7–D4 are Timer 1 D3–D0 are Timer 0
The upper 4 bits of TMOD are used to set Timer 1, and the lower 4 bits are used to set Timer 0. The corresponding 4 bits have the following meanings:
GATE is a gate control bit. GATE=0, the timer/counter start and stop are only controlled by TRX (X=0, 1) in the TCON register; GATE=1, the timer/counter start and stop are controlled by TRX (X=0, 1) Commonly controlled with the level status on the external interrupt pin (INTO or INT1).
C/T-Timer mode and counter mode selection bit. C/T=1, is the counter mode: C/T=0 is the timer mode. M1 M0 is the working mode selection bit. Each timer counter has 4 working modes, which are set by MIMO, as shown in the following table

M1 M0 Way of working
0 0 Mode 0 is 13-bit timer/counter
0 1 Method 1 is a 16-bit timer/counter
1 0 Method 2 is an 8-bit timer/counter with 8-bit initial value and automatic reloading
1 1 Mode 3 is only applicable to two 8-bit counters with T0, T1 stops counting

Timer/Counter Control Register TCON

The timer/counter control register TCON is in the special function register. The byte address is 88H, and the bit address (from low to high) is 88H~8FH respectively, which can be bit addressed. The TCON register is used to control the start and stop of the timer, and to mark the timer overflow and interrupt conditions. When the microcontroller is reset, TCON is all cleared to 0.
Its bits are defined as shown in the table below. The TFI, TRI, TFO and TRO bits are used for timers/counters; the 1IEI, ITI, IEO and ITO bits are used for external interrupts.

Bit number D7 D6 D5 D4 D3 D2 D1 D0
Bit symbol TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Bit address 8FH 8EH 8DH 8CH 8BH 8AH 89H 88H

TF1 Timer 1 overflow flag. When Timer 1 overflows, TF1 is set to 1 by hardware and an interrupt is requested. After entering the interrupt service routine, it is automatically cleared to 0 by the hardware. Note that if the timer interrupt is used, then this bit does not need to be manipulated at all; if the software query method is used, when the bit is inquired to 1, it needs to be cleared by software 0.
TR1 Timer 1 operation control bit. Cleared by software to close timer 1. When GATE=I and INT1 is high, TR1 is set to 1, and timer 1 is started; when GATE=0, TRI is set to 1, and timer 1 is started.
TF0 Timer 0 overflow flag , Its function and operation method are the same as TF1.
TR0 timer 0 running control bit, and its function and operation method are the same as TR1.
IE1 external interrupt 1 request flag.

To make the digital tube display the time, use the register TMOD timer 0, working mode 1; the
code is as follows

void T0_init(){
    
                        
 TMOD = 0x01; //设置定时器0为工作方式1(M1 M0为01)
 TH0 = (65536-50000)/256;//装初值12Mhz晶振定时50ms  
 TL0 = (65536-50000)%256;  
 EA = 1;//开总开关     
 ET0 = 1;//开定时器0中断     
 TR0 = 1;//启动定时器0    
}
void timer0_int() interrupt 1{
    
    
 TH0 = (65536-50000)/256; //重装初值
 TL0 = (65536-50000)%256;
 i++;//每加一次就判断一次看是否到达1s
   if(i == 20){
    
    
   i = 0;  
   miao++//每加一次就判断一次看是否到达1分
     if(miao == 60){
    
    
     miao = 0;
     fen++;//每加一次就判断一次看是否到达1小时
       if(fen == 60){
    
    
        fen = 0;  
        shi++;
    	  if(shi == 24){
    
    
     	  shi = 0;  
     	  fen = 0;
     	  miao = 0;
     }
   }
 }
 miao_g = miao%10;//求余得到秒的个位   
 miao_s = miao/10;//取十得到十位 这就是送到数码管元素的下标与数组相对应 下同
 fen_g = fen%10;  
 fen_s = fen/10;
 shi_g = shi%10;  
 shi_s = shi/10;
  }
}

The display function of the digital tube, such as the dynamic display of the multi-segment digital tube, can be seen above. The
code and the schematic diagram have been uploaded to the resource. If you are interested, you can check it, and please correct it;

Guess you like

Origin blog.csdn.net/Lucifer_min/article/details/109264704