Button Debounce Program of ATmega8

Status
Not open for further replies.

imranahmed

Advanced Member level 3
Joined
Dec 4, 2011
Messages
817
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
6,492
Please let me know about button debounce program for ATmega8,

I wrote a program but 1 sec delay is not achieved.Can anyone tell me any mistake in the given below code please?


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<avr/io.h>
#include<util/delay.h>
#include <avr/interrupt.h>
#define Red 240  
#define Yellow 232  
 
unsigned char button_timer;
unsigned char button_pressed;
 
 
 
//timer 1 overflow ISR
ISR (TIMER1_COMPA_vect){
 
 
    if (button_timer != 0){
        button_timer--;
        }
        
}
 
 
 
void LED(void)
{
if(button_pressed==Red)
{
PORTB=0xFF;
_delay_ms(500);
PORTB=0x00;
_delay_ms(500);
}
 
else if(button_pressed==Yellow)
{
PORTB=0xFF;
_delay_ms(250);
PORTB=0x00;
_delay_ms(250);
}
}
 
 
 
int main(void)
{
    
    DDRD=0x07;
    DDRB=0xff;
    PORTB=0x00;
    button_pressed = 0;
    button_timer = 0;
    // set up timer 0     
    OCR1A = 7800;
 
    TCCR1B |= (1 << WGM12);
    // Mode 4, CTC on OCR1A.
 
    TIMSK |= (1 << OCIE1A);
    //Set interrupt on compare match.
 
    TCCR1B |= (1 << CS12) | (1 << CS10);
    // set prescaler to 1024 and start the timer.
    sei();
    
    while(1)
    {
 
    if((button_timer == 0) && (PIND != 248))
            {
            button_pressed = PIND;
            button_timer = 0;
            }
    else 
            button_pressed = 248;  
            LED();
    }
    return 0;
}

 
Last edited by a moderator:

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…