C51 Programming 16-Interrupts (Timer Interrupts 3)

Publisher:WanderlustHeartLatest update time:2022-06-24 Source: eefocusKeywords:C51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

To use the timer/counter interrupt, the following conditions must be met.


    1) Interrupts are always enabled EA = 1;


    2) The interrupt source is allowed to be turned on ET0 = 1 or ET1 = 1;


    3) Set the working mode of the timer/counter (set TMOD)


    4) Load the timer and set the initial calculation value of the counter.


    5) Start the timer (TR0 = 1 or TR1 = 1 in TCON)


    6) Interrupt service function


Through the study of some previous interrupt articles, I believe that except for the 4th point above, using timers/counters is not difficult.


Load the timer. As we know from the previous timer chapter, there are 4 modes for timer/counter. They are 13-bit manual load (mode 0), 16-bit manual load (mode 1), 8-bit auto-reload (mode 2), and 8-bit manual load (mode 3). According to the number of bits of the timer/counter, TH0 (1) and TL0 (1) are required to form the number of bits of the timer/counter.


The input clock pulse is obtained by dividing the output of the crystal oscillator by 12, so the timer is a counter of the machine cycle, each machine cycle +1, that is, the value of the number of bits of TH0 (1) and TL0 (1) that make up the timer/counter is +1 in each machine cycle. When the value overflows, a timer/counter interrupt is generated.


The following uses method 1 and method 2 to perform timing and light up the LED. 


Method 1

 

/****************************************

Header:

File Name: main.c

Author: adam

Date: September 19, 2020

**********************************************/

#include "reg52.h"

#include "stdio.h"

typedef unsigned char uchar;

typedef unsigned int uint;

uint countms = 0;

void DelayMs(uint ms);

void main(){

   EA = 1; //General interrupt enabled

   ET0 = 1; //Timer T0 interrupt enabled

   TMOD = 0x01; //Set to mode 1, timer mode: C/T=0; do not start external INT0 control to start timing: GATE=0,

   //Timing 1ms, machine cycle 1us, 1000 machine cycles 1ms

   //16-bit timer overflow condition: full value 65535 (#FFFF) + 1 overflows, so timing 1ms requires (65535+1-1000);

   // Divide the value into two registers, each of which can hold 256 numbers.

   TH0 = (65536-1000)/256; //Load initial value TH     

   TL0 = (65536-1000)%256; //Load initial value TL

   TR0 =1; //Start timer TR0

   while(1)

   {

        DelayMs(1000);

        P0=~P0; 

   }

}

void DelayMs(uint ms)

