89C51 MCU Timer/Counter 0

Publisher:灵感发电站Latest update time:2015-01-15 Source: 51heiKeywords:89C51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The timing counting function of the 89C51 microcontroller is controlled by the special function registers TMOD and TCON.
TMOD has no bit address and cannot perform bit operations. The names and functions of each bit are as follows:
TMOD: GATE C/T1 M1 M0 GATE C/T1 M1 M0
                         D7 D6 D5 D4 D3 D2 D1 D0
 GATE C/T1 M1 M0 controls timing timer 1;
 GATE C/T0 M1 M0 controls timing timer 0;
When GATE=0, the timing counting signal is the state of the T0 pin (P3.4) or the machine cycle (C/T=0 counts the machine cycle, i.e. timing, C/T=1 counts the state of the T0 pin, i.e. counting), and the counting start switch is TR0 (0 turns off counting, 1 starts counting).
When GATE=1, the timing counting function is determined by the T0 pin (P3.4) or the machine cycle (C/T=0 counts the machine cycle, i.e. timing, C/T=1 counts the change state of the T0 pin, i.e. counting), and the counting start switch is determined by TR0 (0 turns off counting, 1 starts counting) and INT0 (pin P3.2).
 M1M0 is the counting mode selection: counting is performed in TH0 and TL0.
 00 is mode 0, 13-bit (8 bits of TH0 and the lower 5 bits of TL0) binary counter, which can count from 0 to 2 to the 13th power -1;
 01 is mode 1, 16-bit binary counter, which can count from 0 to 2 to the 16th power -1;
 10 is mode 2, 8-bit binary counter, TH0 puts the initial value of the count, counting is performed in TL0, and 
                     the initial value in TH0 is automatically loaded into TL0 after overflow;
11 is mode 3, TH0 is an 8-bit counter, and HL0 is an 8-bit counter. The two counters are independent of each other.
T1 and T0 have the same functions.
Special function register TCON can be operated by bit, but generally does not use bit address, but uses bit function name:
Bit address: 8FH 8EH 8DH 8Ch 8Bh 8Ah 89H 88H
TCON: TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 
               D7 D6 D5 D4 D3 D2 D1 D0 
TF0 is T0 count flag: When the timing count overflows, this bit is automatically set to 1, and automatically cleared to 0 after the interrupt count ends.
TR0 is the count switch, when GATE=0, TR0=1 turns on the count, and 0 turns it off. When GATE=1, it controls the counter to open together with INT0, that is, GATE=1, TR0=1, INT0=1 can turn on the count, otherwise it is turned off.
In timing/counting, the value of TH0 TL0 is incremented by 1 and counts to FFFFH. If it is in timing/counting mode, TF0 is set to 1 after overflow and the counting time ends. If it is in interrupt mode, the interrupt request is set to 1 by hardware. After the counting ends, that is, after the interrupt ends, TF0 is automatically cleared to 0.
/***************Timer timing 50ms, 1 second at count 20, display 60 seconds timing (query mode)***********/
#include
#include
#define uchar unsigned char
#define uint unsigned int

int yu[]={
               0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f
             };
int t,k,dat;
void delay(int ms)
{
 while(ms--)
   {
    int i;
    for(i=0;i<30;i++);
   }
}
void Timer0()
   {       
     if(TF0==1)//Query TF0 and it is 1, which means the count is full and the count is ended.
         {
          t++;
          TH0=0x3c;//Here is to reload the initial value after the count is full. You can also use TH0=(65536-5000)/256
          TL0=0xb0;//After the count overflows, count again from here, or TL0=(65536-5000)%256
       /*If it is troublesome to calculate the initial value first, you can also use the maximum value of 16-bit binary 65536-timer count times to assign TH0 and TL0. For example, if the timing is 40ms, you need to count 4000 machine cycles, you can use
            TH0=(65536-4000)/256; TL0=(65536-4000)%*/
          TF0=0;
         }
            //Clear overflow flag
   }
