Use of interrupt timers

to interrupt

In the process of performing tasks, the microcontroller needs to deal with other urgent affairs, and needs to interrupt the currently executing tasks, so it needs to use the interrupt service.
Interrupts are divided into timer/counter interrupts, external interrupts, and serial port interrupts according to their types. This article mainly explains the timer interrupt.

timer

Below, we directly introduce the code for explanation.
#include<STC15F2K60S2.H>//General operation, import header file

unsigned char tab[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF,0XC6};//digital tube

unsigned char tt;//variable definition
unsigned char num;
void Timer0Init(void);

void main(void)
{

P2=0XA0;P0=0X00;P2=0X80;P0=0XFF;  
P2=0XC0;P0=0X04;P2=0XFF;P0=0XFF;   //数码管初始化程序

Timer0Init();//定时器初始化
EA=1;ET0=1;//打开总中断,打开定时器0,在这里,EA、ET0相当于一个开关,EA相当于一个总开关,ET0相当于一个小开关,这两个开关不打开,定时器就不会响应。

while(1)//这里该写什么写什么
{	
}

}

void Timer0Init(void) //5 [email protected], here is generated in the programming program, set the required time in the programming program, and the programming device will automatically generate it. The range is about 0-6.5535ms
{ AUXR |= 0x80; //Timer clock 1T mode // AUXR = AUXR|0x80; 0010 1010 | 1000 0000 =1010 1010 TMOD &= 0xF0; //Set timer mode TL0 = 0x00; //Set timing initial value TH0 = 0x28; //Set timing initial value TF0 = 0; //clear TF0 flag TR0 = 1; //timer 0 starts counting }






void tm0_isr() interrupt 1//Interrupt service function, the function name is defined by yourself, followed by the interrupt number, the interrupt number must correspond to the interrupt to be used.
{ tt++; if(tt = = 200) { tt=0; P0=tab[num]; num++;if(num = = 12)num=0; } } The above figure shows the number and priority order of the interrupt service, you can refer to it yourself. If you need to understand the principle, you can go to https://editor.csdn.net/md/?articleId=112068955, MCU review. If you have any questions, please send a private message.








insert image description here

—————————————————————————————————————————
This article is purely original, if there is any infringement, please contact to delete, if there is any error, please criticize and correct, thank you all.

Guess you like

Origin blog.csdn.net/G_Shengn/article/details/116068291