您的位置 首页 被动

stm32快速学习5——串口中止接纳

串口自发自收设定串口时钟设定引脚功能中断优先级设定串口Main文件#includestm32f10x.hvoidRCC_Configuration(void);voidGPIO_Configurat.

串口自发自收

设定串口时钟

设定引脚功用

中止优先级

设定串口

Main文件

#include”stm32f10x.h”

voidRCC_Configuration(void);

voidGPIO_Configuration(void);

voidUSART_Configuration(void);

voidNVIC_Configuration(void);

intmain(void)

{

RCC_Configuration();

GPIO_Configuration();

NVIC_Configuration();

USART_Configuration();

while(1);

}

voidRCC_Configuration(void)

{

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA,ENABLE);

}

voidGPIO_Configuration(void)

{

GPIO_InitTypeDefGPIO_InitStructure;

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOA,&GPIO_InitStructure);

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);

}

voidUSART_Configuration(void)

{

USART_InitTypeDefUSART_InitStructure;

USART_InitStructure.USART_BaudRate=115200;

USART_InitStructure.USART_WordLength=USART_WordLength_8b;

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_Tx|USART_Mode_Rx;

USART_Init(USART1,&USART_InitStructure);

USART_Cmd(USART1,ENABLE);

USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);/*接纳中止使能*/

}

voidNVIC_Configuration(void)

{

NVIC_InitTypeDefNVIC_InitStructure;

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;/*3.4的库不是运用USART1_IRQChannel,看stm32f10x.h吧*/

NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;

NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;

NVIC_Init(&NVIC_InitStructure);

}

Stm32f10x_it.c参加

voidUSART1_IRQHandler(void)

{

unsignedinti;

if(USART_GetFlagStatus(USART1,USART_IT_RXNE)==SET)

{

i=USART_ReceiveData(USART1);

USART_SendData(USART1,i);

while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);

}

if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)

{

/*清楚接纳中止标志*/

USART_ClearITPendingBit(USART1,USART_IT_RXNE);

}

}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/ziliao/beidong/268262.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部