用定时器T0中断控制1位LED秒闪烁
1、设计目的
用定时器T0中断控制1位LED秒闪烁
2、仿真电路
![](https://img-blog.csdnimg.cn/a1fdf83dc66b440d97d088c8b0bc9597.png)
![](https://img-blog.csdnimg.cn/4a5e75533d5c4af0a3519cde965ff165.gif)
3、程序设计(C语言)
#include<reg51.h>
#include<math.h>
sbit led=P1^0; //定义P1.0为led接口
int t0_num=0; //定义T0中断响应次数
/****************************************
函数功能:定时器T0的初始值设置
*****************************************/
void t0_init_value(int t)
{
TH0=((int) (pow(2,16)-t*pow(10,-3)*12*pow(10,6)/12))/256;
TL0=((int) (pow(2,16)-t*pow(10,-3)*12*pow(10,6)/12))%256; //设置定时器初值
}
/****************************************
函数功能:定时器T0的定时初始化
*****************************************/
void t0_init(int t)
{
TMOD=0x01; //设置T0工作于定时工作方式1
t0_init_value(t);
IE=0x00; //禁止中断
ET0=1; //开启T0溢出中断
EA=1; //开启总中断
TR0=1; //启动T0
}
/****************************************
函数功能:定时器T0的中断服务子程序,1s定时闪烁
*****************************************/
void second_1s() interrupt 1
{
TH0=15536/256;
TL0=15536%256; //设置定时器初值
if(t0_num==20)
{
led=~led; //led闪烁
t0_num=0; //1s定时结束,重置中断次数计数
}
else
t0_num++; //中断次数计数
}
/****************************************
函数功能:主函数
*****************************************/
void main()
{
led=1; //led初始状态熄灭
t0_init(50); //调用定时初始化函数
while(1); //持续运行
}
4、补充知识
![](https://img-blog.csdnimg.cn/eb93cfafaa574203888f1f229983d580.png)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)