/*
Filename: switchStates
Author: George Howard
Date: 20/02/2024
Description: This is a program that displays a specific pattern of LED's based on switch inputs.
*/
#include <avr/io.h>
#include <util/delay.h>
#define mask1 = 0b10000011
int main(void) {
unsigned char ledsOut = 0x00;
unsigned char switches = 0x00;
unsigned char masked = 0x00;
DDRC = 0x00; //Port C pins configured as input
DDRA = 0xFF; //Port A pins configured as output
PORTC = 0xFF; //Enabling the pull-up resistors
while(1) {
switches = PINC; //read from the Port C switches
masked = switches & 0b10000011;
switch (masked) {
case 0b00000001:
ledsOut = 0xAA;
break;
case 0b00000010:
ledsOut = 0x55;
break;
case 0b00000011:
ledsOut = 0x00;
break;
case 0b10000000:
ledsOut = switches;
break;
case 0b10000001:
ledsOut = switches;
break;
case 0b10000010:
ledsOut = switches;
break;
case 0b10000011:
ledsOut = switches;
break;
default:
ledsOut = 0x00;
break;
}
PORTA = ledsOut; //display ledsOut to Port A
}
}
/* Alternative solutiton not utilising masking and switch statements. I think it's a bit cleaner :)
if ((switches & 0x80) == 0x00) {
if ((switches & 0x03) == 0x03) {
ledsOut = 0x00;
}
else if ((switches & 0x02) == 0x02) {
ledsOut = 0x55;
}
else if ((switches & 0x01) == 0x01) {
ledsOut = 0xAA;
}
else {
ledsOut = 0x00;
}
}
else if ((switches & 0x80) == 0x80) {
ledsOut = switches;
}
else {
ledsOut = 0x00;
}
*/