PCINT behaves weird

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

Hi guys.

ATMEGA2560

I tried to toggle a LED on PB7 with 2 PCINTs. It works, more or less.

My problem is, when "number=0" at the beginning the LED lits up and I can also turn off the LED with the "PCINT2_vect". Now if I want to turn on the LED with "PCINT0_vect", it turns on but after appr. 2 seconds it's off again. After 1 second it turns on again(without doing something).

If "number=1" at the beginning it's exactly the other way around. Means that the LED is off, then on for a short time, and finally keeps off.

 

Is there a mistake in my code?
Thanks in advance! ;-)

 

/*
 * Pinchange_interrupt.c
 *
 * Created: 30.11.2017 18:56:15
 * Author : Andreas
 */

#define F_CPU 16000000L // Specify oscillator frequency
#include <avr/io.h>
#include <avr/interrupt.h>

volatile short number = 0;

int main(void)
{

    DDRB    = 0b00100000;    //PORTB7 = LED; PCI0; PCINT6 = PORTB6
    DDRK    = 0b00000000;    //PCIN16
    PORTB   = 0b10000000;
    PORTK   = 0b00000000;
    PCMSK0  = 0b01000000;    //PB6
    PCMSK2    = 0b00000001;    //PK0
    PCICR    = 0b00000101;
    sei();                    //Global INT enable
    /* Replace with your application code */
    while (1)
    {
        if(number==0){PORTB   = 0b00100000;}
        else if (number==1){PORTB   = 0b00000000;}
    }
}

ISR(PCINT0_vect)
{
    number=0;
}

ISR(PCINT2_vect)
{
    number=1;
}

 

This topic has a solution.
Last Edited: Fri. Dec 1, 2017 - 06:39 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

andi_avr_2560 wrote:
it turns on but after appr. 2 seconds it's off again.
Watchdog? You haven't enabled WDTON fuse have you?

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

No, I didn't.

Why do I need to enable the watchdog?

 

I found it in the datasheet but I'm not sure how to configure the registers...

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

I'm not saying you HAVE to enable the watchdog I am saying that when AVRs behave unexpectedly after a certain amount of time (often a few seconds) it's because a WDT reset has occurred. It was just a guess as to what may be wrong. If you are certain WDTON has not been enabled (even accidentally) then ignore the guess.

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

andi_avr_2560 wrote:
it turns on but after appr. 2 seconds it's off again. After 1 second it turns on again(without doing something).
Floating inputs, I guess.

At least you have not enabled the internal pull-ups for the interrupt pins.

Stefan Ernst

Last Edited: Fri. Dec 1, 2017 - 01:18 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
    DDRB    = 0b00100000;    //PORTB7 = LED; PCI0; PCINT6 = PORTB6
    DDRK    = 0b00000000;    //PCIN16
    PORTB   = 0b10000000;
    PORTK   = 0b00000000;

Silly question... Why should I enable the pull-ups?

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

andi_avr_2560 wrote:
Silly question... Why should I enable the pull-ups?

 

Not silly at all!

  Any input pin should be "pulled" high or low so it is in a known state, if left "floating" i.e. no connection, it may wander between states.

In low power modes, this can cause excessive current to flow when not wanted, in normal apps, may cause unexpected behavior. 

Buttons connected to input pins, normally from pin to gnd, need a pull up so when the button is open a high is assured.

if button is connect from pin to VCC, then a pull down (resistor) is needed to assure a low is asserted when button is open.

Since AVR's have built in pull ups, normally, buttons are to gnd, since no external resistors are needed.

hope that helps.

 

Jim

 

Mission: Improving the readiness of hams world wide : flinthillsradioinc.com

Interests: Ham Radio, Solar power, futures & currency trading - whats yours?

 

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

Thansks!

Okay so the INPUTS can be set as PULL UP.
Can I also set the INPUT to PULL DOWN in the code? Or do I have to do that with an extern resistor?

 

Andi

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

Hi again.

 

I tried with two pull down resistors and it worked perfectly ;-) thanks a lotyes

But is it possible to a intern pull down resistance(so you don't need separate resistors)?

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

Not on the m2560, nor any (?) of the mega or tiny devices.  Some (all?) Xmega devices have both pull-up and pull-down.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

Mega's and tiny's only have pull ups, xmegas may have both, I'm not sure.

 

Most of us use the internal pull ups with buttons connected to gnd. 

 

Jim

 

 

Mission: Improving the readiness of hams world wide : flinthillsradioinc.com

Interests: Ham Radio, Solar power, futures & currency trading - whats yours?