下面是相关的接口代码(已经增加中文注释帮助理解),代码支持使用不同串口控制器来实现484通信:
//时钟初始化
void uart_RCC(USART_TypeDef* USARTx,uint32_t RCC_APB_Tx,uint32_t RCC_APB_Rx)
{
//相对应串口时钟
if(USARTx == USART1)
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
else if(USARTx == USART2)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
else if(USARTx == USART3)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
else if(USARTx == UART4)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
//相对串口的TxRx GPIO时钟
RCC_APB2PeriphClockCmd(RCC_APB_Tx|RCC_APB_Rx,ENABLE);
}
//GPIO初始化
void uart_GPIO(GPIO_TypeDef* TX_GpioPort,uint16_t TX_GpioPin,GPIO_TypeDef* RX_GpioPort,uint16_t RX_GpioPin,GPIO_TypeDef* Dir_GpioPort,uint16_t Dir_GpioPin)
{
GPIO_InitTypeDef GPIO_InitStructure;
//USARTx_TX
GPIO_InitStructure.GPIO_Pin = TX_GpioPin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽
GPIO_Init(TX_GpioPort, &GPIO_InitStructure);
//USARTx_RX
GPIO_InitStructure.GPIO_Pin = RX_GpioPin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//浮空输入
GPIO_Init(RX_GpioPort, &GPIO_InitStructure);
//direction
GPIO_InitStructure.GPIO_Pin = Dir_GpioPin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
GPIO_Init(Dir_GpioPort, &GPIO_InitStructure);
}
//串口中断配置
void uart_NVIC(uint8_t NVIC_IRQChannel,uint8_t NVIC_IRQChannelPreemptionPriority,uint8_t NVIC_IRQChannelSubPriority)
{
NVIC_InitTypeDef NVIC_InitStructure;
//设置NVIC优先级分组为2
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
//串口接收中断打开
NVIC_InitStructure.NVIC_IRQChannel = NVIC_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = NVIC_IRQChannelPreemptionPriority;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = NVIC_IRQChannelSubPriority;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//
///华丽的分割线
///
//串口初始化
uint8 u8_drv_uart_Init(USART_TypeDef* USARTx)
{
if(USARTx == USART1)
{
uart_RCC(USART1,D_UART1_TX_RCC,D_UART1_RX_RCC);
//重映射
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO,ENABLE);//开启端口B和复用时钟功能
//GPIO_PinRemapConfig(GPIO_Remap_USART1,ENABLE);//使能端口重映射
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//开启端口B和复用时钟功能
uart_GPIO(D_UART1_TX_PORT,D_UART1_TX_PIN,D_UART1_RX_PORT,D_UART1_RX_PIN);
v_drv_uart_NVIC(USART1_IRQn,D_USART1_IRQn_PreemptionPriority,D_USART1_IRQn_SubPriority);
}
else if(USARTx == USART2)
{
uart_RCC(USART2,D_UART2_TX_RCC,D_UART2_RX_RCC);
uart_GPIO(D_UART2_TX_PORT,D_UART2_TX_PIN,D_UART2_RX_PORT,D_UART2_RX_PIN);
v_drv_uart_NVIC(USART2_IRQn,D_USART2_IRQn_PreemptionPriority,D_USART2_IRQn_SubPriority);
}
else if(USARTx == USART3)
{
uart_RCC(USART3,D_UART3_TX_RCC,D_UART3_RX_RCC);
uart_GPIO(D_UART3_TX_PORT,D_UART3_TX_PIN,D_UART3_RX_PORT,D_UART3_RX_PIN);
v_drv_uart_NVIC(USART3_IRQn,D_USART3_IRQn_PreemptionPriority,D_USART3_IRQn_SubPriority);
}
else if(USARTx == UART4)
{
uart_RCC(UART4,D_UART4_TX_RCC,D_UART4_RX_RCC);
uart_GPIO(D_UART4_TX_PORT,D_UART4_TX_PIN,D_UART4_RX_PORT,D_UART4_RX_PIN);
uart_NVIC(UART4_IRQn,D_USART4_IRQn_PreemptionPriority,D_USART4_IRQn_SubPriority);
}
else
{
}
}
//串口发送
void uart_Send(USART_TypeDef* USARTx,char *pc_data,uint8_t u8_len)
{
uint8_t i = 0;
GPIO_SetBits(Dir_GpioPort, Dir_GpioPin);//收发器发送模式
for(i=0;i<u8_len;i++)
{
USART_SendData(USARTx, *pc_data);
while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET){}
pc_data++;
}
GPIO_ResetBits(Dir_GpioPort, Dir_GpioPin);//收发器接收模式
} |
版权声明:本文为博主原创文章,遵循 CC 4.0 BY 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/geek_liyang/article/details/130167836 ———————————————— 版权声明:本文为博主原创文章,遵循 CC 4.0 BY 版权协议,转载请附上原文出处链接和本声明。 原文链接:https://blog.csdn.net/geek_liyang/article/details/130167836