0
\$\begingroup\$

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; 

}
\$\endgroup\$
2
  • 2
    \$\begingroup\$ Right off the bat, I notice you're setting the ICIE1 bit in the TIMSK0 register, but that bit doesn't exist in TIMSK0. Try TIMSK1 = (1 << ICIE1); instead. \$\endgroup\$
    – kibowki
    Commented Nov 8, 2017 at 21:06
  • 1
    \$\begingroup\$ @Tylerlee12 Oh no! this is embarrassing, Thanks it worked. :) \$\endgroup\$
    – Abel Tom
    Commented Nov 9, 2017 at 4:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.