[SOLVED] STM32 Led Blinking code not working

Status
Not open for further replies.

thannara123

Advanced Member level 5
Joined
Jan 7, 2010
Messages
1,602
Helped
122
Reputation
244
Reaction score
116
Trophy points
1,353
Activity points
10,615
I am new ,in STM32 . I have an old board STM32 value discovery (STM32f100RBT6B)
I wrote a code for LED blinking But not working ,whats the wrong
C:
LED PC8 & PC9 trying to Blink
i am not well in shift operator programming







#include <stdint.h>
#define RCC_CFGR_BASE_ADDR      0x40021000UL
#define RCC_CFGR_REG_OFFSET     0x04UL
#define RCC_CFGR_REG_ADDR      (RCC_CFGR_BASE_ADDR + RCC_CFGR_REG_OFFSET )

#define RCC_APB2ENR_REG_OFFSET  0x18UL
#define RCC_APB2ENR_REG_ADDR    (RCC_CFGR_BASE_ADDR + RCC_APB2ENR_REG_OFFSET )


#define GPIO_MODE_BASE_ADDR     0x40011000UL
#define GPIO_MODE_REG_OFFSET    0x04UL
#define GPIO_MODE_REG_ADDR     (GPIO_MODE_BASE_ADDR  + GPIO_MODE_REG_OFFSET  )

#define GPIO_MODE_ODR_OFFSET    0x0CUL
#define GPIO_MODE_ODR_ADDR      (GPIO_MODE_BASE_ADDR  + GPIO_MODE_ODR_OFFSET )

int main(void)
{

     uint32_t *pRccCfrReg = (uint32_t*)RCC_CFGR_REG_ADDR;
     uint32_t *pRccApb2EnReg = (uint32_t*)RCC_APB2ENR_REG_ADDR ;
     uint32_t *pGPIOModeReg = (uint32_t*)GPIO_MODE_REG_ADDR;
     uint32_t *pGPIO_OdrReg = (uint32_t*)GPIO_MODE_ODR_ADDR;

     *pRccCfrReg        |= (0x5 << 24); // Selected HSI as clock bit position 24(1),25(0),26(1)
     *pRccApb2EnReg     |= (0x2 << 3);// Pheriperal clock enable
     *pGPIOModeReg      |= (0x22);// // Output mode, max speed 2 MHz. & 00: General purpose output push-pull

     while(1)
          {
              *pGPIO_OdrReg = (0xfffff);
              for(volatile uint32_t i =0; i<1000; i++)
                  ;
              *pGPIO_OdrReg = (0x0);
          }
}
 
Last edited:

*pGPIOModeReg |= (0x22);
By or-ing to initial 0x44, the instruction sets the mode reg to 0x66 (open drain) and doesn't light the ground referenced LEDs. Not sure if the delay loop is actually executing. Might need a dummy asm statement to protect from being optimized away. Also probably too fast to see blinking.
 

Can I directly give the value as *pGPIOModeReg = (0x22); ?

The LED is not light up yet

Also changed as follow
*pRccCfrReg |= (0x5 << 23); // Selected HSI as clock bit position 24(1),25(0),26(1)
*pRccApb2EnReg = (0x1);// Pheriperal clock enable
*pGPIOModeReg = (0x22);// // Output mode, max speed 2 MHz. & 00: General purpose output push-pull
 
Last edited:

Hi,

Code of post#1:
Code:
*pGPIO_OdrReg = (0xfffff); ON
Delay
 *pGPIO_OdrReg = (0x0); (delayed OFF)
 *pGPIO_OdrReg = (0xfffff); immedate ON
Delay
 *pGPIO_OdrReg = (0x0); (delayed OFF)
...

So you should expect a very low duty cycle.
Also the frequency will be too high for the eyes to see it blinking, it's just dimmed.

Klaus
 

    thannara123

    Points: 2
    Helpful Answer Positive Rating
I tried it only turn ON ,But it ON as follows

C:
 while(1)
          {
              *pGPIO_OdrReg = (0xfffff);
             // for(volatile uint32_t i =0; i<1000; i++)
             //     ;
         //     *pGPIO_OdrReg = (0x0);
          }

is there any other problem ?
I dont have debugging fascility in my older board not supported SWV
 
Last edited:

Hi,

We don't know how you connected your LEDs. A schematic would be helpful.

What compiler do you use? I can't find "GPIO_MODE_BASE_ADDR" on internet search.

Klaus
 

    thannara123

    Points: 2
    Helpful Answer Positive Rating
Hi

Mistake fount and cleared .The Reset clock cotroll register RCC_CFGR_BASE_ADDR address was wrong .
#define RCC_CFGR_BASE_ADDR 0x40021000UL changed as it is .Worked
thanks
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…