I am trying to program an ATtiny13A to count the number of button presses and light up the corresponding LED.
I.E. IF the button (PB4) is pressed once, the button in PB1 lights up. The second time it is pressed LED2 (PB2) lights up.
I am new to Programming in C, I read a lot about button debouncing but I am not sure if I am using it correctly.
Here is my code:
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRB = 0b00001111; //initial i/o port settings
PORTB = 0b00111110; //initial h/l port settings according to PB-
void LEDcount (int led)
int Pressed = 0;
int Pressed_Confide nce_Level = 0; //Measure button press confidence
int Released_Confid ence_Level = 0; //Measure button release confidence
while (1) //infinite loop
{
if (bit_is_clear(P INB, 4)) //if PB4 (button) is pressed
{
Pressed_Confide nce_Level ++; //Increase Pressed confidence
Released_Confid ence_Level = 0; //Reset released button confidence since there is a button press
if (Pressed_Confid ence_Level >1) //Indicator of good button press
{
if (Pressed == 0)
{
PORTB = 0b00110001;
LEDcount(0);
}
Pressed_Confide nce_Level = 0;
}
}
else
{
Released_Confid ence_Level ++; //This works just like the pressed
Pressed_Confide nce_Level = 0; //Reset pressed button confidence since the button is released
if (Released_Confi dence_Level >100)
{
PORTB = 0b00111110; //return all ports to their original output
Pressed = 0;
Released_Confid ence_Level = 0;
}
}
}
}
void LEDcount (int led)
{
led=led+1;
if led=1{
PORTB=0b0011110 0;
}
if led=2{
PORTB=0b0011101 0;
}
if led=3{
PORTB=0b0011011 0;
}
}
I.E. IF the button (PB4) is pressed once, the button in PB1 lights up. The second time it is pressed LED2 (PB2) lights up.
I am new to Programming in C, I read a lot about button debouncing but I am not sure if I am using it correctly.
Here is my code:
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRB = 0b00001111; //initial i/o port settings
PORTB = 0b00111110; //initial h/l port settings according to PB-
void LEDcount (int led)
int Pressed = 0;
int Pressed_Confide nce_Level = 0; //Measure button press confidence
int Released_Confid ence_Level = 0; //Measure button release confidence
while (1) //infinite loop
{
if (bit_is_clear(P INB, 4)) //if PB4 (button) is pressed
{
Pressed_Confide nce_Level ++; //Increase Pressed confidence
Released_Confid ence_Level = 0; //Reset released button confidence since there is a button press
if (Pressed_Confid ence_Level >1) //Indicator of good button press
{
if (Pressed == 0)
{
PORTB = 0b00110001;
LEDcount(0);
}
Pressed_Confide nce_Level = 0;
}
}
else
{
Released_Confid ence_Level ++; //This works just like the pressed
Pressed_Confide nce_Level = 0; //Reset pressed button confidence since the button is released
if (Released_Confi dence_Level >100)
{
PORTB = 0b00111110; //return all ports to their original output
Pressed = 0;
Released_Confid ence_Level = 0;
}
}
}
}
void LEDcount (int led)
{
led=led+1;
if led=1{
PORTB=0b0011110 0;
}
if led=2{
PORTB=0b0011101 0;
}
if led=3{
PORTB=0b0011011 0;
}
}
Comment