Hello there, I am new to keil and I tried to program a stm32 with a simple blinky code. Here is my code and after compling, i m getting a single error and i dont knw how to figure it out. This is the error that I m being showed :
main.c(9): error: #268: declaration may not appear after executable statement in block GPIO_InitTypeDef GPIOC_str; main.c: 2 warnings, 1 error ".\Objects\first.axf" - 1 Error(s), 2 Warning(s). Target not created. Build Time Elapsed: 00:00:02
Code :
#include <stm32f10x.h> #include <stm32f10x_rcc.h> #include <stm32f10x_gpio.h> int i,j;
void GPIO_Setup() { RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC , ENABLE); GPIO_DeInit(GPIOC); GPIO_InitTypeDef GPIOC_str; GPIO_StructInit( &GPIOC_str); GPIOC_str.GPIO_Speed= GPIO_Speed_2MHz; GPIOC_str.GPIO_Pin= GPIO_Pin_13; GPIOC_str.GPIO_Mode= GPIO_Mode_Out_PP;
GPIO_Init( GPIOC, &GPIOC_str);
}
void delay() { for(i=0;i<100;i++) for(j=0;j<100;j++); } main() { GPIO_Setup(); while(1) { GPIO_SetBits( GPIOC, GPIO_Pin_13); delay(); GPIO_SetBits( GPIOC, GPIO_Pin_13); delay(); }
and what does this mean.. ??.. i mean in context of the above code as when i click at the error, it points to the declaration of the initialization structure of the gpio.
void GPIO_Setup() { RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC , ENABLE); GPIO_DeInit(GPIOC); GPIO_InitTypeDef GPIOC_str; // <<< Likely place of error GPIO_StructInit( &GPIOC_str); GPIOC_str.GPIO_Speed= GPIO_Speed_2MHz; GPIOC_str.GPIO_Pin= GPIO_Pin_13; GPIOC_str.GPIO_Mode= GPIO_Mode_Out_PP; GPIO_Init( GPIOC, &GPIOC_str); }
void GPIO_Setup() { GPIO_InitTypeDef GPIOC_str; // <<< Should more likely be here RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC , ENABLE); GPIO_DeInit(GPIOC); GPIO_StructInit( &GPIOC_str); GPIOC_str.GPIO_Speed= GPIO_Speed_2MHz; GPIOC_str.GPIO_Pin= GPIO_Pin_13; GPIOC_str.GPIO_Mode= GPIO_Mode_Out_PP; GPIO_Init( GPIOC, &GPIOC_str); }
Hey thanks @A Coda I tried this and my code compiled with no error. But i m still unable to understand whats wrong with that as the RCC clocks dont depend upon the initialization structure and moreover, anything that uses the init structure for gpio is written below the struct's declaration/definition. So why was I getting this error ?
You are not using Classic C syntax, where you define variables immediately after you open the compound statement. You are using a C++ style where you randomly define variables in the middle of the compound statement after you have already added code or called a function.
You could try selecting "C99 Mode" in the Options C/C++ tab..