attiny2313 button handler [need help]

Started by "Marojahan M.T. Sigiro" March 9, 2009
hi all,,

i have a small project using attiny2313, i'm trying to handle two buttons
using pin change interrupt. the problem i got that, sometime it gives a good
output by send some byte using uart communication, but sometime its not
working. i've read about debounce handling (i'm not sure if its correct) but
i dont know it mutch yet. This is my first time make a program to handle
external interrupt and i really confuse right know. hope you can help me
here..

i'm not sure about the schematic yet (attached) maybe you can give some
suggestion, and the code of course.
i'm using avr- gcc, and maybe my code little bit messy, sorry for that..

global.h
*Code:*

#ifndef GLOBAL_H
#define GLOBAL_H
// global AVRLIB defines
#include "avrlibdefs.h"
// global AVRLIB types definitions
#include "avrlibtypes.h"

#define F_CPU 8000000
#define BUTTON_PORT PORTB
#define NEXT_DDR DDRB
#define NEXT_PORT PORTB
#define NEXT_BTN_PIN PINB3
#define NEXT_LED_PIN PINB1

#define RECALL_DDR DDRB
#define RECALL_PORT PORTB
#define RECALL_BTN_PIN PINB2
//default internal oscilliator
#define RECALL_LED_PIN PINB0

#define NEXT_TOKEN 0x01
#define RECALL_TOKEN 0x02

#define UART_RX_BUFFER_SIZE 0x0010
#define UART_TX_BUFFER_SIZE 0x0010

#define CYCLES_PER_US ((F_CPU+500000)/1000000) // cpu cycles per
microsecond

#endif
*Code:*

#include "global.h"
#include
#include
#include
#include

#ifndef BYTE
#define BYTE unsigned char
#endif

BYTE recallSENT;
BYTE nextSENT;
SIGNAL(SIG_USART0_RX)
{
BYTE data;

data = UDR; // Store received byte
//sendByte(data);
}
SIGNAL(SIG_PCINT)
{
if((PINB & 0x0F) == 0x0B){
sendToken(RECALL_TOKEN);
}else if ((PINB & 0x0F) == 0x07){
sendToken(NEXT_TOKEN);
}
delay_ms(50);
}

}
void initmyUART(){
UBRRH = 0;
UBRRL = 25;
//baud rate 2400

UCSRB = _BV(RXEN) | _BV(TXEN) | _BV(RXCIE); // Rx Complete Interrupt &
Enable Rx/Tx
UCSRC = _BV(UCSZ1) | _BV(UCSZ0); // 8N1

sei(); // Enable global interrupts
}
void sendByte(BYTE c){
while(!(UCSRA & _BV(UDRE))); // Wait for empty transmit buffer
UDR = c;
}

void inituC(){
//init PIN/PORT/DDR
NEXT_DDR &= ~_BV(NEXT_BTN_PIN); //set button pin as input

NEXT_DDR |= _BV(NEXT_LED_PIN);

BUTTON_PORT |= (_BV(NEXT_BTN_PIN) | _BV(NEXT_LED_PIN)); //enable pull-up
resistor

RECALL_DDR &= ~_BV(RECALL_BTN_PIN);
//RECALL_DDR &= ~_BV(RECALL_LED_PIN);
RECALL_DDR |= _BV(RECALL_LED_PIN);

RECALL_PORT |= (_BV(RECALL_BTN_PIN) | _BV(RECALL_LED_PIN));

//enable pin change interrupt
GIMSK = _BV(PCIE); // Pin Change Interrupt Enable
PCMSK = 0xFF;
sei();
}

void sendToken(BYTE TOKEN){
sendByte(TOKEN);
}

void delay_ms(uint16_t ms) {
while ( ms )
{
_delay_ms(1);
ms--;
}
}

void delay_second(BYTE sec){
BYTE i;
for(i = 0; i < sec; i++){
delay_ms(1000);
}
}
int main(void)
{
// Initiate UART

BYTE bStat;

initmyUART();
inituC();
sendByte(0x00);

while(1);

return 1;
}
Thanks to you all

--
---
hey, just because I don't care doesn't mean I'm not listening -- homer
simpson
--
Marojahan M.T. Sigiro
m...@gmail.com
GTalk : marojahan
Y! : ojak_s
http://ojakaja.com


Hello

In avr "the interrupt pin" it must be configured as an output pin.

