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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
#include "gd32f30x.h"
#include "systick.h"
void rcu_config(void);
/* configure GPIO peripheral */
void gpio_config(void);
/* configure NVIC peripheral */
void nvic_config(void);
/* configure ADC peripheral */
void adc_config(void);
/* configure DAC peripheral */
void dac_config(void);
/*!
\brief main function
\param[in] none
\param[out] none
\retval none
*/
int main(void)
{
/* configure systick */
systick_config();
/* configure RCU peripheral */
rcu_config();
/* configure GPIO peripheral */
gpio_config();
/* configure NVIC peripheral */
nvic_config();
/* configure DAC peripheral */
dac_config();
/* configure ADC peripheral */
adc_config();
while(1) {
}
}
void rcu_config(void)
{
/* enable GPIOA clock */
rcu_periph_clock_enable(RCU_GPIOA);
/* enable GPIOC clock */
rcu_periph_clock_enable(RCU_GPIOC);
/* enable ADC clock */
rcu_periph_clock_enable(RCU_ADC0);
/* enable DAC clock */
rcu_periph_clock_enable(RCU_DAC);
}
void gpio_config(void)
{
/* configure PC3 as ADC input */
gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_3);
/* configure PA4 as DAC output */
gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_4);
}
void nvic_config(void)
{
nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
nvic_irq_enable(ADC0_1_IRQn, 0, 0);
}
void adc_config(void)
{
/* ADC continuous function enable */
adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, ENABLE);
/* ADC data alignment config */
adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
/* ADC channel length config */
adc_channel_length_config(ADC0, ADC_ROUTINE_CHANNEL, 1);
/* ADC routine channel config */
adc_routine_channel_config(ADC0, 0, ADC_CHANNEL_13, ADC_SAMPLETIME_55POINT5);
/* ADC external trigger enable */
adc_external_trigger_config(ADC0, ADC_ROUTINE_CHANNEL, ENABLE);
/* ADC trigger config */
adc_external_trigger_source_config(ADC0, ADC_ROUTINE_CHANNEL, ADC0_1_2_EXTTRIG_ROUTINE_NONE);
/* enable ADC interface */
adc_enable(ADC0);
delay_1ms(1);
/* ADC calibration and reset calibration */
adc_calibration_enable(ADC0);
/* ADC interrupt enable */
adc_interrupt_flag_clear(ADC0, ADC_INT_FLAG_EOC);
adc_interrupt_enable(ADC0, ADC_INT_EOC);
/* enable ADC software trigger */
adc_software_trigger_enable(ADC0, ADC_ROUTINE_CHANNEL);
}
void dac_config(void)
{
/* initialize DAC */
dac_deinit(DAC0);
/* DAC trigger config */
dac_trigger_source_config(DAC0, DAC_OUT0, DAC_TRIGGER_SOFTWARE);
/* DAC trigger enable */
dac_trigger_enable(DAC0, DAC_OUT0);
/* DAC wave mode config */
dac_wave_mode_config(DAC0, DAC_OUT0, DAC_WAVE_DISABLE);
/* DAC output buffer config */
dac_output_buffer_enable(DAC0, DAC_OUT0);
/* DAC enable */
dac_enable(DAC0, DAC_OUT0);
}
|