前言
本篇文章记录了如何使用stm32f103zet6核心板(下位机)通过串口向stm32f103zet6学习板(上位机)发送数据,stm32f103zet6学习板完成接收,并将接收到的数据显示在LCD显示屏上。
一、硬件准备及接线
硬件准备
正点原子精英STM32F103ZET6学习板一块(带2.8寸显示屏)
STM32F103ZET6核心板一块
电源适配器、数据线以及杜邦线若干。
接线
PA2(核心板)------------PA3(学习板)
GND(核心板)----------GND(学习板)
二、程序
上位机程序
main.c文件:
#include "sys.h"
#include "lcd.h"
#include "delay.h"
#include "usart.h"
#include "usart2.h"
int main()
{
u8 x=0,i=0;
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart2_init(115200);
uart_init(115200);
LCD_Init();
POINT_COLOR=RED;
while(1)
{
for(i=0;i<10;i++)
{
LCD_ShowxNum(20*(i+1),120,receive_buff[i],1,24,1); //显示接收到的数据
}
x++;
delay_ms(100);
if(x == 10)
{
LCD_Clear(WHITE); //清屏
x = 0;
}
}
}
usart2.c文件:
#include "sys.h"
#include "usart2.h"
#include "stm32f10x.h"
#if EN_USART2_RX //如果使能了接收
u8 receive_buff[10] = {0};
u8 i=0;
u8 rx_state = 0; //接收标志量
void uart2_init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //使能GPIOA时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); //使能USART2
USART_DeInit(USART2);
//USART2_TX GPIOA.2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA.2
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA,&GPIO_InitStructure);//初始化GPIOA.2
//USART2_RX GPIOA.3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //PA.3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化GPIOA.3
//Usart2 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3 ; //抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
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(USART2,&USART_InitStructure); //初始化串口2
USART_ITConfig(USART2,USART_IT_RXNE, ENABLE);//开启串口接受中断
USART_Cmd(USART2,ENABLE); //使能串口2
}
void USART2_IRQHandler(void)//串口2中断服务程序
{
if(USART_GetITStatus(USART2,USART_IT_RXNE) != RESET)
{
if(rx_state == 1)
{
receive_buff[i] = USART_ReceiveData(USART2);
i++;
if(i == 10)
{
rx_state = 0; //停止接收
i = 0;
}
}
if(USART_ReceiveData(USART2) == 0x0d)
{
rx_state = 1; //开始接收
}
}
}
#endif
下位机程序
main.c文件:
#include "sys.h"
#include "usart2.h"
#include "delay.h"
#include "led.h"
u8 send_buff[10] = {0,1,2,3,4,5,6,7,8,9};
void Usart2_Sendata()
{
u8 i = 0;
USART_SendData(USART2,0x0d);
while( USART_GetFlagStatus(USART2,USART_FLAG_TC)!= SET);
for(i = 0;i < 11;i++)
{
USART_SendData(USART2,send_buff[i]);
while( USART_GetFlagStatus(USART2,USART_FLAG_TC)!= SET);
}
}
int main(void)
{
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart2_init(115200);
while(1)
{
Usart2_Sendata(); //发送数据
}
}
三、运行结果
文章来源:https://www.toymoban.com/news/detail-601733.html
后记
两块stm32单片机串口通信完整工程文件点此获得。文章来源地址https://www.toymoban.com/news/detail-601733.html
到了这里,关于两块stm32单片机串口通信讲解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!