-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Seven segment display handling and refactoring
- Loading branch information
Showing
9 changed files
with
228 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* filename: seg.c | ||
* date: 04.28.24 | ||
* author: Lucas Merritt/merrittlj | ||
* description: Implementation for seg.h | ||
*/ | ||
#include <stdint.h> | ||
#include <math.h> | ||
|
||
#include "seg.h" | ||
#include "hal.h" | ||
|
||
|
||
static uint16_t _display_num; | ||
static uint8_t _decimal_loc; | ||
static uint8_t _num_index; | ||
|
||
static uint16_t _ser; | ||
static uint16_t _rclk; | ||
static uint16_t _srclk; | ||
|
||
void seg_pins(uint16_t ser, uint16_t rclk, uint16_t srclk) | ||
{ | ||
_ser = ser; | ||
_rclk = rclk; | ||
_srclk = srclk; | ||
} | ||
|
||
void seg_update_output() | ||
{ | ||
gpio_write(_rclk, GPIO_OUTPUT_SET); | ||
gpio_write(_rclk, GPIO_OUTPUT_CLEAR); | ||
} | ||
|
||
void seg_send_bit(uint8_t bit) | ||
{ | ||
gpio_write(_ser, bit); | ||
gpio_write(_srclk, GPIO_OUTPUT_SET); | ||
gpio_write(_srclk, GPIO_OUTPUT_CLEAR); | ||
gpio_write(_ser, GPIO_OUTPUT_CLEAR); | ||
} | ||
|
||
void seg_clear_output() | ||
{ | ||
for (uint8_t i = 0; i < 8; ++i) seg_send_bit(0); | ||
seg_update_output(); | ||
} | ||
|
||
void seg_display_digit(uint8_t digit, uint8_t decimal_enabled) | ||
{ | ||
/* | ||
* Index correspondent to digit. Encodings are little-endian from A-G, with 0 = off and 1 = on. | ||
* Ex: 0 is all segments but G on, 0x3F = 0b0111111, we send 0, then 1, then 1, etc. | ||
*/ | ||
uint8_t decode_nums[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F}; | ||
|
||
/* We iterate in reverse order through the bits (ex: 0 = 0, then 1, then 1, etc.) because of how the shift register is buffered. */ | ||
seg_send_bit(decimal_enabled); | ||
for (int8_t i = 6; i >= 0; --i) seg_send_bit((decode_nums[digit] >> i) & 1); | ||
|
||
seg_update_output(); | ||
} | ||
|
||
void seg_init(uint16_t num, uint8_t decimal) | ||
{ | ||
set_display_num(num); | ||
set_decimal_loc(decimal); | ||
set_num_index(0); | ||
} | ||
|
||
void set_display_num(uint16_t num) | ||
{ | ||
_display_num = num; | ||
} | ||
|
||
uint16_t get_display_num() | ||
{ | ||
return _display_num; | ||
} | ||
|
||
void set_decimal_loc(uint8_t decimal) | ||
{ | ||
_decimal_loc = decimal; | ||
} | ||
|
||
uint8_t get_decimal_loc() | ||
{ | ||
return _decimal_loc; | ||
} | ||
|
||
void set_num_index(uint8_t index) | ||
{ | ||
_num_index = index; | ||
} | ||
|
||
uint8_t get_num_index() | ||
{ | ||
return _num_index; | ||
} | ||
|
||
void seg_display_next() | ||
{ | ||
seg_clear_output(); | ||
/* TODO: set display to _num_index */ | ||
seg_display_digit((uint8_t)((_display_num / (uint16_t)(pow(10, 3 - _num_index))) % 10), _num_index == _decimal_loc); | ||
|
||
++_num_index; | ||
_num_index = _num_index % 4; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* filename: seg.h | ||
* date: 04.28.24 | ||
* author: Lucas Merritt/merrittlj | ||
* description: Driver code for 4-digit seven segment display with 8-bit shift register. | ||
*/ | ||
|
||
#ifndef SEG_H | ||
#define SEG_H | ||
#include <stdint.h> | ||
|
||
#include "common.h" | ||
|
||
|
||
extern void seg_pins(uint16_t ser, uint16_t rclk, uint16_t srclk); /* Set pins to use. */ | ||
|
||
extern void seg_update_output(); /* Clock RCLK. */ | ||
extern void seg_send_bit(uint8_t bit); /* Send a bit to the shift register. */ | ||
extern void seg_clear_output(); | ||
extern void seg_display_digit(uint8_t digit, uint8_t decimal_enabled); | ||
|
||
extern void seg_init(uint16_t num, uint8_t decimal); | ||
|
||
/* To display a multiple-digit number, we have to multiplex. Set a number to display and display each digit with a function. */ | ||
extern void set_display_num(uint16_t num); | ||
extern uint16_t get_display_num(); | ||
|
||
/* Where the decimal is in _display_num, 0:3. */ | ||
extern void set_decimal_loc(uint8_t decimal); | ||
extern uint8_t get_decimal_loc(); | ||
|
||
/* Current digit index in _display_num, 0:3. */ | ||
extern void set_num_index(uint8_t index); | ||
extern uint8_t get_num_index(); | ||
|
||
extern void seg_display_next(); /* Display the next digit in the num. */ | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
LIB = $(MAKE_DIR)/libs/libseg.a | ||
SRCS = $(wildcard *.c) | ||
OBJS = $(patsubst %.c, %.o, $(SRCS)) | ||
|
||
$(LIB): $(OBJS) | ||
@mkdir -p $(MAKE_DIR)/libs | ||
@$(AR) cr $@ $^ | ||
@echo "Archive $(notdir $@)" | ||
|
||
%.o: %.c | ||
@$(CC) $(CFLAGS) -c $^ -o $@ | ||
@echo "CC $@" | ||
|
||
clean: | ||
@$(RM) -f $(LIB) $(OBJS) | ||
@$(RM) -f *.expand | ||
@echo "Remove objects: $(OBJS)" | ||
|
||
.PHONY = clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters