Topic : STM32F103 DAC+DMA+Tim

Forum : ARM

Original Post
Post Information Post
March 7, 2016 - 7:24pm
Sylvain LAURENT

Hello,

i'm trying this code to generate a sinus signal but it doesn't work in 12bit.

In 8 bit no problem.

is there someone can help me please ?

//**************************************************************************************
 
const uint16_t Sine12bit[32] = {
  2047, 2447, 2831, 3185,
  3498, 3750, 3939, 4056,
  4095, 4056, 3939, 3750,
  3495, 3185, 2831, 2447,
  2047, 1647, 1263,  909,
   599,  344,  155,   38,
     0,   38,  155,  344,
   599,  909, 1263, 1647};
 
//**************************************************************************************
 
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
 
  /* GPIOA Periph clock enable */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
 
  /* Configure PA4 in analogue (output) mode */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
 
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; /* overkill? */
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}
 
//**************************************************************************************
 
void TIM_Configuration(void)
{
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  int Period;
 
  /* TIM2 Periph clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
 
  Period = (SystemCoreClock / (20000 * 32)); // 20 KHz Sine, 32 sample wave table
 
  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = Period - 1;
  TIM_TimeBaseStructure.TIM_Prescaler = 0;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0; // not relevant here
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
 
  /* TIM2 TRGO selection: update event is selected as trigger for DAC */
  TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);
 
  /* TIM2 enable counter */
  TIM_Cmd(TIM2, ENABLE);
}
 
//**************************************************************************************
 
void DAC_Configuration(void)
{
  DAC_InitTypeDef DAC_InitStructure;
 
  /* DAC Periph clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
 
  /* Fill DAC InitStructure */
  DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; /* Select the receiving end */
  DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
 
  /* DAC channel1 Configuration */
  DAC_Init(DAC_Channel_1, &DAC_InitStructure);
 
  /* Enable DAC Channel1: Once the DAC channel1 is enabled, PA.04 is
     automatically connected to the DAC converter. */
  DAC_Cmd(DAC_Channel_1, ENABLE);
 
  /* Enable DMA for DAC Channel2 */
  DAC_DMACmd(DAC_Channel_1, ENABLE);
}
 
//**************************************************************************************
 
void DMA_Configuration(void)
{
  DMA_InitTypeDef DMA_InitStructure;
 
  /* Enable DMA1 clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
 
  DMA_DeInit(DMA1_Channel3);
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&DAC->DHR12R1;
  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; // 16-bit
  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_Channel3, &DMA_InitStructure);
 
  /* Enable DMA1 Channel3 */
  DMA_Cmd(DMA1_Channel3, ENABLE);
}
 
//**************************************************************************************
 
int main(void)
{
  GPIO_Configuration();
 
  DMA_Configuration();
 
  DAC_Configuration();
 
  TIM_Configuration();
 
  /* Infinite loop */
  while(1);
}
 
//**************************************************************************************
 
#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