void T1s()
{
  if(t==20)//Count once for 50ms, 20 times for 1 second
      {t=0;
       dat++;
       if(dat==60)//60 times is 1 minute
        dat=0;
      }
}
void main()
{
  TMOD=0x01;
   /*This setting (0000 0101) T0's GATE=0, C/T is 0, M1M0 is 01.
    GATE=0, turn off INT0 (P3.2) pin's control of counting start, only TR0 controls the start.
    C/T=0, timing function, count the number of machine cycles.
    M1M0=01, select counting mode 1, 16-bit binary counter, count from TH0 TL0 to FFFFH, and
    then overflow. If it is timing/counting mode, set TF0 to 1 after overflow, and the counting timing ends. If it is
    interrupt mode, clear 0, hardware sets TF0 to 1, counting ends, that is, interrupt ends, and TF0 is automatically cleared to 0.
    Turn on counter 0 interrupt and set ET0=1, EA=1 */
  TH0=0x3c;//Set the initial value of the counter to count for 50ms and start counting from 3CB0H
  TL0=0xb0;//Start counting from the setting here for the first time
  TR0=1; 
  while(1)
   { 
      Timer0();
      T1s();     
     P0=yu[dat%10];
      delay(1);
      P2=0x01;   
      delay(1);
      P2=0x00;
     P0=yu[dat/10];
      delay(1);
      P2=0x02;
      delay(1);
      P2=0x00;   
   }

/*******Timer/Counter 0 interrupt application, the digital tube display increases by 1 every time the key is pressed (interrupt mode)*************/
#include
#include
#define uchar unsigned char
#define uint unsigned int
int yu[]={
               0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f
             };
int k;
void delay(int ms)
{
 while(ms--)
   {
    int i;
    for(i=0;i<30;i++);
   }
}
void ddd() interrupt 1
   {
     TH0=0xff;//Here is the initial value of the count after the count is full
     TL0=0xfa;//After the count overflows, the count starts again from here
     delay(5);
     if(TF0==0)//When the count is interrupted, TF0 is set to 1 by hardware, responds to the interrupt and starts counting, and automatically clears to 0 after the count overflows, ending the count.
            { //Therefore, after responding to the interrupt, querying the status of TF0 can determine whether the interrupt count has ended
               if(k==99)
               k=0;
              else
               k++;
           }        
   }
void main()
{
  TMOD=0x05;
/*This setting (0000 0101) T0's GATE=0, C/T is 1, M1M0 is 01.
   GATE=0, turn off INT0 (P3.2) pin's control of count start, only TR0 controls start.
    C/T=1, count function, count pin T0 (P3.4) state changes.
   M1M0=01, select counting mode 1, 16-bit binary counter, count from TH0 TL0 to FFFFH, and then overflow. If it is timing/counting mode, TF0 will be set to 1 after overflow, and the counting timing will end. If it is interrupt mode, it will be cleared to 0. Hardware will set TF0 to 1, and the counting will end, that is, the interrupt will end, and TF0 will be automatically cleared to 0. Turn on counter 0 interrupt and set ET0=1, EA=1 */[page]
  TH0=0xff;//Set the initial value of the counter
  TL0=0xfc;//Start counting from the setting here for the first time
  ET0=1;
  TR0=1;
  EA=1;
  while(1)
   {
   P0=yu[k%10];
      delay(1);
      P2=0x01;   
      delay(1);
      P2=0x00;
      P0=yu[k/10];
      delay(1);
      P2=0x02;
      delay(1);
      P2=0x00;   
   }
} /******Count in query mode #include #include #define uchar unsigned char #define uint unsigned int






int yu[]={
               0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f
             };
int k;
void delay(int ms)
{
 while(ms--)
   {
    int i;
    for(i=0;i<30;i++);
   }
}
void jishu()
   {     
     if(TF0==1)//Query TF0, it is 1, indicating that the count is full and the count is over.
         {
           TH0=0xff;//Here is the initial value of the count after the count is full
           TL0=0xff;//After the count overflows, the count starts again from here
           delay(5);
           if(k==99)
           k=0;
           else
           k++;
         }
        TF0=0; //Clear overflow flag
   }
void main()
{
  TMOD=0x05;
/*This setting (0000 0101) T0's GATE=0, C/T is 1, M1M0 is 01.
   GATE=0, turn off INT0 (P3.2) pin's control of count start, only TR0 controls start.
    C/T=1, count function, count pin T0 (P3.4) state changes.
   M1M0=01, select counting mode 1, 16-bit binary counter, count from TH0 TL0 to FFFFH, and then overflow. If it is timing/counting mode, TF0 will be set to 1 after overflow, and the counting timing will end. If it is interrupt mode, it will be cleared to 0. Hardware will set TF0 to 1, and the counting will end, that is, the interrupt will end, and TF0 will be automatically cleared to 0. Turn on counter 0 interrupt and set ET0=1, EA=1 */
  TH0=0xff;//Set the initial value of the counter
  TL0=0xff;//Start counting from the setting here for the first time
  TR0=1;
  while(1)
   { 
      jishu();
   P0=yu[k%10];
      delay(1);
      P2=0x01;   
      delay(1);
      P2=0x00;
     P0=yu[k/10];
      delay(1);
      P2=0x02;
      delay(1);
      P2=0x00;   
   }
}
/**********************Design stopwatch and display using timing interrupt********************************/

