?? 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 + -