Skip to content

Commit

Permalink
Seven segment display working, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
merrittlj committed Apr 30, 2024
1 parent 898ff93 commit 45bc5c1
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 16 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ HAL_DIR := $(SRC_DIR)/hal
PROG_DIR := $(SRC_DIR)/prog
STATE_DIR := $(SRC_DIR)/state
SEG_DIR := $(SRC_DIR)/seg
EMBMATH_DIR := $(SRC_DIR)/embmath

INC_SRCH_PATH :=
INC_SRCH_PATH += -I$(INCLUDE_DIR)
Expand All @@ -19,11 +20,12 @@ INC_SRCH_PATH += -I$(HAL_DIR)
INC_SRCH_PATH += -I$(PROG_DIR)
INC_SRCH_PATH += -I$(STATE_DIR)
INC_SRCH_PATH += -I$(SEG_DIR)
INC_SRCH_PATH += -I$(EMBMATH_DIR)

LIB_SRCH_PATH :=
LIB_SRCH_PATH += -L$(MAKE_DIR)/libs

LIBS := -lhal -lprog -lstate -lseg -lc -lgcc -lm
LIBS := -lhal -lprog -lstate -lseg -lembmath -lc -lgcc

# Toolchain
CC := arm-none-eabi-gcc
Expand Down Expand Up @@ -59,6 +61,7 @@ build: dep
@$(MAKE) -C $(PROG_DIR) -f prog.mk
@$(MAKE) -C $(STATE_DIR) -f state.mk
@$(MAKE) -C $(SEG_DIR) -f seg.mk
@$(MAKE) -C $(EMBMATH_DIR) -f embmath.mk
@$(MAKE) -C $(ROOT_DIR) -f root.mk

full_clean: clean dep_clean
Expand All @@ -68,6 +71,7 @@ clean:
@$(MAKE) -C $(PROG_DIR) -f prog.mk clean
@$(MAKE) -C $(STATE_DIR) -f state.mk clean
@$(MAKE) -C $(SEG_DIR) -f seg.mk clean
@$(MAKE) -C $(EMBMATH_DIR) -f embmath.mk clean
@$(MAKE) -C $(ROOT_DIR) -f root.mk clean

dep_clean:
Expand Down
4 changes: 3 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* Get basic data on segmented display
* Seven segment display improvements
- No leading zeros
- ifndefs to optionally remove 4 digit support
** Further modularize build/files
*** Logging?
Binary file added datasheets/components/c945.pdf
Binary file not shown.
25 changes: 25 additions & 0 deletions src/embmath/embmath.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* filename: embmath.c
* date: 04.29.24
* author: Lucas Merritt/merrittlj
* description: Implementation for embmath.h.
*/

#include "embmath.h"


int ipow(int base, int exp)
{
int result = 1;
for (;;)
{
if (exp & 1)
result *= base;
exp >>= 1;
if (!exp)
break;
base *= base;
}

return result;
}
16 changes: 16 additions & 0 deletions src/embmath/embmath.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* filename: embmath.h
* date: 04.29.24
* author: Lucas Merritt/merrittlj
* description: Small math functions for embedded systems.
*/

#ifndef EMBMATH_H
#define EMBMATH_H

#include "common.h"


extern int ipow(int base, int exp);

#endif
19 changes: 19 additions & 0 deletions src/embmath/embmath.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
LIB = $(MAKE_DIR)/libs/libembmath.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
11 changes: 11 additions & 0 deletions src/prog/prog.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "prog.h"
#include "hal.h"
#include "seg.h"


