|
/***************************************
* 文件名 :usart1.c
* 描述 :配置USART1
* 實(shí)驗(yàn)平臺(tái):MINI STM32開發(fā)板 基于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 時(shí)鐘*/
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; //復(fù)用推挽輸出
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; //波特率設(shè)置:115200
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //數(shù)據(jù)位數(shù)設(shè)置:8位
USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位設(shè)置:1位
USART_InitStructure.USART_Parity = USART_Parity_No ; //是否奇偶校驗(yàn):無
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //硬件流控制模式設(shè)置:沒有使能
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//接收與發(fā)送都使能
USART_Init(USART1, &USART_InitStructure); //初始化USART1
USART_Cmd(USART1, ENABLE);// USART1使能
}
/*發(fā)送一個(gè)字節(jié)數(shù)據(jù)*/
void UART1SendByte(unsigned char SendData)
{
USART_SendData(USART1,SendData);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
/*接收一個(gè)字節(jié)數(shù)據(jù)*/
unsigned char UART1GetByte(unsigned char* GetData)
{
if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
{
return 0;//沒有收到數(shù)據(jù)
}
*GetData = USART_ReceiveData(USART1);
return 1;//收到數(shù)據(jù)
}
/*接收一個(gè)數(shù)據(jù),馬上返回接收到的這個(gè)數(shù)據(jù)*/
void UART1Test(void)
{
unsigned char i = 0;
while(1)
{
while(UART1GetByte(&i))
{
USART_SendData(USART1,i);
}
}
}
上面的程序用串口助手為什么發(fā)送接收不了數(shù)據(jù)呢?
|
|