使用STM32串口的中断方式接收数据,接收来自另外一板子的按键数字,同时点亮相应的LED灯。工程结构图:
本文引用地址://m.amcfsurvey.com/article/201611/321299.htm
![](//m.amcfsurvey.com/editerupload/fetch/20161101/321299_1_0.jpg)
1、 main.c代码截图如下;
![](//m.amcfsurvey.com/editerupload/fetch/20161101/321299_1_1.jpg)
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_InitTypeDefGPIO_InitStructure;
USART_InitTypeDefUSART_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);
NVIC_Configuration();
}
评论