stm32單片機實現的。已驗證,實現功能,收到串口發送數據,再回送給串口
0.png (56.95 KB, 下載次數: 191)
下載附件
2017-4-8 15:20 上傳
源碼工程下載:
基本例程-USART收發.rar
(585.15 KB, 下載次數: 452)
2017-4-8 15:21 上傳
點擊文件名下載附件
源代碼:
- /********************
- * 文件名 :main.c
- * 描述 :通過串口調試軟件,向板子發送數據,板子接收到數據后,立即回傳給電腦。
- * 實驗平臺:MINI STM32開發板 基于STM32F103VET6
- * 庫版本 :ST3.0.0
- *********************************************************/
- #include "stm32f10x.h"
- #include "usart1.h"
- int main(void)
- {
-
- SystemInit(); //配置系統時鐘為 72M
-
- USART1_Config(); //USART1 配置
- while (1)
- {
- UART1Test();
- }
- }
復制代碼- /****************
- * 文件名 :usart1.c
- * 描述 :配置USART1
- * 實驗平臺:MINI STM32開發板 基于STM32F103VET6
- * 硬件連接:------------------------
- * | PA9 - USART1(Tx) |
- * | PA10 - USART1(Rx) |
- * ------------------------
- * 庫版本 :ST3.0.0
- **********************************************************************************/
- #include "usart1.h"
- #include <stdarg.h>
- void USART1_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- /* 使能 USART1 時鐘*/
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
- /* USART1 使用IO端口配置 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復用推挽輸出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空輸入
- GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIOA
-
- /* USART1 工作模式配置 */
- USART_InitStructure.USART_BaudRate = 115200; //波特率設置:115200
- USART_InitStructure.USART_WordLength = USART_WordLength_8b; //數據位數設置:8位
- USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位設置: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(USART1, &USART_InitStructure); //初始化USART1
- USART_Cmd(USART1, ENABLE);// USART1使能
- }
- /*發送一個字節數據*/
- void UART1SendByte(unsigned char SendData)
- {
- USART_SendData(USART1,SendData);
- while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
- }
- /*接收一個字節數據*/
- unsigned char UART1GetByte(unsigned char* GetData)
- {
- if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
- { return 0;//沒有收到數據
- }
- *GetData = USART_ReceiveData(USART1);
- return 1;//收到數據
- }
- /*接收一個數據,馬上返回接收到的這個數據*/
- void UART1Test(void)
- {
復制代碼
|