使用STM32串口的中斷方式接收數(shù)據(jù),接收來(lái)自另外一板子的按鍵數(shù)字,同時(shí)點(diǎn)亮相應(yīng)的LED燈。 工程結(jié)構(gòu)圖:
本文引用地址:http://m.butianyuan.cn/article/201611/321299.htm
1、 main.c代碼截圖如下;
2、其中的LED代碼與另外一篇《STM32 基于庫(kù)函數(shù)控制按鍵蜂鳴器 LED顯示》代碼完全同。這里就不上了。
3、USART驅(qū)動(dòng)部分:
#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); //開(kāi)啟USART使用的GPIO的時(shí)鐘
#ifdef usart1
RCC_APB2PeriphClockCmd(USART_RCC,ENABLE); //開(kāi)啟USART的時(shí)鐘
#elif defined usart2
RCC_APB1PeriphClockCmd(USART_RCC,ENABLE);
#endif
GPIO_InitStructure.GPIO_Pin =USART_TX_Pin;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP; //復(fù)用推免式輸出
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位字長(zhǎng)
USART_InitStructure.USART_StopBits=USART_StopBits_1; //1位停止位
USART_InitStructure.USART_Parity=USART_Parity_No; //無(wú)奇偶效驗(yàn)位
USART_InitStructure.USART_Mode=USART_Mode_Tx|USART_Mode_Rx; //發(fā)送接收模式
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //無(wú)硬件流控
USART_Init(USART,&USART_InitStructure);
USART_ITConfig(USART,USART_IT_RXNE,ENABLE);
USART_Cmd(USART,ENABLE);
NVIC_Configuration();
}
評(píng)論