#include "stm32c0xx_hal.h"
#define SYSTICK_LOAD (SystemCoreClock/1000000U)
#define SYSTICK_DELAY_CALIB (SYSTICK_LOAD >> 1)
 
#define DELAY_US(us) \
    do { \
         uint32_t start = SysTick->VAL; \
         uint32_t ticks = (us * SYSTICK_LOAD)-SYSTICK_DELAY_CALIB;  \
         while((start - SysTick->VAL) < ticks); \
    } while (0)

#define DELAY_MS(ms) \
    do { \
        for (uint32_t i = 0; i < ms; ++i) { \
            DELAY_US(1000); \
        } \
    } while (0)

void displayDigit(int digit, int value);
void displayNumber(int number);
void ClockSetup();
void TimerConfig(void);
int usDelay(uint16_t us );
int msDelay(uint16_t ms );

int main(void)
{
  ClockSetup();
    HAL_Init();
      // void TimerConfig(void);

    GPIO_InitTypeDef GPIO_InitStruct;
    __HAL_RCC_GPIOA_CLK_ENABLE();
    __HAL_RCC_GPIOB_CLK_ENABLE();

    GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3|GPIO_PIN_5;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6
            | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
// RCC->IOPENR |= (1<<0);  // Enable the GPIOA clock
// GPIOA->MODER |= (1<<10);  // pin PA5(bits 11:10) as Output (01)
// GPIOA->OTYPER &= ~(1<<5);  // bit 5=0 --> Output push pull
// GPIOA->OSPEEDR |= (1<<11);  // Pin PA5 (bits 11:10) as Fast Speed (1:0)
// GPIOA->PUPDR &= ~((1<<10) | (1<<11));  // Pin PA5 (bits 11:10) are 0:0 --> no pull up or pulldown

    while (1)
    {

// GPIOA->BSRR |= (1<<5); // Set the Pin PA5
// GPIOA->BSRR |= (1<<21); // Reset the Pin PA5


      // HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);
GPIOA->ODR |= 1<<5; // Set the Pin PA5

          // HAL_Delay(1000);
          DELAY_MS(1);
          DELAY_US(500);
      // HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET);
      GPIOA->ODR &= ~(1<<5); // Reset the Pin PA5

          // HAL_Delay(1000);
          DELAY_MS(20);
          // DELAY_US(1000);

      // for(int i=0;i<10000;i++) displayNumber(88);
       
      //   HAL_Delay(1000);
      //   for(int i=0;i<10000;i++) displayNumber(2222);
      //   HAL_Delay(1000);
    }
}
void TimerConfig(void)
{
     	RCC->APBENR1|= RCC_APBENR1_TIM3EN;
		TIM3->PSC|=48-1;
		TIM3->ARR|=0xffff;
		TIM3->CR1|=TIM_CR1_CEN;
		while(!(TIM3->SR & TIM_SR_UIF)) ;
}

int usDelay(uint16_t us )
{
	TIM3->CNT=0;


	while(TIM3->CNT < us);
	return 0;
}

int msDelay(uint16_t ms )
{
	for(int i=0;i<ms;i++)
	{
		usDelay(1000);
	}
	return 0;

}
void ClockSetup()
{
	    RCC->CR|= RCC_CR_HSEON;
	    while(!(RCC->CR&RCC_CR_HSERDY));
	    FLASH->ACR|=FLASH_ACR_LATENCY_1;
	    
	    RCC->CFGR|=RCC_CFGR_SW_0;
	    while(!(RCC->CFGR&RCC_CFGR_SWS_0));

}

void displayDigit(int digit, int value)
{
    switch (digit)
    {
    case 1:
        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_RESET);
        break;
    case 2:
        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
        break;
    case 3:
        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_RESET);
        break;
    case 4:
        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_RESET);
        break;
    }

    switch (value)
    {
    case 0:
        HAL_GPIO_WritePin(GPIOB,
                GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7
                        | GPIO_PIN_8, GPIO_PIN_SET);
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9 | GPIO_PIN_10, GPIO_PIN_RESET);
        break;
    case 1:
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4 | GPIO_PIN_5, GPIO_PIN_SET);
        HAL_GPIO_WritePin(GPIOB,
                GPIO_PIN_3 | GPIO_PIN_6 | GPIO_PIN_8 | GPIO_PIN_7 | GPIO_PIN_9
                        | GPIO_PIN_10, GPIO_PIN_RESET);
        break;
    case 2:
        HAL_GPIO_WritePin(GPIOB,
                GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_7 | GPIO_PIN_6 | GPIO_PIN_9,
                GPIO_PIN_SET);
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8 | GPIO_PIN_5 | GPIO_PIN_10,
                GPIO_PIN_RESET);
        break;
    case 3:
        HAL_GPIO_WritePin(GPIOB,
                GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_3 | GPIO_PIN_9,
                GPIO_PIN_SET);
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_10,
                GPIO_PIN_RESET);
        break;
    case 4:
        HAL_GPIO_WritePin(GPIOB,
                GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_4 | GPIO_PIN_5,
                GPIO_PIN_SET);
        HAL_GPIO_WritePin(GPIOB,
                GPIO_PIN_3 | GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_10,
                GPIO_PIN_RESET);
        break;
    case 5:
        HAL_GPIO_WritePin(GPIOB,
                GPIO_PIN_3 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_5 | GPIO_PIN_6,
                GPIO_PIN_SET);
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4 | GPIO_PIN_7 | GPIO_PIN_10,
                GPIO_PIN_RESET);
        break;
    case 6:
        HAL_GPIO_WritePin(GPIOB,
                GPIO_PIN_3 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8
                        | GPIO_PIN_9, GPIO_PIN_SET);
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4 | GPIO_PIN_10, GPIO_PIN_RESET);
        break;
    case 7:
        HAL_GPIO_WritePin(GPIOB,
                GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 ,
                GPIO_PIN_SET);
        HAL_GPIO_WritePin(GPIOB,
                GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_9 | GPIO_PIN_10| GPIO_PIN_8,
                GPIO_PIN_RESET);
        break;
    case 8:
        HAL_GPIO_WritePin(GPIOB,
                GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7
                        | GPIO_PIN_8 | GPIO_PIN_9, GPIO_PIN_SET);
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_10, GPIO_PIN_RESET);
        break;
    case 9:
        HAL_GPIO_WritePin(GPIOB,
                GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_8
                        | GPIO_PIN_9 |GPIO_PIN_7 , GPIO_PIN_SET);
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_10, GPIO_PIN_RESET);
        break;
    }
}

void displayNumber(int number)
{
    int digit1 = number / 1000;
    int digit2 = (number / 100) % 10;
    int digit3 = (number / 10) % 10;
    int digit4 = number % 10;

    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3,
            GPIO_PIN_SET);
    displayDigit(1, digit1);
    // HAL_Delay(1000);

    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3,
            GPIO_PIN_SET);
    displayDigit(2, digit2);
    // HAL_Delay(1000);

    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3,
            GPIO_PIN_SET);
    displayDigit(3, digit3);
    // HAL_Delay(1000);

    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3,
            GPIO_PIN_SET);
    displayDigit(4, digit4);
    // HAL_Delay(1000);

    HAL_GPIO_WritePin(GPIOB,
                GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_8
                        | GPIO_PIN_9 |GPIO_PIN_7|GPIO_PIN_10 , GPIO_PIN_RESET);
}
Loading
st-nucleo-c031c6