Timer/Counter Usage (Natural Interrupt Priority)

Publisher:感恩的7号Latest update time:2015-05-18 Source: 51heiKeywords:Timers Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The use of timer/counter can be divided into two major steps and codes

1. Initialize the timer
2. Write an interrupt subroutine (also called an interrupt function, in layman's terms, it is to call a function)
 
                    
                        Initialize the timer
      1) Set the timer/counter mode (in the front)
     2) Enable the timer interrupt (timer/counter 0 or 1 or other)
     3) Enable the timer/counter (timer/counter 0 or 1 or other)
     4) Enable the general interrupt
     5) Assign a value to the timer
    Note: The above steps can be disrupted but not omitted. (If some conditions are initialized in other functions, they can be omitted here)

                                     Writing an interrupt subroutine

        void tim0_isr (void) interrupt 1 
       1) void tim0_isr(void) (This is the function that needs to be executed after the interrupt) 
        2) interrupt 1 (There is a detailed description of the interrupt number before the interrupt number)
 

Code
 #include
sbit LED=P0^7; //define the LED pin to be lit
unsigned int i=0;

void tim0_isr (void) interrupt 1 //This is the interrupt execution function
{
i++; //Every interruption i+1
}


void csh_dsq_0() // Initialize timer/counter 0
{
TMOD = 0x02; // Use mode 3 (auto-reload mode)
TH0 = 0x00; // Timer 0 reload data (here is the value assigned when given)
TL0 = 0x00; // Initialization (here is the value assigned when given)
ET0 = 1; // Turn on timer 0 interrupt
TR0 = 1; // Enable timer 0
EA = 1; // Turn on general interrupt
} void main()
{
LED = 0;
csh_dsq_0(); // Call interrupt initialization
while (1)
{
if (i == 5000) // Interrupt 5000 times and LED turns on or off once
{
LED = ~LED; // LED turns on or off
i = 0; // Clear i to restart the interrupt count
}
}
}

Keywords:Timers Reference address:Timer/Counter Usage (Natural Interrupt Priority)

Previous article:A brief introduction to the principles of computer communication
Next article:1602 screen with infrared remote control (to achieve cursor movement) and new understanding of 1602

Recommended ReadingLatest update time:2024-11-16 13:47

Timers interrupt processing function in STM32
1. In the firmware library function, the function used to read the value of the interrupt status register to determine the interrupt type is: ITStatus TIM_GetITStatus(TIM_TypeDef* TIMx, uint16_t) Function: Determine whether the interrupt type TIM_IT of timer TIMx has an interrupt. For example, we want to determine wh
[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号