+ Post New Thread
Results 1 to 6 of 6
  1. #1
    Full Member level 1
    Points: 1,716, Level: 9

    Join Date
    Oct 2012
    Posts
    111
    Helped
    0 / 0
    Points
    1,716
    Level
    9

    Unable to understand a part of code for ARM

    Hi there,

    I was going through Mazidi's STM32 book and came across this code:

    Code:
    #include "stm32fxx.h"
    
    void delayMs(int n);
    
    int main(void){
    
     RCC -> AHB1ENR |= 1; 
     GPIOA -> MODER &= ~0x00000C00;
     GPIOA -> MODER |= 0x00000400; 
     
     while(1) {
     
      GPIO -> ODR |= 0X00000020;
      delayMs(500);
      GPIO -> ODR &= ~0X00000020;
      delayMs(500);  
      }
    }
    
    void delayMs(int n) {
    
     int i;
     for(; n>0; n--)
      for (i=0; i<3195; i++);
    
    }
    This is basically a code to blink an LED with .5 sec on /off period. The code in bold letters seems to be a clock generator. Depending on the value passed to delayMs routine by the main program (here it is 500) the clock pulse gets generated. in this case, it seems to generate a on/off pulse of 1 sec time period with 50 % duty cycle.

    I am unable to understand how this clock pulse generation code works. How has the upper limit of i (3195) been calculated? As per the other examples in the book, the same code is used to generate a clock pulse of 8ms etc (by passing n=8 to delayMs from main program).

    Also is there any simpler way to generate a generic clock pulse signal of required on/off time duration?

    Looking forward to any answers.

    Thanks and Regards,
    Arvind Gupta

    •   AltAdvertisment

        
       

  2. #2
    Super Moderator
    Points: 72,752, Level: 65
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    14,791
    Helped
    3373 / 3373
    Points
    72,752
    Level
    65

    Re: Unable to understand a part of code for ARM

    Hi,

    This is a dirty solution of a busy ms delay.

    Dirty, because it works only with the given processor (archetecture) and a known processor_clock_frequency.

    The inner loop runs 3159 times (0 ...3194), every loop needs a known count of clock cycles.
    And (3159 x clock_cycles_per_loop) / clock frequency = run time = 1ms.

    Use the same function on a different processir or with different clock frequency, then you don't get 1ms anymore.

    Do you know the game "packman"? It suffered from exactly this problem.

    Klaus
    Please don´t contact me via PM, because there is no time to respond to them. No friend requests. Thank you.


    1 members found this post helpful.

    •   AltAdvertisment

        
       

  3. #3
    Full Member level 1
    Points: 1,716, Level: 9

    Join Date
    Oct 2012
    Posts
    111
    Helped
    0 / 0
    Points
    1,716
    Level
    9

    Re: Unable to understand a part of code for ARM

    OK thanks. So can you suggest a general way of generating a 1 sec clock with 05 sec on/off time ?

    Regards,
    Arvind Gupta



    •   AltAdvertisment

        
       

  4. #4
    Super Moderator
    Points: 72,752, Level: 65
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    14,791
    Helped
    3373 / 3373
    Points
    72,752
    Level
    65

    Re: Unable to understand a part of code for ARM

    Hi,

    I prefer a timer solution, mainly because of absolute accuracy and because of the low (no) processing power.
    But to make it independent of processor and clock frequency it needs some software and a RTC or any other absolute reference.

    Klaus
    Please don´t contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



  5. #5
    Advanced Member level 5
    Points: 8,990, Level: 22
    Achievements:
    7 years registered
    Easyrider83's Avatar
    Join Date
    Oct 2011
    Location
    Tallinn, Estonia
    Posts
    1,585
    Helped
    362 / 362
    Points
    8,990
    Level
    22

    Re: Unable to understand a part of code for ARM

    Look for systick.c in SPL package.
    Love me or hate me. All infractions will be ignored.



    •   AltAdvertisment

        
       

  6. #6
    Advanced Member level 4
    Points: 7,384, Level: 20

    Join Date
    Jan 2015
    Posts
    1,037
    Helped
    326 / 326
    Points
    7,384
    Level
    20

    Re: Unable to understand a part of code for ARM

    One of the great dangers of using a delay timer such as the one in Post #1 is that any compiler optimisation can result in no delay at all.
    The compiler sees a code line such as:
    Code:
    for (i=0; i<3195; i++);
    and sees that nothing changes except for the value of 'i'. Therefore, the functionally equivalent code is:
    Code:
    i = 0;
    i = 3195;
    Again, any smart compiler will remove the first instruction as it has no effect beyond this code block.
    It then applies the same logic to the outer 'for' loop and therefore can replace the whole function with:
    Code:
    return;
    and still generate functionally equivalent code. While it does not do what you want, it does do exactly what you have told the compiler to do.
    If the compiler generates code that is long enough for the CPU to spend any time in there at all (such as adding a 'nop' to the inner loop and altering the 'magic numbers') then the delay will be extended if any interrupts are processed while in the function.
    Use of timers (as mentioned above) is by far the best way if you want repeatable, stable delays.
    Susan



--[[ ]]--