打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
stm32串口中断接收

串口自发自收

设定串口时钟

设定引脚功能

中断优先级

设定串口

 

Main文件

#include "stm32f10x.h"

void RCC_Configuration(void);

void GPIO_Configuration(void);

void USART_Configuration(void);

void NVIC_Configuration(void);

int main(void)

{

  RCC_Configuration();

  GPIO_Configuration();

  NVIC_Configuration();

  USART_Configuration();

 

  while(1);

}

void RCC_Configuration(void)

{    

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

}

void GPIO_Configuration(void)

{

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

}

void USART_Configuration(void)

{

  USART_InitTypeDef USART_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); /*接收中断使能*/

}

void NVIC_Configuration(void) 

  NVIC_InitTypeDef NVIC_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加入

void USART1_IRQHandler(void) 

    unsigned int i; 

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

    }

}


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
九九的STM32笔记(四)DMA、USART的演示 (www.ourdev.cn)
stm32串口程序(全)
stm32 外部中断嵌套[操作寄存器+库函数] -Changing's Blog
STM32的USART
stm32的一个UART应用
【新提醒】STM32 USART 串口 DMA 接收和发送的源码详解!
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服