Edson
----- Original Message -----
From: Marojahan M.T. Sigiro
To: a...
Sent: Monday, March 09, 2009 6:20 PM
Subject: [AVR club] attiny2313 button handler [need help]
hi all,,

i have a small project using attiny2313, i'm trying to handle two buttons
using pin change interrupt. the problem i got that, sometime it gives a good
output by send some byte using uart communication, but sometime its not
working. i've read about debounce handling (i'm not sure if its correct) but
i dont know it mutch yet. This is my first time make a program to handle
external interrupt and i really confuse right know. hope you can help me
here..

i'm not sure about the schematic yet (attached) maybe you can give some
suggestion, and the code of course.

i'm using avr- gcc, and maybe my code little bit messy, sorry for that..

global.h
*Code:*

#ifndef GLOBAL_H
#define GLOBAL_H
// global AVRLIB defines
#include "avrlibdefs.h"
// global AVRLIB types definitions
#include "avrlibtypes.h"

#define F_CPU 8000000
#define BUTTON_PORT PORTB
#define NEXT_DDR DDRB
#define NEXT_PORT PORTB
#define NEXT_BTN_PIN PINB3
#define NEXT_LED_PIN PINB1

#define RECALL_DDR DDRB
#define RECALL_PORT PORTB
#define RECALL_BTN_PIN PINB2
//default internal oscilliator
#define RECALL_LED_PIN PINB0

#define NEXT_TOKEN 0x01
#define RECALL_TOKEN 0x02

#define UART_RX_BUFFER_SIZE 0x0010
#define UART_TX_BUFFER_SIZE 0x0010

#define CYCLES_PER_US ((F_CPU+500000)/1000000) // cpu cycles per
microsecond

#endif

*Code:*

#include "global.h"
#include
#include
#include
#include

#ifndef BYTE
#define BYTE unsigned char
#endif

BYTE recallSENT;
BYTE nextSENT;

SIGNAL(SIG_USART0_RX)
{
BYTE data;

data = UDR; // Store received byte
//sendByte(data);
}

SIGNAL(SIG_PCINT)
{
if((PINB & 0x0F) == 0x0B){
sendToken(RECALL_TOKEN);
}else if ((PINB & 0x0F) == 0x07){
sendToken(NEXT_TOKEN);
}
delay_ms(50);
}

}
void initmyUART(){
UBRRH = 0;
UBRRL = 25;
//baud rate 2400

UCSRB = _BV(RXEN) | _BV(TXEN) | _BV(RXCIE); // Rx Complete Interrupt &
Enable Rx/Tx
UCSRC = _BV(UCSZ1) | _BV(UCSZ0); // 8N1

sei(); // Enable global interrupts
}
void sendByte(BYTE c){
while(!(UCSRA & _BV(UDRE))); // Wait for empty transmit buffer
UDR = c;
}

void inituC(){
//init PIN/PORT/DDR
NEXT_DDR &= ~_BV(NEXT_BTN_PIN); //set button pin as input

NEXT_DDR |= _BV(NEXT_LED_PIN);

BUTTON_PORT |= (_BV(NEXT_BTN_PIN) | _BV(NEXT_LED_PIN)); //enable pull-up
resistor

RECALL_DDR &= ~_BV(RECALL_BTN_PIN);
//RECALL_DDR &= ~_BV(RECALL_LED_PIN);
RECALL_DDR |= _BV(RECALL_LED_PIN);

RECALL_PORT |= (_BV(RECALL_BTN_PIN) | _BV(RECALL_LED_PIN));

//enable pin change interrupt
GIMSK = _BV(PCIE); // Pin Change Interrupt Enable
PCMSK = 0xFF;
sei();
}

void sendToken(BYTE TOKEN){
sendByte(TOKEN);
}

void delay_ms(uint16_t ms) {
while ( ms )
{
_delay_ms(1);
ms--;
}
}

void delay_second(BYTE sec){
BYTE i;
for(i = 0; i < sec; i++){
delay_ms(1000);
}
}

int main(void)
{
// Initiate UART

BYTE bStat;

initmyUART();
inituC();
sendByte(0x00);

while(1);

return 1;
}

Thanks to you all

--
---
hey, just because I don't care doesn't mean I'm not listening -- homer
simpson
--
Marojahan M.T. Sigiro
m...@gmail.com
GTalk : marojahan
Y! : ojak_s
http://ojakaja.com





