[STM32基础学习]--串行通信--UART

一. 设备通信的两种方式

1.并行通信

传输原理:数据各个位同时传输。

优点:速度快。

缺点:浪费IO口资源。

2.串行通信

传输原理:数据按位顺序传输。

优点:占用IO口资源少。

缺点:速度慢。

二. 串行通信

1.数据传输方向的分类

单工:数据只支持在一个方向传输。

半双工:可以在两个方向传输,但是,在同一时刻,只能在一个方向上传输。

全双工:可以同时在两个方向上传输,,要求发送设备和接收设备都具有独立的接收和发送能力。

2.通信方式的分类

同步通信:带时钟同步信号传输。比如:SPI,I2C通信接口

异步通信:不带时钟同步信号 。比如:(通用异步收发器)UART,单总线。

易记:带时钟信号是同步,不带时钟信号是异步。

3.UART的接口图

RXD:数据输入引脚。数据接收。         TXD:数据输出引脚,数据发送。

GND:共地。                                        两电路板可以通过RS232,RS485,TTL接口。

三. 系统框图

波动率的计算

UART发送配置

1.编程USARTx_CR1的M位来定义字长。

2.编程USARTx_CR2的STOP位来定义停止位的位数。

3.编程USARTx_BRR寄存器确定波动率。

4.使能USARTx_CR1的UE位使能USARTx.

5.如果进行多缓冲通信,要配置USARTxCR3的DMA使能。

6.使能USARTx_CR1的TE使能发生器。

7.向发送数据寄存器TDR写入要发送的数据

8.向TRD寄存器写入最后一个数据,等待状态寄存器TC位置1。

串口发送数据代码

#include "sys.h"
#include "delay.h"
#include "usart.h"

UART_HandleTypeDef usart1_handler;

void uart1_init()
{
	usart1_handler.Instance=USART1;
	usart1_handler.Init.BaudRate=115200;
	usart1_handler.Init.WordLength=UART_WORDLENGTH_8B;
	usart1_handler.Init.HwFlowCtl=UART_HWCONTROL_NONE;
	usart1_handler.Init.Mode=UART_MODE_TX_RX;
	usart1_handler.Init.Parity=UART_PARITY_NONE;
	HAL_UART_Init(&usart1_handler);
}

void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
	GPIO_InitTypeDef GPIO_Initure;
	if(huart->Instance==USART1)
	{
		__HAL_RCC_GPIOA_CLK_ENABLE();
		__HAL_RCC_USART1_CLK_ENABLE();
		
		GPIO_Initure.Pin=GPIO_PIN_9;
		GPIO_Initure.Mode=GPIO_MODE_AF_PP;
		GPIO_Initure.Speed=GPIO_SPEED_HIGH;
		GPIO_Initure.Pull=GPIO_PULLUP;
		GPIO_Initure.Alternate=GPIO_AF7_USART1;
		HAL_GPIO_Init(GPIOA,&GPIO_Initure);
		
		GPIO_Initure.Pin=GPIO_PIN_10;
		HAL_GPIO_Init(GPIOA,&GPIO_Initure);
	}
}

int main(void)
{
     u8 buff[]="test";
    HAL_Init();                     //初始化HAL库    
    Stm32_Clock_Init(360,25,2,8);   //设置时钟,180Mhz
	delay_init(180);
	uart1_init();
	while(1)
	{
		HAL_UART_Transmit(&usart1_handler,buff,sizeof(buff),1000);
		delay_ms(300);
	}

}

 

串口接收数据与发送数据大同小异

在发送数据的基础上加入中断,接收到数据的时候进入中断服务函数

#include "sys.h"
#include "delay.h"
#include "usart.h"

u8 rdata[1];

UART_HandleTypeDef usart1_handler;

void uart1_init()
{
	usart1_handler.Instance=USART1;
	usart1_handler.Init.BaudRate=115200;
	usart1_handler.Init.WordLength=UART_WORDLENGTH_8B;
	usart1_handler.Init.HwFlowCtl=UART_HWCONTROL_NONE;
	usart1_handler.Init.Mode=UART_MODE_TX_RX;
	usart1_handler.Init.Parity=UART_PARITY_NONE;
	HAL_UART_Init(&usart1_handler);
}

void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
	GPIO_InitTypeDef GPIO_Initure;
	if(huart->Instance==USART1)
	{
		__HAL_RCC_GPIOA_CLK_ENABLE();
		__HAL_RCC_USART1_CLK_ENABLE();
		
		GPIO_Initure.Pin=GPIO_PIN_9;
		GPIO_Initure.Mode=GPIO_MODE_AF_PP;
		GPIO_Initure.Speed=GPIO_SPEED_HIGH;
		GPIO_Initure.Pull=GPIO_PULLUP;
		GPIO_Initure.Alternate=GPIO_AF7_USART1;
		HAL_GPIO_Init(GPIOA,&GPIO_Initure);
		
		GPIO_Initure.Pin=GPIO_PIN_10;
		HAL_GPIO_Init(GPIOA,&GPIO_Initure);
		
		HAL_NVIC_SetPriority(USART1_IRQn,3,3);
		HAL_NVIC_EnableIRQ(USART1_IRQn);
		
	}
}

void USART1_IRQHandler(void)
{
	HAL_UART_IRQHandler(&usart1_handler);
	HAL_UART_Receive_IT(&usart1_handler,rdata,sizeof(rdata));
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	u8 rec;
	if(huart->Instance==USART1)
	{
		rec=*((huart->pRxBuffPtr)-1);
//		rec=rdata[0];
		HAL_UART_Transmit(&usart1_handler,&rec,1,1000);
	}
}
int main(void)
{

    HAL_Init();                     //初始化HAL库    
    Stm32_Clock_Init(360,25,2,8);   //设置时钟,180Mhz
	delay_init(180);
	uart1_init();
	
	HAL_UART_Receive_IT(&usart1_handler,rdata,sizeof(rdata));
	while(1)
	{
		
	}

}

  • 9
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值