avr/lcd/main.c

101 lines
2.6 KiB
C
Raw Normal View History

2022-06-26 15:38:20 +00:00
#include <avr/io.h>
#include <util/delay.h>
#include <base.h>
#include <spi.h>
#include <reg.h>
typedef struct HD44780 {
shift_register_t reg;
gpio_t register_select;
gpio_t enable;
} hd44780_t;
void strobe_line(gpio_t *line) {
set_line(line);
_delay_ms(1);
clear_line(line);
_delay_ms(1);
}
void hd44780_init(hd44780_t *disp) {
spi_initialize(disp);
set_line_direction(&disp->register_select, LINE_OUT);
set_line_direction(&disp->enable, LINE_OUT);
send_msb(&disp->reg, 0b00111100);
}
void hd44780_clear(hd44780_t *disp) {
clear_line(&disp->register_select);
send_msb(&disp->reg, 0b00000001);
_delay_ms(1);
strobe_line(&disp->enable);
send_msb(&disp->reg, 0b00000010);
_delay_ms(1);
strobe_line(&disp->enable);
}
void hd44780_display(hd44780_t *disp) {
clear_line(&disp->register_select);
send_msb(&disp->reg, 0b00001110);
_delay_ms(1);
strobe_line(&disp->enable);
}
void hd44780_write_char(hd44780_t *disp, uint8_t bitcode) {
set_line(&disp->register_select);
send_msb(&disp->reg, bitcode);
strobe_line(&disp->enable);
}
void hd44780_write(hd44780_t *disp) {
set_line(&disp->register_select);
hd44780_write_char(disp, 0b01001000);
hd44780_write_char(disp, 0b01000101);
hd44780_write_char(disp, 0b01001100);
hd44780_write_char(disp, 0b01001100);
hd44780_write_char(disp, 0b01001111);
hd44780_write_char(disp, 0b00101100);
hd44780_write_char(disp, 0b00100000);
hd44780_write_char(disp, 0b01001101);
hd44780_write_char(disp, 0b01011001);
hd44780_write_char(disp, 0b00100000);
hd44780_write_char(disp, 0b01000010);
hd44780_write_char(disp, 0b01000001);
hd44780_write_char(disp, 0b01010100);
}
int main(void) {
hd44780_t display = {
.reg = {
.output = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 5 },
.latch_clock = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 6 },
.shift_clock = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 7 },
},
.register_select = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 1 },
.enable = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 0 },
};
gpio_t light = { .ddr = &DDRC, .port = &PORTC, .pin = &PINC, .addr = 7 };
set_line_direction(&light, LINE_OUT);
clear_line(&light);
hd44780_init(&display);
hd44780_clear(&display);
_delay_ms(500);
hd44780_write(&display);
hd44780_display(&display);
set_line(&light);
while(1) { _delay_ms(1); }
}