Hi,
I am trying to make a simple counter for every change (0V-5V) in a pin of an atmega328p. The variable, however, is NOT incrementing every time the square wave passes from 0V to 5V and vice versa. But instead, whenever there is a change in the pin, the variable is incrementing by hundreds and not just one increment.
Here is my code:
void setup() { PCMSK1 |= (1<<PCINT10); PCICR |= (1<<PCIE1); PCIFR |= (1<<PCIF1); sei(); } //How many times does the MCU reads 0V? or 5V? volatile int changes_low; volatile int changes_high; //+5V ISR(PCINT1_vect) { //If high (5V) if( PINC & (1<<PC2)){ changes_high++; } else{ changes_low++; } }
In the while loop I am just reading those variables, and sending data over the serial port... Every time the interrupt is triggered, the variables are updated and incremented by 10 or even 100 sometimes. It's supposed to increment by one though....
What am I doing wrong here?
P.S the signal that goes into pin PC2 is a square wave, and it's looking alright on the oscilloscope.
Thanks in advance!