tm1637: fix timing for first bit

This commit is contained in:
King Kévin 2017-05-04 12:52:50 +02:00
parent 97e8de0bc9
commit ecdcf07811
1 changed files with 3 additions and 4 deletions

View File

@ -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<length; i++) { // send all bytes
uint8_t byte = data[i];
gpio_set_mode(GPIO(LED_TM1637_DIO_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(LED_TM1637_DIO_PIN)); // switch DIO to output to send bits
for (uint8_t b=0; b<8; b++) { // send all bits
if (byte&0x1) { // send a 1
gpio_set(GPIO(LED_TM1637_DIO_PORT), GPIO(LED_TM1637_DIO_PIN)); // put DIO high
@ -240,10 +239,10 @@ static bool led_tm1637_write(const uint8_t* data, uint8_t length)
}
led_tm1637_tick(); // wait for next tick
gpio_clear(GPIO(LED_TM1637_CLK_PORT), GPIO(LED_TM1637_CLK_PIN)); // put CLK low
gpio_set_mode(GPIO(LED_TM1637_DIO_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(LED_TM1637_DIO_PIN)); // switch DIO back to output
}
// send stop condition
gpio_set_mode(GPIO(LED_TM1637_DIO_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(LED_TM1637_DIO_PIN)); // switch DIO to output to send stop condition
led_tm1637_tick(); // wait for next tick
gpio_set(GPIO(LED_TM1637_CLK_PORT), GPIO(LED_TM1637_CLK_PIN)); // put CLK high
led_tm1637_tick(); // wait for next tick