{

        while(countms        countms=0; 

}

void Timer0(void) interrupt 1

{

    //Reinstall the initial value

    TH0 = (65535-1000)/256;

    TL0 = (65535-1000)%256;  

    countms++; //Every ms, countms++

}

 

Method 2


/****************************************

Header:

File Name: main.c

Author: adam

Date: September 19, 2020

**********************************************/

#include "reg52.h"

#include "stdio.h"

typedef unsigned char uchar;

typedef unsigned int uint;

uint count = 0;

uint countms = 0;

void DelayMs(uint ms);

void main(){

   EA = 1; //General interrupt enabled

   ET0 = 1; //Timer T0 interrupt enabled

   TMOD = 0x02; //Set to mode 2, timer mode: C/T=0; do not start external INT0 control to start timing: GATE=0,

   //Since the mode is only 8 bits, the maximum timing can only be 0.256us, so first time 100us, and then time ms through 100us.

   TH0 = 256-100; //Load value TH, automatic reload     

   TL0 = 256-100; //Load value TL, overflow generates interrupt.

   TR0 =1; //Start timer TR0

   while(1)

   {

        DelayMs(100);

        P0=~P0;

   }

}

void DelayUs(uint us){  

      while(countus      count = 0;

}

void DelayMs(uint ms)

{

           while(countms            DelayUs(10);   

            countms++;  

        }      

        countms=0;

}

void Timer0(void) interrupt 1

{  

    countus++; //every ms, countms++   

}

 

Note: In the above two examples, the interrupts are 1ms and 100us respectively. It is not recommended to use the timer to set the interrupt to a smaller time.


Although the timer can be set to generate an interrupt in 1us, 1us is only one machine cycle. After the interrupt is responded, the interrupt service function is entered. It may take four machine cycles to execute a simple statement, plus the protection and restoration of the scene, which will take up a long machine cycle and make the timing inaccurate.


When GATE = 1, the timer/counter is affected by the INT0/INT1 level. INT0/INT1 = 1 starts counting, and INT0/INT1 = 0 stops counting. When GATE = 1, after the microcontroller is powered on, the initial values ​​of P0~P3 are all high levels, so there is no need to connect an external high level to start the timer/counter. Generally, INT0/INT1 uses the following circuit. When the button is pressed, it is low level and the timer/counter stops counting.

Keywords:C51 Reference address:C51 Programming 16-Interrupts (Timer Interrupts 3)

Previous article:C51 Programming 15-Interrupts (Timer Interrupts 2)
Next article:C51 Programming 17-Interrupt (Serial Communication 1)

Recommended ReadingLatest update time:2024-11-16 00:46

Mixed Programming of C51 and Assembly Language 1
1. Mixed programming within functions If you want to use assembly language inside a C language function, you should use the following Cx51 compiler control commands: #pragma asm ;;; Assembly code #pragma endasm function: The asm and endasm commands are used to merge the assembler program marked by them into
[Microcontroller]
C51 Programming Summary - Strange Knowledge of MCU 2 (Keil Compiler Error)
1: The following error occurs when compiling with C51 Keil: error C249: 'DATA': SEGMENT TOO LARGE,  error C249: 'DATA': SEGMENT TOO LARGE Solution: Set as follows  2: Warning appears in C51 Keil compilation: UNRESOLVED EXTERNAL SYMBOL *** WARNING L2: REFERENCE MADE TO UNRESOLVED EXTERNAL     SYMBOL:  _DS1302_W
[Microcontroller]
C51 Programming Summary - Strange Knowledge of MCU 2 (Keil Compiler Error)
Performance comparison of AVR, C51 and PIC 8-bit microcontrollers
1. 51 Series   The most widely used eight-bit microcontroller is Intel's 51 series. Due to its reasonable hardware structure, standardized instruction system and long production history, it has the advantage of first-mover advantage. Many famous chip companies in the world have purchased the core patent technology of
[Microcontroller]
How to set ROM using keil c51 to generate hex
I use at89s52 (256RAM, 8K ROM), and after compiling the program, it shows program size: data=56.0 xdata=0 code=3529. There is no external ROM in my circuit. How can I set it so that the generated hex code is only stored in the at89s52? It is definitely not okay to choose small, because my code is larger than 2K; if I c
[Microcontroller]
Operators in C51 language
[Microcontroller]
Operators in C51 language
How to embed assembly language in C51 language
Keil C language embeds assembly language for mixed programming, the method is as follows: 1. To embed the assembly code snippet in the C file, add the assembly code as follows #pragma ASM ; Assembler Code Here #pragma ENDASM 2. Right click on the C file containing the assembly code in the Project window and select "Op
[Microcontroller]
How to embed assembly language in C51 language
c51 1ms-11.0592MHZ delay program
#include void delay(unsigned char n)  //约n(ms)延时 { unsigned char i,j,k;   for(k=0;k n;k++)     {        for(j=0;j 10;j++) // about 1ms delay        {          for(i=0;i 30;i++) // about 0.1ms delay           {;}        }     } } main() {  while(1)   {      delay(15);       } }        /*About 1
[Microcontroller]
How to convert numbers into strings in C51
Convert an integer to a string, how do you do it in C51?  This question seems to be very simple. KeilC also uses the sprintf() function, so just use it. But for the microcontroller where every inch of land is valuable, The question is not that simple. Look at this unused code in my code: //sprintf(sFileLen,"%d",
[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号