AnsweredAssumed Answered

STM32F429i - Discovery PWM

Question asked by alferez.ian on Apr 30, 2014
Latest reply on May 2, 2014 by alferez.ian
I was wondering what is the problem with the code that I have copied and changed the pin configuration. The objective of the code is to have a PWM output on the LEDS of the STM32F429 Discovery Board. The LEDs are assigned in PG.13 and PG.14. Credits to sourcer32 for the code he shared in the forum.

By the way the board has a 168MHz Clock

/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx.h"
// STM32 PWM 8 MHz (TIM1 CH1 PG.13, CH1N PG.14) STM32F4 Discovery - sourcer32@gmail.com
   
   
/**************************************************************************************/
   
void RCC_Configuration(void)
{
  /* --------------------------- System Clocks Configuration -----------------*/
  /* TIM1 clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
   
  /* GPIOG clock enable */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
}
   
/**************************************************************************************/
   
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
   
  /*-------------------------- GPIO Configuration ----------------------------*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14;
  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(GPIOG, &GPIO_InitStructure);
   
  /* Connect TIM1 pins to AF */
  GPIO_PinAFConfig(GPIOG, GPIO_PinSource13, GPIO_AF_TIM1); // PG.13 TIM1_CH1
  GPIO_PinAFConfig(GPIOG, GPIO_PinSource14, GPIO_AF_TIM1); // PG.14 TIM1_CH1N
}
   
/**************************************************************************************/
   
void TIM1_Configuration(void)
{
  TIM_OCInitTypeDef  TIM_OCInitStructure;
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  uint16_t Period;
   
  Period = (SystemCoreClock / 8000000);
   
  /* 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_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
   
  /* TIM PWM1 Mode configuration */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_Pulse = Period / 2; // 50%
 
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
 
  TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
  TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
  TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
   
  /* Output Compare PWM1 Mode configuration: Channel1 PG.13/PA.14 */
  TIM_OC1Init(TIM1, &TIM_OCInitStructure);
   
  /* 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\r\n", file, line) */
 
  /* Infinite loop */
  while (1)
  {
  }
}
#endif
 
/**
  * @}
  */
 
/**************************************************************************************/

Outcomes