From ecdcf078117e9c136d7c562d7baceb1525c3cadc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Thu, 4 May 2017 12:52:50 +0200 Subject: [PATCH] tm1637: fix timing for first bit --- lib/led_tm1637.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/led_tm1637.c b/lib/led_tm1637.c index 8cec959..2d47ac9 100644 --- a/lib/led_tm1637.c +++ b/lib/led_tm1637.c @@ -178,7 +178,7 @@ void led_tm1637_setup(void) timer_reset(TIM(LED_TM1637_TIMER)); // reset timer state timer_set_mode(TIM(LED_TM1637_TIMER), TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); // set timer mode, use undivided timer clock, edge alignment (simple count), and count up timer_set_prescaler(TIM(LED_TM1637_TIMER), 0); // don't prescale to get most precise timing ( 1/(72E6/1/(2**16))=0.91 ms > 0.5 us ) - timer_set_period(TIM(LED_TM1637_TIMER), 500); // set the clock frequency (emprical value until the signal starts to look bad) + timer_set_period(TIM(LED_TM1637_TIMER), 500); // set the clock frequency (500 kHz is the maximum per datasheet, but also because of the bit banging implementation) timer_clear_flag(TIM(LED_TM1637_TIMER), TIM_SR_UIF); // clear flag timer_update_on_overflow(TIM(LED_TM1637_TIMER)); // only use counter overflow as UEV source (use overflow as start time or timeout) } @@ -205,12 +205,12 @@ static bool led_tm1637_write(const uint8_t* data, uint8_t length) } // enable timer for signal generation - gpio_set_mode(GPIO(LED_TM1637_DIO_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(LED_TM1637_DIO_PIN)); // ensure we can output to sent start condition timer_set_counter(TIM(LED_TM1637_TIMER), 0); // reset timer counter timer_enable_counter(TIM(LED_TM1637_TIMER)); // enable timer to generate timing led_tm1637_tick(); // wait to enforce minimum time since last write // send start condition (DIO then CLK low) + gpio_set_mode(GPIO(LED_TM1637_DIO_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(LED_TM1637_DIO_PIN)); // ensure we can output to sent start condition gpio_clear(GPIO(LED_TM1637_DIO_PORT), GPIO(LED_TM1637_DIO_PIN)); // put DIO low led_tm1637_tick(); // wait for next tick gpio_clear(GPIO(LED_TM1637_CLK_PORT), GPIO(LED_TM1637_CLK_PIN)); // put CLK low @@ -218,7 +218,6 @@ static bool led_tm1637_write(const uint8_t* data, uint8_t length) // send data bytes (MSb first) for (uint8_t i=0; i