100Hz signal Pin Change interrupt

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

Hi,

I have a 100Hz digital signal to a pin of my atmega168. I want to increment by one a variable when my PIN is high. However, it seems it doesn't works. is it too fast to be detected by my MCU?

This is my interrupt code:

volatile int32_t count;

void setup() {

PCMSK2 |= (1<<PCINT18); //The PCMSK2 register controls the //PCINT18 (PD2 pin)

PCICR |= (1<<PCIE2); //Pin change interrupt 2 enabled
PCIFR = (1<<PCIF2);  //Pin change interrupt 2, flag enabled.
sei();
}


//Whenever there is a change in the pin status from low to //high, or vice versa...
//Fires an interrupt!
ISR(PCINT2_vect)
{
if(PIND & (1<<PD2)) {
      //  PD2 is high
count++;
} else {
      // PD2 is low

}

}

while loop:

while(1)
{
 if( count>=10)
 DDRB |= (1<<PB3); //Turns on a LED
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No, its not too fast! 100Hz means 10ms for one period, 5ms for a half period. That is forever for a micro!

How do you know it isn't working? Changing the DDRB is not a very efficient way to turn a LED on or off. Try PORTB, instead. Also, the way you have it, the LED will be in one state for 10 counts (10 cycles or 100ms (barely enough to see) before it switches states. And, it will never switch back!

Jim

 

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

 

 

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

ka7ehk wrote:
No, its not too fast! 100Hz means 10ms for one period, 5ms for a half period. That is forever for a micro!

How do you know it isn't working? Changing the DDRB is not a very efficient way to turn a LED on or off. Try PORTB, instead. Also, the way you have it, the LED will be in one state for 10 counts (10 cycles or 100ms (barely enough to see) before it switches states. And, it will never switch back!

Jim

I don't know what is going on. I tried with the PORTB, but it is the same. What I am trying to do, is increment by one everytime there is a change in my pin. When that variable reaches an specific number, it will light up a LED, forever.

I can do it manually like with a switch, and it works! But not with my digital signal. (It is a memsic accelerometer which at 0g its output is 10 ms for one period with a 50% duty cycle). I can see the square wave in an oscilloscope, it looks fine, it is a 100Hz signal.

Thanks in advance

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

//I use Mage8 in ICCAVR..

void port_init(void)
{
PORTD = 0x00;
DDRD = 0x00; //PD2 is in put
PORTB = 0x00;
DDRB = 0x08; //PB3 is out low
}

void main(void)
{
port_init();

while(1)
{
if(PIND & (1<<PD2))
{
if(++counter > 100) //1s
{
PORTB ^= 0x08; //your LED port
}
}
}
}

//you will look 1ms is ON 1ms if OFF.
//100Hz is not too fast ..

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

ADD counter = 0 when counter > 100