cancel
Showing results for 
Search instead for 
Did you mean: 

STM32f407 board 5V pulse with 40kHz frequency

fiulala
Associate II
Posted on March 13, 2015 at 12:33

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??

#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 #stm32f407 #stm32f4 #discovery #timers
7 REPLIES 7
Posted on March 13, 2015 at 13:32

12.8 KHz because you haven't set the HSE_VALUE and PLL settings correctly for your board. Looks to be assuming you have a 25 MHz source, but you really have an 8 MHz one.

4V would probably do with loading, and how aggressively you're pulling up the output. Think potential divider.

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

So how do I have to configure the HSE_VALUE and PLL settings?

Thank you for response

Posted on March 20, 2015 at 13:26

HSE_VALUE is often defined in stm32f4xx_conf.h for the project, and the PLL settings in system_stm32f4xx.c

For examples of these files/settings review the examples in the

http://www.st.com/web/en/catalog/tools/PF257904

. The red download button at the bottom.

...
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __STM32F4xx_CONF_H
#define __STM32F4xx_CONF_H
#if defined (HSE_VALUE)
/* Redefine the HSE value; it's equal to 8 MHz on the STM32F4-DISCOVERY Kit */
#undef HSE_VALUE
#define HSE_VALUE ((uint32_t)8000000)
#endif /* HSE_VALUE */
...

/* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N */
#define PLL_M 8
#define PLL_N 336

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

Thank you very much! I made it.

fiulala
Associate II
Posted on March 23, 2015 at 15:54

Hi Clive, one more question. Now that I have a timer making a pulse indefinitely I would like to configure another timer that control the first timer. The idea is that the new timer let me to do for example, pulses for 1 sec and then stop for 10 sec and then again pulses for 1 sec.....

Can you help me please?

Thanky you very much

Posted on March 23, 2015 at 17:35

Quick blind example, using a single timer, with modulating output.

// STM32F4-Discovery TIM13 40KHz PWM, modulating 1 second ON, 10 seconds OFF (PA.06 TIM13_CH1) - sourcer32@gmail.com

&sharpinclude ''stm32f4_discovery.h''

/**************************************************************************/

void RCC_Configure(void)

{

/* GPIOA clock enable */

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

/* TIM13 clock enable */

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM13, ENABLE);

}

/**************************************************************************/

void NVIC_Configure(void)

{

NVIC_InitTypeDef NVIC_InitStructure;

/* Enable the TIM13 global Interrupt */

NVIC_InitStructure.NVIC_IRQChannel = TIM8_UP_TIM13_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

}

/**************************************************************************/

void GPIO_Configure(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

/* GPIOA Configuration: TIM13 CH1 (PA6) */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;

GPIO_Init(GPIOA, &GPIO_InitStructure);

/* Connect TIM13 pins to AF */

GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_TIM13);

}

/**************************************************************************/

uint32_t Period;

void TIM13_Configure(void)

{

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

TIM_OCInitTypeDef TIM_OCInitStructure;

Period = (SystemCoreClock / 2) / 40000; // 40 KHz, from APB1 clock *2, nominally 84 MHz

/* Time base configuration */

TIM_TimeBaseStructure.TIM_Prescaler = 0;

TIM_TimeBaseStructure.TIM_Period = Period - 1;

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM13, &TIM_TimeBaseStructure);

/* PWM1 Mode configuration: Channel1 */

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

TIM_OCInitStructure.TIM_Pulse = Period / 2; // 50/50 duty

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

TIM_OC1Init(TIM13, &TIM_OCInitStructure);

TIM_OC1PreloadConfig(TIM13, TIM_OCPreload_Enable);

/* TIM Interrupts enable */

TIM_ITConfig(TIM13, TIM_IT_Update, ENABLE);

/* TIM enable counter */

TIM_Cmd(TIM13, ENABLE);

}

/**************************************************************************/

void TIM8_UP_TIM13_IRQHandler(void) // 40KHz

{

static int modulatingcount = 0;

// 1 Second On - 40000 ticks

// 10 Seconds Off - 400000 ticks

// full cycle 440000 ticks

if (TIM_GetITStatus(TIM13, TIM_IT_Update) != RESET) // Validate source

{

TIM_ClearITPendingBit(TIM13, TIM_IT_Update); // Clear source

modulatingcount = (modulatingcount + 1) % 440000;

if (modulatingcount > 40000)

TIM13->CCR1 = 0; // Off

else

TIM13->CCR1 = Period / 2; // 50/50 duty

}

}

/**************************************************************************/

int main(void)

{

RCC_Configure();

NVIC_Configure();

GPIO_Configure();

TIM13_Configure();

while(1); // Do not exit

}

/**************************************************************************/

&sharpifdef 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) */

while (1)

{}

}

&sharpendif

/**************************************************************************/

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
fiulala
Associate II
Posted on April 13, 2015 at 19:42

That looks good! But actually the pulses need to be very precise. I'm using this to create an ultrasonic sensor and calculate some distances, so I should configure the ''hardware'' not ''software'' (code). I read in some posts that maybe the solution is the slave mode, buy I don't know how that works.

Thank you very much