Counting the changes in a pin

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

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!

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

What's the frequency of the squarewave ? :)

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

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

Is it a real square wave or is it a switch?

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

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

ka7ehk wrote:
Is it a real square wave or is it a switch?

Jim

Well the MCU is actually connected to the output of an IR sensor. I am using an IR LED to change its output (so it's either on or off, hence a digital signal). Therefore, it's a non periodic and low frequency signal that goes into the pin of the microcontroller.

Here's a screenshot of that signal(1.00s/DIV - its peak is 5V):

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

There's no flag variable in the ISR, so how do you know there's a change each time so new count can be sent to comms ? Show the whole code .

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

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

Is your input connected to the correct pin? Sounds like the pin is floating, are your grounds connected from mcu to sensor?

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

I just tried to simulate the square wave with a switch, and the counter works perfectly, so it's definitely not the code.

What I am thinking right now is that maybe it has something to do with the IR sensor I am using. Currently, I am using an infrared sensor that only detects an IR signal from a LED with PWM at 38Khz. Even though the signal looks digital on the oscilloscope, do you think that the frequency of the PWM that is turning on and off the LED is causing abrupt changes from 0V to 5V in the signal in whcih the oscilloscope is not able to detect?

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

Post your ir circuit. Is that scope scan above reading directly from the pin connected to the ir circuit or is it just the circuit? When the scope probe is attached to pin and circuit is it reading correctly then? Perhaps capacitance on the pin is not allowing the signal to fall but when probe attached the probe discharges the capacitance and counter works?

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

Quote:

in whcih the oscilloscope is not able to detect?

Well, you are on a very slow scan on your 'scope image, and there does appear to be some "fuzz" on the signal. Sample faster, and see if there are indeed any fluctuations in the signal below/above logic low/high for the AVR.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Trigger on an edge. Use a sensible trace speed. e.g. 10us /div or 100us/div. See if you get bounce / ringing.

David.