前言
朋友最近在做一个东西,上面要用到两个串口,一个只管发送一个只管接收,但是波特率啥的乱七八糟配置不一样,芯片用的是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