From 0e27f675ddbe239b27f1fa7251dd3900e1c02cff Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Sat, 25 Jun 2022 23:35:14 -0400 Subject: [PATCH] Add functions to work with a 74HC595 shift register --- shift_register/reg.c | 27 +++++++++++++++++++++++++++ shift_register/reg.h | 15 +++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 shift_register/reg.c create mode 100644 shift_register/reg.h diff --git a/shift_register/reg.c b/shift_register/reg.c new file mode 100644 index 0000000..ba23d75 --- /dev/null +++ b/shift_register/reg.c @@ -0,0 +1,27 @@ +#include +#include + +void shift_register_initialize(shift_register_t *reg) { + set_line_direction(®->output, LINE_OUT); + set_line_direction(®->shift_clock, LINE_OUT); + set_line_direction(®->latch_clock, LINE_OUT); + + clear_line(®->output); + clear_line(®->shift_clock); + clear_line(®->latch_clock); +} + +void send_byte_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); + } +} + +void shift_out(shift_register_t *reg, uint8_t byte) { + send_byte_lsb(reg, byte); + clear_line(®->output); + strobe_line(®->latch_clock); +} + + diff --git a/shift_register/reg.h b/shift_register/reg.h new file mode 100644 index 0000000..1d92b34 --- /dev/null +++ b/shift_register/reg.h @@ -0,0 +1,15 @@ + +#ifndef __REG_H__ +#define __REG_H__ + +typedef struct SHIFT_REGISTER { + gpio_t output; + gpio_t shift_clock; + gpio_t latch_clock; +} shift_register_t; + + +void shift_register_initialize(shift_register_t *reg); +void shift_out(shift_register_t *reg, uint8_t byte); + +#endif