2015-01-09 07:49 AM
Hi
I'm a newbie to stm32 , I have a STM32F4 Discovery board with me. So what I want to do is to generate 2Mhz,4Mhz,8Mhz square wave on any of the Pins on the Header. Can you please provide some sample code for this? I've searched the forum but only found sine wave of 40Mhz generation Can you please help me #stm32f4 #pwm #discovery #output2015-01-09 08:51 AM
// STM32 PWM STM32F4 Discovery - sourcer32@gmail.com
#include ''stm32f4_discovery.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
/* --------------------------- System Clocks Configuration -----------------*/
/* TIM4 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
/* GPIOD clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*-------------------------- GPIO Configuration ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
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(GPIOD, &GPIO_InitStructure);
/* Connect TIM1 pins to AF */
GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4);
}
/**************************************************************************************/
void TIM4_Configuration(void)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
uint16_t Period;
Period = ((SystemCoreClock / 2000000) / 2); // 2 MHz (Integer Division of APB1*2 clock)
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = 0; // DIV1
TIM_TimeBaseStructure.TIM_Period = Period - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
/* Enable TIM4 Preload register on ARR */
TIM_ARRPreloadConfig(TIM4, 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 PD.12 */
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
/* TIM4 enable counter */
TIM_Cmd(TIM4, ENABLE);
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
TIM4_Configuration();
while(1); // Don't wait to exit
}
/**************************************************************************************/
2015-01-14 09:53 AM
Thanks Clive,
But this code seems to generate frequency at one pin, I need to generate 3 different frequencies on three pins simultaneously. How can I do it?2015-01-14 10:27 AM
But this code seems to generate frequency at one pin, I need to generate 3 different frequencies on three pins simultaneously.
And I'm not here to do your work.. Want to come paint my fence or shovel my driveway? You'd need to use multiple timers, and perhaps picking a different operating speed for the CPU so the specific frequencies you want are cleanly divisible (integer multiple).2023-10-17 02:37 AM
Thank you sm for this ! I am a student and this helped me a lot
I justneed a little help can you please guide me what the value of the system clock is and how I can change the output to 50Hz square wave or any value of hz for a square wave.
2023-10-17 08:24 AM
Don't hijack threads.
Start your own thread, stating what hardware are you using, what is the problem you try to solve and what did you do so far.
JW