I am trying to use ICP (Input Capture Pin) functionality of ATMEGA328p (Arduino Uno board) and somehow it does not seem to work. I have connected a switch to ICP pin PB0 in this case, and want to toggle a LED at PC0 every time the switch is pressed. Somehow i am not landing in the ISR. ICRL seems to show zero too. Here's my code:
void init_input_capture()
{
TIMSK0 = ( (1<<ICIE1) ); /*Enable ICP Interrupt */
TCCR1B = ( (1<<ICES1) | (1<<ICNC1) | (1<<CS11)); /*Enable rising edge detection, noise cancellaton*/
}
void Transmit_string(char *str )
{
for(i=0; i<strlen(str)+1;i++)
Transmit(&str[i]);
Transmit("\n");
Transmit("\r");
for(i=0; i<strlen(str)+1;i++)
str[i]=0;
}
void capture_function()
{
if(capture_occured==1)
{
LED_PIN(SET);
}
else
LED_PIN(CLEAR);
}
void main()
{
DDRC = 1<<PC0; /*Pin PC0 os output LED */
PORTC = 1<<PC0; /*Enable PC0 internal pullup */
DDRB = 0x00; /*PB0 as INPUT*/
PORTB= 1<<PB0;
usart_init(baud);
init_input_capture();
sei();
while(1)
{
capture_function();
sprintf(value_buf,"%1u", temp);
Transmit_string(value_buf);
_delay_ms(300);
}
}
ISR(TIMER1_CAPT_vect)
{
capture_occured ^= 0x01;
PORTC ^= 1<<PC0; //not working
temp=ICR1L;
}
ICIE1
bit in theTIMSK0
register, but that bit doesn't exist inTIMSK0
. TryTIMSK1 = (1 << ICIE1);
instead. \$\endgroup\$