void loop()
{
// ===== First Digit =====
// We iterate through each of the segments of the first digit.
for (int i = 0; i < 7; i++){
// we access the bits of the binary number 0b01100000, which corresponds to the segments A, B, C, D, E, F, and the decimal point (DP).
digitalWrite(segmentPins[i], bitRead(0b01100000, 7 - i) == 1 ? segmentON : segmentOFF);
// We use the bitRead() function to get the i-th bit, and if it is equal to 1, we set the segment to segmentON, otherwise we set it to segmentOFF.
}
digitalWrite(D1, digitON); // We turn on the D1 input to display the first digit.
delay(10); // We add a delay to control the brightness of the digit.
// ===== Second Digit =====
digitalWrite(D1, digitOFF); // We turn off the D1 input.
// We iterate through each of the segments of the second digit. We use the same approach as before to set the appropriate segments for the number '2'
for (int i = 0; i < 7; i++){
digitalWrite(segmentPins[i], bitRead(0b11011010, 7 - i) == 1 ? segmentON : segmentOFF);
}
digitalWrite(D2, digitON); // We turn on the D2 input to display the second digit.
delay(10); // We add another delay to control the brightness.
digitalWrite(D2, digitOFF); // We turn off the D2 input and repeat the process again.
}
/* USER CODE BEGIN PFP */
void myFunction();
/* USER CODE END PFP */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while(1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
myFunction();
}
/* USER CODE END 3 */
/* USER CODE BEGIN 4 */
void myFunction(){
// ...
}
/* USER CODE END 4 */
void loop() {
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the difference
// between the current time and last time you blinked the LED is bigger than
// the interval at which you want to blink the LED.
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
STM32 Approach
/* USER CODE BEGIN PV */
uint32_t lastTick = 0;
/* USER CODE END PV */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
uint32_t currentTick= HAL_GetTick() - lastTick;
if(currentTick> 1000)
{
// update last tick
lastTick = HAL_GetTick();
// your code ...
}
}
/* USER CODE END 3 */
Derived from HAL_Delay Function
/**
* @brief This function provides minimum delay (in milliseconds) based
* on variable incremented.
* @note In the default implementation , SysTick timer is the source of time base.
* It is used to generate interrupts at regular time intervals where uwTick
* is incremented.
* @note This function is declared as __weak to be overwritten in case of other
* implementations in user file.
* @param Delay specifies the delay time length, in milliseconds.
* @retval None
*/
__weak void HAL_Delay(uint32_t Delay)
{
uint32_t tickstart = HAL_GetTick();
uint32_t wait = Delay;
/* Add a freq to guarantee minimum wait */
if (wait < HAL_MAX_DELAY)
{
wait += (uint32_t)(uwTickFreq);
}
while ((HAL_GetTick() - tickstart) < wait)
{
}
}