运用STM32串口的中止方法接纳数据,接纳来自别的一板子的按键数字,一起点亮相应的LED灯。
工程结构图:

1、 main.c代码截图如下;

2、其间的LED代码与别的一篇《STM32 根据库函数操控按键蜂鸣器 LED显现》代码彻底同。这儿就不上了。
3、USART驱动部分:
#include”stm32f10x.h”
#include”usart1.h”
#include
#include
//========================================================
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
//========================================================
static void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(USART_Port_RCC|RCC_APB2Periph_AFIO,ENABLE); //敞开USART运用的GPIO的时钟
#ifdef usart1
RCC_APB2PeriphClockCmd(USART_RCC,ENABLE); //敞开USART的时钟
#elif defined usart2
RCC_APB1PeriphClockCmd(USART_RCC,ENABLE);
#endif
GPIO_InitStructure.GPIO_Pin =USART_TX_Pin;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP; //复用推免式输出
GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
GPIO_Init(USART_TX_Port,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin =USART_RX_Pin;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_Init(USART_RX_Port,&GPIO_InitStructure);
USART_InitStructure.USART_BaudRate=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_Mode=USART_Mode_Tx|USART_Mode_Rx; //发送接纳形式
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //无硬件流控
USART_Init(USART,&USART_InitStructure);
USART_ITConfig(USART,USART_IT_RXNE,ENABLE);
USART_Cmd(USART,ENABLE);
NV%&&&&&%_Configuration();
}
声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/zhishi/shuzi/259152.html