AnsweredAssumed Answered

PWM control with DMA and ADC

Question asked by siqueira.erisson on Jun 21, 2012
Latest reply on Jun 21, 2012 by siqueira.erisson

How can I control PWM led PB.8 (TIM4_CH3)
changing value through  potentiometer at PC.4 (GPIO_Mode_AIN)
usind DMA???


%%%%%CODE BELOW HAS A PROBLEM%%%%%%%%

/**************************************************** Include ********************************************************/
#include <stm32f10x_lib.h>  
/*********************************************************************************************************************/

/**************************************************** Define *********************************************************/
#define ADC1_DR_Address     0x4001244C 
#define TIM4_CCR3_Address   0x4000083C 
/*********************************************************************************************************************/

/************************************************* Init Structure ****************************************************/
ErrorStatus    HSEStartUpStatus;
GPIO_InitTypeDef   GPIO_InitStructure;
DMA_InitTypeDef      DMA_InitStructure;
ADC_InitTypeDef      ADC_InitStructure;
TIM_OCInitTypeDef    TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/*********************************************************************************************************************/

/********************************************** Function Prototype ***************************************************/
void ADC_Configuration(void);
void DMA_Configuration(void);
void RCC_Configuration(void);
void TIM_Configuration(void);
void GPIO_Configuration(void);
/*********************************************************************************************************************/

/**************************************************** main ***********************************************************/
int main()
{  
  RCC_Configuration();
  GPIO_Configuration();
  DMA_Configuration();
  ADC_Configuration();
  TIM_Configuration();

  // Enable ADC1
    ADC_Cmd(ADC1, ENABLE);

    // Enable ADC1 reset calibaration register 
    ADC_ResetCalibration(ADC1);

    // Check the end of ADC1 reset calibration register
    while(ADC_GetResetCalibrationStatus(ADC1));

    // Start ADC1 calibaration
    ADC_StartCalibration(ADC1);

    // Check the end of ADC1 calibration
    while(ADC_GetCalibrationStatus(ADC1));
 

    // Start ADC1 conversion 
    ADC_SoftwareStartConvCmd(ADC1, ENABLE);

  while (1);
}
/*********************************************************************************************************************/

 

/************************************************** CONFIGURATION ****************************************************/
/*********************************************************************************************************************/
/************************************************ RCC_Configuration **************************************************/
void RCC_Configuration(void)
{
  RCC_DeInit();                // Deinitialize RCC
    RCC_HSEConfig(RCC_HSE_ON);              // Enable HSE (external crystal)
   HSEStartUpStatus = RCC_WaitForHSEStartUp();          // Wait till HSE is ready

    if (HSEStartUpStatus == SUCCESS)
    {  
      FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);      // Enable Prefetch Buffer
      FLASH_SetLatency(FLASH_Latency_2);          // Flash 2 wait state
      RCC_HCLKConfig(RCC_SYSCLK_Div1);          // HCLK = SYSCLK    = 72Mhz
      RCC_ADCCLKConfig(RCC_PCLK2_Div6);           // ADCCLK = PCLK2/6 = 12MHz
   RCC_PCLK2Config(RCC_HCLK_Div1);           // PCLK2 = HCLK    = 72Mhz
      RCC_PCLK1Config(RCC_HCLK_Div2);           // PCLK1 = HCLK / 2   = 36Mhz
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);     // PLLCLK = 8Mhz x 9   = 72Mhz
      RCC_PLLCmd(ENABLE);              // Enable PLL
      
   while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);     // Wait till PLL is ready
   
      RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);         // Select PLL as system clock source
   
      while (RCC_GetSYSCLKSource() != 0x08);         // Wait till PLL is used as system clock source
    }

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);       // DMA clock enable
  
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);      // Enable TIMER4 clock

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC |   // Enable GPIOB, GPIOC , ADC1 and TIM1 Periph clock
                              RCC_APB2Periph_ADC1, ENABLE);                                    
}    
/*********************************************************************************************************************/

/*********************************************** GPIO_Configuration **************************************************/
void GPIO_Configuration(void)
{
  // ADC1 CH14 pin configuration
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;           
  GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_4); 
  GPIO_Init(GPIOC, &GPIO_InitStructure);

  // Configure LED (PB.8) = TIM4_CH3
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;         // PB.8 = TIM4_CH3
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;       // Configure the speed (Output Maximum Frequency = 50 MHz)
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;           // Configure the operating mode (Alternate push-pull)
  GPIO_Init(GPIOB , &GPIO_InitStructure);           // Initialize port B peripheral
}                        
/*********************************************************************************************************************/

/************************************************ TIM_Configuration **************************************************/
void TIM_Configuration(void)

  // Time base configuration
  TIM_TimeBaseStructure.TIM_Period = 0xFFF;       // TIM4->ARR = 1s , TIM_Period configures the period value to be loaded into the active Auto-Reload Register at the next update event(0x0000...0xFFFF)     
    TIM_TimeBaseStructure.TIM_Prescaler = 3000;          // Configure the prescaler value used to divide the TIM clock (0x0000...0xFFFF)
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;       // Configure the clock division
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // Select the counter mode
  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);    // Initializes the TIM Time Base Unit according to the parameters specified in the TIM_TimeBaseInitStruct
  
  // PWM1 Mode configuration: Channel3
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;           // TIM Output Compare mode = PWM
    //TIM_OCInitStructure.TIM_Channel = TIM_Channel_3;           // Select the TIM channel
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; // Sets the TIM Output Compare polarity to high
    TIM_OCInit(TIM4, &TIM_OCInitStructure);      // Initializes the TIM Channel 1 according to the parameters specified in the TIM_OCInitStruct
  
    TIM_Cmd(TIM4, ENABLE);          // TIM enable counter  

    // Enable TIM1 DMA interface
    TIM_DMACmd(TIM4, TIM_DMABase_CCR3, ENABLE);        
}
/*********************************************************************************************************************/

/*********************************************** ADC_Configuration ***************************************************/
void ADC_Configuration(void)

    ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
    ADC_InitStructure.ADC_ScanConvMode = DISABLE;
    ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
    ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_InitStructure.ADC_NbrOfChannel = 1;
    ADC_Init(ADC1, &ADC_InitStructure);
 
    ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_55Cycles5);

  ADC_DMACmd(ADC1, ENABLE);
}
/*********************************************************************************************************************/

/*********************************************** DMA_Configuration ***************************************************/
void DMA_Configuration(void)
{   
  DMA_DeInit(DMA1_Channel1);
    DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)TIM4_CCR3_Address;
    DMA_InitStructure.DMA_MemoryBaseAddr = (u32)ADC1_DR_Address;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
    DMA_InitStructure.DMA_BufferSize = 1;
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_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_M2M = DMA_M2M_Disable;
    DMA_Init(DMA1_Channel1, &DMA_InitStructure);
 
  // Enable DMA Channel1
    DMA_Cmd(DMA1_Channel1, ENABLE);        
}
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*********************************************************************************************************************/

// warning
//Source\main.c(129): warning:  #223-D: function "TIM_OCInit" declared implicitly

Outcomes