ATMega8 T0不工作,计数器没累加,也不溢出~求高手帮忙解决~
时间:10-02
整理:3721RD
点击:
情况是这样的:我想给PD2(即INT0)引脚一个PWM信号,触发外部中断,然后用T0去算PWM的高电平部分的脉宽,但是出了以下问题:T0不工作,计数器TCNT0不累加,也不进入T0溢出中断;但可以进去外部中断(上升沿和下降沿都可以进去)。用的是外部晶振8Mhz,CKSEL=1111,SUT=00。编程熔丝为0xD9CF。
求大神高手帮帮忙,小弟新手~帮帮忙~
程序如下:
#include <ATmega8.h>
#include <interrupt.h>
#include <signal.h>
#include <stdio.h>
#define TIMER0OVERFLOW_INT_ENABLE TIMSK |= 0x01 //T0溢出中断使能
#define TIMER0OVERFLOW_INT_DISABLE TIMSK &= ~0x01 //T0溢出中断禁能
#define TIMER1OVERFLOW_INT_ENABLE TIMSK |= 0x04 //T1溢出中断使能
#define TIMER1OVERFLOW_INT_DISABLE TIMSK &= ~0x04 //T1溢出中断禁能
#define TIMER2OVERFLOW_INT_ENABLE TIMSK |= 0x40 //T2溢出中断使能
#define TIMER2OVERFLOW_INT_DISABLE TIMSK &= ~0x40 //T2溢出中断禁能
#define INT0_INT_ENABLE GICR |= 0x40 //INT0外部中断使能
#define INT0_INT_DISABLE GICR &= ~0x40 //INT0外部中断禁能
#define INT0_FALL_ENABLE MCUCR |= 0x02 //INT0引脚上下降沿引发INT0中断
#define INT0_FALL_DISABLE MCUCR &= ~0x02
#define INT0_RISE_ENABLE MCUCR |= 0x03 //INT0引脚上上升沿引发INT0中断
#define INT0_RISE_DISABLE MCUCR &= ~0x03
#define CLR_INT0_FLAG GIFR &= ~0x40 //INT0电平发生跳变中断标志清0
unsigned char Timer0Overflow;
volatile unsigned int PPM_Signal = 0;
volatile unsigned char fall_time = 0,rise_time = 0;
unsigned int aaa;
void Timer0_Init(void)
{
TCCR0 = 0x01;
TIMER0OVERFLOW_INT_ENABLE; //T0溢出中断使能
}
void PPM_Init(voi //PPM初始化,INT0设置
{
INT0_RISE_ENABLE;
INT0_INT_ENABLE; //INT0外部中断使能
CLR_INT0_FLAG; //INT0电平发生跳变中断标志清0
}
SIGNAL(SIG_INTERRUPT0) //INT0引脚产生跳变,中断请求
{
static unsigned int ppm;
static unsigned char count0_value;
CLR_INT0_FLAG;
if(MCUCR & (1<<ISC00))
{
INT0_RISE_DISABLE;
INT0_FALL_ENABLE;
count0_value=TCNT0;
rise_time++;
}
else
{
INT0_FALL_DISABLE;
INT0_RISE_ENABLE;
ppm= TCNT0 - count0_value + Timer0Overflow * 256;
PPM_Signal=ppm;
fall_time++;
}}
SIGNAL(SIG_OVERFLOW0) //T0溢出中断服务程序
{
Timer0Overflow++; //T0溢出中断次数
}
int main(void)
{
cli();
DDRC = 0xFF;
PORTC = 0xE0;
DDRD = 0xFB;
PORTD = 0xFB;
DDRB = 0xFF;
PORTB = 0xFF;
Timer0_Init();
PPM_Init();
sei();
while(1)
{
if(PPM_Signal < 80) PORTC |=0x01;
if(fall_time > 100) PORTC |=0x02;
if(rise_time > 100) PORTC |=0x04;
}
}
允许T0开启了吗?~
GCC的IDE啊,ICC能看懂~~~~~
小编要做PPM模块吗?