Maker Pro
Maker Pro

STM32f407 board 5V pulse with 40kHz frequency

Hi! I modified the following program to get a pulse of 5V and 40kHz of frequency. I tried it in an oscilloscope and I get a 4V pulse and 12,8kHz frequency. What is wrong in the code??

Code:
#include "stm32f4xx.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_tim.h"
void TM_LEDS_Init(void) {
    GPIO_InitTypeDef GPIO_InitStruct;
    /* Clock for GPIOA */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    */
    /* Alternating functions for pins */
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_TIM13);
    /* Set pins */
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
    GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_Init(GPIOA, &GPIO_InitStruct);
}
void TM_TIMER_Init(void) {
    TIM_TimeBaseInitTypeDef TIM_BaseStruct;
    /* Enable clock for TIM13 */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM13, ENABLE);
/*
    Set timer prescaler
    Timer count frequency is set with
    timer_tick_frequency = Timer_default_frequency / (prescaller_set + 1)
    In our case, we want a max frequency for timer, so we set prescaller to 0
    And our timer will have tick frequency
    timer_tick_frequency = 84000000 / (0 + 1) = 84000000
*/
    TIM_BaseStruct.TIM_Prescaler = 0;
    /* Count up */
    TIM_BaseStruct.TIM_CounterMode = TIM_CounterMode_Up;
/*
    Set timer period when it have reset
    First you have to know max value for timer
    In our case it is 16bit = 65535
    To get your frequency for PWM, equation is simple
    PWM_frequency = timer_tick_frequency / (TIM_Period + 1)
    If you know your PWM frequency you want to have timer period set correct
    TIM_Period = timer_tick_frequency / PWM_frequency - 1
    In our case, for 40Khz PWM_frequency, set Period to
    TIM_Period = 84000000 / 40000 - 1 = 2099
    If you get TIM_Period larger than max timer value (in our case 65535),
    you have to choose larger prescaler and slow down timer tick frequency
*/
    TIM_BaseStruct.TIM_Period = 2099; /* 10kHz PWM */
    TIM_BaseStruct.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_BaseStruct.TIM_RepetitionCounter = 0;
    /* Initialize TIM13 */
    TIM_TimeBaseInit(TIM13, &TIM_BaseStruct);
    /* Start count on TIM13 */
    TIM_Cmd(TIM13, ENABLE);
}
void TM_PWM_Init(void) {
    TIM_OCInitTypeDef TIM_OCStruct;
    /* Common settings */
    /* PWM mode 2 = Clear on compare match */
    /* PWM mode 1 = Set on compare match */
    TIM_OCStruct.TIM_OCMode = TIM_OCMode_PWM2;
    TIM_OCStruct.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCStruct.TIM_OCPolarity = TIM_OCPolarity_Low;
/*
    To get proper duty cycle, you have simple equation
    pulse_length = ((TIM_Period + 1) * DutyCycle) / 100 - 1
    where DutyCycle is in percent, between 0 and 100%
    50% duty cycle:     pulse_length = ((2099 + 1) * 50) / 100 - 1 = 1049
    Remember: if pulse_length is larger than TIM_Period, you will have output HIGH all the time
*/
    TIM_OCStruct.TIM_Pulse = 1049; /* 50% duty cycle */
    TIM_OC1Init(TIM13, &TIM_OCStruct);
    TIM_OC1PreloadConfig(TIM13, TIM_OCPreload_Enable);
}
int main(void) {
    /* Initialize system */
    SystemInit();
    /* Init leds */
    TM_LEDS_Init();
    /* Init timer */
    TM_TIMER_Init();
    /* Init PWM */
    TM_PWM_Init();
    while (1) {
    }
}

Thank you
 
Top