avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] Simple but wrong code


From: dfx
Subject: Re: [avr-gcc-list] Simple but wrong code
Date: Wed, 15 Jan 2014 10:20:37 +0100
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0

I would like to thank all those who, in various ways, have helped me to solve my problem.

The changes, small but significant, were the abolition of reti () and the use of PIND instead of PORTD for the reading the port.

thanks
Domenico Formenton
Tel. 06 92731757
Fax. 178 2250847
Cell. 333 7075929
Il 15/01/2014 02:18, Pink Boy ha scritto:
dfx see notes below.


// #define KEY_FUNC_SEL PIND4 // --> first key (Port D pin 4)
// in WinAvr PIND4 is a bit position and is equal to 4

#define KEY_FUNC_SEL (1<<PIND4)   // resolves to 0x10


ISR(PCINT3_vect) { // FUNCTION KEY

  uint8_t i;

  // i = PORTD;
  // PORTD is the output register for PORTD. To read the port pin state you need to read PIND

  i = PIND;

  if ((i & FUNC_SEL_KEY) > 0) { // Test a key
    LED_PORT |= (1 << PORTB0);
    _delay_ms(100);
    LED_PORT &= ~(1 << PORTB0);
    _delay_ms(300);
  }

  // reti();  <- this will mess up the stack because
  // the compiler handles restoring registers and the return from interrupt for you.
}

Matt



From: dfx <address@hidden>
To: address@hidden
Sent: Tuesday, January 14, 2014 8:54 AM
Subject: [avr-gcc-list] Simple but wrong code

Please consider this code:

#define KEY_PORT PORTD
#define KEY_DDR DDRD
#define KEY_FUNC_SEL PIND4 // --> first key (Port D pin 4)
....
#define LED_PORT PORTB // Test led, to see action on keypress
....

#define FUNC_SEL_KEY 0x10 // First function --> first key (Port D pin 4)
....

int main() {
  init();

  while (1) {
    // waits indefinitely for keypress
  }
  return (EXIT_SUCCESS); // Never reached
}

void init() {
  cli();
  LED_DDR |= (1 << DDB0); // Led out Port B pin 0

  KEY_PORT |= (1 << PORTD4) ; // pull-up resistor
  PCICR |= (1 << PCIE3); // Enable interrupts on PORTD (PCINT31:24)

  PCMSK0 = 0X00; // Disable unnecessary
  PCMSK1 = 0X00;
  PCMSK2 = 0X00;
  PCMSK3 |= (1 << PCINT28); // Enable  key
  sei();
}

ISR(PCINT3_vect) { // FUNCTION KEY
  uint8_t i;

  i = PORTD;
  if ((i & FUNC_SEL_KEY) > 0) { // Test a key
    LED_PORT |= (1 << PORTB0);
    _delay_ms(100);
    LED_PORT &= ~(1 << PORTB0);
    _delay_ms(300);
  }
  reti();
}


ISR(BADISR_vect) {
  LED_PORT |= (1 << PORTB0);
  _delay_ms(200);
  LED_PORT &= ~(1 << PORTB0);
  _delay_ms(200);
  LED_PORT |= (1 << PORTB0);
  _delay_ms(200);
  LED_PORT &= ~(1 << PORTB0);
  _delay_ms(200);
  LED_PORT |= (1 << PORTB0);
  _delay_ms(200);
  LED_PORT &= ~(1 << PORTB0);
  _delay_ms(200);
  reti();
}

the button has a capacitor 1uF for debouncing (together with the pull-up
from 10 kohm)

The result is the following:

when I press the button,  are generated two flashes (instead of one),
  and when I release the button  are generated two more flashes.

Similarly, If I comment out the routine (ISR PCINT3_vect),
  the error routine (ISR BADISR_vect) generates double of the expected
flashes.

Can anyone help me to understand the problem?

Thank you very much.


--
Domenico



---
Questa e-mail è priva di virus e malware perché è attiva la protezione avast! Antivirus.
http://www.avast.com
_______________________________________________
AVR-GCC-list mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/avr-gcc-list






Questa e-mail è priva di virus e malware perché è attiva la protezione avast! Antivirus .


Attachment: dfx.vcf
Description: Vcard


reply via email to

[Prev in Thread] Current Thread [Next in Thread]