很简单的使用定时器0定时两秒,控制PWM波,发现没有进入定时器0中断,一直没有发现问题出在哪一步,哪位大哥帮忙看看!
void main()
{
IO_Config(); //将所有的IO口配置为准双向口
Uart_Init(); //初始化串口
timer0_Init();
ADC_Init(); //初始化ADC
PWM_Init();
EA = 1; //打开总中断
Start_PWM(CYCLE1M);
while(1)
{
}
}
bit Delay_Status = 0;
volatile u8 cnt = 0;
void Timer0_Init(void) //20毫秒@24.000MHz
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xC0; //设置定时初始值
TH0 = 0x63; //设置定时初始值
TR0 = 1; //打开定时器
ET0 = 1;
}
void PWM_Init(void)
{
P_SW2 |= 0x80; //使能访问XSFR,否则无法访问以下特殊寄存器
PWMCFG |= PWM1A_SET_0; //设置端口的初始电平
PWMCFG |= PWM1B_SET_0;
PWMCFG |= PWM2A_SET_0;
PWMCFG |= PWM2B_SET_0;
PWMCKS = 0x00; //选择PWM时钟为Fosc/(0+1)
//PWMC = CYCLE; //设置PWM周期
PWM4T1 = 1;
PWM4T2 = 0x00;
PWM4CR = 0x00; //PWM4输出到P2.2,使能PWM4T2翻转中断
PWMCR |= 0x84; //使能PWM发生器,使能PWM计数器归零中断
P_SW2 &= ~0x80;
}
//起始频率工作,振幅比
void Start_PWM(u16 CYCLE)
{
P_SW2 |= 0x80;
if(Delay_Status == 1)
{
PWMCR &= ~0x84;
PWM2A = 0;
}
else
{
PWMC = CYCLE; //设置pwm周期
PWM4T1 = 1;
PWM4T2 = CYCLE*DUTY/100+1;
PWMCR |= 0x84; //使能PWM信号输出
}
P_SW2 &= ~0x80;
}
void Timer0_Isr(void) interrupt 1
{
cnt++;
if(cnt >= 100) //100*20ms=2s
{
cnt = 0;
Delay_Status = !Delay_Status;
}
}
|