are you sure about that? why?

On Tue, Mar 10, 2009 at 6:49 PM, Elbest Engenharia wrote:

> Hello
>
> In avr "the interrupt pin" it must be configured as an output pin.
>
> Edson
> ----- Original Message -----
> From: Marojahan M.T. Sigiro
> To: a...
> Sent: Monday, March 09, 2009 6:20 PM
> Subject: [AVR club] attiny2313 button handler [need help]
>
> hi all,,
>
> i have a small project using attiny2313, i'm trying to handle two buttons
> using pin change interrupt. the problem i got that, sometime it gives a
> good
> output by send some byte using uart communication, but sometime its not
> working. i've read about debounce handling (i'm not sure if its correct)
> but
> i dont know it mutch yet. This is my first time make a program to handle
> external interrupt and i really confuse right know. hope you can help me
> here..
>
> i'm not sure about the schematic yet (attached) maybe you can give some
> suggestion, and the code of course.
>
> i'm using avr- gcc, and maybe my code little bit messy, sorry for that..
>
> global.h
> *Code:*
>
> #ifndef GLOBAL_H
> #define GLOBAL_H
> // global AVRLIB defines
> #include "avrlibdefs.h"
> // global AVRLIB types definitions
> #include "avrlibtypes.h"
>
> #define F_CPU 8000000
> #define BUTTON_PORT PORTB
> #define NEXT_DDR DDRB
> #define NEXT_PORT PORTB
> #define NEXT_BTN_PIN PINB3
> #define NEXT_LED_PIN PINB1
>
> #define RECALL_DDR DDRB
> #define RECALL_PORT PORTB
> #define RECALL_BTN_PIN PINB2
> //default internal oscilliator
> #define RECALL_LED_PIN PINB0
>
> #define NEXT_TOKEN 0x01
> #define RECALL_TOKEN 0x02
>
> #define UART_RX_BUFFER_SIZE 0x0010
> #define UART_TX_BUFFER_SIZE 0x0010
>
> #define CYCLES_PER_US ((F_CPU+500000)/1000000) // cpu cycles per
> microsecond
>
> #endif
>
> *Code:*
>
> #include "global.h"
> #include
> #include
> #include
> #include #ifndef BYTE
> #define BYTE unsigned char
> #endif
>
> BYTE recallSENT;
> BYTE nextSENT;
>
> SIGNAL(SIG_USART0_RX)
> {
> BYTE data;
>
> data = UDR; // Store received byte
> //sendByte(data);
> }
>
> SIGNAL(SIG_PCINT)
> {
> if((PINB & 0x0F) == 0x0B){
> sendToken(RECALL_TOKEN);
> }else if ((PINB & 0x0F) == 0x07){
> sendToken(NEXT_TOKEN);
> }
> delay_ms(50);
> }
>
> }
> void initmyUART(){
> UBRRH = 0;
> UBRRL = 25;
> //baud rate 2400
>
> UCSRB = _BV(RXEN) | _BV(TXEN) | _BV(RXCIE); // Rx Complete Interrupt &
> Enable Rx/Tx
> UCSRC = _BV(UCSZ1) | _BV(UCSZ0); // 8N1
>
> sei(); // Enable global interrupts
> }
> void sendByte(BYTE c){
> while(!(UCSRA & _BV(UDRE))); // Wait for empty transmit buffer
> UDR = c;
> }
>
> void inituC(){
> //init PIN/PORT/DDR
> NEXT_DDR &= ~_BV(NEXT_BTN_PIN); //set button pin as input
>
> NEXT_DDR |= _BV(NEXT_LED_PIN);
>
> BUTTON_PORT |= (_BV(NEXT_BTN_PIN) | _BV(NEXT_LED_PIN)); //enable pull-up
> resistor
>
> RECALL_DDR &= ~_BV(RECALL_BTN_PIN);
> //RECALL_DDR &= ~_BV(RECALL_LED_PIN);
> RECALL_DDR |= _BV(RECALL_LED_PIN);
>
> RECALL_PORT |= (_BV(RECALL_BTN_PIN) | _BV(RECALL_LED_PIN));
>
> //enable pin change interrupt
> GIMSK = _BV(PCIE); // Pin Change Interrupt Enable
> PCMSK = 0xFF;
> sei();
> }
>
> void sendToken(BYTE TOKEN){
> sendByte(TOKEN);
> }
>
> void delay_ms(uint16_t ms) {
> while ( ms )
> {
> _delay_ms(1);
> ms--;
> }
> }
>
> void delay_second(BYTE sec){
> BYTE i;
> for(i = 0; i < sec; i++){
> delay_ms(1000);
> }
> }
>
> int main(void)
> {
> // Initiate UART
>
> BYTE bStat;
>
> initmyUART();
> inituC();
> sendByte(0x00);
>
> while(1);
>
> return 1;
> }
>
> Thanks to you all
I've written button/keypad debounce routines for many different
microcontrollers. Generally what I do is assume I will be holding the button
down for more than 100-200 msec, so I read the button/key value, store its
value when the button first changes, read it again 100 msec later and
compare. If it doesn't change I wait another 50 msec, read it again and if
it's still pressed I accept it.

