From 2155868ab3358535d4915f39dc192189bc57e938 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Wed, 23 Mar 2022 17:07:19 +0100 Subject: working display driver :tada: --- src/main.c | 33 +++++++++++++++++++-------------- src/tm1637.c | 45 ++++++++++++++++++++++++--------------------- src/tm1637.h | 37 ++++++++++++++++++++++++++++++++++++- 3 files changed, 79 insertions(+), 36 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index 7978b16..264d56a 100644 --- a/src/main.c +++ b/src/main.c @@ -84,8 +84,10 @@ void interrupt_setup() { * 0 or 1 which indicates if the LED should be turned on (1) or off (0). */ void led_write(int num, int on) { - GPIOB->ODR &= ~(1 << leds[num]); - GPIOB->ODR |= (on << leds[num]); + int pin = leds[num]; + GPIOB->ODR ^= (((GPIOB->ODR & (1 << pin)) >> pin) ^ on) << pin; + // GPIOB->ODR &= ~(1 << leds[num]); + // GPIOB->ODR |= (on << leds[num]); } /* @@ -132,31 +134,34 @@ void timer_delay(unsigned short millis) { while ((TIM3->SR & TIM_SR_CC1IF) == 0); // wait until c/c goes high } +void timer_display(unsigned int minutes, unsigned int seconds, bool colon) { + tm1637_segmentsend(0, tm1637_font[minutes / 10]); + tm1637_segmentsend(1, tm1637_font[minutes % 10] | (colon * TM1637_COLON)); + tm1637_segmentsend(2, tm1637_font[seconds / 10]); + tm1637_segmentsend(3, tm1637_font[seconds % 10]); +} + int main() { shield_config(); tm1637_begin(); - timer_delay(2000); + timer_delay(100); unsigned int minutes = 0; unsigned int seconds = 0; - TM1637Sequence seq; - uint8_t dis_br_data[] = { 0b10001111 }; - seq.data = dis_br_data; - seq.length = 1; - _tm1637_send(seq); - - uint8_t dis_seg_data[] = { 0b11000001, 0b01111111 }; - seq.data = dis_seg_data; - seq.length = 2; - _tm1637_send(seq); + tm1637_dispcfg(7, 1); while (1) { - timer_delay(1e3); + timer_delay(500); + timer_display(minutes, seconds, false); + timer_delay(500); + timer_display(minutes, seconds, true); + seconds++; if (seconds >= 60) { seconds = 0; minutes++; + if (minutes >= 100) minutes = 0; } } } diff --git a/src/tm1637.c b/src/tm1637.c index 4e6e68c..9cc79d6 100644 --- a/src/tm1637.c +++ b/src/tm1637.c @@ -2,11 +2,6 @@ #include "tm1637.h" #include "stm32f091xc.h" -void _tm1637_micro_delay() { - timer_delay(10); - // for(unsigned int x = 0; x < 10e4; x++); -} - /** @brief reset gpio mode registers for dio */ void _tm1637_GPIOMODE_reset() { GPIOA->MODER &= ~(0b11 << (PINOUT_DISP_DIO * 2)); @@ -36,9 +31,6 @@ void _tm1637_GPIOMODE_open_drain() { */ void _tm1637_pin_write(uint32_t pin, bool state) { GPIOA->ODR ^= (((GPIOA->ODR & (1 << pin)) >> pin) ^ state) << pin; - - if (pin == PINOUT_DISP_CLK) led_write(0, state); - if (pin == PINOUT_DISP_DIO) led_write(1, state); } void tm1637_begin() { @@ -50,31 +42,24 @@ void tm1637_begin() { _tm1637_pin_write(PINOUT_DISP_CLK, 1); _tm1637_pin_write(PINOUT_DISP_DIO, 1); - - _tm1637_micro_delay(); } void _tm1637_start() { _tm1637_pin_write(PINOUT_DISP_DIO, 0); - _tm1637_micro_delay(); } void _tm1637_stop() { _tm1637_pin_write(PINOUT_DISP_CLK, 1); _tm1637_pin_write(PINOUT_DISP_DIO, 1); - _tm1637_micro_delay(); } bool _tm1637_ack() { _tm1637_pin_write(PINOUT_DISP_CLK, 0); _tm1637_pin_write(PINOUT_DISP_DIO, 0); _tm1637_GPIOMODE_open_drain(); - _tm1637_micro_delay(); _tm1637_pin_write(PINOUT_DISP_CLK, 1); - _tm1637_micro_delay(); _tm1637_pin_write(PINOUT_DISP_CLK, 0); _tm1637_GPIOMODE_gp_output(); - _tm1637_micro_delay(); return true; } @@ -90,21 +75,39 @@ TM1637Sequence _tm1637_send(TM1637Sequence command) { for (uint32_t byte = 0; byte < command.length; byte++) { for (uint8_t bit = 0; bit < 8; bit++) { - uint8_t rev_bit = 7 - bit; _tm1637_pin_write(PINOUT_DISP_CLK, 0); - _tm1637_pin_write(PINOUT_DISP_DIO, (command.data[byte] & (1 << rev_bit)) >> rev_bit); - _tm1637_micro_delay(); + _tm1637_pin_write(PINOUT_DISP_DIO, (command.data[byte] & (1 << bit)) >> bit); _tm1637_pin_write(PINOUT_DISP_CLK, 1); - _tm1637_micro_delay(); } //TODO: confirm ack /*bool ack =*/ _tm1637_ack(); } - _tm1637_micro_delay(); - // stop condition _tm1637_stop(); return response; } + +void tm1637_dispcfg(uint8_t brightness, bool on) { + uint8_t dis_data[] = { + (uint8_t)(0b10000000 | (on << 3) | (brightness)), + }; + TM1637Sequence seq = { + .data = dis_data, + .length = 1, + }; + _tm1637_send(seq); +} + +void tm1637_segmentsend(uint8_t segment, uint8_t data) { + uint8_t dis_data[] = { + (uint8_t)(0b11000000 | segment), + data, + }; + TM1637Sequence seq = { + .data = dis_data, + .length = 2, + }; + _tm1637_send(seq); +} diff --git a/src/tm1637.h b/src/tm1637.h index b9fa42c..0c1ed66 100644 --- a/src/tm1637.h +++ b/src/tm1637.h @@ -3,6 +3,24 @@ #include #include +#define TM1637_FONT_0 (0x3f) +#define TM1637_FONT_1 (0x06) +#define TM1637_FONT_2 (0x5b) +#define TM1637_FONT_3 (0x4f) +#define TM1637_FONT_4 (0x66) +#define TM1637_FONT_5 (0x6d) +#define TM1637_FONT_6 (0x7d) +#define TM1637_FONT_7 (0x07) +#define TM1637_FONT_8 (0x7f) +#define TM1637_FONT_9 (0x6f) +#define TM1637_FONT_A (0x77) +#define TM1637_FONT_B (0x7c) +#define TM1637_FONT_C (0x39) +#define TM1637_FONT_D (0x5e) +#define TM1637_FONT_E (0x79) +#define TM1637_FONT_F (0x71) +#define TM1637_COLON (0x80) + typedef struct { uint8_t* data; uint32_t length; @@ -25,5 +43,22 @@ void tm1637_dispcfg(uint8_t brightness, bool on); */ void tm1637_segmentsend(uint8_t segment, uint8_t data); +static const uint8_t tm1637_font[16] = { + TM1637_FONT_0, + TM1637_FONT_1, + TM1637_FONT_2, + TM1637_FONT_3, + TM1637_FONT_4, + TM1637_FONT_5, + TM1637_FONT_6, + TM1637_FONT_7, + TM1637_FONT_8, + TM1637_FONT_9, + TM1637_FONT_A, + TM1637_FONT_B, + TM1637_FONT_C, + TM1637_FONT_D, + TM1637_FONT_E, + TM1637_FONT_F, +}; -TM1637Sequence _tm1637_send(TM1637Sequence command); -- cgit v1.2.3