MCU Practic Lab 2

Learn How a 4-Digit 7-Segment LED Display Works and how to control it using an Arduino – Software Particles 

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.
}

Bitwise Operators in C – GeeksforGeeks 

Number System Conversion in C – GeeksforGeeks 

Number system in C 

Binary0b10010010 
Hexa0x92
Decimal146
GPIO_PIN_0 0x0001 0b00000001 
GPIO_PIN_1 0x0002 0b0000001
GPIO_PIN_2 0x0004 0b00000100 
GPIO_PIN_3 0x0008 0b00001000 
GPIO_PIN_4 0x0010 16 0b00010000 
GPIO_PIN_5 0x0020 32 0b00100000 
GPIO_PIN_6 0x0040 64 0b01000000 
GPIO_PIN_7 0x0080 128 0b10000000 
// Basic 
for(unsigned char i = 0; i < 8; i++) { 
    uint16_t currentPin = GPIO_PIN_0 << i; 
    HAL_GPIO_WritePin(GPIOC, currentPin, (Code7Seg[5] & currentPin) >> i); 
} 
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_11, GPIO_PIN_RESET); 

// Using (1 << i) 
uint8_t Code7Seg_num5 = 0b10010010; 
for(unsigned char i = 0; i < 8; i++) { 
    uint16_t currentPin = GPIO_PIN_0 << i; 
    uint8_t currentPinState = (Code7Seg_num5 & (1 << i)) >> i; // get current pin state [0,1]; 

    HAL_GPIO_WritePin(GPIOC, currentPin, currentPinState); 
} 
    
// Using !! operator 
uint8_t Code7Seg_num5 = 0b10010010; 
for(unsigned char i = 0; i < 8; i++) { 
    uint16_t currentPin = GPIO_PIN_0 << i; 
    uint8_t currentPinState = (Code7Seg_num5 & (1 << i)); // get current pin state [0,1]; 
    currentPinState = !!currentPinState; // [0,1] 

    HAL_GPIO_WritePin(GPIOC, currentPin, currentPinState); 
}

C Functions (w3schools.com) 

C Function Declaration and Definition (w3schools.com) 

STM32 Practice

/* 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 */ 

Get bit position’s value 

bit manipulation – How do I get bit-by-bit data from an integer value in C? – Stack Overflow 

uint8_t Code7Seg_num5 = 0b10010010;       

uint8_t bit_0 =Code7Seg_num5 &0x1; = 0 

uint8_t bit_1 = (Code7Seg_num5 & (0x1<<1)) >>1; = 1  

uint8_t bit_2 = (Code7Seg_num5 & (0x1<<2)) >>2; 

uint8_t bit_3 = (Code7Seg_num5 & (0x1<<3)) >>3; 

uint8_t bit_4 = (Code7Seg_num5 & (0x1<<4)) >>4; 

// ... 

uint8_t bit_7 = (Code7Seg_num5 & (0x1<<7)) >>7; = 1 



// for approach 

for(uint8_t i =0;i <8; ++i) { 

uint8_t bit_i = (Code7Seg_num5 & (0x1<<i)) >>i; 

} 

Update (int) variable in C inside a function – Stack Overflow 

Tách các chữ số thuộc hàng trăm, hàng chục, hàng đơn vị | VnCoding 

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)
  {
  }
}