电工技术基础_电工基础知识_电工之家-电工学习网

欢迎来到电工学习网!

stm32串口接纳数据程序

2017-11-13 00:55分类:电子技术 阅读:

 

stm32串口接纳数据程序
void init_usart(void)
{


//RCC初始化
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);//使能GPIOA时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

//nvic

NVIC_InitTypeDef NVIC_InitStructure;

#ifdef VECT_TAB_RAM

NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else

NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif


NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

//GPIO初始化

GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOA,&GPIO_InitStructure);

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOA,&GPIO_InitStructure);

//USART初始


//USART_DeInit(USART2);

USART_InitTypeDef USART_InitStructure; //串口设置康复默许参数
USART_ClockInitTypeDef USART_ClockInitStructure;


USART_InitStructure.USART_BaudRate = 9600; //波特率9600
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发送功用

USART_ClockInitStructure.USART_Clock =USART_Clock_Disable;
USART_ClockInitStructure.USART_CPOL =USART_CPOL_High;
USART_ClockInitStructure.USART_CPHA =USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit =USART_LastBit_Disable;

USART_ClockInit(USART2,&USART_ClockInitStructure);
USART_Init(USART2, &USART_InitStructure); //初始化

USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); //翻开接纳中止,这个有必要在翻开串口之前设置

USART_Cmd(USART2, ENABLE); //主张串口
}

void USART2_IRQHandler(void)
{
//接纳中止
if(USART_GetITStatus(USART2,USART_IT_RXNE)==SET)
{
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
usart_rx=USART_ReceiveData(USART2);
usart_rx_flag=1;
}
//溢出-假定发作溢出需求先读SR,再读DR寄存器则可根除不断入中止的疑问[牛人说要这么]
if(USART_GetFlagStatus(USART2,USART_FLAG_ORE)==SET)
{
USART_ClearFlag(USART2,USART_FLAG_ORE); //读SR正本即是根除象征
USART_ReceiveData(USART2); //读DR
}

}

上一篇:数字电子时钟电路图计划原理

下一篇:啥是反时限过流维护,反时限过流维护动作原理

相关推荐

电工推荐

    电工技术基础_电工基础知识_电工之家-电工学习网
返回顶部