Prototype LCD control

This commit is contained in:
Savanni D'Gerinel 2022-06-26 11:38:20 -04:00
parent 0e27f675dd
commit 8837ec4cd2
5 changed files with 124 additions and 6 deletions

View File

@ -9,3 +9,13 @@ uint8_t rng_sample(rng_t *state) {
state->seed = (state->a * state->seed + state->c) % state->mod; state->seed = (state->a * state->seed + state->c) % state->mod;
return state->seed; return state->seed;
} }
/*
void strobe_line(gpio_t *line, uint32_t us) {
set_line(line);
_delay_us(us);
clear_line(line);
_delay_us(us);
}
*/

View File

@ -1,8 +1,9 @@
#include <avr/io.h>
#ifndef __BASE_H__ #ifndef __BASE_H__
#define __BASE_H__ #define __BASE_H__
#include <avr/io.h>
#include <avr/delay.h>
#define LINE_OUT 1 #define LINE_OUT 1
#define LINE_IN 0 #define LINE_IN 0

100
lcd/main.c Normal file
View File

@ -0,0 +1,100 @@
#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); }
}

View File

@ -1,3 +1,4 @@
#include <util/delay.h>
#include <base.h> #include <base.h>
#include <reg.h> #include <reg.h>
@ -11,15 +12,20 @@ void shift_register_initialize(shift_register_t *reg) {
clear_line(&reg->latch_clock); clear_line(&reg->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++) { for (uint8_t i = 0; i < 8; i++) {
byte & _BV(i) ? set_line(&reg->output) : clear_line(&reg->output); byte & _BV(i) ? set_line(&reg->output) : clear_line(&reg->output);
strobe_line(&reg->shift_clock); strobe_line(&reg->shift_clock);
} }
clear_line(&reg->output);
strobe_line(&reg->latch_clock);
} }
void shift_out(shift_register_t *reg, uint8_t byte) { void send_msb(shift_register_t *reg, uint8_t byte) {
send_byte_lsb(reg, byte); for (int8_t i = 7; i >= 0; i--) {
byte & _BV(i) ? set_line(&reg->output) : clear_line(&reg->output);
strobe_line(&reg->shift_clock);
}
clear_line(&reg->output); clear_line(&reg->output);
strobe_line(&reg->latch_clock); strobe_line(&reg->latch_clock);
} }

View File

@ -10,6 +10,7 @@ typedef struct SHIFT_REGISTER {
void shift_register_initialize(shift_register_t *reg); 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 #endif