I'm sure there are a lot of ways to do it but I've been doing it that way
for at least 15 years.

Zack

On Tue, Mar 10, 2009 at 1:36 PM, Marojahan M.T. Sigiro
wrote:

> are you sure about that? why?
>
> On Tue, Mar 10, 2009 at 6:49 PM, Elbest Engenharia >wrote:
> > Hello
> >
> > In avr "the interrupt pin" it must be configured as an output pin.
> >
> > Edson
> >
> >
> > ----- Original Message -----
> > From: Marojahan M.T. Sigiro
> > To: a... > 40yahoogroups.com>
> > Sent: Monday, March 09, 2009 6:20 PM
> > Subject: [AVR club] attiny2313 button handler [need help]
> >
> > hi all,,
> >
> > i have a small project using attiny2313, i'm trying to handle two buttons
> > using pin change interrupt. the problem i got that, sometime it gives a
> > good
> > output by send some byte using uart communication, but sometime its not
> > working. i've read about debounce handling (i'm not sure if its correct)
> > but
> > i dont know it mutch yet. This is my first time make a program to handle
> > external interrupt and i really confuse right know. hope you can help me
> > here..
> >
> > i'm not sure about the schematic yet (attached) maybe you can give some
> > suggestion, and the code of course.
> >
> > i'm using avr- gcc, and maybe my code little bit messy, sorry for that..
> >
> > global.h
> > *Code:*
> >
> > #ifndef GLOBAL_H
> > #define GLOBAL_H
> > // global AVRLIB defines
> > #include "avrlibdefs.h"
> > // global AVRLIB types definitions
> > #include "avrlibtypes.h"
> >
> > #define F_CPU 8000000
> > #define BUTTON_PORT PORTB
> > #define NEXT_DDR DDRB
> > #define NEXT_PORT PORTB
> > #define NEXT_BTN_PIN PINB3
> > #define NEXT_LED_PIN PINB1
> >
> > #define RECALL_DDR DDRB
> > #define RECALL_PORT PORTB
> > #define RECALL_BTN_PIN PINB2
> > //default internal oscilliator
> > #define RECALL_LED_PIN PINB0
> >
> > #define NEXT_TOKEN 0x01
> > #define RECALL_TOKEN 0x02
> >
> > #define UART_RX_BUFFER_SIZE 0x0010
> > #define UART_TX_BUFFER_SIZE 0x0010
> >
> > #define CYCLES_PER_US ((F_CPU+500000)/1000000) // cpu cycles per
> > microsecond
> >
> > #endif
> >
> > *Code:*
> >
> > #include "global.h"
> > #include
> > #include
> > #include
> > #include
> >
> > #ifndef BYTE
> > #define BYTE unsigned char
> > #endif
> >
> > BYTE recallSENT;
> > BYTE nextSENT;
> >
> > SIGNAL(SIG_USART0_RX)
> > {
> > BYTE data;
> >
> > data = UDR; // Store received byte
> > //sendByte(data);
> > }
> >
> > SIGNAL(SIG_PCINT)
> > {
> > if((PINB & 0x0F) == 0x0B){
> > sendToken(RECALL_TOKEN);
> > }else if ((PINB & 0x0F) == 0x07){
> > sendToken(NEXT_TOKEN);
> > }
> > delay_ms(50);
> > }
> >
> > }
> > void initmyUART(){
> > UBRRH = 0;
> > UBRRL = 25;
> > //baud rate 2400
> >
> > UCSRB = _BV(RXEN) | _BV(TXEN) | _BV(RXCIE); // Rx Complete Interrupt &
> > Enable Rx/Tx
> > UCSRC = _BV(UCSZ1) | _BV(UCSZ0); // 8N1
> >
> > sei(); // Enable global interrupts
> > }
> > void sendByte(BYTE c){
> > while(!(UCSRA & _BV(UDRE))); // Wait for empty transmit buffer
> > UDR = c;
> > }
> >
> > void inituC(){
> > //init PIN/PORT/DDR
> > NEXT_DDR &= ~_BV(NEXT_BTN_PIN); //set button pin as input
> >
> > NEXT_DDR |= _BV(NEXT_LED_PIN);
> >
> > BUTTON_PORT |= (_BV(NEXT_BTN_PIN) | _BV(NEXT_LED_PIN)); //enable pull-up
> > resistor
> >
> > RECALL_DDR &= ~_BV(RECALL_BTN_PIN);
> > //RECALL_DDR &= ~_BV(RECALL_LED_PIN);
> > RECALL_DDR |= _BV(RECALL_LED_PIN);
> >
> > RECALL_PORT |= (_BV(RECALL_BTN_PIN) | _BV(RECALL_LED_PIN));
> >
> > //enable pin change interrupt
> > GIMSK = _BV(PCIE); // Pin Change Interrupt Enable
> > PCMSK = 0xFF;
> > sei();
> > }
> >
> > void sendToken(BYTE TOKEN){
> > sendByte(TOKEN);
> > }
> >
> > void delay_ms(uint16_t ms) {
> > while ( ms )
> > {
> > _delay_ms(1);
> > ms--;
> > }
> > }
> >
> > void delay_second(BYTE sec){
> > BYTE i;
> > for(i = 0; i < sec; i++){
> > delay_ms(1000);
> > }
> > }
> >
> > int main(void)
> > {
> > // Initiate UART
> >
> > BYTE bStat;
> >
> > initmyUART();
> > inituC();
> > sendByte(0x00);
> >
> > while(1);
> >
> > return 1;
> > }
> >
> > Thanks to you all
>


