使用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_IRQChannelPreemptionPrio
rity = 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);
NVIC_Configuration();
}
//========================================================
PUTCHAR_PROTOTYPE
{
USART_SendData(USART, (uint8_t) ch);
while (USART_GetFlagStatus(USART, USART_FLAG_TC) == RESET)
{
}
return ch;
}
//========================================================
//========================================================
#ifdef Use_USART_Printf
static char *itoa(int value, char *string, int radix)
{
int i, d;
int flag = 0;
char *ptr = string;
if (radix != 10)
{
*ptr = 0;
return string;
}
if (!value)
{
*ptr++ = 0x30;
*ptr = 0;
return string;
}
if (value < 0)
{
*ptr++ = -;
value *= -1;
}
for (i = 10000; i > 0; i /= 10)
{
d = value / i;
if (d || flag)
{
*ptr++ = (char)(d + 0x30);
value -= (d * i);
flag = 1;
}
}
*ptr = 0;
return string;
}
void USART_printf(USART_TypeDef* USARTx, uint8_t *Data,...)
{
const char *s;
int d;
char buf[16];
va_list ap;
va_start(ap, Data);
while ( *Data != 0) // 判断是否到达字符串结束符
{
if ( *Data == 0x5c ) //
{
switch ( *++Data )
{
case r: //回车符
USART_SendData(USARTx, 0x0d);
Data ++;
break;
case n: //换行符
USART_SendData(USARTx, 0x0a);
Data ++;
break;
default:
Data ++;
break;
}
}
else if ( *Data == %)
{ //
switch ( *++Data )
{
case s: //字符串
s = va_arg(ap, const char *);
for ( ; *s; s++)
{
USART_SendData(USARTx,*s);
while( USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET );
}
Data++;
break;
case d://十进制
d = va_arg(ap, int);
itoa(d, buf, 10);
for (s = buf; *s; s++)
{
USART_SendData(USARTx,*s);
while( USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET );
}
Data++;
break;
default:
Data++;
break;
}
}
else USART_SendData(USARTx, *Data++);
while( USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET );
}
}
#endif
//========================================================