#include <avr/io.h>
#include <util/delay.h>
#define LCD_RS (1 << PB0)
#define LCD_EN (1 << PB1)
#define DIP_SWITCH_MASK 0xFF
bool dipSwitch[8];
bool buttonPressed = false;
void LCD_command(uint8_t cmd) {
PORTB &= ~LCD_RS;
PORTD = cmd & 0xF0;
PORTB |= LCD_EN;
_delay_us(1);
PORTB &= ~LCD_EN;
_delay_us(100);
PORTD = (cmd << 4) & 0xF0;
PORTB |= LCD_EN;
_delay_us(1);
PORTB &= ~LCD_EN;
_delay_us(100);
}
void LCD_init() {
DDRB |= (LCD_RS | LCD_EN);
DDRD = 0xFF;
_delay_ms(15);
LCD_command(0x02);
LCD_command(0x28);
LCD_command(0x0C);
LCD_command(0x06);
LCD_command(0x01);
}
void readDipSwitch() {
for (int i = 0; i < 8; ++i) {
dipSwitch[i] = (PIND & (1 << i));
}
}
int main(void) {
DDRD = 0x00; // Set DIP switch pins as input
PORTD = 0xFF; // Enable internal pull-up resistors
DDRB &= ~(1 << PB2); // Set button pin as input
PORTB |= (1 << PB2); // Enable internal pull-up resistor for the button
LCD_init();
while (1) {
readDipSwitch();
buttonPressed = !(PINB & (1 << PB2));
if (buttonPressed && (dipSwitch[0] || dipSwitch[1] || dipSwitch[2] || dipSwitch[3] ||
dipSwitch[4] || dipSwitch[5] || dipSwitch[6] || dipSwitch[7])) {
LCD_command(0x01); // Clear display
LCD_command(0x80); // Set cursor to line 1
LCD_command('H');
LCD_command('l');
LCD_command('a');
LCD_command('s');
LCD_command('o');
LCD_command('v');
LCD_command('a');
LCD_command('n');
LCD_command('i');
LCD_command('e');
LCD_command(' ');
LCD_command('p');
LCD_command('r');
LCD_command('e');
LCD_command('s');
LCD_command('l');
LCD_command('o');
}
}
return 0;
}