本帖最后由 ghye 于 2011-5-11 14:32 编辑
代码如下:#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 8000000UL
#include <util/delay.h>
unsigned short icp1_tmr1;
unsigned short icp1_tmr2;
unsigned short icp1_cnt;
unsigned char icp1_ok;
inline void gpio_init(void)
{
DDRA = 0x0f;//LED gpio
DDRC = 0x00;
PORTC =0x01;//接key1的PC0口输入,上拉有效
DDRD = 0x00;
PORTD =0x10;//接红外一体接收头的引脚为输入,上拉有效
SFIOR = 0x00;//全局上拉使能。默认使能
}
inline int chk_key1(void)
{
if( !(PINC & 0x01))
{
_delay_ms(20);
if( !(PINC &0x01))
return 1;
}
return 0;
}
inline void icp1_init(void)
{
icp1_cnt = 0;
}
ISR(TIMER1_CAPT_vect)
{
if(icp1_cnt > 0)
{
TIMSK &= ~_BV(TICIE1); //使能T/C1输入捕捉中断。没定是否需要T/C1溢出中断
PORTA = 0x02;//LED灯测试
_delay_ms(1000);
}
else if(icp1_cnt == 0)
{
PORTA = ~PORTA;//LED灯测试
icp1_tmr1 = ICR1;
}
icp1_cnt++;
}
int main(void)
{
gpio_init();
TCCR1B = 0x01;//icp1下降沿输入触发捕捉,1分频 . T/C1正常计数方式
sei();
while(1)
{
if(chk_key1())
{
icp1_init();
TIMSK |= _BV(TICIE1); //使能T/C1输入捕捉中断。没定是否需要T/C1溢出中断
}
}
return 0;
}
我的目的是检测ICP1信号。这是一个测试的程序。
下载atmega128a板子上测试发现,一直运行:
else if(icp1_cnt == 0)
{
PORTA = ~PORTA;
icp1_tmr1 = ICR1;
}
而从来都没icp1_cnt > 0的情况。
为何下次捕捉中断时,icp1_cnt还是0呢? 请指教
环境:
avr-gcc (GCC) 4.3.2
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |