一、本次实验用到的单片机是STM32F103ZET6,正点原子战舰开发板 用到的单片机资源:LED、按键、USART1(两组IO) 两个LED接PB5和PE5,都是高电平灭,低电平亮 两个按键KEY0和KEY1接到PE4和PE2,都是按下低电平,不按高电平 在STM32F103ZET6里面,有两组IO都可以作为串口1来使用,如下表,但是在使用的过程中配置上有区别
USART1_TX | USART1_RX |
---|
PA9 | PA10 | PB6 | PB7 |
实现的目的:
1、按下KEY0,红灯亮、绿灯灭、串口调试助手接收到0x0a,0x0b,0x0c,0x0d
2、按下KEY1,绿灯亮、红灯灭、串口调试助手接收到0x11,0x22,0x33,0x44
3、串口调试助手发送0xaa,0xbb,红灯绿灯都点亮。发送其他数据,红灯绿灯都熄灭
二、先使用PA9和PA10来实现想要的效果 硬件连接: TX-PA10 RX-PA9
led初始化
#include "led.h"
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOE, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_5);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_SetBits(GPIOE,GPIO_Pin_5);
}
按键初始化
#include "stm32f10x.h"
#include "key.h"
#include "sys.h"
void KEY_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOE, &GPIO_InitStructure);
}
串口1初始化(PA9、PA10)
void uart_init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_InitStructure.USART_BaudRate = bound;
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_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
}
串口1中断服务函数
void USART1_IRQHandler(void)
{
u8 Res;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
Res =USART_ReceiveData(USART1);
if((USART_RX_STA&0x8000)==0)
{
if(USART_RX_STA&0x4000)
{
if(Res!=0x0a)USART_RX_STA=0;
else USART_RX_STA|=0x8000;
}
else
{
if(Res==0x0d)USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;
}
}
}
}
}
主函数
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "usart.h"
int a[]={0x0a,0x0b,0x0c,0x0d};
int a1[]={0x11,0x22,0x33,0x44};
int main(void)
{
u16 i;
u16 len;
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(115200);
LED_Init();
KEY_Init();
while(1)
{
if(KEY0==0)
{
LED0=0;
LED1=1;
for(i=0;i<4;i++)
{
USART_SendData(USART1, a[i]);
delay_ms(20);
}
}
if(KEY1==0)
{
LED0=1;
LED1=0;
for(i=0;i<4;i++)
{
USART_SendData(USART1, a1[i]);
delay_ms(20);
}
}
if(USART_RX_STA&0x8000)
{
len=USART_RX_STA&0x3fff;
if((USART_RX_BUF[0]==0xaa)&&(USART_RX_BUF[1]==0xbb))
{
LED0=0;
LED1=0;
}
else
{
LED0=1;
LED1=1;
}
USART_RX_STA=0;
}
}
}
开发板上电,红灯、绿灯均熄灭
图1
|
图2
|
按下KEY0,红灯亮、绿灯灭,串口持续接收到0x0a,0x0b,0x0c,0x0d
图1
|
图2
|
按下KEY1,红灯灭、绿灯亮,串口持续接收到0x11,0x22,0x33,0x44
图1
|
图2
|
串口发送aa bb,红灯绿灯都亮
图1
|
图2
|
三、再使用PB6、PB7来实现上述效果 硬件连接: TX-PB7 RX-PB6
所有的配置都一样,除了串口的初始化,需要修改两个地方 1、开启重映射时钟、GPIOB时钟,开启重映射
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE);
2、IO口初始化GPIOB_6和GPIOB_7
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
其他地方都和配置PA_9和PA_10一样
注意事项:
1、发送数据的时候,需要勾选发送新行,或者不勾选发送新行,但需要在后面加上0d 0a
2、串口1重定义对应引脚
百度云链接: 链接:https://pan.baidu.com/s/168ataeLQULKHx6S36T_XPA?pwd=kezp 提取码:kezp
|