Counting External Events on PIN T0 Timer0

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

#include"STD_Types_H.h"
#include<avr/io.h>

void main(void){
    /*I`m connecting a Tacticle switch to the T0 pin on PORTB and enabling the pin as input , pullup resistor*/

    DDRB =0x00;                                                //PB0 Direction is Input
    PORTB|=(1<<0);                                            //Enabling internal pullup resistor
    DDRC =0xff;                                                //PORTC Direction is output

    TCCR0 |=(1<<CS01)|(1<<CS02)|(1<<FOC0);     //External Clock Source , Falling Edge
    //OCR0=5;
    TCNT0=(u8)0;
    while(1){
        if(TCNT0==(u8)5){
            PORTC^=0xff;                                    //Toggle PORTC whenever TCNT0 = 5
            TCNT0=(u8)0;

        }

    }
}

 

I`m Using timer0 to count how many times the switch has been pushed , the switch is connected to pin T0 on portB , .......... whenever the TCNT0 reaches 5 that means i pushed the switch 5 times the LED on PortC should be toggled , 
The problem is the LED is toggling whenever i push the switch 4 times and sometimes 2 times , can anyone explain to me why does this behviour occur?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It is called "contact bounce" and it is a classic (and perpetual) electrical problem that is often "solved" by software. The go-to reference on this is Jack Ganssle's report, here:

 

http://www.ganssle.com/debouncin...

 

One take-away is that you DO NOT use edge-interrupts and you DO NOT use counter event inputs.

 

Jim

 

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Wed. Sep 18, 2019 - 05:57 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

BTW, when you posts questions like this in future you may want to furnish a few more details like which AVR model you are talking about - it may be obvious to you but not to us ;-)

 

(but "bounce" affects all micros so I guess it doesn't matter)