#include
#include
#define uchar unsigned char
#define uint unsigned int
int yu[]={
               0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f
             };
int k,dat;
void delay(int ms)
{
 while(ms--)
   {
    int i;
    for(i=0;i<30;i++);
   }
}
void ddd() interrupt 1
   {
     TH0=(8192-4000)-(8192-4000)%32;//Here is the reload initial value after the count is full
     TL0=(8192-4000)%32;//After the count overflows, the count starts again from here
     delay(5);
     if(TF0==0)//When the count is interrupted, TF0 is set to 1 by hardware, responds to the interrupt and starts counting, and is automatically cleared to 0 after the count overflows, ending the counting.
            { //Therefore, after responding to the interrupt, querying the status of TF0 can determine whether the interrupt count has ended
              if(k==250)
               {k=0;
               dat++;}
              else
               k++;
                //Close T0 counter, TR0=0 sentence cannot be in the main program, it must be in the query function or interrupt function, otherwise it cannot be stopped.
           }       
   }
void main()
{
  TMOD=0x00;
/*This setting (0000 0101) T0's GATE=0, C/T is 1, M1M0 is 01.
   GATE=0, turn off INT0 (P3.2) pin's control of count start, only TR0 controls start.
    C/T=1, count function, count pin T0 (P3.4) state changes.
   M1M0=01, select counting mode 1, 16-bit binary counter, count from TH0 TL0 to FFFFH, if it is in timing/counting mode, set TF0 to 1 after overflow, and the counting timing ends. If it is in interrupt mode, it is cleared to 0, and the hardware sets TF0 to 1, the counting ends, that is, the interrupt ends, and TF0 is automatically cleared to 0. Turn on counter 0 interrupt and set ET0=1, EA=1 */
  TH0=0xa3; //Set the initial value of the counter
  TL0=0x00; //Start counting from the setting here for the first time
  ET0=1;
  TR0=1;
  EA=1;
  while(1)
   {
    
   P0=yu[dat%10];
      delay(1);
      P2=0x01;   
      delay(1);
      P2=0x00;
      P0=yu[((dat%1000)%100)/10];
      delay(1);
      P2=0x02;
      delay(1);
      P2=0x00;
   P0=yu[(dat%1000)/100];
      delay(1);
      P2=0x04;   
      delay(1);
      P2=0x00;
     P0      = yu[     dat      /       1000]      ; delay
      (      1      )      ;      P2
      =      0x08      ;
      delay      (      1      )      ; P2
      =      0x00      ;
   }

=== ... P0,a      mov P2,#02h      nop      nop      mov P2,#00h      mov a,b      movc a,@a+dptr      mov P0,a      mov P2,#01h      nop      nop      mov P2,#00h      sjmp disp T0ZD:mov TL0,#00h      mov TH0,#83h      cpl p1.7          cjne r7,#250D,zzz      mov r7,#00h      cjne r3,#60h,ddd      mov r3,#00h ddd: inc r3 zzz: inc r7      reti      org 0300h db 3fh,06h,5bh,4fh,66h,6dh,7dh,07h,7fh,6fh      end =================================================== 













































Keywords:89C51 Reference address:89C51 MCU Timer/Counter 0

Previous article:4x4 keyboard program written using 51 single chip interrupt
Next article:About 51 single chip microcomputer two-machine communication

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号