微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32 UART5 中断接收程序

STM32 UART5 中断接收程序

时间:12-03 来源:互联网 点击:

= 0;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);*/

}

/****************************************

* 函数名称 :GPIO_Configuration()

* 功能描述 : GPIO配置

* 参数 : 无

* 返回值 : 无

* 全局变量 : 无

* 全局静态变量: 无

* 局部静态变量: 无

****************************************/

void GPIO_Configuration()

{

GPIO_InitTypeDef GPIO_InitStructure; //定义GPIO初始化结构体

//--------将UART5 的TX 配置为复用推挽输出 AF_PP---------------------//

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_12; //管脚位置定义,标号可以是NONE、ALL、0至15。

GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //输出速度50MHz

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //推挽输出模式 Out_PP

GPIO_Init(GPIOC,&GPIO_InitStructure); //E组GPIO初始化

//--------将USART1 的TX 配置为复用推挽输出 AF_PP---------------------//

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

GPIO_Init(GPIOA, &GPIO_InitStructure);

//--------将UART5 的RX 配置为复用浮空输入 IN_FLOATING---------------------//

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2; //管脚位置定义

//输入模式下配置输出速度无意义

//GPIO_InitStructure.GPIO_Speed=GPIO_Speed_2MHz; //输出速度2MHz

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //浮空输入 IN_FLOATING

GPIO_Init(GPIOD,&GPIO_InitStructure); //C组GPIO初始化

//--------将USART1 的RX 配置为复用浮空输入 IN_FLOATING---------------------//

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

GPIO_Init(GPIOA, &GPIO_InitStructure);

}

/****************************************************

* 函数名称 :USART1_Configuration( )

* 功能描述 : 配置USART1数据格式、波特率等参数

* 参数 : 无

* 返回值 : 无

* 全局变量 : 无

* 全局静态变量: 无

* 局部静态变量: 无

*******************************************************/

void UART5_Configuration( )

{

USART_InitTypeDef USART_InitStructure; //串口设置恢复默认参数

USART_InitStructure.USART_BaudRate = 115200; //波特率115200

USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长8位

USART_InitStructure.USART_StopBits = USART_StopBits_1; //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; //打开Rx接收和Tx发送功能

/* Configure USART1 */

//USART_Init(USART1, &USART_InitStructure);

/* Configure UART5 */

USART_Init(UART5, &USART_InitStructure); //初始化

/* Enable USART1 Receive and Transmit interrupts */

// USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

// USART_ITConfig(USART1, USART_IT_TXE, ENABLE);

/* Enable UART5 Receive and Transmit interrupts */

USART_ITConfig(UART5, USART_IT_RXNE, ENABLE); // 若接收数据寄存器满,则产生中断

// USART_ITConfig(UART5, USART_IT_TXE, ENABLE);

/* Enable the USART1 */

// USART_Cmd(USART1, ENABLE);

/* Enable the UART5 */

USART_Cmd(UART5, ENABLE); //启动串口

//-----如下语句解决第1个字节无法正确发送出去的问题-----//

USART_ClearFlag(UART5, USART_FLAG_TC); // 清标志

// USART_ClearFlag(USART1, USART_FLAG_TC);

}

/********这是中断服务子程序,在stm32f10x_it.c中************************ */

void UART5_IRQHandler(void)

{

if(USART_GetITStatus(UART5, USART_IT_RXNE) != RESET) //若接收数据寄存器满

{

TxBuffer5[i]=USART_ReceiveData(UART5);

i++;

}

if(i==20)

{ flag=1;

i=0;}

// USART_SendData(UART5, USART_ReceiveData(UART5)); //回发给PC

// while(USART_GetFlagStatus(UART5, USART_IT_TXE)==RESET);//等待发完

}

/*

void delay(void)

{

unsigned int a,b;

for(a=0;a<1000;a++)

for(b=0;b<200;b++)

} */

/**************************************************

* 函数名称 :main()

* 功能描述 : 主函数

* 参数 : 无

* 返回值 : 无

* 全局变量 : 无

* 全局静态变量: 无

* 局部静态变量: 无

****************************************************/

int main()

{

unsigned int j;

RCC_Configuration();

G

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top