Hi,
In my experience for handle debounce key I using these function (I using WINAVR):
while (bit_is_clear(PINB,7)) {
//place your code here
}
if your button is connect to port B number 7 (for example)
or
if (bit_is_clear (PINB,7)) {
delay ( ); //put delay function for handle debounce key
//place your code here
}

________________________________
From: Marojahan M.T. Sigiro
To: a...
Sent: Wednesday, March 11, 2009 1:36:19 AM
Subject: Re: [AVR club] attiny2313 button handler [need help]
are you sure about that? why?

On Tue, Mar 10, 2009 at 6:49 PM, Elbest Engenharia wrote:

> Hello
>
> In avr "the interrupt pin" it must be configured as an output pin.
>
> Edson
> ----- Original Message -----
> From: Marojahan M.T. Sigiro
> To: avrclub@yahoogroups .com
> Sent: Monday, March 09, 2009 6:20 PM
> Subject: [AVR club] attiny2313 button handler [need help]
>
> hi all,,
>
> i have a small project using attiny2313, i'm trying to handle two buttons
> using pin change interrupt. the problem i got that, sometime it gives a
> good
> output by send some byte using uart communication, but sometime its not
> working. i've read about debounce handling (i'm not sure if its correct)
> but
> i dont know it mutch yet. This is my first time make a program to handle
> external interrupt and i really confuse right know. hope you can help me
> here..
>
> i'm not sure about the schematic yet (attached) maybe you can give some
> suggestion, and the code of course.
>
> i'm using avr- gcc, and maybe my code little bit messy, sorry for that..
>
> global.h
> *Code:*
>
> #ifndef GLOBAL_H
> #define GLOBAL_H
> // global AVRLIB defines
> #include "avrlibdefs. h"
> // global AVRLIB types definitions
> #include "avrlibtypes. h"
>
> #define F_CPU 8000000
> #define BUTTON_PORT PORTB
> #define NEXT_DDR DDRB
> #define NEXT_PORT PORTB
> #define NEXT_BTN_PIN PINB3
> #define NEXT_LED_PIN PINB1
>
> #define RECALL_DDR DDRB
> #define RECALL_PORT PORTB
> #define RECALL_BTN_PIN PINB2
> //default internal oscilliator
> #define RECALL_LED_PIN PINB0
>
> #define NEXT_TOKEN 0x01
> #define RECALL_TOKEN 0x02
>
> #define UART_RX_BUFFER_ SIZE 0x0010
> #define UART_TX_BUFFER_ SIZE 0x0010
>
> #define CYCLES_PER_US ((F_CPU+500000) /1000000) // cpu cycles per
> microsecond
>
> #endif
>
> *Code:*
>
> #include "global.h"
> #include
> #include
> #include
> #include #ifndef BYTE
> #define BYTE unsigned char
> #endif
>
> BYTE recallSENT;
> BYTE nextSENT;
>
> SIGNAL(SIG_USART0_ RX)
> {
> BYTE data;
>
> data = UDR; // Store received byte
> //sendByte(data) ;
> }
>
> SIGNAL(SIG_PCINT)
> {
> if((PINB & 0x0F) == 0x0B){
> sendToken(RECALL_ TOKEN);
> }else if ((PINB & 0x0F) == 0x07){
> sendToken(NEXT_ TOKEN);
> }
> delay_ms(50) ;
> }
>
> }
> void initmyUART() {
> UBRRH = 0;
> UBRRL = 25;
> //baud rate 2400
>
> UCSRB = _BV(RXEN) | _BV(TXEN) | _BV(RXCIE); // Rx Complete Interrupt &
> Enable Rx/Tx
> UCSRC = _BV(UCSZ1) | _BV(UCSZ0); // 8N1
>
> sei(); // Enable global interrupts
> }
> void sendByte(BYTE c){
> while(!(UCSRA & _BV(UDRE))); // Wait for empty transmit buffer
> UDR = c;
> }
>
> void inituC(){
> //init PIN/PORT/DDR
> NEXT_DDR &= ~_BV(NEXT_BTN_ PIN); //set button pin as input
>
> NEXT_DDR |= _BV(NEXT_LED_ PIN);
>
> BUTTON_PORT |= (_BV(NEXT_BTN_ PIN) | _BV(NEXT_LED_ PIN)); //enable pull-up
> resistor
>
> RECALL_DDR &= ~_BV(RECALL_ BTN_PIN);
> //RECALL_DDR &= ~_BV(RECALL_ LED_PIN);
> RECALL_DDR |= _BV(RECALL_LED_ PIN);
>
> RECALL_PORT |= (_BV(RECALL_ BTN_PIN) | _BV(RECALL_LED_ PIN));
>
> //enable pin change interrupt
> GIMSK = _BV(PCIE); // Pin Change Interrupt Enable
> PCMSK = 0xFF;
> sei();
> }
>
> void sendToken(BYTE TOKEN){
> sendByte(TOKEN) ;
> }
>
> void delay_ms(uint16_ t ms) {
> while ( ms )
> {
> _delay_ms(1) ;
> ms--;
> }
> }
>
> void delay_second( BYTE sec){
> BYTE i;
> for(i = 0; i < sec; i++){
> delay_ms(1000) ;
> }
> }
>
> int main(void)
> {
> // Initiate UART
>
> BYTE bStat;
>
> initmyUART() ;
> inituC();
> sendByte(0x00) ;
>
> while(1);
>
> return 1;
> }
>
> Thanks to you all



