我正在尝试以下列方式使用 ATMega32 输入捕获单元:
我有一个使用 TIMER0(通过 OC0 引脚)生成的波(XTAL=8MHz,预分频器 =1024,CTC 模式,OCR0=244,给出 ~32Hz)。然后我尝试使用 TIMER1(通过 ICP 引脚)捕获此波(XTAL=8MHz,预分频器=1024,正常模式,导致 ~7812.5 Hz)
代码片段提供了更多详细信息。
我只有0的结果的问题。
**
修改的。
** 非常感谢@clifford,根据下面的代码,它可以工作,但是:
1-有一点错误(结果不准确)。
2-当我改变我的方程式时
从
count=(capture2+(captOVf*65536))-capture1;
to
count=capture2-capture1;
我在液晶显示屏上显示垃圾。
非常抱歉问题格式不好。
typedef enum{
CAPTURE1,
CAPTURE2,
WAIT
}timer_states_t;
volatile timer_states_t flag= WAIT;
volatile u8 x,y,captOVf,TOVfs=0;
volatile u16 capture1,capture2;
void setup_pins(){
SET_BIT(DDRB,3); //SET OC0 pin as output
CLEAR_BIT(DDRD,6); //set ICP pin as input
SET_BIT(PORTD,6); //set ICP pin pullup resistance
}
//Timer0 Configurations / wave generator .
void timer0_init(){
OCR0=244;
TCCR0=0x1D; //ctc , 1024 , toggle OC0 pin on compare match ;
}
//Timer1 Configurations / capturer .
void timer1_init(){
TCCR1A=0x00; // Timer1 , normal mode, no on compare output ,
TCCR1B=0x45; // prescaler 1024, Input capture edge as rising .
SET_BIT(TIMSK,TICIE1); // enable input capture interrupt .
SET_BIT(TIMSK,TOIE1); //enable timer1 overflow interrupt .
CLEAR_BIT(ACSR,ACIC); //disable Analog comparator .
SET_BIT(SREG,7); //GLOOBAL ITERRUPT
}
ISR(TIMER1_OVF_vect){
TOVfs++;
}
ISR(TIMER1_CAPT_vect){
switch(flag){
case CAPTURE1:
//capture1=ICR1;
x=ICR1L;
y=ICR1H;
capture1=x | (y<<8);
TOVfs=0;
flag=CAPTURE2;
break;
case CAPTURE2:
//capture2=ICR1;
x=ICR1L;
y=ICR1H;
capture2=x | (y<<8);
flag=WAIT;
captOVf=TOVfs;
break;
}
}
int main(){
setup_pins();
timer0_init();
timer1_init();
LCD_init();
unsigned long count ;
u8* countString ;
while(1){
flag=CAPTURE1;
while(flag != WAIT);
count=(capture2+(captOVf*65536))-capture1;
itoa(count,countString,10);
LCD_send_string("count:");
LCD_send_string(countString);
_delay_ms(500);
LCD_send_cmd(lcd_Clear);
}
}