#include "stm32f10x.h"
#include "stdio.h" //标准IO库函数
#include "Usart.h"
void Usart_Configurtion(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9; //TX IO口配置
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; //RX IO口配置
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate=115200; //波特率115200,直接配置多少就是多少,系统会根据时钟自动调整
USART_InitStructure.USART_WordLength=USART_WordLength_8b; //数据位
USART_InitStructure.USART_StopBits=USART_StopBits_1; //停止位
USART_InitStructure.USART_Parity=USART_Parity_No; //奇偶校验位
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); //接收中断使能
USART_Cmd(USART1,ENABLE); //使能USART1
}
void Usart_NVIC_Configuration(void) //配置串口中断函数
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* Enable the USARTz Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
//NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void Usart_Init(void) //USART配置
{
Usart_NVIC_Configuration(); //中断配置
Usart_Configurtion(); //USART_IO&工作模式配置
}
{
USART_SendData(USART1, (unsigned char) ch);// USART1 ???? USART2 ?
while (!(USART1->SR & USART_FLAG_TXE));
return (ch);
#include "stdio.h" //标准IO库函数
#include "Usart.h"
void Usart_Configurtion(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
//开启GPIOA & USART1时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9; //TX IO口配置
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; //RX IO口配置
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate=115200; //波特率115200,直接配置多少就是多少,系统会根据时钟自动调整
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_ITConfig(USART1,USART_IT_RXNE,ENABLE); //接收中断使能
USART_Cmd(USART1,ENABLE); //使能USART1
}
void Usart_NVIC_Configuration(void) //配置串口中断函数
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* Enable the USARTz Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
//NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void Usart_Init(void) //USART配置
{
Usart_NVIC_Configuration(); //中断配置
Usart_Configurtion(); //USART_IO&工作模式配置
}
//标准库函数重定向发送数据,重定向后即可使用printf();直接发送数据到USART1
//*注:在target中勾选MicroLib
int fputc(int ch, FILE *f){
USART_SendData(USART1, (unsigned char) ch);// USART1 ???? USART2 ?
while (!(USART1->SR & USART_FLAG_TXE));
return (ch);
}
//发送数据函数
void Uart_Senddata(u8 data)
{
while(USART_GetBitState(USART1, USART_FLAG_TBE) == RESET);
//确保数据全部发送后再执行下次发送
USART_DataSend(USART1,data);
}