STM32 code inside while not working

Thread Starter

yef smith

Joined Aug 2, 2020
717
Hello I am using STM32F407 descovery, I managed to turn on all of its LEDS as shown in the picture bellow.
But when i put the code shown bellow inside the while(1) loop.
I get no LED's ON at all, Its like no code is working at all,but before the WHILE(1) i turned all LEDS on,so why i dont get any LED's on when i do the blinking LED in the while(1)?
Thanks.

Code:
#include "stm32f407xx.h"

static void delay2(int n)
{
    while(n>0) n--;
    
    
}
int main()
{
    
    //RCC_AHB1ENR_GPIODEN
    RCC->AHB1ENR|=(1uL<<3); //set tirdbit
    //GPIOD->MODER&=~(1uL<<31);//reset 0 on 31 01 to gpio_moder 31,30 bits in register
    //GPIOD->MODER|=(1uL<<30);//set 1 on 30 output mode for pin 15 port D
    GPIOD->MODER=0x55000000;
    
    
    GPIOD->OTYPER=0; //all register is push pull
    GPIOD->OSPEEDR=0;//speed low
     //GPIOD->ODR|=(1<<15); //Sets pin 15
    GPIOD->ODR=0xF000;
    
    while(1)
    {
        GPIOD->ODR=0x8000;
        delay2(2000000);
        GPIOD->ODR=0x4000;
        delay2(200000);
        GPIOD->ODR=0x2000;
      delay2(2000000);
         GPIOD->ODR=0x1000;
        delay2(200000);
    }
}

1612357998047.png
 

AlbertHall

Joined Jun 4, 2014
12,345
Some of the values passed to delay2 are two hundred thousand and some are two million.
How long is the delay with an argunent of two million?
Is the int value 32 bit on your compiler?
 

Thread Starter

yef smith

Joined Aug 2, 2020
717
even before going to the while loop i gave a command to turn on all leds.
the problem starts when i put the ode in the while loop,it doesnt turn on anything.
 

AlbertHall

Joined Jun 4, 2014
12,345
Put one of those delay lines immediately before the while(1).
If you still do not see any lights, even briefly, then the problem is before the while loop.
 

Thread Starter

yef smith

Joined Aug 2, 2020
717
Very weird behavior, i have commented all the code inside while(1).
I said to turn on all LEDs then delayd as much as possible and turn on only the blue LED.
How ever when i ranflashed the code it straight away turned on the blue
Also i need to point that my flashed code starts to work when i pull out the cable and put it back in.

What could cause such a thing?
Thanks.

Code:
#include "stm32f407xx.h"

void delay2(int n)
{
    while(n>0) n--;
    
    return;
}

int main()
{
    
    //RCC_AHB1ENR_GPIODEN
    RCC->AHB1ENR|=(1uL<<3); //set tirdbit
    //GPIOD->MODER&=~(1uL<<31);//reset 0 on 31 01 to gpio_moder 31,30 bits in register
    //GPIOD->MODER|=(1uL<<30);//set 1 on 30 output mode for pin 15 port D
    GPIOD->MODER=0x55000000;
    
    
    GPIOD->OTYPER=0; //all register is push pull
    GPIOD->OSPEEDR=0;//speed low
     //GPIOD->ODR|=(1<<15); //Sets pin 15
    GPIOD->ODR=0xF000;
    delay2(400000000);
    GPIOD->ODR=0x8000;
    while(1)
    {
    //    GPIOD->ODR=0x8000;
    //    delay2(2000000);
    //    GPIOD->ODR=0x4000;
    //    delay2(200000);
    //    GPIOD->ODR=0x2000;
  //    delay2(2000000);
    //     GPIOD->ODR=0x1000;
    //    delay2(200000);
    }
}
 

Thread Starter

yef smith

Joined Aug 2, 2020
717
It gets even stranger ,when i put GPIOD->BSRR=(1uL<<15); out side of while(1) loop then it turns on the LED
but when i put this line inside the while(1) then nothing happens.
Where did i go wrong?
Thanks.

Code:
#include "stm32f407xx.h"

//void delay2(int n)
//{
//    while(n>0) n--;
    
//    return;
//}

int main()
{
    
    //RCC_AHB1ENR_GPIODEN
    RCC->AHB1ENR|=(1uL<<3); //set tirdbit
    //GPIOD->MODER&=~(1uL<<31);//reset 0 on 31 01 to gpio_moder 31,30 bits in register
    //GPIOD->MODER|=(1uL<<30);//set 1 on 30 output mode for pin 15 port D
    GPIOD->MODER=0x55000000;
    
    
    GPIOD->OTYPER=0; //all register is push pull
    GPIOD->OSPEEDR=0;//speed low
     //GPIOD->ODR|=(1<<15); //Sets pin 15
  GPIOD->BSRR=(1uL<<15);
    while(1)
        
    {
        
    
    }
}
 

BobaMosfet

Joined Jul 1, 2009
2,110
It gets even stranger ,when i put GPIOD->BSRR=(1uL<<15); out side of while(1) loop then it turns on the LED
but when i put this line inside the while(1) then nothing happens.
Where did i go wrong?
Thanks.

