欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標題:
單片機定時器T0被四次調用,控制led燈
[打印本頁]
作者:
cjc342019965
時間:
2017-4-14 11:21
標題:
單片機定時器T0被四次調用,控制led燈
定時器T0被四次調用,控制led燈
0.png
(62.33 KB, 下載次數(shù): 87)
下載附件
2017-4-14 20:42 上傳
#include<reg52.h> //頭文件
#define MY_TIMER_MAX (4) //最多四個定時器
#define NULL (0)
typedef void (*pFun)(void); //callback 函數(shù)指針類型
typedef struct myTimer
{
char on; //開關
char is_period; //是否周期循環(huán)
unsigned short int time_out; //定時時間,單位ms
unsigned short int count; //定時計數(shù)用
}
MY_TIMER;
pFun callback[MY_TIMER_MAX] = {NULL}; //定時器回調函數(shù)數(shù)組
MY_TIMER myTimerList[MY_TIMER_MAX] = {0}; //定時器結構數(shù)組
int gMyTimerMessage[MY_TIMER_MAX] = {0}; //定時器消息數(shù)組
sbit LED1=P1^0;
sbit LED2=P1^1;
sbit LED3=P1^2;
sbit LED4=P1^3;
#define ALL_ON {LED1=0;LED2=0;LED3=0;LED4=0;} //燈全開
//創(chuàng)建定時器,簡化版本。
int CreatTimer(int index,unsigned short int time_out,char is_period,pFun callbackFun)
{
if(index >= MY_TIMER_MAX) return -1;
myTimerList[index].on = 1;
myTimerList[index].is_period = is_period;
myTimerList[index].time_out = time_out;
myTimerList[index].count = 0;
callback[index] = callbackFun;
return index;
}
//四個LED控制函數(shù),on初始是0,第一次調用on變?yōu)?,是關燈。
void led_1_ctrl(void)
{
static char on = 0;
on = !on;
LED1 = on;
}
void led_2_ctrl(void)
{
static char on = 0;
on = !on;
LED2 = on;
}
void led_3_ctrl(void)
{
static char on = 0;
on = !on;
LED3 = on;
}
void led_4_ctrl(void)
{
static char on = 0;
on = !on;
LED4 = on;
}
void Init_Timer0(void) //初始化定時器0
{
TMOD=0x01; //定時器0,使用模式1,16位定時器
TH0=(65536-1000)/256; //給定初值
TL0=(65536-1000)%256;
EA=1; //打開總中斷
ET0=1; //打開定時器中斷
TR0=1; //開定時器
}
void main() //主函數(shù)
{
unsigned int i;
ALL_ON;
CreatTimer(0,250,1,led_1_ctrl);
CreatTimer(1,500,1,led_2_ctrl);
CreatTimer(2,1000,1,led_3_ctrl);
CreatTimer(3,2000,1,led_4_ctrl);
Init_Timer0();//初始化定時器0
while(1)
{
for(i = 0; i<MY_TIMER_MAX; ++i)
{
if(gMyTimerMessage[i]) //定時器消息來到,啟動。
{
gMyTimerMessage[i] = 0; //消息清除
if(callback[i] != NULL)
{
(*callback[i])(); //調用回調函數(shù)
}
}
}
}
}
//定時器中斷函數(shù),1ms 定時。
void Timer0_isr(void) interrupt 1
{
unsigned int i = 0;
TH0=(65536-1000)/256;//重新賦值 1ms
TL0=(65536-1000)%256;
EA = 0;
for(i = 0; i<MY_TIMER_MAX; ++i)
{
if(myTimerList[i].on)
{
++(myTimerList[i].count); //計數(shù)
if(myTimerList[i].count >= myTimerList[i].time_out) //定時到
{
gMyTimerMessage[i] = 1; //發(fā)消息
if(myTimerList[i].is_period) //是否周期循環(huán)
{
myTimerList[i].count = 0; //計數(shù)重置
}
…………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
下載:
四個led燈.zip
(20.83 KB, 下載次數(shù): 14)
2017-4-14 11:19 上傳
點擊文件名下載附件
定時器
下載積分: 黑幣 -5
作者:
wdliming
時間:
2019-4-20 11:57
想請問一下,為何最多4個led,是因為再多了,定時就不準了是吧??
歡迎光臨 (http://m.raoushi.com/bbs/)
Powered by Discuz! X3.1