Code 0001: Wait rx completed
生活随笔
收集整理的這篇文章主要介紹了
Code 0001: Wait rx completed
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
注意:以下Demo適用于不帶DMA功能的串口。
Demo0001
/* 方法: wait_rx函數每1ms掃描串口是否有接受數據,如果長時間沒有收到數據,則接受完成。
?* 分析: 該方法存在的問題是掃描時間需要配合串口波特率進行設置, 好處是數據傳輸沒有特殊格式要求。
?*/
static struct {uint16 index;uint8 items[LORA_UART_RECV_BUF_SIZE]; }lora_uart_rx_buf;/** @fn halKeyPort1Isr* @brief Port1 ISR* @param* @return*/ HAL_ISR_FUNCTION(halGPRSUartIsr,URX0_VECTOR) { URX0IF = 0; if(lora_uart_rx_buf.index >= LORA_UART_RECV_BUF_SIZE)lora_uart_rx_buf.index = 0;lora_uart_rx_buf.items[lora_uart_rx_buf.index ++] = U0DBUF; } /** @fn wait_rx* @brief wait for rx completed* @param none* @return none*/ uint8 wait_rx(uint32 times) {uint8 timeout = 0;uint8 pre_cnt = 0;while(timeout++<times){if (lora_uart_rx_buf.index > 0) {pre_cnt = lora_uart_rx_buf.index;break;}lora_delayms(1); /* todo: should be replaced */}if (timeout >= times) {return 0;}lora_delayms(1); /* todo: should be replaced */while(lora_uart_rx_buf.index != pre_cnt) {pre_cnt = lora_uart_rx_buf.index;lora_delayms(1); /* todo: should be replaced */}return 1; }?Demo0002
/* 方法: 約定結束標志,以接收到結束標志完成接受 。
?* 分析: 該方法存在的問題是必須按照規定的格式發送數據, 好處是不需要考慮波特率等串口特性。
?*/
void USART1_IRQHandler(void) {u8 Res;if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) {Res =USART_ReceiveData(USART1);if((USART_RX_STA&0x8000)==0) {if(USART_RX_STA&0x4000) {if(Res!=0x0a) {USART_RX_STA=0;
} else {
USART_RX_STA|=0x8000; }
}else { if(Res==0x0d) { /* end with 0x0d */
USART_RX_STA|=0x4000;
} else {USART_RX_BUF[USART_RX_STA&0X3FFF]=Res;USART_RX_STA++;if(USART_RX_STA>(USART_REC_LEN-1)) {
USART_RX_STA=0; } }} } }
?
?Demo0003
/* 方法: timeout實現,timeout時間內未收到數據,結束本次數據接收 。
?* 分析: 該方法和Demo0001原理相同,只是實現方式有所區別,這邊將timeout寫入接受函數里。
?*/
/**? *name? : UART_Recv
? *brief : UART receive data
? *param : UARTx : Select UART peripheral, UART0/2/3 and UART1
? *??????? *rxbuf: pointer of rx buffer
? *??????? rxlen : length of receiving data
? *retval: length of receiving data
? */
uint8_t UART_Recv(UART_TypeDef* UARTx, uint8_t *rxbuf, uint8_t rxlen)
{
?? ?uint32_t len = 0, timeout;
?? ?while(rxlen){
?? ??? ?timeout = UART_BLOCKING_TIMEOUT;
?? ??? ?while (!(UARTx->LSR & UART_LSR_RDR)){
?? ??? ??? ?if (timeout == 0) break;
?? ??? ??? ?timeout--;
?? ??? ?}
?? ??? ?if(timeout == 0) break;
?? ??? ?
?? ??? ?*rxbuf++ = UART_RecvData(UARTx);
?? ??? ?rxlen--;
?? ??? ?len++;
?? ?}
?? ?return len;
}
轉載于:https://www.cnblogs.com/HongZheng/p/5954366.html
總結
以上是生活随笔為你收集整理的Code 0001: Wait rx completed的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jQuery基本知识
- 下一篇: 初始Angularjs2