See the datasheet...

The External Interrupts are triggered by the INT1 and INT0 pins. Observe that, if

enabled, the interrupts will trigger even if the INT0/INT1 pins are configured as outputs.

This feature provides a way of generating a software interrupt. The External Interrupts

can be triggered by a falling or rising edge or a low level. This is set up as indicated in

the specification for the MCU Control Register (MCUCR). When the External Interrupt is

enabled and is configured as level-triggered, the interrupt will trigger as long as the pin

is held low.

Edson

----- Original Message -----
From: Marojahan M.T. Sigiro
To: a...
Sent: Tuesday, March 10, 2009 3:36 PM
Subject: Re: [AVR club] attiny2313 button handler [need help]
are you sure about that? why?

On Tue, Mar 10, 2009 at 6:49 PM, Elbest Engenharia wrote:

> Hello
>
> In avr "the interrupt pin" it must be configured as an output pin.
>
> Edson
>
>
> ----- Original Message -----
> From: Marojahan M.T. Sigiro
> To: a...
> Sent: Monday, March 09, 2009 6:20 PM
> Subject: [AVR club] attiny2313 button handler [need help]
>
> hi all,,
>
> i have a small project using attiny2313, i'm trying to handle two buttons
> using pin change interrupt. the problem i got that, sometime it gives a
> good
> output by send some byte using uart communication, but sometime its not
> working. i've read about debounce handling (i'm not sure if its correct)
> but
> i dont know it mutch yet. This is my first time make a program to handle
> external interrupt and i really confuse right know. hope you can help me
> here..
>
> i'm not sure about the schematic yet (attached) maybe you can give some
> suggestion, and the code of course.
>
> i'm using avr- gcc, and maybe my code little bit messy, sorry for that..
>
> global.h
> *Code:*
>
> #ifndef GLOBAL_H
> #define GLOBAL_H
> // global AVRLIB defines
> #include "avrlibdefs.h"
> // global AVRLIB types definitions
> #include "avrlibtypes.h"
>
> #define F_CPU 8000000
> #define BUTTON_PORT PORTB
> #define NEXT_DDR DDRB
> #define NEXT_PORT PORTB
> #define NEXT_BTN_PIN PINB3
> #define NEXT_LED_PIN PINB1
>
> #define RECALL_DDR DDRB
> #define RECALL_PORT PORTB
> #define RECALL_BTN_PIN PINB2
> //default internal oscilliator
> #define RECALL_LED_PIN PINB0
>
> #define NEXT_TOKEN 0x01
> #define RECALL_TOKEN 0x02
>
> #define UART_RX_BUFFER_SIZE 0x0010
> #define UART_TX_BUFFER_SIZE 0x0010
>
> #define CYCLES_PER_US ((F_CPU+500000)/1000000) // cpu cycles per
> microsecond
>
> #endif
>
> *Code:*
>
> #include "global.h"
> #include
> #include
> #include
> #include
>
> #ifndef BYTE
> #define BYTE unsigned char
> #endif
>
> BYTE recallSENT;
> BYTE nextSENT;
>
> SIGNAL(SIG_USART0_RX)
> {
> BYTE data;
>
> data = UDR; // Store received byte
> //sendByte(data);
> }
>
> SIGNAL(SIG_PCINT)
> {
> if((PINB & 0x0F) == 0x0B){
> sendToken(RECALL_TOKEN);
> }else if ((PINB & 0x0F) == 0x07){
> sendToken(NEXT_TOKEN);
> }
> delay_ms(50);
> }
>
> }
> void initmyUART(){
> UBRRH = 0;
> UBRRL = 25;
> //baud rate 2400
>
> UCSRB = _BV(RXEN) | _BV(TXEN) | _BV(RXCIE); // Rx Complete Interrupt &
> Enable Rx/Tx
> UCSRC = _BV(UCSZ1) | _BV(UCSZ0); // 8N1
>
> sei(); // Enable global interrupts
> }
> void sendByte(BYTE c){
> while(!(UCSRA & _BV(UDRE))); // Wait for empty transmit buffer
> UDR = c;
> }
>
> void inituC(){
> //init PIN/PORT/DDR
> NEXT_DDR &= ~_BV(NEXT_BTN_PIN); //set button pin as input
>
> NEXT_DDR |= _BV(NEXT_LED_PIN);
>
> BUTTON_PORT |= (_BV(NEXT_BTN_PIN) | _BV(NEXT_LED_PIN)); //enable pull-up
> resistor
>
> RECALL_DDR &= ~_BV(RECALL_BTN_PIN);
> //RECALL_DDR &= ~_BV(RECALL_LED_PIN);
> RECALL_DDR |= _BV(RECALL_LED_PIN);
>
> RECALL_PORT |= (_BV(RECALL_BTN_PIN) | _BV(RECALL_LED_PIN));
>
> //enable pin change interrupt
> GIMSK = _BV(PCIE); // Pin Change Interrupt Enable
> PCMSK = 0xFF;
> sei();
> }
>
> void sendToken(BYTE TOKEN){
> sendByte(TOKEN);
> }
>
> void delay_ms(uint16_t ms) {
> while ( ms )
> {
> _delay_ms(1);
> ms--;
> }
> }
>
> void delay_second(BYTE sec){
> BYTE i;
> for(i = 0; i < sec; i++){
> delay_ms(1000);
> }
> }
>
> int main(void)
> {
> // Initiate UART
>
> BYTE bStat;
>
> initmyUART();
> inituC();
> sendByte(0x00);
>
> while(1);
>
> return 1;
> }
>
> Thanks to you all