AnsweredAssumed Answered

STM32F4 discovery board using hal driver for pwm generation

Question asked by t.a.001 on Oct 18, 2015
Latest reply on Oct 20, 2015 by Clive One

Hi,
I'm learning in a private project how to program a stm32f4 discovery board using Keil uvision 5 and how to realize some projects. Therefore I am using hal driver.
At the moment I try to programm a pwm using timer 3 and to dim a led. I looked through a lot of examples and stuff I could find on the internet, but my code is not working. I read the description of the hal drivers (third edition) and tried to test my code by connecting a led to PC6 and GND. But the led is not blinking... I have enough after three weeks without
success ... If someone can see the problem of my code, I would be very happy.
Greets Alex.

#include "stm32f4xx_hal.h"

#include "stm32f4xx_hal_gpio.h"

#include "stm32f4xx_hal_tim.h"

 

 

#define Periode 2562

 

static void SystemClock_Config(void);

void InitIODriver (GPIO_InitTypeDef* GPIO_InitStruct);

void Define_OutputChannelStruct(TIM_OC_InitTypeDef* Struct);

void Define_MyPWM_Struct(TIM_HandleTypeDef* TIM_Handle_PWM_Struct);

void CalculatePWMPercent(TIM_OC_InitTypeDef* Struct,uint8_t Percent);

 

int main(void)

{

                GPIO_InitTypeDef GPIO_InitSt;

                TIM_HandleTypeDef PWM_Struct;

                TIM_OC_InitTypeDef StrOutputChannel;

                TIM_ClockConfigTypeDef TIM_CCT;

               

                HAL_Init();        

                /*SysCLK-----------------------*/

                SystemClock_Config();

                /*define and init TIM3!*/

                Define_MyPWM_Struct(&PWM_Struct);

                HAL_TIM_Base_Init(&PWM_Struct);

                HAL_TIM_PWM_Init(&PWM_Struct);

   /*define PWM output!*/       

                Define_OutputChannelStruct(&StrOutputChannel);

                HAL_TIM_PWM_ConfigChannel(&PWM_Struct,&StrOutputChannel,TIM_CHANNEL_1);

                InitIODriver(&GPIO_InitSt);

                HAL_GPIO_Init(GPIOC,&GPIO_InitSt);

 

                HAL_TIM_PWM_Start(&PWM_Struct,TIM_CHANNEL_1);

               

                while(1){};

               

                //return 0;

}

 

static void SystemClock_Config(void)

{

                RCC_ClkInitTypeDef RCC_ClkInitStruct;

                RCC_OscInitTypeDef RCC_OscInitStruct;

                /* Enable HSE Oscillator and activate PLL with HSE as source */

                RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;

                RCC_OscInitStruct.HSEState = RCC_HSE_ON;

                RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

                RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;

                RCC_OscInitStruct.PLL.PLLM = 25;

                RCC_OscInitStruct.PLL.PLLN = 336;

                RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;

                RCC_OscInitStruct.PLL.PLLQ = 7;

                HAL_RCC_OscConfig(&RCC_OscInitStruct);

                /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks

                dividers */

                RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);

                RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

                RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

                RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;

                RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

                HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);

               

                __GPIOC_CLK_ENABLE();

}

 

 

void InitIODriver (GPIO_InitTypeDef* GPIO_InitStruct)

{

                __TIM3_CLK_ENABLE();

                GPIO_InitStruct->Pin = GPIO_PIN_6;

  GPIO_InitStruct->Mode = GPIO_MODE_AF_PP;  /*alternade mode for PWM*/

  GPIO_InitStruct->Pull = GPIO_PULLUP;                                            /*no pull up nor pull down!*/

  GPIO_InitStruct->Speed = GPIO_SPEED_HIGH;

                GPIO_InitStruct->Alternate = GPIO_AF2_TIM3;

}             

 

 

void Define_MyPWM_Struct(TIM_HandleTypeDef* TIM_Handle_PWM_Struct)

{

                /*Enable the TIM interface clock using*/

                //__TIM3_CLK_ENABLE();              //Enable TIM interface Clock 

                TIM_Handle_PWM_Struct->Instance=TIM3;

                TIM_Handle_PWM_Struct->Init.Prescaler=0xFFFF;       //168MHz/2^16=2563Hz=0,39ms

                TIM_Handle_PWM_Struct->Init.CounterMode=TIM_COUNTERMODE_UP;

                TIM_Handle_PWM_Struct->Init.Period=Periode;//Time=2563*(1/2563Hz)=1s

                TIM_Handle_PWM_Struct->Init.ClockDivision=TIM_CLOCKDIVISION_DIV1;

                TIM_Handle_PWM_Struct->Init.RepetitionCounter=0;

 

}

 

void Define_OutputChannelStruct(TIM_OC_InitTypeDef* Struct)

{

                Struct->OCMode=TIM_OCMODE_PWM1;

                Struct->OCIdleState=TIM_OCIDLESTATE_SET;

                Struct->OCNIdleState=TIM_OCNIDLESTATE_RESET;

                Struct->OCPolarity=TIM_OCPOLARITY_HIGH;//by happening will be one?

                Struct->OCNPolarity=TIM_OCPOLARITY_HIGH;

                Struct->OCFastMode=TIM_OCFAST_DISABLE;

}

 

void CalculatePWMPercent(TIM_OC_InitTypeDef* Struct, uint8_t Percent)

{

                Struct->Pulse=(uint32_t)((Periode*Percent)/100);

}

Outcomes