我在使用MEGA168开发一个项目,需要用到外部中断,但是在测试外部中断(下降沿中断)程序时发现一个问题
当选用1MHz(内部RC)时,可以产生中断,当选用8MHz(内部RC)时,无法进入外部中断,但是在8MHz时,低电平中断是正常的
编译器用的是ICCAVR7
1MHz和8MHz是通过FUSES的CKDIV8设置的
//ICC-AVR application builder : 2010-4-23 9:21:58
// Target : m168
// Crystal: 8.0000Mhz
#include <iom168v.h>
#include <macros.h>
char YY=0;
void port_init(void)
{
PORTB = 0x00;
DDRB = 0x80;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x08;
DDRD = 0x00;
}
#pragma interrupt_handler int1_isr:iv_INT1
void int1_isr(void)
{
if(YY==0)
{
PORTB |= 0x80;
YY=1;
}
else
{
PORTB &= 0x7f;
YY=0;
}
// EIFR|=2;
//external interupt on INT1
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
MCUCR = 0x00;
EICRA = 0x08; //extended ext ints
EIMSK = 0x02;
TIMSK0 = 0x00; //timer 0 interrupt sources
TIMSK1 = 0x00; //timer 1 interrupt sources
TIMSK2 = 0x00; //timer 2 interrupt sources
PCMSK0 = 0x00; //pin change mask 0
PCMSK1 = 0x00; //pin change mask 1
PCMSK2 = 0x00; //pin change mask 2
PCICR = 0x00; //pin change enable
PRR = 0x00; //power controller
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//
void main(void)
{
init_devices();
PORTB |= 0x80;
while(1)
{
}
//insert your functional code here...
} |