適用于新手學習串口通信,運行時用的是串口調試助手“ v4.11綠色版”來接收數據。。。
- #include "bsp_usart.h"
- /**
- * @brief USART GPIO 配置,工作參數配置
- * @param 無
- * @retval 無
- */
- void USART_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- // 打開串口GPIO的時鐘
- DEBUG_USART_GPIO_APBxClkCmd(DEBUG_USART_GPIO_CLK, ENABLE);
-
- // 打開串口外設的時鐘
- DEBUG_USART_APBxClkCmd(DEBUG_USART_CLK, ENABLE);
- // 將USART Tx的GPIO配置為推挽復用模式
- GPIO_InitStructure.GPIO_Pin = DEBUG_USART_TX_GPIO_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(DEBUG_USART_TX_GPIO_PORT, &GPIO_InitStructure);
- // 將USART Rx的GPIO配置為浮空輸入模式
- GPIO_InitStructure.GPIO_Pin = DEBUG_USART_RX_GPIO_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(DEBUG_USART_RX_GPIO_PORT, &GPIO_InitStructure);
-
- // 配置串口的工作參數
- // 配置波特率
- USART_InitStructure.USART_BaudRate = DEBUG_USART_BAUDRATE;
- // 配置 針數據字長
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- // 配置停止位
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- // 配置校驗位
- USART_InitStructure.USART_Parity = USART_Parity_No ;
- // 配置硬件流控制
- USART_InitStructure.USART_HardwareFlowControl =
- USART_HardwareFlowControl_None;
- // 配置工作模式,收發一起
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- // 完成串口的初始化配置
- USART_Init(DEBUG_USARTx, &USART_InitStructure);
-
- // 使能串口
- USART_Cmd(DEBUG_USARTx, ENABLE);
- }
- /***************** 發送一個字符 **********************/
- void Usart_SendByte( USART_TypeDef * pUSARTx, uint8_t ch)
- {
- /* 發送一個字節數據到USART */
- USART_SendData(pUSARTx,ch);
-
- /* 等待發送數據寄存器為空 */
- while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
- }
- /***************** 發送字符串 **********************/
- void Usart_SendString( USART_TypeDef * pUSARTx, char *str)
- {
- unsigned int k=0;
- do
- {
- Usart_SendByte( pUSARTx, *(str + k) );
- k++;
- } while(*(str + k)!='\0');
-
- /* 等待發送完成 */
- while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TC)==RESET)
- {}
- }
- /***************** 發送一個16位數 **********************/
- void Usart_SendHalfWord( USART_TypeDef * pUSARTx, uint16_t ch)
- {
- uint8_t temp_h, temp_l;
-
- /* 取出高八位 */
- temp_h = (ch&0XFF00)>>8;
- /* 取出低八位 */
- temp_l = ch&0XFF;
-
- /* 發送高八位 */
- USART_SendData(pUSARTx,temp_h);
- while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
-
- /* 發送低八位 */
- USART_SendData(pUSARTx,temp_l);
- while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
- }
- ///重定向c庫函數printf到串口,重定向后可使用printf函數
- int fputc(int ch, FILE *f)
- {
- /* 發送一個字節數據到串口 */
- USART_SendData(DEBUG_USARTx, (uint8_t) ch);
-
- /* 等待發送完畢 */
- while (USART_GetFlagStatus(DEBUG_USARTx, USART_FLAG_TXE) == RESET);
-
- return (ch);
- }
- ///重定向c庫函數scanf到串口,重寫向后可使用scanf、getchar等函數
- int fgetc(FILE *f)
- {
- /* 等待串口輸入數據 */
- while (USART_GetFlagStatus(DEBUG_USARTx, USART_FLAG_RXNE) == RESET);
- return (int)USART_ReceiveData(DEBUG_USARTx);
- }
復制代碼
全部資料51hei下載地址:
USART發送字符和字符串.rar
(293.81 KB, 下載次數: 80)
2018-7-27 00:30 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|