Add functions to work with a 74HC595 shift register

This commit is contained in:
Savanni D'Gerinel 2022-06-25 23:35:14 -04:00
parent 3727225940
commit 0e27f675dd
2 changed files with 42 additions and 0 deletions

27
shift_register/reg.c Normal file
View File

@ -0,0 +1,27 @@
#include <base.h>
#include <reg.h>
void shift_register_initialize(shift_register_t *reg) {
set_line_direction(&reg->output, LINE_OUT);
set_line_direction(&reg->shift_clock, LINE_OUT);
set_line_direction(&reg->latch_clock, LINE_OUT);
clear_line(&reg->output);
clear_line(&reg->shift_clock);
clear_line(&reg->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(&reg->output) : clear_line(&reg->output);
strobe_line(&reg->shift_clock);
}
}
void shift_out(shift_register_t *reg, uint8_t byte) {
send_byte_lsb(reg, byte);
clear_line(&reg->output);
strobe_line(&reg->latch_clock);
}

15
shift_register/reg.h Normal file
View File

@ -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