Rebuild the code and the board for a 3-wire protocol

This required switching to the display's 4-wire protocol so that I could use some of the pins on the shift register for register select and enable.
This commit is contained in:
Savanni D'Gerinel 2022-06-30 09:19:30 -04:00
parent fffa9b3e89
commit 02e0ab5ae0
3 changed files with 81 additions and 27 deletions

View File

@ -15,44 +15,87 @@ You should have received a copy of the GNU General Public License along with Lum
#include <util/delay.h>
#include <base.h>
void display_strobe_line(gpio_t *line) {
set_line(line);
_delay_us(50);
clear_line(line);
_delay_us(50);
#define ENABLE _BV(5)
#define CHARCODE _BV(4)
void ping(gpio_t *light) {
set_line(light);
_delay_ms(100);
clear_line(light);
_delay_ms(100);
}
void send_half_code(display_t *disp, uint8_t reg, uint8_t data) {
sr_send_msb(&disp->reg, (reg ? CHARCODE : 0) | data << 2);
_delay_us(1);
sr_send_msb(&disp->reg, (ENABLE | (reg ? CHARCODE : 0) | data) << 2);
_delay_us(1);
sr_send_msb(&disp->reg, ((reg ? CHARCODE : 0) | data) << 2);
_delay_us(1);
}
void send_code(display_t *disp, uint8_t reg, uint8_t data) {
send_half_code(disp, reg, data >> 4);
send_half_code(disp, reg, data & 0xf);
}
void write_instruction(display_t *disp, uint8_t bitcode) {
clear_line(&disp->register_select);
sr_send_msb(&disp->reg, bitcode);
display_strobe_line(&disp->enable);
send_code(disp, 0, bitcode);
}
void write_char(display_t *disp, uint8_t bitcode) {
set_line(&disp->register_select);
sr_send_msb(&disp->reg, bitcode);
display_strobe_line(&disp->enable);
send_code(disp, 1, bitcode);
_delay_us(100);
}
void display_init(display_t *disp) {
sr_initialize(&disp->reg);
set_line_direction(&disp->register_select, LINE_OUT);
set_line_direction(&disp->enable, LINE_OUT);
write_instruction(disp, 0b00111000);
/* Initializing for 4-bit channel */
send_half_code(disp, 0, 0b0011);
_delay_ms(5);
send_half_code(disp, 0, 0b0011);
_delay_us(150);
send_half_code(disp, 0, 0b0011);
_delay_us(100);
send_half_code(disp, 0, 0b0010);
_delay_us(100);
/* Function set, 4-bit channel, two lines, 5x10 dots */
write_instruction(disp, 0b00101000);
_delay_us(100);
/* display off */
write_instruction(disp, 0b00001000);
_delay_us(100);
/* display clear */
write_instruction(disp, 0b00000001);
_delay_ms(3);
/* set entry mode */
write_instruction(disp, 0b00000110);
_delay_us(100);
/* display on */
write_instruction(disp, 0b00001100);
_delay_us(100);
}
void display_clear(display_t *disp) {
write_instruction(disp, 0b00000001);
_delay_ms(3);
_delay_us(100);
write_instruction(disp, 0b00000010);
_delay_ms(3);
_delay_us(100);
}
void display_enable(display_t *disp) {
write_instruction(disp, 0b00001100);
write_instruction(disp, _BV(5));
_delay_us(100);
write_instruction(disp, 0b00000100);
write_instruction(disp, _BV(5) | _BV(3));
_delay_us(100);
}
@ -68,4 +111,3 @@ void display_write_message(display_t *disp, const char *msg) {
}
}

View File

@ -46,8 +46,10 @@ You should have received a copy of the GNU General Public License along with Lum
typedef struct DISPLAY {
shift_register_t reg;
/*
gpio_t register_select;
gpio_t enable;
*/
} display_t;
void display_init(display_t *);

View File

@ -159,16 +159,29 @@ void display_bits(display_t *display, uint8_t bits) {
}
}
void strobe_light(gpio_t *light) {
set_line(light);
_delay_ms(100);
clear_line(light);
_delay_ms(100);
}
int main(void) {
display_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 },
.output = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 7 },
.shift_clock = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 6 },
.latch_clock = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 5 },
},
.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);
strobe_light(&light);
strobe_light(&light);
strobe_light(&light);
_delay_ms(15);
display_init(&display);
rfm_t rfm = {
@ -184,16 +197,13 @@ int main(void) {
rfm_initialize(&rfm);
_delay_ms(15);
display_enable(&display);
display_clear(&display);
display_write_message(&display, "Waking up");
_delay_ms(1000);
/*
rfm_set_mode(&rfm, standby);
*/
rfm_listen(&rfm);
while(1) {