×
嵌入式 > 嵌入式开发 > 详情

STM32 串口中断接收数据

发布时间:2020-09-14 发布时间:
|
#include "stm32f10x.h"

/***********************************************************************
***********************************************************************/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void delay(vu32 nCount)
{
for(; nCount != 0; nCount--);
}
/***********************************************************************
************************************************************************/


main()
{
RCC_Configuration();//系统时钟配置

NVIC_Configuration();//中断配置

GPIO_Configuration();//GPIO口配置

while(1) //LED灯循环亮灭,串口循环发送ASCII“9”
{
delay(5000000);
GPIO_WriteBit(GPIOD,GPIO_Pin_2,Bit_RESET);
USART_SendData(USART1,9);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);
{
}
delay(5000000);
GPIO_WriteBit(GPIOD,GPIO_Pin_2,Bit_SET);
}
}
/*****************************************************************************
*****************************************************************************/
//系统时钟配置
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//使能串口1的GPIO时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOD, ENABLE); //pa.9 pa.10 led0
}
/*****************************************************************************
*****************************************************************************/
//串口GPIO口配置
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;

/* LED0*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);

// GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //悬浮输入
GPIO_Init(GPIOA, &GPIO_InitStructure);


USART_InitStructure.USART_BaudRate = 9600; //设定波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //传输数据位数
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; //使用接收和发送功能
USART_Init(USART1, &USART_InitStructure); //初始化串口1
USART_Cmd(USART1,ENABLE); //串口1使能

USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //使能串口1 读中断


}

/*****************************************************************************
*****************************************************************************/
//中断配置
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //先占优先权2,从优先级2位
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //开串口中断1
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0; //指定抢占优先级别1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //指定相应优先级别0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*****************************************************************************
*****************************************************************************/
void USART1_IRQHandler(void) //串口接收中断,并将接收到得数据发送出
{
u16 temp_trx;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)//判断 是否 接收中断
{
temp_trx = USART_ReceiveData(USART1);
USART_SendData(USART1,temp_trx);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);//判断 发送标志
}
}
/*****************************************************************************
*****************************************************************************/



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
scsi接口