STC12 -定时器0 定时 1s LED0翻转1次 (二)中断法

程序:中断法

1. Timer0_Init()  在上次  (一)查询法   中说过

2.main.c 用 flag 

#include<reg52.h>
#include<Timer0_Init.h>
#define uchar unsigned char
#define uint unsigned int

sbit LED0 = P2^0;
uchar count = 0;
uchar flag = 0;
void main()
{
    
    Timer0_Init();
    while(1)
    {
        if(flag == 1)
        {
            flag = 0;
            LED0=~LED0;
        }      
    }
}

void Timer0() interrupt 1
{
    TH0 = (65535-10000+1)/256; 
    TL0 = (65535-10000+1)%256;
    count++;
    if (count >= 100) //中断 100 次即 1 秒
    {
        count = 0; //清零计数值以重新开始下 1 秒计时
        flag = 1; //设置 1 秒定时标志为 1
    }    
}

猜你喜欢

转载自www.cnblogs.com/oneme1world/p/12751143.html