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
Posted on March 11, 2015 at 14:31

You'd need two counters for two frequencies in that range.

// STM32 PWM 1 MHz (TIM1 CH1 PA.08) STM32F4 Discovery - sourcer32@gmail.com
#include ''stm32f4_discovery.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
/* --------------------------- System Clocks Configuration -----------------*/
/* TIM1 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
/* GPIOA clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*-------------------------- GPIO Configuration ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Connect TIM1 pins to AF */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1);
}
/**************************************************************************************/
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 = 0; // Dump 1X clock into timer
TIM_TimeBaseStructure.TIM_Period = 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 = Period / 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);
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
TIM1_Configuration();
while(1); // Don't want to exit
}
/**************************************************************************************/
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf(''Wrong parameters value: file %s on line %d

'', file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/**
* @}
*/
/**************************************************************************************/

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
bharadwajtke
Associate II
Posted on March 11, 2015 at 15:39

Hi Clive, I got 1Mhz, but it fluctuating between 990Khz-1Mhz,DSO which I am using is calibrated and works perfectly fine, I am generating 1Mhz with the Atmel XMEGA controller, which gives consistant 1Mhz, but STM failed.

Below is the modification i did WRT to the suggested code by you, rest all same.

 //Period = (SystemCoreClock / (1000000));

  Period = 54;

Kindly let me know, why the behavior is not consistent, I am testing it in room temperature which is about 28 degree Celsius, hope this should not affect the crystal right?

Posted on March 11, 2015 at 15:45

Check your system clock. You may be running on 16MHz HSI instead of on PLL set to 168MHz. You might want to read and check the relevant RCC registers content.

JW
Posted on March 11, 2015 at 15:54

I am getting 96.2kHz insted 1Mhz, I am trying to tune to 1Mhz, till now no success. I am not getting the calculation part, so as to calculate and set the register to 1Mhz.

I don't know what development tools you are using, perhaps you need to call SystemInit()? Tools like CooCox seem to ignore the CMSIS method of calling SystemInit() prior to main(), in which case you must call SystemInit() from main() to get the clocks set up as defined in system_stm32f4xx.c.

If the part is running from HSI (16 MHz), which is does at reset, then you can replace the SystemCoreClock value with 16000000, and compute the Period counter that way. Remember that the TIM acts as a divider of the input clock source, and that Period and Prescaler values are in the form N-1
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
bharadwajtke
Associate II
Posted on March 12, 2015 at 07:29

Thank you very much clive, sorry I did not mention which IDE I am using.

I am using Coocox 1.7.7, i am calling System Init, below is the configuration of the system 

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

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

  *        Supported STM32F4xx device revision    | Rev A

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

  *        System Clock source                    | PLL (HSE)

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

  *        SYSCLK(Hz)                             | 168000000

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

  *        HCLK(Hz)                               | 168000000

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

  *        AHB Prescaler                          | 1

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

  *        APB1 Prescaler                         | 4

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

  *        APB2 Prescaler                         | 2

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

  *        HSE Frequency(Hz)                      | 25000000

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

  *        PLL_M                                  | 25

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

  *        PLL_N                                  | 336

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

  *        PLL_P                                  | 2

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

  *        PLL_Q                                  | 7

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

  *        PLLI2S_N                               | NA

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

  *        PLLI2S_R                               | NA

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

  *        I2S input clock                        | NA

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

  *        VDD(V)                                 | 3.3

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

  *        Main regulator output voltage          | Scale1 mode

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

  *        Flash Latency(WS)                      | 5

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

  *        Prefetch Buffer                        | OFF

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

  *        Instruction cache                      | ON

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

  *        Data cache                             | ON

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

  *        Require 48MHz for USB OTG FS,          | Enabled

  *        SDIO and RNG clock                     |

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

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

With this setting I just ran the code shared by you without modifying it, but i did not get the 1Mhz :(

Any tool to calculate the TImer frequency, as like in case of Atmel online Calculator available  (

http://eleccelerator.com/avr-timer-calculator/

).

I serached but did not get apart from ''STM32F4xx_Clock_Configuration_V1.0.1.xls'' tool.

bharadwajtke
Associate II
Posted on March 12, 2015 at 07:33

Thank you very much waclawek, 

I am using Coocox 1.7.7, i am calling System Init, below is the configuration of the system 

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

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

  *        Supported STM32F4xx device revision    | Rev A

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

  *        System Clock source                    | PLL (HSE)

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

  *        SYSCLK(Hz)                             | 

168000000

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

  *        HCLK(Hz)                               | 

168000000

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

  *        AHB Prescaler                          | 1

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

  *        APB1 Prescaler                         | 4

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

  *        APB2 Prescaler                         | 2

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

  *        HSE Frequency(Hz)                      | 

25000000

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

  *        PLL_M                                  | 25

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

  *        PLL_N                                  | 336

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

  *        PLL_P                                  | 2

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

  *        PLL_Q                                  | 7

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

  *        PLLI2S_N                               | NA

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

  *        PLLI2S_R                               | NA

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

  *        I2S input clock                        | NA

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

  *        VDD(V)                                 | 3.3

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

  *        Main regulator output voltage          | Scale1 mode

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

  *        Flash Latency(WS)                      | 5

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

  *        Prefetch Buffer                        | OFF

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

  *        Instruction cache                      | ON

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

  *        Data cache                             | ON

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

  *        Require 48MHz for USB OTG FS,          | Enabled

  *        SDIO and RNG clock                     |

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

later I am calling the code which is shared by Clive(in the above reply of Clive it has).

Thank you for suggesting..i Am trying to understand the System clock WRT to the timer...I am not able to sync with it :(

Posted on March 12, 2015 at 13:18

Your board has an 8 MHz source, not 25 MHz, so you'd need to adjust the PLL and HSE_VALUE settings to reflect that.

The PWM output should be solid.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
bharadwajtke
Associate II
Posted on March 16, 2015 at 15:01

Hi Clive1,

Thank you very much, I wll make the changes and check it.

Your gem for this forum... :) thanks again

bharadwajtke
Associate II
Posted on March 16, 2015 at 15:33

After making below changes, given code of you worked fine...thanks Sir

Below are the changes I made, then got 1Mhz consistantly