GD32串口接收发送数据总结

在使用GD32串口接收数据的过程中,遇到了数据无法接收的问题,在导师的帮助下,需要调用usart_flag_get函数来使RBNE置1,RBNE:读数据时缓冲区非空,当数据缓存区接收到来自移位寄存器的数据时,该位置1. 0:读数据缓存区为空, 1:读数据缓存区不为空。主要代码如下:

while(1)  {		 
			  delay_1ms(400);
		      gd_eval_led_on(LED3);
		      delay_1ms(400);
	      	  gd_eval_led_off(LED3);
			  for ( i =0;i<16;i++)  {
			    vref_value[i]=adc_value[i]*3.3/4096;
		}
		   **if(usart_flag_get(USART0,USART_FLAG_RBNE)==1) {  
			    if(usart_data_receive(USART0)=='0') {**
			    delay_1ms(400);
                gd_eval_led_on(LED2);		
            	printf("通道0的值为%5.3f\r\n",vref_value[0]);					
		}

使用GD32串口接收ADC通道的值代码如下:


/*GD32通过串口接收ADC通道的值,并打印出  */

/*

    \file  main.c
    \brief USART printf demo
*/

/*
    Copyright (C) 2017 GigaDevice

    2017-06-23, V1.0.0, demo for GD32F30x
*/

#include "gd32f30x.h"
#include "gd32f303c_eval.h"
#include "systick.h"
#include <stdio.h>
#include "gd32f30x_it.h"

uint16_t i;
float adc_value[16];
float vref_value[16];
uint16_t j;
void led_init(void);
void led_flash(int times);
void rcu_config(void);
void gpio_config(void);
void adc_config();
uint16_t adc_channel_sample(uint8_t channel);

/*!
    \brief      main function
    \param[in]  none
    \param[out] none
    \retval     none
*/

int main(void)
{
    /* initialize the LEDs */
    led_init();
   
    /* configure systick */
    systick_config();       //系统滴答定时器配置 
    
    /* flash the LEDs for 1 time */
    led_flash(1);
    
    /* configure EVAL_COM1 */
    gd_eval_com_init(EVAL_COM1); 

   rcu_config();
	 
	  gpio_config();
	
	  adc_config();
    /* configure WAKEUP key */
    gd_eval_key_init(KEY_WAKEUP, KEY_MODE_GPIO);
    
    /* output a message on hyperterminal using printf function */
    printf("\r\n USART receive example: please transmit a number \r\n");
    
    /* wait for completion of USART transmission */
		    adc_value[0]=adc_channel_sample(ADC_CHANNEL_0);
			  adc_value[1]=adc_channel_sample(ADC_CHANNEL_1);
        adc_value[2]=adc_channel_sample(ADC_CHANNEL_2);
        adc_value[3]=adc_channel_sample(ADC_CHANNEL_3);
        adc_value[4]=adc_channel_sample(ADC_CHANNEL_4);
			  adc_value[5]=adc_channel_sample(ADC_CHANNEL_5);
			  adc_value[6]=adc_channel_sample(ADC_CHANNEL_6);
			  adc_value[7]=adc_channel_sample(ADC_CHANNEL_7);
			  adc_value[8]=adc_channel_sample(ADC_CHANNEL_8);
			  adc_value[9]=adc_channel_sample(ADC_CHANNEL_9);
			  adc_value[10]=adc_channel_sample(ADC_CHANNEL_10);
			  adc_value[11]=adc_channel_sample(ADC_CHANNEL_11);
			  adc_value[12]=adc_channel_sample(ADC_CHANNEL_12);
			  adc_value[13]=adc_channel_sample(ADC_CHANNEL_13);
			  adc_value[14]=adc_channel_sample(ADC_CHANNEL_14);
			  adc_value[15]=adc_channel_sample(ADC_CHANNEL_15);
		while(1){
	
			 
			 
			  	delay_1ms(400);
		      gd_eval_led_on(LED3);
		      delay_1ms(400);
	      	gd_eval_led_off(LED3);
					for ( i =0;i<16;i++) {
			    vref_value[i]=adc_value[i]*3.3/4096;
					}
					//printf("请输入一个数\r\n");
					//scanf("%d\r\n",&j);
		   if(usart_flag_get(USART0,USART_FLAG_RBNE)==1)
		   {  
			    if(usart_data_receive(USART0)=='0'){
					    delay_1ms(400);
              gd_eval_led_on(LED2);		
           // printf("%d\r\n",usart_data_receive(USART0));
            	printf("通道0的值为%5.3f\r\n",vref_value[0]);					
					}
					else if(usart_data_receive(USART0)==1) {
              printf("通道1的值为%5.3f\r\n",vref_value[1]);
					}	
          else if(usart_data_receive(USART0)==2){
					
					     printf("通道2的值为%5.3f\r\n",vref_value[2]);
					}		
          else if(usart_data_receive(USART0)==3) {
					     printf("通道3的值为%5.3f\r\n",vref_value[3]);
					}		
          else if(usart_data_receive(USART0)==4) {
					     printf("通道4的值为%5.3f\r\n",vref_value[4]);
					}								
					else if(usart_data_receive(USART0)==5) {
					     printf("通道5的值为%5.3f\r\n",vref_value[5]);
					}			
					else if(usart_data_receive(USART0)==6) {
					     printf("通道6的值为%5.3f\r\n",vref_value[6]);
					}			
					else if(usart_data_receive(USART0)==7) {
					     printf("通道7的值为%5.3f\r\n",vref_value[7]);
					}			
					else if(usart_data_receive(USART0)==8) {
					     printf("通道8的值为%5.3f\r\n",vref_value[8]);
					}			
					else if(usart_data_receive(USART0)==9) {
					     printf("通道9的值为%5.3f\r\n",vref_value[9]);
					}			
					else if(usart_data_receive(USART0)==10) {
					     printf("通道10的值为%5.3f\r\n",vref_value[10]);
					}			
					else if(usart_data_receive(USART0)==11) {
					     printf("通道11的值为%5.3f\r\n",vref_value[11]);
					}		
          else if(usart_data_receive(USART0)==12) {
					     printf("通道12的值为%5.3f\r\n",vref_value[12]);
					}		
          else if(usart_data_receive(USART0)==13) {
					     printf("通道13的值为%5.3f\r\n",vref_value[13]);
					}		
          else if(usart_data_receive(USART0)==14) {
					     printf("通道14的值为%5.3f\r\n",vref_value[14]);
					}	
          else if (usart_data_receive(USART0)==15){
						   printf("通道15的值为%5.3f\r\n",vref_value[15]);
						
						}	
          else {
						   printf("receive error\r\n");
						   
						}						
	}	
}
		}
void rcu_config(void)
{
    /* enable GPIOA clock */
    rcu_periph_clock_enable(RCU_GPIOA);
	  rcu_periph_clock_enable(RCU_GPIOB);
	  rcu_periph_clock_enable(RCU_GPIOC);
    /* enable ADC clock */
    rcu_periph_clock_enable(RCU_ADC0);
    /* config ADC clock */
    rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV6);
}

void gpio_config(void)
{
    /* config the GPIO as analog mode */
	  gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_10MHZ, GPIO_PIN_0);
    gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_10MHZ, GPIO_PIN_1);
    gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_10MHZ, GPIO_PIN_2);
    gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_10MHZ, GPIO_PIN_3);
    gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_10MHZ, GPIO_PIN_4);
	  gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_10MHZ, GPIO_PIN_5);
	  gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_10MHZ, GPIO_PIN_6);
	  gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_10MHZ, GPIO_PIN_7);
	  gpio_init(GPIOB, GPIO_MODE_AIN, GPIO_OSPEED_10MHZ, GPIO_PIN_0);
	  gpio_init(GPIOB, GPIO_MODE_AIN, GPIO_OSPEED_10MHZ, GPIO_PIN_1);
	  gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_10MHZ, GPIO_PIN_0);
	  gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_10MHZ, GPIO_PIN_1);
	  gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_10MHZ, GPIO_PIN_2);
    gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_10MHZ, GPIO_PIN_3);
	  gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_10MHZ, GPIO_PIN_4);
	  gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_10MHZ, GPIO_PIN_5);
	  
	  
}
void adc_config(void)
{
    /* ADC mode config */
    adc_mode_config(ADC_MODE_FREE);
    /* ADC data alignment config */
    adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
    /* ADC channel length config */
    adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 1U);
    
    /* ADC trigger config */
    adc_external_trigger_source_config(ADC0, ADC_REGULAR_CHANNEL, ADC0_1_2_EXTTRIG_REGULAR_NONE); 
    /* ADC external trigger config */
    adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, ENABLE);

    /* enable ADC interface */
    adc_enable(ADC0);
    delay_1ms(1U);
    /* ADC calibration and reset calibration */
    adc_calibration_enable(ADC0);
}
uint16_t adc_channel_sample(uint8_t channel)
{
    /* ADC regular channel config */
    adc_regular_channel_config(ADC0, 0U, channel, ADC_SAMPLETIME_7POINT5);      //adc规则通道配置
    /* ADC software trigger enable */
    adc_software_trigger_enable(ADC0, ADC_REGULAR_CHANNEL);                     //adc软件触发使能

    /* wait the end of conversion flag */
    while(!adc_flag_get(ADC0, ADC_FLAG_EOC));                              //等待转换完成标志
    /* clear the end of conversion flag */
    adc_flag_clear(ADC0, ADC_FLAG_EOC);                                     //清除转换完成标志
    /* return regular channel sample value */
    return (adc_regular_data_read(ADC0));                                    //返回规则通道样品值
}
/*!
    \brief      initialize the LEDs
    \param[in]  none
    \param[out] none
    \retval     none
*/
void led_init(void)
{
    gd_eval_led_init(LED2);
    gd_eval_led_init(LED3);
    gd_eval_led_init(LED4);
    gd_eval_led_init(LED5);
}

/*!
    \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++){
        /* delay 400 ms */
        delay_1ms(400);

        /* turn on LEDs */
        gd_eval_led_on(LED2);
        gd_eval_led_on(LED3);
        gd_eval_led_on(LED4);
        gd_eval_led_on(LED5);

        /* delay 400 ms */
        delay_1ms(400);
        /* turn off LEDs */
        gd_eval_led_off(LED2);
        gd_eval_led_off(LED3);
        gd_eval_led_off(LED4);
        gd_eval_led_off(LED5);
    }
}

/* retarget the C library printf function to the USART */
int fputc(int ch, FILE *f)
{
    usart_data_transmit(EVAL_COM1, (uint8_t)ch);
    while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
    return ch;
}

上一篇:数学笔记2:概率分布


下一篇:MybatisPlus自定义SQL如何支持多表分页查询