const struct state DEFAULT_FSM[NUM_DEFAULT_STATES] = {
Expand All @@ -35,6 +36,10 @@ void max_speed_action()
{
gpio_set_mode(LED_RED_PIN, GPIO_MODE_OUTPUT);
gpio_write(LED_RED_PIN, GPIO_OUTPUT_SET);

seg_pins(SHIFT_SER_PIN, SHIFT_RCLK_PIN, SHIFT_SRCLK_PIN, DP1_PIN, DP2_PIN, DP3_PIN, DP4_PIN); /* TODO: Change this. */
seg_init(2395, 2);
seg_display_next();
}

void max_speed_transition()
Expand All @@ -46,6 +51,9 @@ void trip_distance_action()
{
gpio_set_mode(LED_GREEN_PIN, GPIO_MODE_OUTPUT);
gpio_write(LED_GREEN_PIN, GPIO_OUTPUT_SET);

seg_init(7253, 1);
seg_display_next();
}

void trip_distance_transition()
Expand All @@ -57,6 +65,9 @@ void total_distance_action()
{
gpio_set_mode(LED_BLUE_PIN, GPIO_MODE_OUTPUT);
gpio_write(LED_BLUE_PIN, GPIO_OUTPUT_SET);

seg_init(92, 255);
seg_display_next();
}

void total_distance_transition()
Expand Down
4 changes: 4 additions & 0 deletions src/prog/prog.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
#define SHIFT_SER_PIN (PIN('A', 10))
#define SHIFT_RCLK_PIN (PIN('A', 9))
#define SHIFT_SRCLK_PIN (PIN('A', 8))
#define DP1_PIN (PIN('B', 6))
#define DP2_PIN (PIN('B', 7))
#define DP3_PIN (PIN('B', 8))
#define DP4_PIN (PIN('B', 9))

/* States for the default FSM. */
enum {STATE_MAX_SPEED, STATE_TRIP_DISTANCE, STATE_TOTAL_DISTANCE, NUM_DEFAULT_STATES};
Expand Down
17 changes: 8 additions & 9 deletions src/root/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ int main()
uint32_t debounce_timer, debounce_period = 20;
uint32_t idle_timer, idle_period = 10000;
uint8_t is_idle = 0;
uint32_t seg_timer, seg_period = 1000;
uint32_t seg_timer, seg_period = 5;

uint8_t button_pressed = 0;
gpio_set_mode(STATEBTN_PIN, GPIO_MODE_INPUT);
Expand All @@ -33,11 +33,12 @@ int main()
gpio_set_mode(SHIFT_RCLK_PIN, GPIO_MODE_OUTPUT);
gpio_set_mode(SHIFT_SRCLK_PIN, GPIO_MODE_OUTPUT);

seg_pins(SHIFT_SER_PIN, SHIFT_RCLK_PIN, SHIFT_SRCLK_PIN);
seg_clear_output();

gpio_set_mode(DP1_PIN, GPIO_MODE_OUTPUT);
gpio_set_mode(DP2_PIN, GPIO_MODE_OUTPUT);
gpio_set_mode(DP3_PIN, GPIO_MODE_OUTPUT);
gpio_set_mode(DP4_PIN, GPIO_MODE_OUTPUT);

NVIC_SetPriority (SysTick_IRQn, 0);
uint8_t x = 0;
for (;;) {
/* To prevent debounce, simply poll the button every 50ms or so. */
if (timer_expired(&debounce_timer, debounce_period, s_ticks)) {
Expand All @@ -61,10 +62,8 @@ int main()
gpio_write(LED_BLUE_PIN, GPIO_OUTPUT_CLEAR);
seg_clear_output();
}
if (timer_expired(&seg_timer, seg_period, s_ticks)) {
seg_clear_output();
seg_display_digit(x, 0);
++x; x = x % 10;
if (timer_expired(&seg_timer, seg_period, s_ticks) && !is_idle) {
seg_display_next();
}
}

Expand Down
18 changes: 14 additions & 4 deletions src/seg/seg.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
* description: Implementation for seg.h
*/
#include <stdint.h>
#include <math.h>

#include "seg.h"
#include "hal.h"
#include "embmath.h"


static uint16_t _display_num;
Expand All @@ -19,11 +19,17 @@ static uint16_t _ser;
static uint16_t _rclk;
static uint16_t _srclk;

void seg_pins(uint16_t ser, uint16_t rclk, uint16_t srclk)
static uint16_t _displays[4];

void seg_pins(uint16_t ser, uint16_t rclk, uint16_t srclk, uint16_t dp1, uint16_t dp2, uint16_t dp3, uint16_t dp4)
{
_ser = ser;
_rclk = rclk;
_srclk = srclk;
_displays[0] = dp1;
_displays[1] = dp2;
_displays[2] = dp3;
_displays[3] = dp4;
}

void seg_update_output()
Expand Down Expand Up @@ -100,9 +106,13 @@ uint8_t get_num_index()

void seg_display_next()
{
/* TODO: Do not display 0 if it is leading. */
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);
if (_num_index > 0) gpio_write(_displays[_num_index - 1], GPIO_OUTPUT_CLEAR);
if (_num_index == 0) gpio_write(_displays[3], GPIO_OUTPUT_CLEAR);
gpio_write(_displays[_num_index], GPIO_OUTPUT_SET);
uint8_t decimal_enabled = (_num_index == (_decimal_loc - 1)) && _decimal_loc <= 4;
seg_display_digit((uint8_t)((_display_num / (uint16_t)(ipow(10, 3 - _num_index))) % 10), decimal_enabled);

++_num_index;
_num_index = _num_index % 4;
Expand Down
2 changes: 1 addition & 1 deletion src/seg/seg.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "common.h"


extern void seg_pins(uint16_t ser, uint16_t rclk, uint16_t srclk); /* Set pins to use. */
extern void seg_pins(uint16_t ser, uint16_t rclk, uint16_t srclk, uint16_t dp1, uint16_t dp2, uint16_t dp3, uint16_t dp4); /* 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. */
Expand Down

0 comments on commit 45bc5c1

Please sign in to comment.