51 MCU T0 timer application 2

Publisher:RainbowMelodyLatest update time:2017-12-31 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Experimental task:
  Use the timer/counter T0 of AT89S51 to generate a 2-second timer. When the 2-second timer comes, the indicator light flashes. The frequency of each indicator flashing is 0.2 seconds. That is to say, the L1 indicator light flashes at a rate of 0.2 seconds at the beginning. When the 2-second timer comes, L2 starts to flash at a rate of 0.2 seconds, and so on. The 0.2-second flashing rate is also completed by the timer/counter T0.
2. Circuit schematic diagram
Timing Counter T0 Timing Application Technology (Part 2)
Figure 4.16.1
3. System board hardware connection
(1). Connect P1.0-P1.3 in the "single-chip microcomputer system" area to L1-L4 in the "eight-way light-emitting diode indicator module" area with wires
4. Program design content
(1). Since the interrupt method is used, the interrupt source must have its interrupt entry address. For the timer/counter T0, the interrupt entry address is 000BH, so a long jump instruction is added to the interrupt entry to execute the interrupt service program. The format of writing assembly source program is as follows:
                            ORG 00H
                            LJMP START
                            ORG 0BH; Timer/Counter T0 interrupt entry address
                            LJMP INT_T0
START: NOP; Main program starts
                            .
                            .
 
INT_T0: PUSH A CC                   ; Timer/Counter T0 interrupt service program
                            PUSH PSW
                            .
                            .
                            POP PSW
                            POP ACC
                            RETI; Interrupt service program returns
                            END
(2). Timing for 2 seconds, using 16-bit timing for 50ms, a total of 40 times of timing can reach 2 seconds, an interrupt is generated every 50ms, and the 40 times of timing are completed in the interrupt service program. Similarly, for a timing of 0.2 seconds, 4 times are required to reach 0.2 seconds. For the interrupt program, the interrupt must be disconnected in the main program.
(3). Since each time the 2-second timing is reached, L1-L4 will flash alternately. Use ID to identify. When ID=0, L1 is flashing, when ID=1, L2 is flashing; when ID=2, L3 is flashing; when ID=3, L4 is flashing
5. Program flowchart
T0 interrupt service program flowchart
Timing Counter T0 Timing Application Technology (Part 2)
Main program flowchart
          Timing Counter T0 Timing Application Technology (Part 2)
Timing Counter T0 Timing Application Technology (Part 2)
Figure 4.16.2
6. Assembly source program
TCOUNT2S EQU 30H
TCNT02S EQU 31H
ID EQU 32H
                                      ORG 00H
                                      LJMP START
                                      ORG 0BH
                                      LJMP INT_T0
START: MOV TCOUNT2S,#00H
                                     MOV TCNT02S,#00H
                                     MOV ID,#00H
                                      MOV TMOD,#01H
                                      MOV TH0,#(65536-50 000) / 256
                                      MOV TL0,#(65536-50000) MOD 256
                                      SETB TR0
                                      SETB ET0
                                      SETB EA
                                      SJMP $
INT_T0: MOV TH0,#(65536-50000) / 256
                                      MOV TL0,#(65536-50000) MOD 256
                                      INC TCOUNT2S
                                      MOV A,TCOUNT2S
                                      CJNE A,#40,NEXT
                                      MOV TCOUNT2S,#00H
                                      INC ID
                                      MOV A,ID
                                      CJNE A,#04H,NEXT
                                      MOV ID,#00H
NEXT: INC TCNT02S
                                      MOV A,TCNT02S
                                      CJNE A,#4,DONE
                                      MOV TCNT02S,#00H
                                      MOV A,ID
                                      CJNE A, #00H,SID1
                                      CPL P1.0
                                      SJMP DONE
SID1: CJNE A,#01H,SID2
                                      CPL P1.1
                                      SJMP DONE
SID2: CJNE A,#02H,SID3
                                      CPL P1.2
                                      SJMP DONE
SID3: CJNE A,#03H,SID4
                                      CPL P1.3
SID4: SJMP DONE
DONE: RETI
                                      END
7. C language source program
#include
unsigned char tcount2s;
unsigned char tcount02s;
unsigned char ID;
void main(void)
{
  TMOD=0x01;
  TH0=(65536-50000)/256;
  TL0=(65536-50000)%6;
  TR0= 1;
  ET0=1;
  EA=1;
  while(1);
}
void t0(void) interrupt 1 using 0
{
  tcount2s++;
  if(tcount2s==40)
    {
      tcount2s=0;
      ID++;
      if(ID==4)
        {
          ID=0;
        }
    }
  tcount02s++;
  if(tcount02s==4)
    {
      tcount02s=0;
      switch(ID)
        {
          case 0:
            P1_0=~P1_0;
            break;
          case 1:
            P1_1=~P1_1;
            break;
          case 2:
            P1_2=~P1_2;
            break;
          case 3:
            P1_3=~P1_3;
            break;
        }
    }

Design of 99-second stopwatch based on 51 single-chip microcomputer


Reference address:51 MCU T0 timer application 2

Previous article:51 MCU PWM control LED on and off
Next article:A complete guide to programming precise timing interrupts for C51 MCUs

Latest Microcontroller Articles
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号