Code:
#include "stm32f407xx.h"

//void delay2(int n)
//{
//    while(n>0) n--;
  
//    return;
//}

int main()
{
  
    //RCC_AHB1ENR_GPIODEN
    RCC->AHB1ENR|=(1uL<<3); //set tirdbit
    //GPIOD->MODER&=~(1uL<<31);//reset 0 on 31 01 to gpio_moder 31,30 bits in register
    //GPIOD->MODER|=(1uL<<30);//set 1 on 30 output mode for pin 15 port D
    GPIOD->MODER=0x55000000;
  
  
    GPIOD->OTYPER=0; //all register is push pull
    GPIOD->OSPEEDR=0;//speed low
     //GPIOD->ODR|=(1<<15); //Sets pin 15
  GPIOD->BSRR=(1uL<<15);
    while(1)
      
    {
      
  
    }
}
@yef smith First of all, make sure you understand how fast your MCU is. Your numbers might not be large enough. Aside from that, another thing that most developers fail to understand is that your compiler might be eliminating your while in the object code because it doesn't understand your logic (your while loop is too simple)-- Never EVER use maximum optimization in a compiler. use the next level up. Do a dissassembly and look at what the compiler generated for your while and see if it's there, if it isn't, or if it altered it.
 

Thread Starter

yef smith

Joined Aug 2, 2020
717
I am doing just like in the video manuals
my C settings in KEIL are as shown bellow.
I cant see maximum optimization there
1612368076385.png
 

MrChips

Joined Oct 2, 2009
30,708
STM32 is a 32-bit processor. The compiler is very likely assigning 32 bits for int.
If you are uncertain of the size of data type int, use uint32_t for 32-bit unsigned integer.
 

djsfantasi

Joined Apr 11, 2010
9,156
Don’t you have the C delay function, which takes a time in milliseconds as a parameter?

Using a counter for a delay is not recommended, as environmental conditions will change the delay time. Use the intrinsic function instead.
 

MrChips

Joined Oct 2, 2009
30,708
I have looked at the first code and did not find anything wrong except for the following:

1) As you gain more experience in writing code you will learn that it is bad practice to use literal constants.
A literal constant is a declared numeric value such as 200000
It is best to define it in a statement such as

#define DELAY 200000

This uses text substitution and DELAY will be replaced with 200000 everywhere in your code by the compiler.

2) Similarly, never write to hardware using literal constants.
Code written in this manner is not transportable and will eventually break.
RCC->AHB1ENR|=(1uL<<3);
GPIOD->MODER=0x55000000;

Every bit in the hardware is defined with a name.
Search for stm32f4xx.h and you should find the defined names.

3) You are writing directly to hardware registers. While this is ok it does required an intimate knowledge of all the bits in all the relevant registers. ARM chips are very complex. Start learning to use the libraries by following written examples.

4) I don't know at what speed your STM32F407 is running. Usually there is an RCC initialization code that is executed before your code. Again, it would be better to start off with an example from the Keil libraries.
 

Thread Starter

yef smith

Joined Aug 2, 2020
717
Hello,I have chose -2 optimisation,

Also i changed int to UINT_32 as shown bellow.
in the example bellow i turn off the LED and after some time turn it one
But its just turns on straight away.no daley,even if we write outside of while loop.
Where did i go wrong?
Thanks.

Code:
#include "stm32f407xx.h"



int main()
{
    
    //RCC_AHB1ENR_GPIODEN
    RCC->AHB1ENR|=(1uL<<3); //set tirdbit
    //GPIOD->MODER&=~(1uL<<31);//reset 0 on 31 01 to gpio_moder 31,30 bits in register
    //GPIOD->MODER|=(1uL<<30);//set 1 on 30 output mode for pin 15 port D
    GPIOD->MODER=0x55000000;
    
    
    GPIOD->OTYPER=0; //all register is push pull
    GPIOD->OSPEEDR=0;//speed low
     //GPIOD->ODR|=(1<<15); //Sets pin 15

        GPIOD->BSRR=(1uL<<(15+16));
    
    for(uint32_t i=0;i<500000;i++);       
 GPIOD->BSRR=(1uL<<15);
    while(1)
        
    {

    }
}
1612420680933.png
 

Thread Starter

yef smith

Joined Aug 2, 2020
717
Hello, The problem is that for loop and while loop have no effect.
in debug mode i see the led going on and off.
Is there some alternative for the delay?
Thanks.
 

Thread Starter

yef smith

Joined Aug 2, 2020
717
Hello djsfantasi, i will try and loop for such function .
another thing is the optimisation
i have-0 and -1,i chose -1,how can i know that my compliler didt swllowed it by optimisation?
Thanks.

1612443917133.png
 

MrChips

Joined Oct 2, 2009
30,708
If your delay is too short the LEDs will flash very fast and may appear to be always on.
If your delay is too long the LEDs may be off and are taking a long time to come on.

We don't know the clock speed of your MCU and we don't know the length of data type int.
Make sure that you get both of these right or at least make sure that the delay time is about 100-2000ms.
 
Top