/***************************************
* 文件名 :usart1.c
* 描述 :配置USART1
* 實驗平臺:MINI STM32開發板 基于STM32F103C8T6
* 硬件連接:------------------------
* | 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);
USART_DeInit(USART1);
/* 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 = 9600; //波特率設置: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)
{
unsigned char i = 0;
while(1)
{
while(UART1GetByte(&i))
{
USART_SendData(USART1,i);
}
}
}
上面的程序用串口助手為什么發送接收不了數據呢?
|