STM32F429 422串口通信 1.422串口的硬件原理图如下:
2.422串口和232串口不同的是引脚的电平控制,还有DE是发送使能,置高即可:RE是接收使能,置低即可。422接出来的是4根线,有RS485_A、RS485_B、RS485_Z、RS485_Y,对应422串口线的DB1、DB2、DB3、DB4,,DB5连接地线,其他的配置用232串口的代码即可。
3.GPIO的配置: - void RS422_Config()
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- /* PC6/USART6_TX , PC7/USART6_RX, PG12/USART6_RTS, PG13/USART6_CTS */
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC |RCC_AHB1Periph_GPIOG, ENABLE);
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;
- GPIO_Init(GPIOG, &GPIO_InitStructure);
- GPIO_ResetBits(GPIOG,GPIO_Pin_12); //USART6_RTS
- GPIO_SetBits(GPIOG,GPIO_Pin_13); //USART6_CTS
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- USART422_NVIC_Config();
- USART_InitStructure.USART_BaudRate = 115200;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No ;
- //USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(USART6, &USART_InitStructure);
- USART_ITConfig(USART6, USART_IT_RXNE, ENABLE);
- USART_Cmd(USART6, ENABLE);
- USART_ClearFlag(USART6, USART_FLAG_TC);
- }
复制代码
4.422串口中断的配置: - void USART422_NVIC_Config(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置中断分组2
- NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn; // 选择 USART6 通道
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢占优先级设置为3
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //响应优先级设置为1
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //开启中断
- NVIC_Init(&NVIC_InitStructure); //初始化配置
- }
- void USART6_IRQHandler (void)
- {
- char Date422 = 0;
复制代码
5.主函数的发送: - int main(void)
- {
- Delay_Init(); //延时函数初始化
- RS422_Config(); //422串口初始化
- while(1)
- {
- Delay_ms(1000);
- USART_SendData(USART6,3); //将接收到的数据发回
- }
- }
复制代码———————————————— 版权声明:weixin_44553164
|