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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
#include "gd32f30x.h"
#include "systick.h"
#include "gd32f307c_eval.h"
#include <stdio.h>
void rcu_config(void);
void gpio_config(void);
void dma_config(void);
void timer_config(void);
void adc_config(void);
/*!
\brief main function
\param[in] none
\param[out] none
\retval none
*/
int main(void)
{
/* system clocks configuration */
rcu_config();
/* systick configuration */
systick_config();
/* GPIO configuration */
gpio_config();
/* configure COM port */
gd_eval_com_init(EVAL_COM0);
/* TIMER configuration */
timer_config();
/* DMA configuration */
dma_config();
/* ADC configuration */
adc_config();
/* enable TIMER1 */
timer_enable(TIMER1);
while(1){
delay_1ms(1000);
printf("\n ADC0: PA0, adc_value[0] = %08X \n",adc_value[0]);
printf("\n ADC1: PA1, adc_value[1] = %08X \n",adc_value[1]);
printf("\n ******************* \n");
}
}
void rcu_config(void)
{
/* enable GPIOA clock */
rcu_periph_clock_enable(RCU_GPIOA);
/* enable ADC0 clock */
rcu_periph_clock_enable(RCU_ADC0);
/* enable ADC1 clock */
rcu_periph_clock_enable(RCU_ADC1);
/* enable DMA0 clock */
rcu_periph_clock_enable(RCU_DMA0);
/* enable timer1 clock */
rcu_periph_clock_enable(RCU_TIMER1);
/* config ADC clock */
rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV4);
}
void gpio_config(void)
{
/* config the GPIO as analog mode */
gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_MAX, GPIO_PIN_0|GPIO_PIN_1);
}
void dma_config(void)
{
/* ADC_DMA_channel configuration */
dma_parameter_struct dma_data_parameter;
/* ADC DMA_channel configuration */
dma_deinit(DMA0, DMA_CH0);
/* initialize DMA data mode */
dma_data_parameter.periph_addr = (uint32_t)(&ADC_RDATA(ADC0));
dma_data_parameter.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
dma_data_parameter.memory_addr = (uint32_t)(&adc_value);
dma_data_parameter.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
dma_data_parameter.periph_width = DMA_PERIPHERAL_WIDTH_32BIT;
dma_data_parameter.memory_width = DMA_MEMORY_WIDTH_32BIT;
dma_data_parameter.direction = DMA_PERIPHERAL_TO_MEMORY;
dma_data_parameter.number = 2;
dma_data_parameter.priority = DMA_PRIORITY_HIGH;
dma_init(DMA0, DMA_CH0, &dma_data_parameter);
dma_circulation_enable(DMA0, DMA_CH0);
/* enable DMA channel */
dma_channel_enable(DMA0, DMA_CH0);
}
void timer_config(void)
{
timer_oc_parameter_struct timer_ocintpara;
timer_parameter_struct timer_initpara;
/* TIMER1 configuration */
timer_initpara.prescaler = 8399;
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = 9999;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_initpara.repetitioncounter = 0;
timer_init(TIMER1,&timer_initpara);
timer_channel_output_struct_para_init(&timer_ocintpara);
/* CH0 configuration in PWM mode1 */
timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
timer_channel_output_config(TIMER1, TIMER_CH_1, &timer_ocintpara);
timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_1, 3999);
timer_channel_output_mode_config(TIMER1, TIMER_CH_1, TIMER_OC_MODE_PWM1);
timer_channel_output_shadow_config(TIMER1, TIMER_CH_1, TIMER_OC_SHADOW_DISABLE);
}
void adc_config(void)
{
/* ADC continous function enable */
adc_special_function_config(ADC0, ADC_SCAN_MODE, ENABLE);
adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, DISABLE);
adc_special_function_config(ADC1, ADC_SCAN_MODE, ENABLE);
adc_special_function_config(ADC1, ADC_CONTINUOUS_MODE, DISABLE);
/* ADC trigger config */
adc_external_trigger_source_config(ADC0, ADC_ROUTINE_CHANNEL, ADC0_1_EXTTRIG_ROUTINE_T1_CH1);
adc_external_trigger_source_config(ADC1, ADC_ROUTINE_CHANNEL, ADC0_1_2_EXTTRIG_ROUTINE_NONE);
/* ADC data alignment config */
adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
adc_data_alignment_config(ADC1, ADC_DATAALIGN_RIGHT);
/* ADC mode config */
adc_mode_config(ADC_DAUL_ROUTINE_PARALLEL);
/* ADC channel length config */
adc_channel_length_config(ADC0, ADC_ROUTINE_CHANNEL, 2);
adc_channel_length_config(ADC1, ADC_ROUTINE_CHANNEL, 2);
/* ADC routine channel config */
adc_routine_channel_config(ADC0, 0, ADC_CHANNEL_0, ADC_SAMPLETIME_55POINT5);
adc_routine_channel_config(ADC0, 1, ADC_CHANNEL_1, ADC_SAMPLETIME_55POINT5);
adc_routine_channel_config(ADC1, 0, ADC_CHANNEL_1, ADC_SAMPLETIME_55POINT5);
adc_routine_channel_config(ADC1, 1, ADC_CHANNEL_0, ADC_SAMPLETIME_55POINT5);
/* ADC external trigger enable */
adc_external_trigger_config(ADC0, ADC_ROUTINE_CHANNEL, ENABLE);
adc_external_trigger_config(ADC1, ADC_ROUTINE_CHANNEL, ENABLE);
/* enable ADC interface */
adc_enable(ADC0);
delay_1ms(1);
/* ADC calibration and reset calibration */
adc_calibration_enable(ADC0);
/* enable ADC interface */
adc_enable(ADC1);
delay_1ms(1);
/* ADC calibration and reset calibration */
adc_calibration_enable(ADC1);
/* ADC DMA function enable */
adc_dma_mode_enable(ADC0);
}
|