AnsweredAssumed Answered

PWM frequency, I do not get the expected values, why?

Question asked by capasso.ornella on Oct 12, 2012
Latest reply on Oct 13, 2012 by capasso.ornella
I want to set the PWM frequency to 42 Khz, I follow the example formulas but the result is ... I'm looking in the manual but can not find an explanation ... Can you help me please?
in file System_stm32f4xx.c I stettato the variable SystemCoreClock = 168000000, then I set Tim_counterClock = 28000000 and run the following accounts:


prescaler = [(SystemCoreClock / 2) / Tim_counterClock] -1 = 2
Tim3frequency Tim_counterClock = / (ARR + 1)
where ARR +1 = 666, but the result instead of 42Khz is 35.99Khz


prescaler = [(SystemCoreClock / 2) / Tim_counterClock] -1 = 2
Tim3frequency Tim_counterClock = / (ARR + 1)
where ARR +1 = 666, but the result instead of 42Khz is 35.99Khz

place the setting of the TIM

/**
  ******************************************************************************
  * @file    ADC3_DMA/main.c
  * @author  MCD Application Team
  * @version V1.0.0
  * @date    19-September-2011
  * @brief   Main program body
  ******************************************************************************
  * @attention
  * un vincente trova  sempre una soluzione :-)
  ******************************************************************************
  */
 
/* Includes ------------------------------------------------------------------*/
#include "stm32f4_discovery.h"
#include <stdio.h>
#include <main.h>
 
/** @addtogroup STM32F4_Discovery_Peripheral_Examples
  * @{
  */
 
/** @addtogroup ADC_ADC3_DMA
  * @{
  */
 
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define ADC3_DR_ADDRESS     ((uint32_t)0x4001224C)
 
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/******VARIABILI UTILIZZATE PER L'ACQUISIZIONE*********/
__IO uint16_t ADC3ConvertedValue[3] = {0,0,0};  //Vettore che contiene i valori convertiti dai tre canali
__IO uint32_t ADC3ConvertedVoltage[3];                  //Vettore sul quale effettuare le operazioni
__IO uint32_t Soglia=1500;
__IO uint32_t K_adc=3000;                                               //Costante di conversione dell'ADC
 
/******VARIABILI UTILIZZATE PER LA GENERAZIONE DEL PWM*********/
uint16_t CCR1_Val = 333;
uint16_t PrescalerValue = 0;
uint16_t Tim_counter=28000000;
//uint16_t  Tim3_period=666;
 
/**********VARIABILI VARIE ************/
static __IO uint32_t TimingDelay;                               // Variabile per il Time Delay
int i=0, j=0;                                                                       //Variabili per cicli
uint32_t v=0,mv=0;                              //Variabili d'appoggio
 
 
/*************INIZIALIZZAZIONI********************/
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure;
 
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
void ADC3_CH_0_1_2_DMA_Config(void);    //Funzione di configurazione ADC e DMA
void TIM_Config(void);                              //Funzione di configurazione del Tim
void TIM_Base_Config(void);                    
void Delay(__IO uint32_t nTime);            ////Funzione di configurazione Delay
 
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f4xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f4xx.c file
     */
  /* ADC3 configuration *******************************************************/
  /*  - Enable peripheral clocks                                              */
  /*  - DMA2_Stream0 channel2 configuration                                   */
  /*  - Configure ADC Channel12 pin as analog input                           */
  /*  - Configure ADC3 Channel12                                              */
     
    ADC3_CH_0_1_2_DMA_Config(); // ADC3 Configuration
  TIM_Config();                             // TIM Configuration
    //TIM_Base_Config();                    //TIM base configuration
 
  /* Start ADC3 Software Conversion */
  ADC_SoftwareStartConv(ADC3);
     
          
  /* Initializza i Led mountati sulla STM32F4-Discovery board */
  STM_EVAL_LEDInit(LED4);
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED5);
  STM_EVAL_LEDInit(LED6);
     
    /* Compute the prescaler value */
  PrescalerValue = (uint16_t)((SystemCoreClock /2) /28000000) - 1;
     
     
    /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 666;
  TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
 
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
 
  /* PWM1 Mode configuration: Channel 1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
 
  TIM_OC1Init(TIM3, &TIM_OCInitStructure);
 
  TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
 
   
 
  TIM_ARRPreloadConfig(TIM3, ENABLE);
 
  /* TIM3 enable counter */
  TIM_Cmd(TIM3, ENABLE);
     
    /*TIM BASE CONFIGURATION END*/
     
  while (1)
  {
        /* convert the ADC value (from 0 to 0xFFF) to a voltage value (from 0V to 3.3V)*/
    for(i=0; i<3; i++)
    ADC3ConvertedVoltage[i] = ADC3ConvertedValue[i] *K_adc/0xFFF;
 
        if( ADC3ConvertedVoltage[0]>Soglia){
        STM_EVAL_LEDOn(LED4);
             
}
        else{
            STM_EVAL_LEDOff(LED4);
}
if( ADC3ConvertedVoltage[1]>Soglia){
        STM_EVAL_LEDOn(LED5);
             
}
        else{
            STM_EVAL_LEDOff(LED5);
}
if( ADC3ConvertedVoltage[2]>Soglia){
        STM_EVAL_LEDOn(LED6);
             
}
        else{
            STM_EVAL_LEDOff(LED6);
}
         
  }
}
 
