一、理论、原理
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.使用标准库
(建立User、BSP、Systems、FWLib文件夹,并将相应文件复制入文件夹中)
(修改部分设置)
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++); // 简单延时
}
}
三、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++); // 延时
}
}
2.软件调试
四、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
}
}
}
}