Skip to content

Instantly share code, notes, and snippets.

@JoshAshby
Created December 9, 2010 23:56
Show Gist options
  • Save JoshAshby/735540 to your computer and use it in GitHub Desktop.
Save JoshAshby/735540 to your computer and use it in GitHub Desktop.
Fake main.c file for a school report, button processing code
//-------------------------------------------
/*
main.c
2010 - Josh Ashby
[email protected]
http://joshashby.com
http://github.com/JoshAshby
freenode/#linuxandsci - JoshAshby
*/
//-------------------------------------------
#include "global.h" //contains all the variables which are public, ie, open to all of the code to use, which is all of the variables here
int main(void) { //Main loop, runs once but can have an infinit loop in it
DDRD |= (0<<3)
| (0<<4); //setup the button pins as inputs
init_button_timer0(); //start the timer that takes care of the interrupt which
//holds the counter for button debouncing
while(1) { //infinit loop that doesn't stop running. (always true since 1 is always 1
check_buttons(); //debounces the buttons with the aid of the timer0 interrupt
buttons(); //if a buttons been pressed, code inside of here will run, if not
//it's skiped over. simply cleans up this part of the code
};
return 0; //never reached since 1 is always true
}
void check_buttons(void) {
/*
This little bit of code takes care of debouncing the button, with the aid of the
Timer0 ISR which keeps track of how stable the button is. If the button is stable
then this bit of code will change it's matching variable which indicates the state
for the rest of the code. If the button isn't stable then it doesn't do anything.
*/
//button one
if (count[0] > 5) { //if the buttons stable, set the array for button 1 as stable
//and reset the counter if the buttons been released
if ((PIND & button_one) == 0) {
count[0] = 0;
}
button[0] = 1;
}
if (count[0] > 250) {
if ((PIND & button_one) == 0) {
count[0] = 0;
}
button[0] = 2;
}
if ((PIND & button_one) == 0) { //reset the counter as soon as button one is released
button[0] = 0;
}
//button two
if (count[1] > 5) {//if the buttons stable, set the array for button 2 as stable
//and reset the counter if the buttons been released
if ((PIND & button_two) == 0) {
count[1] = 0;
}
button[1] = 1;
}
if (count[1] > 250) {
if ((PIND & button_two) == 0) {
count[1] = 0;
}
button[1] = 2;
}
if ((PIND & button_two) == 0) {//reset the counter as soon as button two is released
button[1] = 0;
}
}
void buttons(void) {
/*
This little function holds all of the handlers for the buttons, ie: what to do when
a button has been pressed.
*/
if (button[0] == 1) { //If the first button is pulled high then
//turn pin 1 on port B on
//out('D',stat_led1,!debug);
} else { //if either are off, turn pin1 port B off
out('D',stat_led1,debug);
}
if (button[1] == 1) { //If the second button is pulled high then
//turn pin 2 on port B on
out('D',stat_led2,!debug);
} else { //if either are off, turn pin1 port B off
out('D',stat_led2,debug);
}
}
ISR(TIMER0_OVF_vect) {
/*
Timer0 overflow interrupt servic routine. Whats in here is a very fancy little
for each button which will increase if the button is stable in it's state,
everytime this is ran, or will reset the timer if the button isn't stable, ie
bouncing, which is what we don't want.
*/
//figure out what pins been changed, and take the correct action
if ((PIND & button_one)) {
//simply increases or resets a counter, the main code then looks at the counter
//and determines if the buttons been pressed, or held down
count[0]++; //if buttton_ones been pressed, increase the count, as long as
//the button continues to stay the same
} else {
count[0] = 0;
//if the button changes state, reset the count
}
if ((PIND & button_two)) {
//simply increases or resets a counter, the main code then looks at the counter
//and determines if the buttons been pressed, or held down
count[1]++; //if buttton_ones been pressed, increase the count, as long as
//the button continues to stay the same
} else {
count[1] = 0;
//if the button changes state, reset the count
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment