串口的功能網(wǎng)上有很多,但是處理串口的數(shù)據(jù)需要一些小手段,但是str的函數(shù)有點才長,不適用長的數(shù)據(jù)接收,所以給310搞一個接收數(shù)據(jù)的輕量級API的小玩意。
開發(fā)環(huán)境
Keil-MDK 5.34
Windows10
固件庫-GD32F3x0_Firmware_Library_V2.2.0
新建工程就不贅述,看這個
啟動串口
void uart_init(void) { //使能時鐘 rcu_periph_clock_enable(RCU_GPIOA); rcu_periph_clock_enable(RCU_USART0); //Tx PA9 復(fù)用推挽輸出 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); //Rx PA10 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); nvic_irq_enable(USART0_IRQn,0,0); gpio_af_set(GPIOA,GPIO_AF_1,GPIO_PIN_9|GPIO_PIN_10); /*USART 設(shè)置*/ usart_deinit(USART0); usart_baudrate_set(USART0,115200); usart_word_length_set(USART0,USART_WL_8BIT); usart_stop_bit_set(USART0,USART_STB_1BIT); usart_parity_config(USART0,USART_PM_NONE); usart_hardware_flow_rts_config(USART0,USART_RTS_DISABLE); usart_hardware_flow_cts_config(USART0,USART_CTS_DISABLE); usart_receive_config(USART0,USART_RECEIVE_ENABLE); usart_transmit_config(USART0,USART_TRANSMIT_ENABLE); usart_enable(USART0); usart_interrupt_enable(USART0,USART_INT_RBNE); usart_interrupt_enable(USART0, USART_INT_IDLE); /* 使能USART0空閑中斷 */ } //串口中斷配置 uint16_t t=0; void USART0_IRQHandler(void) { if(RESET!=usart_interrupt_flag_get(USART0,USART_INT_FLAG_RBNE)) { gd_led_toggle(); t=usart_data_receive(USART0);//接收數(shù)據(jù) usart_data_transmit(USART0,USART_RX[t]); usart_interrupt_disable(USART0,USART_INT_RBNE); usart_data_transmit(USART0,t); }else if(RESET!=usart_interrupt_flag_get(USART0,USART_INT_FLAG_IDLE)) { usart_interrupt_disable(USART0,USART_INT_IDLE); } }
數(shù)據(jù)處理API
struct _Chang_Num { char *Source; char Sign[8]; float NUM[8]; char flag; }; char Data_Chang_NUM (struct _Chang_Num *Item) { char Data_Array[12] = {0}; //數(shù)字的str數(shù) char str[36]; //裝source數(shù)據(jù)到這個緩存區(qū),這個緩存區(qū)是要變的 char Data; char n,m = 0; char i,flag = 1; //i是當前找的是 第幾個 Sign char Find_Time = strlen(Item->Sign); //要找多少個 char Length = strlen(Item->Source); //源的長度 strcpy(str,Item->Source); if(Length > 36) Length = 36; for (i = 0; i < Find_Time; i++) { flag = 0; for(n = 0;n < Length;n++) { Data = str[n]; if(str[n] == Item->Sign[i]) //找到標志 { str[n] = 0; flag = 1; //開始識別str m = 0; } if(flag) { if((str[n] <= '9' && str[n] >= '0') || str[n] == '.' || str[n] == '-') { Data_Array[m] = str[n]; //把數(shù)字相關(guān)str的裝起來 m++; //m是數(shù)字在str的長度 } else { if(m > 0) //數(shù)字部分結(jié)束 { if(Data_Array[m-1] == '.' || Data_Array[m-1] == '-') //不讓最后一位為 '.' and '-' Data_Array[m-1] = '\0'; else Data_Array[m] = '\0'; break; } } } } Item->NUM[i] = atof(Data_Array); // Data_Replace ("123",Data_Array,0,sizeof(Data_Array)); } return Item->flag; }
配置好你想要的參數(shù)在結(jié)構(gòu)體中,然后將串口的緩沖區(qū)放入這個函數(shù)即可。
將printf函數(shù)映射到串口,看這里
struct _Chang_Num Item_n = { .Source = "a:3.14......,b:3322..,c:-9.0..-", .Sign = {':',':',':'}, }; int main (void) { printf("sign :%s",Item_n.Sign); Data_Chang_NUM (&Item_n); for (char i = 0; i < 3; i++) { printf("num %d :%.2f \n",i,Item_n.NUM[i]); } while(1); }
API效果
審核編輯:劉清
-
API
+關(guān)注
關(guān)注
2文章
1502瀏覽量
62101 -
數(shù)據(jù)接收
+關(guān)注
關(guān)注
0文章
12瀏覽量
7819 -
GD32F3
+關(guān)注
關(guān)注
0文章
11瀏覽量
3854
發(fā)布評論請先 登錄
相關(guān)推薦
評論