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