#include "bsp_USART.h"
#include <stdio.h>
struct __FILE
{
int handle;
};
FILE __stdout;
void _sys_exit(int x)
{
x=x;
}
//printf 重载函数 ->串口1
int fputc(int ch,FILE *f)
{
while(USART_GetFlagStatus(USART1,USART_FLAG_TC) == RESET);
USART_SendData(USART1,(uint8_t)ch);
return ch;
}
//函数功能:初始化串口1外设
void UART1_MY_Init(uint32_t baud)
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource7,GPIO_AF_USART1);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStruct);
USART_InitStruct.USART_BaudRate = baud; //波特率
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无硬件数据流控制
USART_InitStruct.USART_Mode = USART_Mode_Rx|USART_Mode_Tx; //设置收发模式
USART_InitStruct.USART_Parity = USART_Parity_No; //无奇偶校验位
USART_InitStruct.USART_StopBits = USART_StopBits_1; //一个停止位
USART_InitStruct.USART_WordLength = USART_WordLength_8b; //字长为8位数据格式
USART_Init(USART1,&USART_InitStruct);
USART_Cmd(USART1,ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //配置中断,接受数据寄存器非空
NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority= 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&NVIC_InitStruct);
}
void USART1_IRQHandler(void) //串口1中断服务函数
{
if(USART_GetITStatus(USART1,USART_IT_RXNE))
{
USART_SendData(USART1,USART_ReceiveData(USART1));
}
}