一、串口通信
1、定义
串口通信是一种通过串行接口(通常是RS-232、RS-485或USB)进行数据传输的通信方式。串口通信可以用于连接计算机与外部设备(如打印机、传感器、控制器等)进行数据传输和控制。
在串口通信中,数据是按照位的顺序依次传输的,每个数据位由一个或多个字节组成。通信的双方需要事先约定好数据的格式和通信协议,以确保数据的正确传输和解析。
串口通信具有简单、稳定、成本低等优点,因此在很多领域都被广泛应用,如工业控制、通信设备、汽车电子等领域。随着技术的发展,串口通信逐渐被更快速、更稳定的通信方式(如以太网、无线通信等)所取代,但在一些特定的场景下,串口通信仍然具有重要的地位。
本次用RS-232举例。
2、RS-232(DB9)
RS-232接口是一种用于串行通信的标准接口,通常用于连接计算机和外部设备,如调制解调器、打印机和串行通信设备。这种接口使用串行通信协议来传输数据,通常通过DB-9或DB-25连接器进行连接。
使用RS-232接口进行串口通信涉及将数据通过串行通信协议在RS-232标准下进行传输。通常需要一台计算机或控制设备与另一台设备(如打印机、传感器等)之间建立通信连接。
在进行串口通信时,您需要确保连接正确的RS-232接口线缆(通常是使用DB-9或DB-25连接器),并设置适当的波特率、数据位、停止位和校验位等通信参数。然后,您可以使用适当的串口通信软件(如HyperTerminal、PuTTY等)来发送和接收数据。
二、实验
1、使用标准库完成LED点灯或流水灯
流水灯代码:
#include "stm32f10x.h" // Device header
#include "Delay.h"
int main(void)
{
/*开启时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //开启GPIOA的时钟
//使用各个外设前必须开启时钟,否则对外设的操作无效
/*GPIO初始化*/
GPIO_InitTypeDef GPIO_InitStructure; //定义结构体变量
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //GPIO模式,赋值为推挽输出模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; //GPIO引脚,赋值为所有引脚
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //GPIO速度,赋值为50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //将赋值后的构体变量传递给GPIO_Init函数
//函数内部会自动根据结构体的参数配置相应寄存器
//实现GPIOA的初始化
/*主循环,循环体内的代码会一直循环执行*/
while (1)
{
/*使用GPIO_Write,同时设置GPIOA所有引脚的高低电平,实现LED流水灯*/
GPIO_Write(GPIOA, ~0x0001); //0000 0000 0000 0001,PA0引脚为低电平,其他引脚均为高电平,注意数据有按位取反
Delay_ms(500); //延时100ms
GPIO_Write(GPIOA, ~0x0002); //0000 0000 0000 0010,PA1引脚为低电平,其他引脚均为高电平
Delay_ms(500); //延时100ms
GPIO_Write(GPIOA, ~0x0004); //0000 0000 0000 0100,PA2引脚为低电平,其他引脚均为高电平
Delay_ms(500); //延时100ms
}
}
实验效果:
2、使用串口发送hello world!
main.c代码:
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "Serial.h"
int main()
{
Serial_Init();
char String[]="hello windons!\r\n";
while(1)
{
Serial_SendString(String);
Delay_s(1); //间隔1s发送
}
}
Serial.c代码:
#include "stm32f10x.h" // Device header
#include <stdio.h>
#include <stdarg.h>
void Serial_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); //打开USART1外设的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //因为要用到GPIOA引脚,所以打开GPIOA的时钟
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); //配置好引脚输入输出模式
USART_InitTypeDef USART_InitStructure;
//引出该结构体的所有属性,进行赋值。
USART_InitStructure.USART_BaudRate=9600; //波特率
USART_InitStructure.USART_WordLength=USART_WordLength_8b; //不需要校验选择8bit的字长,如果需要一位奇偶校验位,则选择9bit的字长
USART_InitStructure.USART_StopBits=USART_StopBits_1; //选择一位停止位。
USART_InitStructure.USART_Parity=USART_Parity_No; //奇偶性:奇偶校验,选择不校验。odd是奇校验,even是偶校验。
USART_InitStructure.USART_Mode=USART_Mode_Tx; //选择发送模式
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //硬件流控制:选择无流控制
USART_Init(USART1,&USART_InitStructure); //配置好USART1外设的模式
USART_Cmd(USART1,ENABLE); //使能USART1,串口开始运行。第一个参数是要操作的USART外设,第二个参数是指定是否使能该外设
}
void Serial_SendByte(uint8_t Byte) //int8_t代表字符型
{
USART_SendData(USART1,Byte);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET) //USART_GetFlagStatus(USART1,USART_FLAG_TXE)这个函数的返回值先简单地理解为:数据已经发送了,返回SET;数据还没有发送,返回RESET。
{
;
}
}
void Serial_SendString(char *String) //发送一个字符数组
{
uint8_t i;
for(i=0;String[i]!='\0';i++)
{
Serial_SendByte(String[i]);
}
}
实验效果图:
3、串口控制LED灯的亮灭
代码:
#include "stm32f10x.h"
#include "Delay.h"
int main(void)
{
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP;
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);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_2MHz;
GPIO_Init(GPIOA,&GPIO_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_Mode=USART_Mode_Rx;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1,ENABLE);
GPIO_WriteBit(GPIOA,GPIO_Pin_1,0);
while (1)
{
if(USART_GetFlagStatus(USART1, USART_IT_RXNE) == SET)
{
unsigned char a= USART_ReceiveData(USART1);
if(a=='Y')
{
GPIO_WriteBit(GPIOA,GPIO_Pin_1,1);
}
else if(a=='N')
{
GPIO_WriteBit(GPIOA,GPIO_Pin_1,0);
}
else
{
}
}
}
}
实验效果图:
本人初学,有许多不懂的地方,博客中有误的地方欢迎指出。