STM32F103 blue pill - problems related to blinking led bare metal

783 views Asked by At

I come across a problem when trying to run this code in order to blink the built-in LED (located at PC13) on the blue pill board (STM32F103C8, ARM Cortex M3):

#include "stm32f10x.h"                  // Device header

#define max 1000000

int main(void){
    RCC->APB2ENR |= 1<<4;
    GPIOC->CRH &= 0xFF0FFFF;//clear the necessary bit 
    GPIOC->CRH |= 0x00300000;//set the necessary bit
    GPIOC->ODR &= ~(1<<13);//turn PC_13 ON
        while(1){
                    GPIOC->ODR |= 1<<13;//off
                    for(unsigned int i = 0;i < max;++i);
                    GPIOA->ODR &= ~(1<<13);//on
                    for(unsigned int i = 0;i < max;++i);
    }
}

. Here I receive some errors and I don't know why: enter image description here

Could anybody help me? Also, when I declare the unsigned integer "i" within the while loop all goes fine, but even though, nothing happens, the LED still does not blink. Here is the modified code:

#include "stm32f10x.h"                  // Device header

#define max 1000000

int main(void){
    RCC->APB2ENR |= 1<<4;
    GPIOC->CRH &= 0xFF0FFFF;//clear the necessary bit 
    GPIOC->CRH |= 0x00300000;//set the necessary bit
    GPIOC->ODR &= ~(1<<13);//turn PC_13 ON
        while(1){
                    unsigned int i;
                    GPIOC->ODR |= 1<<13;//off
                    for(i = 0;i < max;++i);
                    GPIOA->ODR &= ~(1<<13);//on
                    for(i = 0;i < max;++i);
    }
}

. Moreover, I tried another trick, to declare the integer "i" outside the loop, like here:

#include "stm32f10x.h"                  // Device header

#define max 1000000

int main(void){
    RCC->APB2ENR |= 1<<4;
    GPIOC->CRH &= 0xFF0FFFF;//clear the necessary bit 
    GPIOC->CRH |= 0x00300000;//set the necessary bit
    GPIOC->ODR &= ~(1<<13);//turn PC_13 ON
        unsigned int i;
        while(1){
                    GPIOC->ODR |= 1<<13;//off
                    for(i = 0;i < max;++i);
                    GPIOA->ODR &= ~(1<<13);//on
                    for(i = 0;i < max;++i);
    }
}

. Then, I've received the next error message: enter image description here . Now, the question is why? Please help me.

1

There are 1 answers

2
Clifford On BEST ANSWER

In the uVision project configuration dialog C/C++ tab, select "use C99".

ISO C90 dies not allow declaration of variables in either a for statement or following non -declaritive code in a statement block.

Alternatively move the declaration to the top of the statement block in which it is required. Better to use C99 though - it has been around long enough to be regarded as lowest common denominator standard I think!