DAC — DAC 输出电压
固件库: GD32F30x_Firmware_Library V3.0.3
芯片: GD32
源文件: GD32F30x_Firmware_Library_V3.0.3/Examples/DAC/DACC_output_voltage/main.c
功能简介
本示例演示 DAC 数模转换器的基本用法。DAC 将数字值转换为模拟电压输出,是生成模拟信号的关键外设。
完整代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
#include "gd32f30x.h"
void rcu_config(void);
/* configure GPIO peripheral */
void gpio_config(void);
/* configure DAC peripheral */
void dac_config(void);
/*!
\brief main function
\param[in] none
\param[out] none
\retval none
*/
int main(void)
{
/* configure RCU peripheral */
rcu_config();
/* configure GPIO peripheral */
gpio_config();
/* configure DAC peripheral */
dac_config();
while(1) {
}
}
void rcu_config(void)
{
/* enable GPIOA clock */
rcu_periph_clock_enable(RCU_GPIOA);
/* enable DAC clock */
rcu_periph_clock_enable(RCU_DAC);
}
void gpio_config(void)
{
/* configure PA4 and PA5 as DAC output */
gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_4 | GPIO_PIN_5);
}
void dac_config(void)
{
/* initialize DAC */
dac_deinit(DAC0);
/* DAC trigger disable */
dac_trigger_disable(DAC0, DAC_OUT0);
dac_trigger_disable(DAC0, DAC_OUT1);
/* DAC wave mode config */
dac_wave_mode_config(DAC0, DAC_OUT0, DAC_WAVE_DISABLE);
dac_wave_mode_config(DAC0, DAC_OUT1, DAC_WAVE_DISABLE);
/* DAC enable */
dac_concurrent_enable(DAC0);
dac_concurrent_data_set(DAC0, DAC_ALIGN_12B_L, 0x7FF0, 0x1FF0);
}
|
代码讲解
GPIO 配置:设置引脚为输出/输入模式,选择推挽/开漏输出,配置上拉/下拉。