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