STM32之串口通信 - USART

内容:使用STM32最小系统板和蓝牙模块实现 手机串口助手和单片机的通讯。

硬件:STM32F103C8T6,HC-06蓝牙模块

软件:蓝牙串口助手(手机应用商店)

引脚定义:

#define GPIO_TXD GPIO_Pin_9;

#define GPIO_RXD GPIO_Pin_10;

注意:

TX(单片机)-RX(HC-06)

RX(单片机)-DX(HC-06)

代码:

UART.c文件代码:

1. 打开GPIOA和USART的时钟

2. 配置GPIOA引脚以及USART通信参数 

void UART_Init(void)

{

//步骤一:

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);

//步骤二:

配置GPIOA引脚

(TX)

GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure);

(RX)

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉输入

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure);

//配置USART参数:

USART_InitTypeDef UART_InitStructure;

UART_InitStructure.USART_BaudRate = 9600; // 设置波特率为9600

UART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //禁用硬件流控制

UART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; // 同时支持发送和接收

UART_InitStructure.USART_Parity = USART_Parity_No; // 不使用奇偶校验

UART_InitStructure.USART_StopBits = USART_StopBits_1; // 设置停止位为1位

UART_InitStructure.USART_WordLength = USART_WordLength_8b; // 设置数据位长度为8位

USART_Init(USART1, &UART_InitStructure); // 使用上述配置初始化USART1

USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//函数用于配置 USART 的中断使能状态

NVIC_Init_1();

USART_Cmd(USART1,ENABLE);//使能USART1

}

        当USART在接受到数据时,需要触发中断(NVIC),并在中断函数中对接收到的数据进行处理。

3. 中断函数

void NVIC_Init_1(void)

{

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

NVIC_InitTypeDef NVIC_InitStructure;

NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

NVIC_Init(&NVIC_InitStructure);

}

 串口中断函数:目的是实现

当发送'1'时,LED灯熄灭;

当发送'0'时,LED灯点亮;

void USART1_IRQHandler(void)

{

u8 RxData;

if(USART_GetFlagStatus(USART1,USART_IT_RXNE) == SET)// 等待数据接收

{

RxData = USART_ReceiveData(USART1);// 读取接收到的数据:把数据存储到RxData

RxData = Serial_GetRxData();

if(RxData == '1')

{

LED_Control(0);

}

if(RxData == '0')

{

LED_Control(1);

}

USART_ClearITPendingBit(USART1,USART_IT_RXNE);// 清除标志位

}

}

led.c文件代码:

这里是直接使用STM32最小系统板上的灯(使能PC13即可点亮)

#include "stm32f10x.h" // Device header

#include "Delay.h"

void LED_Init(void)

{

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOC, &GPIO_InitStructure);

}

//LED_Control(0): 灯亮 | LED_Control(1):灯灭

void LED_Control(uint8_t n)

{

if(n)

GPIO_SetBits(GPIOC, GPIO_Pin_13);

else

GPIO_ResetBits(GPIOC, GPIO_Pin_13);

}

4.主函数

#include "stm32f10x.h" // Device header

#include "UART.h"

#include "LED.h"

uint8_t RxData;

int main(void)

{

UART_Init();

LED_Init();

while (1)

{

}

}

程序现象:

蓝牙窜口APP(HC-6)程序现象程序现象https://live.csdn.net/v/310055

2023年7月7日20:37:23

----------------------------------------------------------

2023年9月3日19:16:10

链接:https://pan.baidu.com/s/1BHjNEUnUUpW4Vguooy1mXA  提取码:1111

----------------------------------------------------------

2024年2月20日

链接:https://pan.baidu.com/s/1JfVN0T02vTSnB2_NsjUnSg  提取码:0220

推荐阅读

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: