STM32 DAC - FabLabSeoul/WingProject GitHub Wiki

DAC (Digital to Analog Converter) ํ”„๋กœ๊ทธ๋ž˜๋ฐ

STM32 Value line discovery ๋ณด๋“œ์šฉ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํŠœํ† ๋ฆฌ์–ผ์ž…๋‹ˆ๋‹ค.

์•ž์œผ๋กœ ์„ค๋ช…ํ•  ์˜ˆ์ œ์ฝ”๋“œ๋Š” http://elk.informatik.fh-augsburg.de/pub/stm32lab/libs/STM32F10x_StdPeriph_Lib_V3.5.0/Project/STM32F10x_StdPeriph_Examples/Library_Examples.html ์›น ํŽ˜์ด์ง€์—์„œ DAC -> DAC one channel noise wave ๋กœ ์ฐพ์•„๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ข‹์€ ์˜ˆ์ œ๊ฐ€ ๋งŽ์•„์„œ ๋„์›€์ด ๋œ๋‹ค.

DAC๋Š” ADC์™€ ๋ฐ˜๋Œ€์˜ ์—ญํ• ์„ ํ•œ๋‹ค. ๋””์ง€ํ„ธ ์ •๋ณด๋ฅผ ์•„๋‚ ๋กœ๊ทธ ์ •๋ณด๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ๊ธฐ๋Šฅ์„ ๋‹ด๋‹นํ•œ๋‹ค. ๋””์ง€ํ„ธ ์ •๋ณด๋Š” ํ”„๋กœ๊ทธ๋žจ๋‚ด์—์„œ ์“ฐ์ด๋Š” double ๊ฐ’์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. ๊ทธ ๊ฐ’์€ ํฌํŠธ๋ฅผ ํ†ตํ•ด ์ „์••์˜ ์„ธ๊ธฐ๋กœ ๋ณ€ํ™˜ํ•ด ์ถœ๋ ฅํ•œ๋‹ค. DAC์— ๊ด€๋ จ๋œ ๋ฌธ์„œ๋Š” ๋งค๋‰ด์–ผ์—๋„ ์ž˜ ๋‚˜์™€ ์žˆ์œผ๋‚˜, http://www.st.com/web/en/resource/technical/document/application_note/DM00049125.pdf ๋ฌธ์„œ์—๋„ ์‰ฝ๊ฒŒ ์ž˜ ์„ค๋ช…๋˜์–ด ์žˆ์–ด ๋„์›€์ด ๋œ๋‹ค. DAC์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋ฌธ์„œ๋กœ ํ™•์ธํ•˜๊ณ , ์—ฌ๊ธฐ์„œ๋Š” DAC๋ฅผ ์ด์šฉํ•ด์„œ noise wave๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์˜ˆ์ œ๋ฅผ ์„ค๋ช…ํ•œ๋‹ค.

DAC๋ฅผ ํ™œ์„ฑํ™”ํ•˜๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ PA4 ๋ฒˆ ํฌํŠธ๊ฐ€ DAC ์ถœ๋ ฅ ํฌํŠธ๋กœ ์„ค์ •๋œ๋‹ค. ๊ทธ๋ž˜์„œ noise wave ์˜ˆ์ œ์—์„œ๋Š” GPIO๋ฅผ ์ดˆ๊ธฐํ™” ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ชจ๋“  ํฌํŠธ๋ฅผ ์ž…๋ ฅํฌํŠธ๋กœ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์—ฌ๋Ÿฌ ๋ฉด์—์„œ ํšจ์œจ์ ์ด๋‹ค. (๋งค๋‰ด์–ผ 187 ํŽ˜์ด์ง€) DAC๋ฅผ ํ™œ์„ฑํ™”ํ•˜๋Š” ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. STM32 ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•œ๋‹ค.

DAC_Cmd(DAC_Channel_1, ENABLE);

DAC ์ฑ„๋„

DAC๋Š” ๋‘ ๊ฐœ์˜ ์ปจ๋ฒ„ํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ๊ทธ๋ž˜์„œ ํ•œ๋ฒˆ์— ๋‘ ๊ฐœ์˜ ์•„๋‚ ๋กœ๊ทธ ๊ฐ’๋งŒ ๋””์ง€ํ„ธ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‘ ๊ฐœ์˜ ์ปจ๋ฒ„ํ„ฐ๋ฅผ ๋‘ ๊ฐœ์˜ ์ฑ„๋„์ด ์žˆ๋‹ค๊ณ  ์–˜๊ธฐํ•œ๋‹ค. ๋‘ ์ฑ„๋„ ๋™์‹œ์— ์ž‘๋™์‹œํ‚ค๋Š” ๊ฒƒ์„ Dual Channel์ด๋ผ๊ณ  ๋งํ•œ๋‹ค. DAC ์ฑ„๋„ ๋ธ”๋ก๋‹ค์ด์–ด๊ทธ๋žจ์€ ๋งค๋‰ด์–ผ 187ํŽ˜์ด์ง€์— ์ž˜ ๋‚˜์™€์žˆ๋‹ค.

DAC ๋ ˆ์ง€์Šคํ„ฐ ์ดˆ๊ธฐํ™”

DAC ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” DAC ๋ ˆ์ง€์Šคํ„ฐ๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค. ์ผ์ผ์ด ๋ฉ”๋ชจ๋ฆฌ์ฃผ์†Œ๋กœ ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์ด ๋ฒˆ๊ฑฐ๋กญ๊ธฐ ๋•Œ๋ฌธ์—, STM32 ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ DAC_TypeDef ๊ตฌ์กฐ์ฒด๋กœ ์ •์˜ํ•˜๊ณ  ์žˆ๋‹ค.

/** 
  * @brief Digital to Analog Converter
  */
typedef struct
{
  __IO uint32_t CR;
  __IO uint32_t SWTRIGR;
  __IO uint32_t DHR12R1;
  __IO uint32_t DHR12L1;
  __IO uint32_t DHR8R1;
  __IO uint32_t DHR12R2;
  __IO uint32_t DHR12L2;
  __IO uint32_t DHR8R2;
  __IO uint32_t DHR12RD;
  __IO uint32_t DHR12LD;
  __IO uint32_t DHR8RD;
  __IO uint32_t DOR1;
  __IO uint32_t DOR2;
#if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL)
  __IO uint32_t SR;
#endif
} DAC_TypeDef;

DAC_CR ๋ ˆ์ง€์Šคํ„ฐ ์˜ต์…˜ ์„ค์ •

DAC_CR ๋ ˆ์ง€์Šคํ„ฐ์— ๊ธฐ๋ณธ์ ์ธ DAC ๊ธฐ๋Šฅ์„ ์ดˆ๊ธฐํ™” ํ•œ๋‹ค. ์ด๋ฅผ ๋„์™€์ฃผ๋Š” STM32 ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ•จ์ˆ˜๊ฐ€ stm32f10x_dac.c์— ์ •์˜ ๋˜์–ด ์žˆ๋‹ค. ์ด ์˜ˆ์ œ์—์„œ๋Š” void DAC_Init(uint32_t DAC_Channel, DAC_InitTypeDef* DAC_InitStruct) ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด์„œ DAC_CR๋ ˆ์ง€์Šคํ„ฐ๋ฅผ ์ดˆ๊ธฐํ™” ํ•œ๋‹ค. (DAC_CR ๋ ˆ์ง€์Šคํ„ฐ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋งค๋‰ด์–ผ 197ํŽ˜์ด์ง€๋ฅผ ์ฐธ์กฐํ•˜์ž.)

/* DAC channel1 Configuration */
DAC_InitStructure.DAC_Trigger = DAC_Trigger_Software;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_Noise;
DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bits8_0;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
DAC_Init(DAC_Channel_1, &DAC_InitStructure);

DAC ์†Œ์Œ๋ฐœ์ƒ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ๋‹ค. ์ด ๋ฐ–์— ์—ฌ๋Ÿฌ๊ฐ€์ง€ wave generation ๊ธฐ๋Šฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ๊ฐ„๋‹จํžˆ ํ”Œ๋ž˜๊ทธ ๊ฐ’๋“ค์„ ์„ค๋ช…ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • DAC_Trigger_Software : DAC ์ฒ˜๋ฆฌ๋ฅผ ๋‚ด๋ถ€์—์„œ ์ƒ์„ฑํ•ด์„œ ์ฒ˜๋ฆฌํ•จ.
  • DAC_WaveGeneration_Noise : ์†Œ์Œ ํŒŒํ˜•์„ ์ถœ๋ ฅ
  • DAC_LFSRUnmask_Bits8_0 : ์†Œ์Œ ์„ธ๊ธฐ ์กฐ์ ˆ
  • DAC_OutputBuffer_Enable : ์ถœ๋ ฅ๋ฒ„ํผ ํ™œ์„ฑํ™”.

DAC ํด๋Ÿญ ์„ค์ •

PA4 ํฌํŠธ์— ์•„๋‚ ๋กœ๊ทธ ์‹ ํ˜ธ๋ฅผ ๋ณด๋‚ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— GPIO ํด๋Ÿญ์ด ํ™œ์„ฑํ™” ๋˜์–ด์žˆ์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋””์ง€ํ„ธ์ •๋ณด๊ฐ€ ์•„๋‚ ๋กœ๊ทธ๋กœ ๋ณ€ํ™˜๋˜์–ด ์ถœ๋ ฅํฌํŠธ๋กœ ์ „์†ก๋œ ํ›„์— ์ •๋ณด๋ฅผ ์—…๋ฐ์ดํŠธ ํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์—, DAC ์ „์šฉ ํด๋Ÿญ ์„ค์ •์ด ํ•„์š”ํ•˜๋‹ค. DAC ํด๋Ÿญ์„ค์ •์€ GPIO ํด๋Ÿญ๊ณผ๋Š” ๋‹ค๋ฅด๋‹ค. (APB_CLK1์„ ์‚ฌ์šฉํ•œ๋‹ค.) ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋งค๋‰ด์–ผ 189ํŽ˜์ด์ง€ ํƒ€์ด๋ฐ ๋‹ค์ด์–ด๊ทธ๋žจ์—์„œ ํ™•์ธํ•˜์ž.

/**
  * @brief  Configures the different system clocks.
  * @param  None
  * @retval None
  */
void RCC_Configuration(void)
{   
  /* Enable peripheral clocks ------------------------------------------------*/
  /* GPIOA Periph clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  /* DAC Periph clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
}

DAC ํ™œ์„ฑํ™”

DAC๋ฅผ ํ™œ์„ฑํ™” ํ•˜๊ณ , DHR12(Data holding register)๋ ˆ์ง€์Šคํ„ฐ์— ๊ฐ’์„ ์ดˆ๊ธฐํ™” ํ•œ๋‹ค. (๋งค๋‰ด์–ผ 187ํŽ˜์ด์ง€ ๋ธ”๋Ÿญ๋‹ค์ด์–ด๊ทธ๋žจ)

/* Enable DAC Channel1: Once the DAC channel1 is enabled, PA.04 is 
   automatically connected to the DAC converter. */
DAC_Cmd(DAC_Channel_1, ENABLE);

/* Set DAC Channel1 DHR12L register */
DAC_SetChannel1Data(DAC_Align_12b_L, 0x7FF0);

SWTRIGR ๋ ˆ์ง€์Šคํ„ฐ ํ™œ์„ฑํ™”

๋งค ๋ฃจํ”„ ๋•Œ๋งˆ๋‹ค DAC->SWTRIGR ๋ ˆ์ง€์Šคํ„ฐ๋ฅผ ํ™œ์„ฑํ™”ํ•œ๋‹ค. SWTRIGR(Software Trigger)๋Š” DAC_DORx๋ ˆ์ง€์Šคํ„ฐ๊ฐ€ ์—…๋ฐ์ดํŠธ ๋  ๋•Œ๋งˆ๋‹ค ์ดˆ๊ธฐํ™” ๋˜๊ธฐ ๋•Œ๋ฌธ์—, ๋งค๋ฒˆ ํ™œ์„ฑํ™” ํ•ด์•ผํ•œ๋‹ค. SWTRIGR๊ฐ€ ํ™œ์„ฑํ™” ๋˜์ง€ ์•Š์œผ๋ฉด, DAC->DOR ๋ ˆ์ง€์Šคํ„ฐ๊ฐ€ ์—…๋ฐ์ดํŠธ ๋˜์ง€ ์•Š์•„์„œ ํ•ญ์ƒ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค.(๋งค๋‰ด์–ผ 190 ํŽ˜์ด์ง€), (๋งค๋‰ด์–ผ 187ํŽ˜์ด์ง€ ๋ธ”๋Ÿญ๋‹ค์ด์–ด๊ทธ๋žจ)

while (1)
{
  /* Start DAC Channel1 conversion by software */
  DAC_SoftwareTriggerCmd(DAC_Channel_1, ENABLE);
}

Noise Wave๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์˜ˆ์ œ

PA4๋ฒˆ ํฌํŠธ์— ๋…ธ์ด์ฆˆ ์›จ์ด๋ธŒ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์˜ˆ์ œ ์ฝ”๋“œ๋‹ค. ์ด ์˜ˆ์ œ๋Š” STM32\stm32vldiscovery_package\Project\Examples\DAC ํด๋”์— ์žˆ๋‹ค.

#include "stm32f10x.h"

DAC_InitTypeDef  DAC_InitStructure;

void RCC_Configuration(void);
void GPIO_Configuration(void);
void Delay(__IO uint32_t nCount);


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_stm32f10x_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f10x.c file
     */     
       
  /* System Clocks Configuration */
  RCC_Configuration();

  /* Once the DAC channel is enabled, the corresponding GPIO pin is automatically 
     connected to the DAC converter. In order to avoid parasitic consumption, 
     the GPIO pin should be configured in analog */
  GPIO_Configuration();

  /* DAC channel1 Configuration */
  DAC_InitStructure.DAC_Trigger = DAC_Trigger_Software;
  DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_Noise;
  DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bits9_0;
  DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
  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);

  /* Set DAC Channel1 DHR12L register */
  DAC_SetChannel1Data(DAC_Align_12b_L, 0x7FF0);

  while (1)
  {
    /* Start DAC Channel1 conversion by software */
    DAC_SoftwareTriggerCmd(DAC_Channel_1, ENABLE);
  }
}

/**
  * @brief  Configures the different system clocks.
  * @param  None
  * @retval None
  */
void RCC_Configuration(void)
{   
  /* Enable peripheral clocks ------------------------------------------------*/
  /* GPIOA Periph clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  /* DAC Periph clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
}

/**
  * @brief  Configures the different GPIO ports.
  * @param  None
  * @retval None
  */
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  /* Once the DAC channel is enabled, the corresponding GPIO pin is automatically 
     connected to the DAC converter. In order to avoid parasitic consumption, 
     the GPIO pin should be configured in analog */
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_4;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}


/**
  * @brief  Inserts a delay time.
  * @param  nCount: specifies the delay time length.
  * @retval None
  */
void Delay(__IO uint32_t nCount)
{
  for(; nCount != 0; nCount--);
}

#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****/

๋‹ค์Œ ์˜ˆ์ œ

  • DAC ๋‘ ์ฑ„๋„์„ ์ด์šฉํ•ด ๋‘ ๊ฐœ์˜ ํฌํŠธ์— ์ถœ๋ ฅ์„ ๋ณด๋‚ด๋Š” ์˜ˆ์ œ
  • STM32 DAC Dual Channel