Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Uses bitmath to assess an 8 bit register of switches and notes any recently-depressed switches to toggle LED lights on a separate register of 8 bits...
/*
* toggle_led_bitmath.c
* Description: Uses bitmath to assess an 8 bit register of switches
* and notes any recently-depressed switches to toggle
* LED lights on a separate register of 8 bits...
*/
#define F_CPU 16000000UL //Define the speed the clock is running at. Used for the delay.h functions
#include <avr/io.h> //Include the headers that handles Input/Output function for AVR chips
#include <util/delay.h> //Include the headers that allow for a delay function
//Prototypes
void Delay_ms(int cnt);
void init_io(void);
//Variables
int last_scan = 0;
//Functions
void Delay_ms(int cnt)
{
while(cnt-->0) _delay_ms(1);
}
void init_io(void)
{
DDRD = 0x00; // Make port D all inputs
DDRB = 0xFF; // Make port B all outputs
PORTB = 0xFF; // Default port B outputs to OFF (pull-up resistors dictate that 1 is off and 0 is on...)
}
void f_switch_led(void)
{
}
//Main Function
int main(void)
{
init_io(); //Set up the pins that control the LEDs
for (;;) //Loop forever
{
int current_scan = PIND;
int current_changes_high_to_low = last_scan & ~current_scan; // Which buttons have recently been released?
PORTB ^= current_changes_high_to_low; // Toggle the LED state of the buttons that have recently been released.
last_scan = current_scan; // Retain the current button scan state for comparing next loop...
Delay_ms(40); // Switch contact debounce
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.