📄 t0intledsexample.c
字号:
/**********************************************
* File: T0IntLEDsExample.c
* Description: LEDs T0 Int Triggle
* Created Date: 2007-09-14
* Last Modified: 2007-09-14
* Author: Jeffrey - Schicksal@126.com
* Notes: None
**********************************************/
#include <REGX51.h>
#define TH0_VALUE 0x06
#define TL0_VALUE 0x00
unsigned int timer_tick;
unsigned int timer_tick_1s;
void LEDs_Move();
void LEDs_Error();
void LEDs_Gragon();
void TIMER_Init();
void TIMER_Start();
#ifndef true
#define true 1
#endif
#define TASK_1 1
#define TASK_2 2
/**********************************************
* Function: main()
* Input Variables: None
* Return Variables: None
* Usage: Program Entry
*********************************************/
void main()
{
unsigned char System_Status = true;
unsigned char System_Task = TASK_2;
TIMER_Init(); // 初始化T0
TIMER_Start(); // 启动T0
while(1)
{
// 程序主任务区
// ............
// 程序主任务区
if(System_Status != true) // 当系统发生错误
{
EA = 0; // 关中断
LEDs_Error(); // 跑马灯指示错误
EA = 1; // 开中断
}
}
}
/**********************************************
* Function: LEDs_Move
* Input Variables: None
* Return Variables: None
* Usage: System Normal Status Report
*********************************************/
void LEDs_Move()
{
static unsigned char LEDs = 0x55; // 静态变量用于存储LEDs发光状态
P0 = LEDs; // LED间隔亮灭并移位
delay(LED_FLASH_T); // 延时
LEDs = ~LEDs; // 状态改变
}
/**********************************************
* Function: LEDs_Error
* Input Variables: None
* Return Variables: None
* Usage: System Error Status Report
*********************************************/
void LEDs_Error()
{
static unsigned char LEDs = 0x00; // 静态变量用于存储LEDs发光状态
P0 = LEDs; // LED警告报警亮灭
delay(LED_FLASH_T); // 延时
LEDs = ~LEDs; // 状态改变
}
/**********************************************
* Function: LEDs_Dragon
* Input Variables: None
* Return Variables: None
* Usage: System Dragon LED Animation
*********************************************/
void LEDs_Dragon()
{
static unsigned char Direction = 1; // 静态变量用于存储龙舞方向
static unsigned char LED_status = 0x0F; // 静态变量用于存储LEDs发光状态
if(Direction==1)
{
if(LED_status>=0x0F)
LED_status=LED_status<<1;
else if(LED_status==0x07)
LED_status=0x0F;
else if(LED_status==0x03)
LED_status=0x07;
else
LED_status=0x03;
if(LED_status==0xC0)
Direction=0;
}
else
{
if(LED_status==0xE0)
LED_status=0xF0;
if(LED_status==0xC0)
LED_status=0xE0;
else if(LED_status<=0xF0)
LED_status=LED_status>>1;
if(LED_status==0x03)
Direction=1;
}
P0=~LED_status;
}
/**********************************************
* Function: TIMER_Init
* Input Variables: None
* Return Variables: None
* Usage: T0 Initialization
*********************************************/
void TIMER_Init(void)
{
ET0 = 0; // 关闭T0的中断
TMOD = 0x00; // T0工作在模式0
TCON = 0x00; // 暂时未启动T0
TL0 = TL0_VALUE;
TH0 = TH0_VALUE; // 产生2ms中断 |24 MHz 晶振
ET0 = 1; // 打开T2的中断
timer_tick = 0;
timer_tick_1s = 0;
}
/**********************************************
* Function: timer0_interrupt
* Input Variables: None
* Return Variables: None
* Usage: TIMER_Interrupt Service Routine
*********************************************/
void timer0_interrupt(void) interrupt 5 using 1
{
EA = 0; // 关全局中断
TF0 = 0; // 清中断标志
timer_tick++; // 2ms
if ( timer_tick == 500 ) // 1s = 2ms X 500
{
timer_tick_1s += 1; // 秒计数增1
timer_tick = 0; // 2ms计数清零
void LEDs_Dragon(); // 龙舞花样状态变换
}
TL0 = TL0_VALUE; // T0初值装载
TH0 = TH0_VALUE; // 产生2ms中断 |24 MHz 晶振
EA = 1; // 开中断
}
/**********************************************
* Function: TIMER_Start
* Input Variables: None
* Return Variables: None
* Usage: Start T0
*********************************************/
void TIMER_Start()
{
TR0 = 1; // 启动T0
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -