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 );
}
当我try 运行代码时,出现以下错误:
./Core/Src/main.c:115:21: error: invalid digit "9" in octal constant
115 | num_display_func(0209);
| ^~~~
据我所知,如果删除0,这个问题就可以修复.但在这种情况下,如何显示数字0209或任何其他以零开始的数字呢?
如果我从数字中删除0,它可能会显示209而不是0209.如何解决这个问题?