cembeddedstm32microcontroller

Error : invalid digit "9" in octal constant


I am trying to write a code for displaying numbers on a four digit seven segment display using STM32-F401RE. The problem lies in the while(1) loop.
Here is the code:

void num_display_func(uint16_t numtodisplay);
void displayDigit(uint8_t number);

int main(void){
while (1)
  {
    /* USER CODE END WHILE */
      num_display_func(0209);
    /* USER CODE BEGIN 3 */
  }


void num_display_func(uint16_t numtodisplay){

    uint8_t digit_extract[4];

    digit_extract[0] = numtodisplay%10;       //extract the last digit of the number
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, SET);
    displayDigit(digit_extract[0]);
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, RESET);

    HAL_Delay(10);

    digit_extract[1] = (numtodisplay/10)%10;  //extract the second last digit
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, SET);
    displayDigit(digit_extract[1]);
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, RESET);

    HAL_Delay(10);

    digit_extract[2] = (numtodisplay/100)%10; //extract the third last digit
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, SET);
    displayDigit(digit_extract[2]);
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, RESET);

    HAL_Delay(10);

    digit_extract[3] = (numtodisplay/1000)%10; //extract the fourth last digit
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, SET);
    displayDigit(digit_extract[3]);
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, RESET);

    HAL_Delay(10);

}

void displayDigit(uint8_t number){


    GPIOC->ODR |= GPIOx_SEGMENTS_MASK;  //Clear the segment bits (Setting the bits to HIGH for common Anode config)

    uint8_t digit = segmentCodes[number];

    GPIOC->ODR |= (digit & GPIOx_SEGMENTS_MASK );

}

When I am trying to run the code, here is the error:

./Core/Src/main.c:115:21: error: invalid digit "9" in octal constant
  115 |    num_display_func(0209);
      |                     ^~~~

As far as I know, this can be fixed if the 0 is removed. but in that case how do I display the number, 0209 or any other number starting with zero for that matter?

If I remove the 0 from the number, it may display 209 instead of 0209. How to solve this?


Solution

  • Octal valid values are 0-7 - 8 digits, a base-8. Therefore, 0209 is not a valid octal number. That's why your compiler is complaining. Octal 0209 doesn't exists, the closest you can get is 0207, then 0210, 0211 (which would be "equivalent"). So the compiler is trying to convert 0209 octal to decimal in order to pass that number as argument, but it fails because 9 is not a valid digit.

    Furthermore, in your num_display_func you are converting the number as if it was a decimal number (dividing by 10, 100, 1000... and getting the remainder as % 10). So you have a problem there.

    If I remove the 0 from the number, it may display 209 instead of 0209. How to solve this?

    Have you tested this in the hardware? Because the program should correctly put the zero in it if you pass just 209.

    1st digit: 209 % 10 == 9
    2nd digit: (209 / 10) % 10 == 20 % 10 == 0
    3rd digit: (209 / 100) % 10 == 2 % 10 == 2
    4th digit: (209 / 1000) % 10 == 0 % 10 == 0
    

    Because you are writing to the hardware on all four digits, so the number zero will be written.