AnsweredAssumed Answered

STM32F0Discovery timing issue

Question asked by padbury.chris on Aug 29, 2012
Latest reply on Aug 31, 2012 by padbury.chris
I am trying to have a simple millisecond delay function and experiencing some very strange results. I am using Keil uVision V4.23.00.0. Here is my code:

#include "stm32f0_discovery.h"
  
//prototypes
void Delay_ms(long ms); 
  
//configure clocks
void RCC_Configuration(void)
{
  /* --------------------------- System Clocks Configuration -----------------*/
  /* GPIOC clock enable */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
}
  
//configure GPIO
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
   
  /*-------------------------- GPIO Configuration ----------------------------*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
}
  
int main(void) {
    RCC_Configuration();
    GPIO_Configuration();
  
    while(1) {          
        GPIO_ResetBits(GPIOC, GPIO_Pin_9);
        GPIO_SetBits(GPIOC, GPIO_Pin_8);
        Delay_ms(5);
        GPIO_ResetBits(GPIOC, GPIO_Pin_8);
        GPIO_SetBits(GPIOC, GPIO_Pin_9);
        Delay_ms(5);
    }
}
  
void Delay_ms(long ms) {
    //ms = ms * 48000 / 4;
    ms = ms * 38400 / 4;
    //ms = 48000;
    while (ms) {
        ms--; 
    
}

This creates an 83.33Hz squarewave on pin PC8 (measured using scope). I had expected to use a value of 48000 rather than 38400 but that gave an 80Hz squarewave. I am trying to create 100Hz, which is why I changed it to 38400 expecting to increase the frequency by 25%. I have tried some different lines and get very stange behaviour:

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
           

 | Multiplier |

           
           

Frequency(Hz) | 

           
           

debug starting Hex |

           
           

debug starting Dec |

           
           

Clk per loop |

           
           

 48000

           
           

80.00 

           
           

 

           
           

 

           
           

 5 

           
           

 24000

           
           

160.00

           
           

 

           
           

 

           
           

 5

           
           

 12000

           
           

320.00 

           
           

 

           
           

 

           
           

 5

           
           

 20000

           
           

192.00 

           
           

 

           
           

 

           
           

 5

           
           

 38400

           
           

83.33 

           
           

 BB80

           
           

 48000

           
           

 6

           
           

 38402

           
           

 100.00

           
           

 BB82

           
           

 48002

           
           

 5

           
           

 38401

           
           

 100.00

           
           

 BB81

           
           

 48001

           
           

 5

           
           

 38399

           
           

 100.00

           
           

 BB7E

           
           

 47998

           
           

 5

           
 
What's so special about "ms = ms * 38400 / 4"? I had expected the loop to take 4 clock cycles (hence starting with "ms = ms * 48000 / 4").

Next I tried hardwiring the starting values, like using "ms = 48000". 48,000 created 125Hz and 60,000 created 100Hz. So that's 4 clocks per cycle. looking at the debug the starting Hex in the first case is BB80!

I'm using Level 3 (O3) optimization under Keil. Any ideas why I am seeing such strange behaviour?

Outcomes