stm32串口通信入门

一、理论、原理

1.串口协议

串口协议是一种计算机通信协议,用于数据在两个设备间的传输。串口通信通常是指串行通信,它将数据一位接一位地按顺序传输,与并行通信不同,后者同时通过多个线路传输多个位。

2.RS-232标准

RS-232(推荐标准232)是串行通信接口的一个早期标准,最初由电子工业联盟(EIA)在1962年提出。它定义了数据通信的电气和机械接口,例如计算机和电话调制解调器之间的连接。

3.RS232电平与TTL电平的区别

RS-232电平:在RS-232标准中,逻辑“1”用-3到-15伏特之间的电压表示,而逻辑“0”用+3到+15伏特之间的电压表示。这种高负电压和正电压的组合使信号对噪声有很好的抵抗能力。
TTL电平:TTL(Transistor-Transistor Logic)电平通常用于数字电路内部,其中逻辑“1”用+2V至+5V之间的电压表示,逻辑“0”用0V至0.8V之间的电压表示。

4."USB/TTL转232"模块的工作原理

USB到TTL转换:模块通过USB接口与计算机或其他USB设备通信。计算机发送的数据以USB协议格式传输,CH340芯片将这些数据转换为TTL电平的串行数据信号。
TTL到RS-232转换:由于TTL电平和RS-232电平不兼容,CH340芯片内部进一步将这些TTL电平的串行数据信号转换为RS-232电平的信号,这样就可以与遵循RS-232标准的设备进行通信。
反向转换:当RS-232设备发送数据时,过程相反。CH340芯片将接收到的RS-232电平信号转换为TTL电平,然后再将TTL电平信号转换为USB协议格式的数据,通过USB接口发送到计算机。

二、标准库方式,完成LED的点灯实验

1.使用标准库

![[5201e158cc95a637d6485682b945ebd.png]]

(建立User、BSP、Systems、FWLib文件夹,并将相应文件复制入文件夹中)

![[8ed3d4002ddfda14b52e76b381ce962.png]]

![[aeaf2d5f59018ae4f2bc9d83b8954f7.png]]

(修改部分设置)

2.代码

#include “stm32f10x.h”

void GPIO_Config(void) {
// 使能GPIOC时钟
RCC->APB2ENR |= RCC_APB2ENR_IOPCEN;

// 配置PC13为推挽输出模式
GPIOC->CRH &= ~GPIO_CRH_MODE13;  // 清楚当前设置
GPIOC->CRH |= GPIO_CRH_MODE13_1; // 设置为输出模式,速度2MHz
GPIOC->CRH &= ~GPIO_CRH_CNF13;  // 推挽模式

}

int main(void)
{
GPIO_Config(); // 配置GPIO

while(1)
{
    // 点亮LED
    GPIOC->BSRR = GPIO_BSRR_BR13;  // 设置PC13为低电平
    for (volatile int i = 0; i < 100000; i++); // 简单延时

    // 熄灭LED
    GPIOC->BSRR = GPIO_BSRR_BS13; // 设置PC13为高电平
    for (volatile int i = 0; i < 100000; i++); // 简单延时
}

}
![[aeaf3a6e8a414d225acee618d71e968.png]]

三、STM32系统给上位机(win10)连续发送“hello windows!”,win10采用“串口助手”工具接收。

1.代码:

#include “stm32f10x.h”

void RCC_Configuration(void) {
/* Enable UART clock and GPIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
}

void GPIO_Configuration(void) {
GPIO_InitTypeDef GPIO_InitStructure;

/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);

/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);

}
void USART_Configuration(void) {
USART_InitTypeDef USART_InitStructure;

/* USART1 configuration ------------------------------------------------------*/
USART_InitStructure.USART_BaudRate = 9600;
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);

/* Enable USART1 */
USART_Cmd(USART1, ENABLE);

}
void USART_SendString(USART_TypeDef* USARTx, char* str) {
while (*str) {
while (USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET);
USART_SendData(USARTx, *str++);
}
}

int main(void) {
SystemInit();
RCC_Configuration();
GPIO_Configuration();
USART_Configuration();

while (1) {
    USART_SendString(USART1, "hello windows!\n");
    for (int i = 0; i < 1000000; i++); // 延时
}

}
![[3b5023df30d52efbf8930616a293372.png]]

2.软件调试

![[8fab446dd9be5ccc96fc8b62a40d7af.png]]

四、STM32以查询方式接收上位机(win11)串口发来的数据,如果接收到“Y”则点亮链接到stm32上的一个LED灯;接收到“N”则熄灭LED灯

代码:
#include “stm32f10x.h”

void RCC_Configuration(void) {
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC, ENABLE);
}

void GPIO_Configuration(void) {
GPIO_InitTypeDef GPIO_InitStructure;

// USART1 TX on PA.09, RX on PA.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);

// LED on PC.13
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);

}
void USART_Configuration(void) {
USART_InitTypeDef USART_InitStructure;

USART_InitStructure.USART_BaudRate = 9600;
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_Cmd(USART1, ENABLE);

}
int main(void) {
char received_char;

SystemInit();
RCC_Configuration();
GPIO_Configuration();
USART_Configuration();

while (1) {
    // 检查是否收到数据
    if (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) != RESET) {
        received_char = USART_ReceiveData(USART1);

        if (received_char == 'Y') {
            GPIO_SetBits(GPIOC, GPIO_Pin_13); // 点亮 LED
        } else if (received_char == 'N') {
            GPIO_ResetBits(GPIOC, GPIO_Pin_13); // 熄灭 LED
        }
    }
}

}
![[8c3d1d2d5a7f3359fbef347a4892e8b.png]]

  • 15
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
STM32CUBEIDE是一种用于开发STM32微控制器的集成开发环境。它支持多种串口通信协议,包括USART和UART。根据引用,STM32微控制器具有多个USART和UART接口。其中,USART1的时钟来源于APB2总线时钟,最大频率为72MHz,而其他四个USART和UART接口的时钟来源于APB1总线时钟,最大频率为36MHz,如引用所述。 要在STM32CUBEIDE中进行串口通信,可以使用USART和UART接口。根据引用,USART接口支持同步单向通信和半双工单线通信,并且还支持LIN、智能卡协议和IrDA SIR ENDEC规范以及调制解调器操作 (CTS/RTS)。而UART接口只支持异步传输功能,因此没有SCLK、nCTS和nRTS功能引脚,如引用所述。 要在STM32CUBEIDE中配置和使用串口通信功能,您可以按照以下步骤进行操作: 1. 打开STM32CUBEIDE集成开发环境,并创建一个新的工程或打开现有的工程。 2. 在工程中选择要使用的STM32微控制器型号,并配置时钟和引脚设置。 3. 在配置文件中找到串口配置部分,并选择要使用的USART或UART接口。 4. 根据您的需求,配置串口的参数,例如波特率、数据位数、停止位数、校验位等。 5. 在代码中编写相应的初始化函数和发送/接收函数,以实现串口通信功能。 6. 构建和烧录代码到STM32微控制器中,并连接电脑与微控制器的串口进行通信。 请注意,具体的配置和代码实现可能会根据您使用的具体STM32微控制器型号和通信需求而有所不同。因此,建议您参考相关的STM32CUBEIDE文档和参考手册,以获取更详细的指导和示例代码。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [STM32F1与STM32CubeIDE快速入门-USART/UART串口通信](https://blog.csdn.net/wujuxKkoolerter/article/details/123263120)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [【CubeIDE】STM32 HAL库史上最详细教程(一):UART串口收发](https://blog.csdn.net/qq_42652838/article/details/107776265)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值