?? da_4.c
字號:
/**********************************************
* File: DA_2.C
* Description: Tri-Angle
* Created Date: 2007-10-01
* Last Modified: 2007-10-01
* Author: Jeffrey - Schicksal@126.com
* Notes: None
**********************************************/
#include "Atmel/AT89X51.h"
#include "INTRINS.H"
#define TH0_VALUE 0x00
#define TL0_VALUE 0x08 // 定時器計數值 2us
#define PWM P2_0 // PWM輸出引腳
volatile unsigned char time_tick = 0;
// 函數聲明
void TIMER_Start();
void TIMER_Init(void);
/**********************************************
* Function: delay(unsigned int t)
* Input Variables: t
* Return Variables: None
* Usage: Common Delay Routine, t as the delay time ticks
**********************************************/
void delay(unsigned int t)
{
for(;t>0;t--); // 延時循環
}
/**********************************************
* Function: main()
* Input Variables: None
* Return Variables: None
* Usage: Program Entry
*********************************************/
void main()
{
unsigned char N = 0;
unsigned char x = 50; // 占空比50%
TIMER_Init();
TIMER_Start();
while(1)
{
PWM = 1; // PWM輸出引腳高
while(1)
{
time_tick = 0;
while(!time_tick); // 等待2us時鐘中斷
if(N==x)
PWM = 0; // PWM輸出引腳低
if(N==100)
break;
N++;
}
N = 0; // 重新計數開始
}
}
/**********************************************
* 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; // 打開T0的中斷
time_tick = 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; // 清中斷標志
time_tick++; // 2us
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 + -