cancel
Showing results for 
Search instead for 
Did you mean: 

Generating 1Mhz PWM with STM32F407(Discovery Board)

bharadwajtke
Associate II
Posted on March 11, 2015 at 13:44

I have been trying to generate a PWM of 1Mhz & 2Mhaz in different pins, with a system clock of 168Mhz, but unable to do so, can any body help me out with the code.

Please help me with a small code snippet for the same...

Thanks in advance :)

#discovery #stm32f407
11 REPLIES 11
bharadwajtke
Associate II
Posted on March 17, 2015 at 15:07

In the Coocox with zStm32f407, with below setting

* 5. This file configures the system clock as follows:

  *=============================================================================

  *=============================================================================

  *        Supported STM32F4xx device revision    | Rev A

  *-----------------------------------------------------------------------------

  *        System Clock source                    | PLL (HSE)

  *-----------------------------------------------------------------------------

  *        SYSCLK(Hz)                             | 64000000

  *-----------------------------------------------------------------------------

  *        HCLK(Hz)                               | 16000000

  *-----------------------------------------------------------------------------

  *        AHB Prescaler                          | 4

  *-----------------------------------------------------------------------------

  *        APB1 Prescaler                         | 1

  *-----------------------------------------------------------------------------

  *        APB2 Prescaler                         | 1

  *-----------------------------------------------------------------------------

  *        HSE Frequency(Hz)                      | 8000000

  *-----------------------------------------------------------------------------

  *        PLL_M                                  | 4

  *-----------------------------------------------------------------------------

  *        PLL_N                                  | 192

  *-----------------------------------------------------------------------------

  *        PLL_P                                  | 6

  *-----------------------------------------------------------------------------

  *        PLL_Q                                  | 8

  *-----------------------------------------------------------------------------

  *        PLLI2S_N                               | NA

  *-----------------------------------------------------------------------------

  *        PLLI2S_R                               | NA

  *-----------------------------------------------------------------------------

  *        I2S input clock                        | NA

  *-----------------------------------------------------------------------------

  *        VDD(V)                                 | 3.3

  *-----------------------------------------------------------------------------

  *        Main regulator output voltage          | Scale1 mode

  *-----------------------------------------------------------------------------

  *        Flash Latency(WS)                      | 0

  *-----------------------------------------------------------------------------

  *        Prefetch Buffer                        | OFF

  *-----------------------------------------------------------------------------

  *        Instruction cache                      | ON

  *-----------------------------------------------------------------------------

  *        Data cache                             | ON

  *-----------------------------------------------------------------------------

  *        Require 48MHz for USB OTG FS,          | Disabled

  *        SDIO and RNG clock                     |

  *-----------------------------------------------------------------------------

i am trying to generate 1Hz of pwm, this is the code for the same

void TIM1_Configuration(void)

{

  TIM_OCInitTypeDef  TIM_OCInitStructure;

  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

  uint16_t Period;

  Period = (SystemCoreClock / 1000000);

  /* Time base configuration */

  TIM_TimeBaseStructure.TIM_Prescaler = 2624;//0; // Dump 1X clock into timer

  TIM_TimeBaseStructure.TIM_Period = 63999;// Period - 1;

  TIM_TimeBaseStructure.TIM_ClockDivision = 0;

  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

  /* Enable TIM1 Preload register on ARR */

  TIM_ARRPreloadConfig(TIM1, ENABLE);

  /* TIM PWM1 Mode configuration */

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

  TIM_OCInitStructure.TIM_Pulse = ( TIM_TimeBaseStructure.TIM_Period +1)/ 2; // 50%

  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  /* Output Compare PWM1 Mode configuration: Channel1 PA.08 */

  TIM_OC1Init(TIM1, &TIM_OCInitStructure);

  TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);

  /* TIM1 Main Output Enable */

  TIM_CtrlPWMOutputs(TIM1, ENABLE);

  /* TIM1 enable counter */

  TIM_Cmd(TIM1, ENABLE);

}

but 1Hz I am not getting(no digital waveform, sinusoidal wave), I generated the values from the MikroElectronika Calculator tool.

I am not getting whats wrong in the code.0690X00000602r3QAA.jpg

Posted on March 17, 2015 at 15:32

Ok, it helps to understand what you're doing rather than use tools that hide the detail.

Your system file suggests the timer is clocking at 64 MHz, not 168 MHz

To get 1 Hz you need to count 64000000 ticks at 64 MHz. A simple factoring of this gives

1000 x 64000

So

Prescaler = 1000 - 1;

Period = 64000 - 1;

Pulse = 64000 / 2; // Half of the Period

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..