TIM+DMA的串口发送实现

本文介绍了如何使用STM32F030F4P6芯片通过TIM+DMA实现串口发送功能。针对只有一个串口但需要分别进行发送和接收的情况,提出利用定时器的PWM输出,并配合DMA将数据加载到TIM寄存器中,以实现不同的通讯接口。
摘要由CSDN通过智能技术生成

TIM+DMA的串口发送实现

前言

朋友最近在做一个东西,上面要用到两个串口,一个只管发送一个只管接收,但是波特率啥的乱七八糟配置不一样,芯片用的是STM32F030F4P6,这颗芯片只有一个串口,于是问我有没有什么办法,于是便想下面办法。

思路

使用定时器的比较输出,输出PWM,申请一个buffer用于存放数据,使用DMA将该数据加载到TIM相应的寄存器上。懒得写注释,自己看看很容易理解的,可以衍生出不同的通讯接口。

代码实现流程


struct sbus_dat
{
   
	uint8_t  syncByte;
	unsigned int ch0 : 11;
	unsigned int ch1 : 11;
	unsigned int ch2 : 11;
	unsigned int ch3 : 11;
	unsigned int ch4 : 11;
	unsigned int ch5 : 11;
	unsigned int ch6 : 11;
	unsigned int ch7 : 11;
	unsigned int ch8 : 11;
	unsigned int ch9 : 11;
	unsigned int ch10 : 11;
	unsigned int ch11 : 11;
	unsigned int ch12 : 11;
	unsigned int ch13 : 11;
	unsigned int ch14 : 11;
	unsigned int ch15 : 11;
	uint8_t  flags;
	uint8_t  endByte;
} __attribute__((__packed__));

typedef union
{
   
	uint8_t byte[25];
	struct sbus_dat msg;
} sbus_msg;

extern sbus_msg sbus;

void Port_Init(void);
void Port_Output(void);
uint8_t sbus_pos = 0;
sbus_msg sbus;

uint16_t SBUS_DMA_DATA[12]={
   58,0,0,0,0,0,0,0,0,58,0,0};

void Port_Init(void)
{
   
	GPIO_InitTypeDef GPIO_InitStructure;
	TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
	TIM_OCInitTypeDef TIM_OCInitStructure;
	DMA_InitTypeDef DMA_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;

	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG , ENABLE);

	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_OType 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值