Skip to main content
Explorer
February 2, 2024
Solved

Problem with loops on STM32F103C8T6

  • February 2, 2024
  • 2 replies
  • 1114 views

Hello! I’m using Keil uVision to program the stm32f103c8t6 (blue pill). I’m having problems with the loops when trying to turn on and off the LED on pin 13 port C. I just wanted to use a loop to generate a delay without needing to use timers, since I wanted to generate a simple program. But when I download it to the board, the LED stays on instead of turning on and off, regardless of the wait value I put in. I would appreciate it if you could tell me if I’m doing something wrong and indicate the solution. Thanks in advance. The code is as follows:

 

#include "stm32f10x.h"
 
#define RCC_CR (*((volatile uint32_t *)0x40021000)) 
#define RCC_APB2ENR     (*((volatile uint32_t *)0x40021018)) 
#define RCC_CFGR      (*((volatile uint32_t *)0x40021004)) 
 
#define GPIOC_CRH (*((volatile uint32_t *)0x40011004)) 
#define GPIOC_ODR (*((volatile uint32_t *)0x4001100C))
 
int main(){
RCC_CR |= 0x1;  
while( !(RCC_CR & 0x2) ){}
 
RCC_APB2ENR |= 0X10;
while( !(RCC_APB2ENR & 0x10) ){} 
 
RCC_CFGR = RCC_CFGR & ~(0x3u); 
 
GPIOC_CRH |= 0x600000;
int i=0;
while(1){
GPIOC_ODR = 0x0; 
 while(i<1000000){ i++}
GPIOC_ODR = 0x2000; 
while(i<1000000){ i++}
}
}

 

 

    This topic has been closed for replies.
    Best answer by TDK

    You're missing some semicolons in your code. It won't even compile.

    Change "int i" to "volatile int i" so the compiler doesn't optimize it out.

    Probably also want to set i back to 0 before each loop. After the first loop, all of the other loops will immediately exit.

    Can always step through the code and verify registers are changed appropriately. I guess not on the blue pill. There are better boards for development.

    2 replies

    TDKAnswer
    Super User
    February 2, 2024

    You're missing some semicolons in your code. It won't even compile.

    Change "int i" to "volatile int i" so the compiler doesn't optimize it out.

    Probably also want to set i back to 0 before each loop. After the first loop, all of the other loops will immediately exit.

    Can always step through the code and verify registers are changed appropriately. I guess not on the blue pill. There are better boards for development.

    sobasanheAuthor
    Explorer
    February 2, 2024

    Oh! Thank you for the response, it was helpful. In the post, I omitted the semicolons and the resets of the variable, but in the original code, I had them. In general, it was solved by making the variable volatile. Seriously, thank you very much for the quick response.