初始化了一个串口,初始化了一个adc、一个dma,一个定时器,一个led。这一套搞懂算是基本入门了呀
#include "gd32f4xx.h"
#include "systick.h"
#include <stdio.h>
#include "gd32f450vi_core_board.h"
#define BUFFER_SIZE (COUNTOF(tx_buffer))
#define COUNTOF(a) (sizeof(a)/sizeof(*(a)))
__IO uint16_t adc_value[256];
/* transmit buffer and receive buffer */
uint8_t tx_buffer[32] = {0};
uint8_t rx_buffer[BUFFER_SIZE];
uint16_t tx_counter = 0, rx_counter = 0;
/* size of transmit buffer and receive buffer */
uint32_t rx_buffer_size = BUFFER_SIZE, tx_buffer_size = BUFFER_SIZE;
volatile ErrStatus transfer_status = ERROR;
void led_init(void);
void led_flash(int times);
void rcu_config(void);
void gpio_config(void);
void dma_config(void);
void adc_config(void);
void timer_config(void);
void usart0_init(void);
/*!
\brief main routine
\param[in] none
\param[out] none
\retval none
*/
int main(void)
{
uint64_t ad_average_value = 0;
uint16_t i = 0;
float ADCVValue = 0;
/* system clocks configuration */
rcu_config();
/* GPIO configuration */
gpio_config();
/* initialize the LEDs */
led_init();
/* configure systick */
systick_config();
timer_config();
/* DMA configuration */
dma_config();
/* ADC configuration */
adc_config();
/* configure Usart0 */
usart0_init();
/* flash the LEDs for 1 time */
led_flash(1);
while(1){
/* TIMER1 counter enable */
timer_enable(TIMER1);
/* test on channel1 transfer complete flag */
while( !dma_flag_get(DMA1, DMA_CH0,DMA_FLAG_FTF ));
/* clear channel1 transfer complete flag */
dma_flag_clear(DMA1, DMA_CH0,DMA_FLAG_FTF );
/* TIMER1 counter disable */
timer_disable(TIMER1);
for(i = 0, ad_average_value = 0;i < 256 ;i++)
{
ad_average_value += adc_value[ i ];
}
ad_average_value = ad_average_value >>8;//ÓÒÒÆ8룬Ҳ¾ÍÊdzýÒÔ256ȡƽ¾ùÖµ
ADCVValue = ((double)ad_average_value*3300.0/4095.0);
printf("\r\n Voltmeter = %3.1f mV \r\n", ADCVValue);
delay_1ms(1000);
GPIO_OCTL(GPIOB) ^= GPIO_PIN_12;
}
}
void usart0_init(void)
{
rcu_periph_clock_enable( RCU_GPIOA);
/* enable USART clock */
rcu_periph_clock_enable(RCU_USART0);
/* connect port to USARTx_Tx */
gpio_af_set(GPIOA,GPIO_AF_7, GPIO_PIN_9);
/* connect port to USARTx_Rx */
gpio_af_set(GPIOA, GPIO_AF_7, GPIO_PIN_10);
/* configure USART Tx as alternate function push-pull */
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_9);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_9);
/* configure USART Rx as alternate function push-pull */
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_10);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_10);
/* USART configure */
usart_deinit(USART0);
usart_baudrate_set(USART0,115200U);
usart_receive_config(USART0, USART_RECEIVE_ENABLE);
usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
usart_enable(USART0);
}
/* retarget the C library printf function to the usart */
int fputc(int ch, FILE *f)
{
usart_data_transmit(USART0, (uint8_t) ch);
while (RESET == usart_flag_get(USART0,USART_FLAG_TBE));
return ch;
}
/*!
\brief initialize the LEDs
\param[in] none
\param[out] none
\retval none
*/
void led_init(void)
{
/* enable the LED GPIO clock */
rcu_periph_clock_enable(RCU_GPIOB);
/* configure LED GPIO port */
gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_12);
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_12);
/* set LED GPIO pin turn off red led*/
gpio_bit_set(GPIOB, GPIO_PIN_12);
}
/*!
\brief configure the nested vectored interrupt controller
\param[in] none
\param[out] none
\retval none
*/
/*!
\brief flash the LEDs for test
\param[in] times: times to flash the LEDs
\param[out] none
\retval none
*/
void led_flash(int times)
{
int i;
for (i = 0;i < times;i++){
gpio_bit_reset(GPIOB, GPIO_PIN_12);
/* delay 800 ms */
delay_1ms(800);
/* turn off LED1 */
gpio_bit_set(GPIOB, GPIO_PIN_12);
/* delay 800 ms */
delay_1ms(800);
}
gpio_bit_set(GPIOB, GPIO_PIN_12);
}
/*!
\brief configure the different system clocks
\param[in] none
\param[out] none
\retval none
*/
void rcu_config(void)
{
/* enable GPIOC clock */
rcu_periph_clock_enable(RCU_GPIOA);
/* enable ADC clock */
rcu_periph_clock_enable(RCU_ADC0);
/* enable timer1 clock */
rcu_periph_clock_enable(RCU_TIMER1);
rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4);
/* enable DMA clock */
rcu_periph_clock_enable(RCU_DMA1);
/* config ADC clock */
adc_clock_config(ADC_ADCCK_PCLK2_DIV6);
}
/*!
\brief configure the GPIO peripheral
\param[in] none
\param[out] none
\retval none
*/
void gpio_config(void)
{
/* config the GPIO as analog mode */
gpio_mode_set(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_3);
}
/*!
\brief configure the DMA peripheral
\param[in] none
\param[out] none
\retval none
*/
void dma_config(void)
{
/* ADC_DMA_channel configuration */
dma_single_data_parameter_struct dma_single_data_parameter;
/* ADC DMA_channel configuration */
dma_deinit(DMA1, DMA_CH0);
/* initialize DMA single data mode */
dma_single_data_parameter.periph_addr = (uint32_t)(&ADC_RDATA(ADC0));
dma_single_data_parameter.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
dma_single_data_parameter.memory0_addr = (uint32_t)(adc_value);
dma_single_data_parameter.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
dma_single_data_parameter.periph_memory_width = DMA_PERIPH_WIDTH_16BIT;
dma_single_data_parameter.direction = DMA_PERIPH_TO_MEMORY;
dma_single_data_parameter.number = 256;
dma_single_data_parameter.priority = DMA_PRIORITY_HIGH;
dma_single_data_mode_init(DMA1, DMA_CH0, &dma_single_data_parameter);
dma_channel_subperipheral_select(DMA1, DMA_CH0, DMA_SUBPERI0);
/* enable DMA circulation mode */
dma_circulation_enable(DMA1, DMA_CH0);
/* enable DMA channel */
dma_channel_enable(DMA1, DMA_CH0);
}
/*!
\brief configure the ADC peripheral
\param[in] none
\param[out] none
\retval none
*/
void adc_config(void)
{
/* ADC mode config */
adc_sync_mode_config(ADC_SYNC_MODE_INDEPENDENT);
/* ADC contineous function disable */
adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, ENABLE);
/* ADC scan mode disable */
adc_special_function_config(ADC0, ADC_SCAN_MODE, ENABLE);
/* ADC data alignment config */
adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
/* ADC channel length config */
adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 1);
/* ADC regular channel config */
adc_regular_channel_config(ADC0, 0, ADC_CHANNEL_3, ADC_SAMPLETIME_15);
/* ADC trigger config */
adc_external_trigger_source_config(ADC0, ADC_REGULAR_CHANNEL, ADC_EXTTRIG_REGULAR_T1_CH1);
adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, EXTERNAL_TRIGGER_DISABLE);//EXTERNAL_TRIGGER_RISING
/* ADC DMA function enable */
adc_dma_request_after_last_enable(ADC0);
adc_dma_mode_enable(ADC0);
/* enable ADC interface */
adc_enable(ADC0);
/* wait for ADC stability */
delay_1ms(1);
/* ADC calibration and reset calibration */
adc_calibration_enable(ADC0);
/* enable ADC software trigger */
adc_software_trigger_enable(ADC0, ADC_REGULAR_CHANNEL);
}
/*!
\brief TIMER configuration function
\param[in] none
\param[out] none
\retval none
*/
void timer_config(void)
{
timer_oc_parameter_struct timer_ocintpara;
timer_parameter_struct timer_initpara;
timer_deinit(TIMER1);
/* TIMER1 configuration */
timer_initpara.prescaler = 11;
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = 299;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_initpara.repetitioncounter = 0;
timer_init(TIMER1,&timer_initpara);
/* CH1 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,190);
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);
/* TIMER0 primary output enable */
timer_primary_output_config(TIMER1, ENABLE);
/* auto-reload preload enable */
timer_auto_reload_shadow_enable(TIMER1);
}