stm32串口通信入门

1.标准库方式,完成LED的流水灯实验

第一步:初始化GPIO时钟,使用库函数将GPIOx的时钟调为ENABLE(允许)状态。

第二步:初始化GPIO输出模式,有三个需要调节的,第一个是Mode(模式),输出模式分推挽输出、开漏输出、复用推挽输出、复用开漏输出,我们选用推挽输出即可;第三个是输出频率,有2M、10M和50M,这里选50M。

第三步:编写代码控制端口输出,控制LED亮灭。

#include "stm32f10x.h"                  // Device header
#include "delay.h"
void LED_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);	//初始化GPIOA时钟
 
	GPIO_InitTypeDef GPIO_InitStructure;		//初始化输出模式
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	GPIO_ResetBits(GPIOA, GPIO_Pin_0);			//设置初始为低电平
}
 
void LED_ON(void)		//灯亮
{
	GPIO_SetBits(GPIOA, GPIO_Pin_0);
}
 
void LED_OFF(void)	    //灯灭
{
	GPIO_ResetBits(GPIOA, GPIO_Pin_0);
}
 
void LED222(void)		//流水灯
{
	GPIO_SetBits(GPIOA, GPIO_Pin_0);
		 Delay_ms(500);
	GPIO_ResetBits(GPIOA, GPIO_Pin_0);
	GPIO_SetBits(GPIOA, GPIO_Pin_1);
		 Delay_ms(500);
	GPIO_ResetBits(GPIOA, GPIO_Pin_1);	
	GPIO_SetBits(GPIOA, GPIO_Pin_2);
		 Delay_ms(500);
	GPIO_ResetBits(GPIOA, GPIO_Pin_2);
	GPIO_SetBits(GPIOA, GPIO_Pin_3);
		 Delay_ms(500);
	GPIO_ResetBits(GPIOA, GPIO_Pin_3);
	GPIO_SetBits(GPIOA, GPIO_Pin_4);
		 Delay_ms(500);
	GPIO_ResetBits(GPIOA, GPIO_Pin_4);
}

效果如下图 

 

2.串口通讯

 1.串口协议

  串口协议通常指的是用于在计算机和外部设备之间进行数据通信的协议。RS-232是一种常见的串口通信标准,定义了数据通信时信号的电气特性、连接器类型等规范。RS-232标准规定了数据通信时使用的信号电平范围、波特率、数据位、校验位等参数。

   RS-232电平和TTL电平之间的区别在于电压范围不同。RS-232标准使用正负12V的电平表示逻辑1和逻辑0,而TTL电平通常使用0-5V表示逻辑1和逻辑0。由于电平范围不同,RS-232和TTL设备之间通常需要使用电平转换器进行适配。

  "USB/TTL转232"模块通常是指一种设备,可以将USB接口的数据转换为TTL或RS-232电平的数据。以CH340芯片模块为例,CH340是一种USB转串口芯片,可以实现USB和串口之间的数据转换。工作原理通常是通过CH340芯片将USB接收的数据转换为TTL或RS-232电平的信号,并通过相应的接口进行数据传输。这样可以方便地将计算机通过USB接口连接到串口设备或TTL设备上进行数据通信。


   2. 采用标准库的查询方式,完成下列要求

1.设置波特率为9600,1位停止位,无校验位;

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

如下图

代码 

串口函数

#include "stm32f10x.h"                  // Device header
#include <stdio.h>
#include <stdarg.h>
 
void Serial_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	
	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;     //波特率为9600
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Tx; //打开发送
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_Init(USART1, &USART_InitStructure);
	
	USART_Cmd(USART1, ENABLE);
}
 
void Serial_SendByte(uint8_t Byte)//发送字节
{
	USART_SendData(USART1, Byte);
	while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
 
void Serial_SendString(char *String)//发送字符
{
	uint8_t i;
	for (i = 0; String[i] != '\0'; i ++)
	{
		Serial_SendByte(String[i]);
	}
}

main

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "Serial.h"
int main(void)
{
	Serial_Init();
	Serial_SendString("Hello Windows!\n");
	while (1)
	{
 
	}
}

上述代码实现了将Hello Windows传输。

串口调试软件效果如下

 

采用标准库的查询方式,完成以下要求:
STM32以查询方式接收上位机(win10)串口发来的数据,如果接收到“Y”则点亮链接到stm32上的一个LED灯;接收到“N”则熄灭LED灯。

串口函数

#include "stm32f10x.h"                  // Device header
#include <stdio.h>
#include <stdarg.h>
 
void Serial_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	
	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);//单片机输出口初始化
	
	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_InitTypeDef USART_InitStructure;
	USART_InitStructure.USART_BaudRate = 9600;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_Init(USART1, &USART_InitStructure);
	
	USART_Cmd(USART1, ENABLE);
}
 
void Serial_SendByte(uint8_t Byte)
{
	USART_SendData(USART1, Byte);
	while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
 
void Serial_SendArray(uint8_t *Array, uint16_t Length)
{
	uint16_t i;
	for (i = 0; i < Length; i ++)
	{
		Serial_SendByte(Array[i]);
	}
}
 
void Serial_SendString(char *String)
{
	uint8_t i;
	for (i = 0; String[i] != '\0'; i ++)
	{
		Serial_SendByte(String[i]);
	}
}

main

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "LED.h"
#include "Serial.h"
uint8_t RxData;
int main(void)
{
	Serial_Init();
	LED_Init();
	Serial_SendString("Hello Windows!\n");
	while (1)
	{
			if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)==SET)
			{
				RxData=USART_ReceiveData(USART1);
				Serial_SendByte(RxData);
				
				if(RxData==89)
				{LED_ON();
				 Serial_SendByte(89);
				}
				
				else if(RxData==78)
				{LED_OFF();
				Serial_SendByte(78);}
			}
 
	}
}

总结

在这次的实验帮助我们更好地理解和学习stm32,如有不足请多多指点

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值