diff --git a/base/base.c b/base/base.c index 2e37f0a..870ee0b 100644 --- a/base/base.c +++ b/base/base.c @@ -9,3 +9,13 @@ uint8_t rng_sample(rng_t *state) { state->seed = (state->a * state->seed + state->c) % state->mod; return state->seed; } + +/* +void strobe_line(gpio_t *line, uint32_t us) { + set_line(line); + _delay_us(us); + clear_line(line); + _delay_us(us); +} +*/ + diff --git a/base/base.h b/base/base.h index 46ae82d..826cd17 100644 --- a/base/base.h +++ b/base/base.h @@ -1,8 +1,9 @@ -#include - #ifndef __BASE_H__ #define __BASE_H__ +#include +#include + #define LINE_OUT 1 #define LINE_IN 0 diff --git a/lcd/main.c b/lcd/main.c new file mode 100644 index 0000000..85bb654 --- /dev/null +++ b/lcd/main.c @@ -0,0 +1,100 @@ +#include +#include +#include +#include +#include + +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); } +} + diff --git a/shift_register/reg.c b/shift_register/reg.c index ba23d75..1b619d9 100644 --- a/shift_register/reg.c +++ b/shift_register/reg.c @@ -1,3 +1,4 @@ +#include #include #include @@ -11,15 +12,20 @@ void shift_register_initialize(shift_register_t *reg) { clear_line(®->latch_clock); } -void send_byte_lsb(shift_register_t *reg, uint8_t byte) { +void send_lsb(shift_register_t *reg, uint8_t byte) { for (uint8_t i = 0; i < 8; i++) { byte & _BV(i) ? set_line(®->output) : clear_line(®->output); strobe_line(®->shift_clock); } + clear_line(®->output); + strobe_line(®->latch_clock); } -void shift_out(shift_register_t *reg, uint8_t byte) { - send_byte_lsb(reg, byte); +void send_msb(shift_register_t *reg, uint8_t byte) { + for (int8_t i = 7; i >= 0; i--) { + byte & _BV(i) ? set_line(®->output) : clear_line(®->output); + strobe_line(®->shift_clock); + } clear_line(®->output); strobe_line(®->latch_clock); } diff --git a/shift_register/reg.h b/shift_register/reg.h index 1d92b34..bd5affa 100644 --- a/shift_register/reg.h +++ b/shift_register/reg.h @@ -10,6 +10,7 @@ typedef struct SHIFT_REGISTER { void shift_register_initialize(shift_register_t *reg); -void shift_out(shift_register_t *reg, uint8_t byte); +void send_lsb(shift_register_t *reg, uint8_t byte); +void send_msb(shift_register_t *reg, uint8_t byte); #endif