中级会员
 
- 积分
- 272
- 金钱
- 272
- 注册时间
- 2015-11-6
- 在线时间
- 84 小时
|
50金钱
请教一个关于STM32串口485多机通信的问题:
共有18个从机,地址分别为0x01、0x02、0x03 ... ... 0x11(17号)、0x12(18号)。
现在遇到的问题是:
如果0x01号从机数据包中有含有数据字节0x11,则0x11(17号)从机也会响应发送数据,两个从机发送数据碰撞一起,造成0x01号从机输出的数据乱码。主机无法正常识别。
举例:主机发送0x01读取命令,0x01号从机响应后回复0xAA 0x11 0x22 0x33 0x0D 0x0A(数据包中含有0x11),这时0x11号分机也会回复数据,造成485总线上的数据错乱。
附上串口的配置代码:
- void uart4_init(u32 bound) //USART4 初始化
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //使能UART4,GPIOC时钟
-
- //USART4_TX PC10初始化
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PC10
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
- GPIO_Init(GPIOC, &GPIO_InitStructure); //初始化GPIOC.10
- //USART4_RX PC11初始化
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //PC11
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //上拉输入
- GPIO_Init(GPIOC, &GPIO_InitStructure); //初始化PC11
-
- //Usart4 NVIC 配置
- NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;//抢占优先级2
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4; //子优先级2
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
- NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
-
- //USART4 初始化设置
- USART_InitStructure.USART_BaudRate = bound; //串口波特率
- USART_InitStructure.USART_WordLength = USART_WordLength_9b;//字长为8位数据格式
- USART_InitStructure.USART_StopBits = USART_StopBits_1; //一个停止位
- USART_InitStructure.USART_Parity = USART_Parity_No; //无奇偶校验位
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
- USART_Init(UART4, &USART_InitStructure); //初始化串口4
-
- //多机通讯模式
- USART_SetAddress(UART4,Read_24C64(0x25)); //n号
- USART_WakeUpConfig(UART4, USART_WakeUp_AddressMark); //地址标记检测
- USART_ReceiverWakeUpCmd(UART4,ENABLE); //UART4进入静默状态
-
- USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); //开启串口中断
- USART_Cmd(UART4, ENABLE); //使能串口4
- }
复制代码 请教有没有遇到类似问题的朋友,能给解决下。谢谢!!!
|
|