|
#include "usart.h"
/*******************************************************************************
* 函 數(shù) 名 : USART1_Init
* 函數(shù)功能 : USART1初始化函數(shù)
* 輸 入 : bound:波特率
* 輸 出 : 無
*******************************************************************************/
void USART3_Init(u32 bound)
{
//GPIO端口設(shè)置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO , ENABLE);
/* 配置GPIO的模式和IO口 */
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//TX //串口輸出PA9
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //復(fù)用推挽輸出
GPIO_Init(GPIOB,&GPIO_InitStructure); /* 初始化串口輸入IO */
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;//RX //串口輸入PA10
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //模擬輸入
GPIO_Init(GPIOB,&GPIO_InitStructure); /* 初始化GPIO */
//USART1 初始化設(shè)置
USART_InitStructure.USART_BaudRate = bound;//波特率設(shè)置
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字長為8位數(shù)據(jù)格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一個停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//無奇偶校驗位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//無硬件數(shù)據(jù)流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收發(fā)模式
USART_Init(USART3, &USART_InitStructure); //初始化串口1
USART_Cmd(USART3, ENABLE); //使能串口1
USART_ClearFlag(USART3, USART_FLAG_TC);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//開啟相關(guān)中斷
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;//串口1中斷通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//搶占優(yōu)先級3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //子優(yōu)先級3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根據(jù)指定的參數(shù)初始化VIC寄存器、
}
/*******************************************************************************
* 函 數(shù) 名 : USART1_IRQHandler
* 函數(shù)功能 : USART1中斷函數(shù)
* 輸 入 : 無
* 輸 出 : 無
*******************************************************************************/
void USART3_IRQHandler(void) //串口1中斷服務(wù)程序
{
u8 r;
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //接收中斷
{
r =USART_ReceiveData(USART3);//(USART1->DR); //讀取接收到的數(shù)據(jù)
USART_SendData(USART3,r);
while(USART_GetFlagStatus(USART3,USART_FLAG_TC) != SET);
}
USART_ClearFlag(USART3,USART_FLAG_TC);
}
把usart1改成usart3,就接收不到數(shù)據(jù)了
|
|