Skip to content

Problem with tm1637_delay_us() #1

Description

@cypamigon

Hi,

Thanks for the library, it works great !
However, I had a little problem with the function "tm1637_delay_us()". Firstly, the instructions "__nop();" were not recognized by the compiler. I've tried with asm("nop"); instead. I could flash the program but unfortunately it didn't worked.
I've tried to use a hardware timer to create the delay and it worked !

If someone, is facing the same issue, here is the way to achieve it :

  • Configure the hardware timer in CubeMX. You must change the prescaler in order to create a 1 MHz timer (to achieve a us clock period). In my case, I've used a F401RE Nucleo Board with a 84 MHz clock so I set the prescaler value to 84-1.
  • In the CubeMX project manager , tick "Generate peripheral initialization as a pair of '.c/.h' files per peripheral" so that you can use timer stuff in "tm1637.c" file.
  • In "tm1637.c", add :
#include "tim.h"

and change :

void tm1637_delay_us(uint8_t delay)
{
     while (delay > 0)
     {
        delay--;
        __nop();__nop();__nop();__nop();
     }
}

by

void tm1637_delay_us(uint8_t delay)
{
    HAL_TIM_Base_Start(&htim4);
    __HAL_TIM_SET_COUNTER(&htim4,0);  // set the counter value a 0
    while (__HAL_TIM_GET_COUNTER(&htim4) < delay);  // wait for the counter to reach the us input in the parameter
    HAL_TIM_Base_Stop(&htim4);
}

Adapt the previous code with the timer you are using (in my case, I was using TIMER 4)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions