2015-03-11 05:44 AM
I have been trying to generate a PWM of 1Mhz & 2Mhaz in different pins, with a system clock of 168Mhz, but unable to do so, can any body help me out with the code.
Please help me with a small code snippet for the same...Thanks in advance :) #discovery #stm32f4072015-03-17 07:07 AM
In the Coocox with zStm32f407, with below setting
* 5. This file configures the system clock as follows: *============================================================================= *============================================================================= * Supported STM32F4xx device revision | Rev A *----------------------------------------------------------------------------- * System Clock source | PLL (HSE) *----------------------------------------------------------------------------- * SYSCLK(Hz) | 64000000 *----------------------------------------------------------------------------- * HCLK(Hz) | 16000000 *----------------------------------------------------------------------------- * AHB Prescaler | 4 *----------------------------------------------------------------------------- * APB1 Prescaler | 1 *----------------------------------------------------------------------------- * APB2 Prescaler | 1 *----------------------------------------------------------------------------- * HSE Frequency(Hz) | 8000000 *----------------------------------------------------------------------------- * PLL_M | 4 *----------------------------------------------------------------------------- * PLL_N | 192 *----------------------------------------------------------------------------- * PLL_P | 6 *----------------------------------------------------------------------------- * PLL_Q | 8 *----------------------------------------------------------------------------- * PLLI2S_N | NA *----------------------------------------------------------------------------- * PLLI2S_R | NA *----------------------------------------------------------------------------- * I2S input clock | NA *----------------------------------------------------------------------------- * VDD(V) | 3.3 *----------------------------------------------------------------------------- * Main regulator output voltage | Scale1 mode *----------------------------------------------------------------------------- * Flash Latency(WS) | 0 *----------------------------------------------------------------------------- * Prefetch Buffer | OFF *----------------------------------------------------------------------------- * Instruction cache | ON *----------------------------------------------------------------------------- * Data cache | ON *----------------------------------------------------------------------------- * Require 48MHz for USB OTG FS, | Disabled * SDIO and RNG clock | *-----------------------------------------------------------------------------i am trying to generate 1Hz of pwm, this is the code for the samevoid TIM1_Configuration(void){ TIM_OCInitTypeDef TIM_OCInitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; uint16_t Period; Period = (SystemCoreClock / 1000000); /* Time base configuration */ TIM_TimeBaseStructure.TIM_Prescaler = 2624;//0; // Dump 1X clock into timer TIM_TimeBaseStructure.TIM_Period = 63999;// Period - 1; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); /* Enable TIM1 Preload register on ARR */ TIM_ARRPreloadConfig(TIM1, ENABLE); /* TIM PWM1 Mode configuration */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = ( TIM_TimeBaseStructure.TIM_Period +1)/ 2; // 50% TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; /* Output Compare PWM1 Mode configuration: Channel1 PA.08 */ TIM_OC1Init(TIM1, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); /* TIM1 Main Output Enable */ TIM_CtrlPWMOutputs(TIM1, ENABLE); /* TIM1 enable counter */ TIM_Cmd(TIM1, ENABLE);}but 1Hz I am not getting(no digital waveform, sinusoidal wave), I generated the values from the MikroElectronika Calculator tool.I am not getting whats wrong in the code.2015-03-17 07:32 AM
Ok, it helps to understand what you're doing rather than use tools that hide the detail.
Your system file suggests the timer is clocking at 64 MHz, not 168 MHzTo get 1 Hz you need to count 64000000 ticks at 64 MHz. A simple factoring of this gives1000 x 64000SoPrescaler = 1000 - 1;Period = 64000 - 1;Pulse = 64000 / 2; // Half of the Period