AnsweredAssumed Answered

TIM1 or TIM8 configuration

Question asked by mukati.umair.001 on Oct 1, 2013
Latest reply on Oct 2, 2013 by mukati.umair.001
Sir,
I am able to run TIM2 and others but in using advance timers like TIM1 or TIM8, I am unable to get it configure to use it with PLL. Although I have configured it. But I cant find it working with the DMA application of mine. I am attaching the file if you are able to detect the issue in configuration. And I am using STM32F303VG
..............................................................................................................................................
#include "main.h"


#define SystemFrequency                     72000000
#define DAC_DHR12R1_ADDRESS            0x40007408
#define DAC_DHR8R1_ADDRESS            0x40007410


static void RCC_Configuration(void);
static void GPIO_Configuration(void);
static void DAC_Configuration(void);
static void TIM2_Configuration(void);
static void TIM8_Configuration(void);
static void WAVE_Configuration(void);


ErrorStatus                                    HSEStartUpStatus;
TIM_TimeBaseInitTypeDef       TIM2_TimeBaseStructure;
GPIO_InitTypeDef                      GPIO_InitStructure;
NVIC_InitTypeDef                          NVIC_InitStructure;
DAC_InitTypeDef           DAC_InitStructure;
DMA_InitTypeDef           DMA_InitStructure;


const uint16_t Sine12bit[32] = {
                                                       4094, 4055, 3938, 3749, 3494, 3184, 2830, 2446, 2047, 1648, 1264, 910, 600, 345, 156, 39, 0, 
                                                       39, 156, 345, 600, 910, 1264, 1648, 2047, 2446, 2830, 3184, 3494, 3749, 3938, 4055};


const uint8_t Escalator8bit[6] = {0x0, 0x33, 0x66, 0x99, 0xCC, 0xFF};


int main(void)
{
     uint32_t i = 0;
     
     /* Setting up of System Clock */
     GPIO_Configuration();
     /* TIM2 Configuration */
     TIM2_Configuration();
     /* Preconfiguration before using DAC */
  DAC_Configuration();
  /* TIM8 configuration to trigger DAC */
  TIM8_Configuration();
     /* Wave configuration */
     WAVE_Configuration();
     
     while(1)
     {
          GPIOE->ODR ^= GPIO_Pin_14;
          for(i=0;i<1000000;i++);
     }
}


static void GPIO_Configuration(void)

  /* Enable the GPIO_LED Clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE, ENABLE);


  /* Configure the GPIO_LED pin */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOE, &GPIO_InitStructure);
     
  /* Enable the GPIO_LED Clock */
     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);


  /* Configure the GPIO_LED pin */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
}


static void DAC_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure_1;


  /* DMA2 clock enable (to be used with DAC) */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);


  /* DAC Periph clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);


  /* GPIOA clock enable */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  
  /* Configure PA.04 (DAC_OUT1) as analog */
  GPIO_InitStructure_1.GPIO_Pin =  GPIO_Pin_4;
  GPIO_InitStructure_1.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure_1.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStructure_1);
}


static void TIM2_Configuration(void)
{
     /* TIM2 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);


  /* Enable the TIM2 gloabal Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
     
     /* Time base configuration */
  TIM2_TimeBaseStructure.TIM_Period = 360-1;                                             // 72M / 4M - 1
  TIM2_TimeBaseStructure.TIM_Prescaler = 0;
  TIM2_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM2_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
     
     TIM_TimeBaseInit(TIM2, &TIM2_TimeBaseStructure);
     
  /* Prescaler configuration */
  TIM_PrescalerConfig(TIM2, 0, TIM_PSCReloadMode_Immediate);
          
  /* TIM Interrupts enable */
  TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
     
     TIM_UpdateRequestConfig(TIM2,TIM_UpdateSource_Regular);
     
  /* TIM2 enable counter */
  TIM_Cmd(TIM2, ENABLE);
     
     TIM2->SR &= ~TIM_SR_UIF;
}


static void TIM8_Configuration(void)
{     
     TIM_TimeBaseInitTypeDef          TIM_TimeBaseStructure;
  /* TIM8 Periph clock enable */
  RCC_TIMCLKConfig(RCC_TIM8CLK_PLLCLK);
  
  /* Time base configuration */
  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); 
  TIM_TimeBaseStructure.TIM_Period = 120;          
  TIM_TimeBaseStructure.TIM_Prescaler = 0x0;       
  TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;    
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
  TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);


  /* TIM8 TRGO selection */
  TIM_SelectOutputTrigger(TIM8, TIM_TRGOSource_Update);
  
  /* TIM8 enable counter */
  TIM_Cmd(TIM8, ENABLE);
}


static void WAVE_Configuration(void)
{
     /* The sine wave and the escalator wave has been selected */
     /* Sine Wave generator ---------------------------------------------*/
     
     /* DAC channel1 Configuration */
     DAC_DeInit(); 
     DAC_InitStructure.DAC_Trigger = DAC_Trigger_T8_TRGO;
     DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
     DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bits11_0;
     DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
     
     /* DAC Channel1 Init */
     DAC_Init(DAC_Channel_1, &DAC_InitStructure);
     
     /* Enable DAC Channel1 */
     DAC_Cmd(DAC_Channel_1, ENABLE);
     
     /* DMA2 channel3 configuration */
     DMA_DeInit(DMA2_Channel3); 
     DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R1_ADDRESS;
     DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&Sine12bit;
     DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
     DMA_InitStructure.DMA_BufferSize = 32;
     DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
     DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
     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(DMA2_Channel3, &DMA_InitStructure);
     
     /* Enable DMA2 Channel3 */
     DMA_Cmd(DMA2_Channel3, ENABLE);
     
     /* Enable DMA for DAC Channel1 */
     DAC_DMACmd(DAC_Channel_1, ENABLE);
}


static void RCC_Configuration(void)
{   
  /* RCC system reset(for debug purpose) */
  RCC_DeInit();


  /* Enable HSE */
  RCC_HSEConfig(RCC_HSE_ON);


  /* Wait till HSE is ready */
  HSEStartUpStatus = RCC_WaitForHSEStartUp();




  if(HSEStartUpStatus == SUCCESS)
  {
    /* Enable Prefetch Buffer */
    FLASH_PrefetchBufferCmd(ENABLE);


    /* Flash 2 wait state */
    FLASH_SetLatency(FLASH_Latency_2);
      
    /* HCLK = SYSCLK */
    RCC_HCLKConfig(RCC_SYSCLK_Div1); 
  
    /* PCLK2 = HCLK */
    RCC_PCLK2Config(RCC_HCLK_Div1); 


    /* PCLK1 = HCLK/2 */
    RCC_PCLK1Config(RCC_HCLK_Div2);


    /* PLLCLK = 8MHz * 9 = 72 MHz */
    RCC_PLLConfig(RCC_CFGR_PLLSRC_PREDIV1, RCC_PLLMul_9);


    /* Enable PLL */ 
    RCC_PLLCmd(ENABLE);


    /* Wait till PLL is ready */
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
    {
    }


    /* Select PLL as system clock source */
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);


    /* Wait till PLL is used as system clock source */
    while(RCC_GetSYSCLKSource() != 0x08)
    {
    }
  }
}


#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 STMicroelectronics *****END OF FILE****/

Attachments

Outcomes