auto_mitch
Full Member level 3
I am trying to read a button by debouncing. I am using cvavr and i have configured the interrupt service routine of timer0. the problem is that i cannot set and reset the output LED1. And how can i read more than one buttons?
thanx
thanx
Code:
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x18 ;PORTA
#endasm
#include <mega8515.h>
#include <lcd.h>
#include<delay.h>
#define LEFT PINC.2
#define UP PINC.3
#define DOWN PINC.4
#define RIGHT PINC.5
#define ENTER PINC.6
#define LED1 PORTB.0
#define LED2 PORTB.1
int timecounter=0,flag1;
bit newstate, oldstate, btnstate;
// Timer 0 overflow ISR.
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Reload for 320 ticks.
TCNT0=6;
if(PINC.2) timecounter++;
else timecounter=0;
btnstate=0;
if(timecounter>10)
btnstate=1;
}
void main(void)
{
// Set pinc.2 to pin.6 as input
PORTC=0xFF;
DDRC=0x83;
// Port B Initialization.
DDRB=0xFF;
PORTB=0x00;
//Timer0 Initialization.
//Set clock/1024 as counter input
TCCR0= 0x05;
//Start timer with 0 in timer.
TCNT0=0;
//Timer interrupt initialization
// Unmask Timer 0 overflow interrupt
TIMSK=0x02;
// LCD Initialization.
lcd_init(16);
// Global enable interrupts
#asm ("sei")
while (1)
{
if(btnstate) flag1=1;
else flag1=0;
if(flag1) LED1=1;
else LED1=0;
};
}