/*****************************************************************************
//函数名:void USART1_Configura
tion(void)
//功能:串口(USART1)重映射初始化配置函数,由TX PA9~PB6 RX PA10~~PB7
*****************************************************************************/
void USART1_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
USART_DeInit(USART1); //复位串口1
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOB, ENABLE);//使能GPIOB以及复用功能时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//开启串口1时钟
GPIO_PinRemapConfig(GPIO_Remap_USART1,ENABLE);//使能端口重映射
//USART1_TX发射引脚 PB.6
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //PB.6
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PB6
//USART1_RX接受引脚 B.7
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PB7
//USART1串口初始化设置
USART_InitStructure.USART_BaudRate = 9600;//一般设置为9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//8位数据格式
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_Init(USART1, &USART_InitStructure); //初始化串口
//USART1 NVIC 配置
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //分组
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;//抢占优先级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;//子优先级0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//IRQ通道使能
NVIC_Init(&NVIC_InitStructure);//根据指定的参数初始化VIC寄存器
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启接收中断
USART_Cmd(USART1, ENABLE); //使能串口
USART_ClearFlag(USART1,USART_FLAG_TC);//解决第一个字节无法发送的问题
}
以上是配置的代码,应该是配置好了的,下面的中断函数是能进入的,我用led是否点亮来测试的,已发送led就有反应,但
手机就是收不到
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)
{
if(USART_ReceiveData(USART1)=='a') GPIO_SetBits(GPIOB,GPIO_Pin_5);//发送‘a’led点亮
delay(1000);
USART_SendData(USART1,USART_ReceiveData(USART1));//发回数据,但没能实现!!!
delay(1000);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
}
}
希望大家帮忙看一下