STM32 DAC Dual Channel - FabLabSeoul/WingProject GitHub Wiki
STM32 DAC Dual Channel ํ๋ก๊ทธ๋๋ฐ
STM32 Value line discovery ๋ณด๋์ฉ ํํ ๋ฆฌ์ผ์ ๋๋ค.
์ฌ๊ธฐ์ ์ค๋ช ํ๋ ์์ ์ฝ๋๋ http://elk.informatik.fh-augsburg.de/pub/stm32lab/libs/STM32F10x_StdPeriph_Lib_V3.5.0/Project/STM32F10x_StdPeriph_Examples/DAC/TwoChannels_TriangleWave/ ์ฌ์ดํธ์ ์๋ค.
STM32 DAC ์์ ์์๋ ํ๋์ ์ฑ๋๋ก ์๋ ๋ก๊ทธ ์ถ๋ ฅ์ ํ๋ค. ์ด ์์ ์์๋ ๋ ๊ฐ์ ์ฑ๋๋ก ์๋ ๋ก๊ทธ ์ถ๋ ฅ์ ๋ณด๋ธ๋ค. ๊ทธ๋ฆฌ๊ณ ํ์ด๋จธ๋ฅผ ํตํด ํด๋ญ์ ์กฐ์ ํ๋ค. STM32 DAC์์๋ Software Trigger๋ฅผ ํตํด ๋๊ธฐํ ์์ผฐ๋ค. DAC ๋๊ฐ์ ์ถ๋ ฅ์ PA4,5 ํฌํธ๋ก ๋์จ๋ค. STM32 ๊ธฐ๋ณธ ์ค์ ์ด๋ค.
๋จผ์ ํ์ด๋จธ๋ฅผ ์ค์ ํ์. STM32๋ PWM์ ์ํ TIM1๊ณผ ์ผ๋ฐ ์ฉ๋์ TIM2,3,4 ๊ฐ ์๋ค. ์์ธํ ๋ด์ฉ์ stm32f103์์ฉ.pdf๋ฅผ ์ฐธ์กฐํ์. ์ฌ๊ธฐ์๋ TIM2๋ฅผ ์ด์ฉํด DAC๋ฅผ ๋๊ธฐํ ์ํจ๋ค.
STM ๋ผ์ด๋ธ๋ฌ๋ฆฌ์์ ํ์ด๋จธ ๋ ์ง์คํฐ๋ฅผ ํํํ๋ ๊ตฌ์กฐ์ฒด๋ ๋ค์๊ณผ ๊ฐ๋ค.
/**
* @brief TIM
*/
typedef struct
{
__IO uint16_t CR1;
uint16_t RESERVED0;
__IO uint16_t CR2;
uint16_t RESERVED1;
__IO uint16_t SMCR;
uint16_t RESERVED2;
__IO uint16_t DIER;
uint16_t RESERVED3;
__IO uint16_t SR;
uint16_t RESERVED4;
__IO uint16_t EGR;
uint16_t RESERVED5;
__IO uint16_t CCMR1;
uint16_t RESERVED6;
__IO uint16_t CCMR2;
uint16_t RESERVED7;
__IO uint16_t CCER;
uint16_t RESERVED8;
__IO uint16_t CNT;
uint16_t RESERVED9;
__IO uint16_t PSC;
uint16_t RESERVED10;
__IO uint16_t ARR;
uint16_t RESERVED11;
__IO uint16_t RCR;
uint16_t RESERVED12;
__IO uint16_t CCR1;
uint16_t RESERVED13;
__IO uint16_t CCR2;
uint16_t RESERVED14;
__IO uint16_t CCR3;
uint16_t RESERVED15;
__IO uint16_t CCR4;
uint16_t RESERVED16;
__IO uint16_t BDTR;
uint16_t RESERVED17;
__IO uint16_t DCR;
uint16_t RESERVED18;
__IO uint16_t DMAR;
uint16_t RESERVED19;
} TIM_TypeDef;
ํ์ด๋จธ ๋ ์ง์คํฐ์ ๊ด๋ จ๋ ๋ด์ฉ์ ๋งค๋ด์ผ 311ํ์ด์ง์ ์ ๋์ ์๋ค.
ํ์ด๋จธ ์ค์
๋ค์ ์ฝ๋๋ STM32 ๋ผ์ด๋ธ๋ฌ๋ฆฌ์์ ์ ๊ณตํ๋ ํ์ด๋จธ ํจ์๋ฅผ ์ด์ฉํด ํ๊ฒฝ์ค์ ์ ํ๋ค. ์์ธํ ๋ด์ฉ์ ๋๋ ์ ๋ชจ๋ฅด๋ ํจ์ค~
/* TIM2 Configuration */
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 0xF;
TIM_TimeBaseStructure.TIM_Prescaler = 0xF;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* TIM2 TRGO selection */
TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);
DAC ์ค์
DAC ๋ฅผ ์ด๊ธฐํ ํ๋ค. ๋ ๊ฐ์ ์ฑ๋์ ์ฌ์ฉํ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ๋ ๋ฒ์ DAC_Init() ํจ์๊ฐ ํธ์ถ๋๋ค. ๋ ์ฑ๋์ ๋ค๋ฅธ์ ์ DAC_TriangleAmplitude_x ๋ฐ์ ์๋ค. ๋ ๋ฒ์งธ ์ฑ๋์ด ์ข๋ ์ฝํ ์ ํธ๋ฅผ ์ถ๋ ฅํ๋ค. ์๋ณธ ์์ ์ฝ๋๋ OutputBuffer๋ฅผ ๋นํ์ฑ์ํ๋ก ์ค์ ํ๋ค. ์ค์ค๋ก์ค์ฝํ๋ก ํํ์ ๋ณด๊ธฐ ์ํด์์ธ๋ฐ, ๋๋ ๊ทธ๋ฐ ๋น์ผ ์ฅ๋น๊ฐ ์๊ธฐ ๋๋ฌธ์ ๊ฒฐ๊ณผ๋ฅผ ์คํผ์ปค๋ก ์ถ๋ ฅํ๋ค. ์ถ๋ ฅ๋จ์์ ์ ํญ์ด ์๋๋ผ๋ ๋๊ฐ์ ์ ์์ ์ถ๋ ฅํ๋ ค๋ฉด OutputBuffer ๋ฅผ ํ์ฑํ ํด์ผํ๋ค. ๊ทธ ๋ถ๋ถ์ด ๋ฐ๋ ๋ถ๋ถ์ด๋ค.
/* DAC channel1 Configuration */
DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_Triangle;
DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_4095;
//DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
/* DAC channel2 Configuration */
DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_1023;
DAC_Init(DAC_Channel_2, &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 DAC Channel2: Once the DAC channel2 is enabled, PA.05 is
automatically connected to the DAC converter. */
DAC_Cmd(DAC_Channel_2, ENABLE);
/* Set DAC dual channel DHR12RD register */
DAC_SetDualChannelData(DAC_Align_12b_R, 0x100, 0x100);
/* TIM2 enable counter */
TIM_Cmd(TIM2, ENABLE);
์ ์ฒด์ฝ๋
์ด ์์ ๋ DAC ๋ ์ฑ๋์ ์ด์ฉํด ์ผ๊ฐํ๋ฅผ ์ถ๋ ฅํ๋ค. ์คํผ์ปค(piezo buzzer)๋ฅผ ๋ฌ๋ฉด ์๋ฆฌ๋ฅผ ๋ค์ ์ ์๋ค. ์์ฒด ํ์ด๋จธ๋ฅผ ์ด์ฉํ๊ณ , ์ฑ๋์ ์ด์ฉํ๋ ๋ฒ์ ๋ฐฐ์ธ ์ ์๋ค. ์ด ์ฝ๋๋ \STM32\stm32vldiscovery_package\Project\Examples\DAC_Channel2 ํด๋์ ์๋ค.
#include "stm32f10x.h"
DAC_InitTypeDef DAC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
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();
/* TIM2 Configuration */
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 0xF;
TIM_TimeBaseStructure.TIM_Prescaler = 0xF;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* TIM2 TRGO selection */
TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);
/* DAC channel1 Configuration */
DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_Triangle;
DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_4095;
//DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
/* DAC channel2 Configuration */
DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_1023;
DAC_Init(DAC_Channel_2, &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 DAC Channel2: Once the DAC channel2 is enabled, PA.05 is
automatically connected to the DAC converter. */
DAC_Cmd(DAC_Channel_2, ENABLE);
/* Set DAC dual channel DHR12RD register */
DAC_SetDualChannelData(DAC_Align_12b_R, 0x100, 0x100);
/* TIM2 enable counter */
TIM_Cmd(TIM2, ENABLE);
while (1)
{
}
}
/**
* @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);
/* TIM2 Periph clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, 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_Pin_5;
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