/*FUNZIONI START*/
 
/*
  * @brief  ADC3 channel12 with DMA configuration
  * @param  None
  * @retval None
  */
void ADC3_CH_0_1_2_DMA_Config(void)
{
  ADC_InitTypeDef       ADC_InitStructure;
  ADC_CommonInitTypeDef ADC_CommonInitStructure;
  DMA_InitTypeDef       DMA_InitStructure;
  GPIO_InitTypeDef      GPIO_InitStructure;
   
  /* Enable ADC3, DMA2 and GPIO clocks ****************************************/
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOA, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE);
   
  /* DMA2 Stream0 channel2 configuration **************************************/
  DMA_InitStructure.DMA_Channel = DMA_Channel_2;
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC3_DR_ADDRESS;
  DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC3ConvertedValue[0];
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
  DMA_InitStructure.DMA_BufferSize = 3;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//Disable
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;    //Disable  
  DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  DMA_Init(DMA2_Stream0, &DMA_InitStructure);
  DMA_Cmd(DMA2_Stream0, ENABLE);
       
     
  /* Configure ADC3 Channel 7 pin as analog input ******************************/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2;               /*MODIFICATO*/
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
   
  /* ADC Common Init **********************************************************/
  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
  ADC_CommonInit(&ADC_CommonInitStructure);
   
  /* ADC3 Init ****************************************************************/
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;//DISABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConvEdge_None;//ADC_ExternalTrigConv_T1_CC1;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfConversion = 3; //Perchè effettuo ttue e tre le conversioni dall'ADC3
  ADC_Init(ADC3, &ADC_InitStructure);
   
  /* ADC3 regular channel7 configuration *************************************/
 /****MODIFICATO*****/
  ADC_RegularChannelConfig(ADC3, ADC_Channel_0, 1, ADC_SampleTime_3Cycles);   /****MODIFICATO*****/
  ADC_RegularChannelConfig(ADC3, ADC_Channel_1, 2, ADC_SampleTime_3Cycles);  /****MODIFICATO*****/
  ADC_RegularChannelConfig(ADC3, ADC_Channel_2, 3, ADC_SampleTime_3Cycles);  /****MODIFICATO*****/
    
 /* Enable DMA request after last transfer (Single-ADC mode) */
  ADC_DMARequestAfterLastTransferCmd(ADC3, ENABLE);
   
  /* Enable ADC3 DMA */
  ADC_DMACmd(ADC3, ENABLE);
   
  /* Enable ADC3 */
  ADC_Cmd(ADC3, ENABLE);
}
 
/**********             *******/
 
void TIM_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
 
  /* TIM3 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
 
  /* GPIOC clock enable */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
   
  /* GPIOC Configuration: TIM3 CH1 (PC6)  */
  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(GPIOC, &GPIO_InitStructure);
   
 
  /* Connect TIM3 pins to AF2 */ 
  GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM3);
}
 
 
 
#ifdef  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) */
 
  /* Infinite loop */
  while (1)
  {
  }
}
#endif
 
/**
  * @}
  */
 
/**
  * @}
  */
 
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/


Outcomes