首页 > 编程知识 正文

485通讯不稳定怎么解决,485中的A是接Rx还是TX

时间:2023-05-05 23:12:38 阅读:164394 作者:1822

1 .有线通信

2 .只是定义了逻辑0和逻辑1的大小,最终从RS485转移到TTL电平与STM32串行通信

RS485 的特点包括

1 )接口水平低,不易损坏芯片。 RS485的电气特性:逻辑“1”将两线间的电压差设为(2)6) v

表示; 逻辑“0”将2线之间的电压差表示为-(2~6) v。 接口信号电平低于RS232,

由于接口电路的芯片不易损坏,且与TTL等级兼容,因此容易与TTL电路连接。

2 )传输速率高。 在10米时,RS485数据的最高传输速率可达到35Mbps,在1200m米时传输

速度达到100Kbps。 3 )抗干扰能力强。 RS485接口采用平衡驱动器和差分接收机的组合,抗共模干扰

增强,即抗噪声干扰能力强。

4 )传输距离远,支持节点多。 RS485总线最长可传输1200m米以上(速率100Kbps ) ) )。

一般最多支持32个节点,但使用特殊的485芯片可以达到128个或256个节点。

最多可支持400个节点。

3.采用 SP3485 作为收发器

SP3485使485通信的高电压成为TTL电平

软件实现:

串行端口是VoidRS485_init(u32bound ) gpio _ inittypedefgpio _ init structure; usart _ inittypedefusart _ init structure; nvic _ inittypedefnvic _ init structure; RCC _ AP B2 periphclockcmd (RCC _ AP B2 per iph _ gpioa|RCC _ AP B2 per iph _ gpiog,ENABLE ); 启用GPIOA的g时钟RCC _ AP B1 periphclockcmd (RCC _ AP B1 per iph _ usar T2,ENABLE ); //启用串行端口2时钟//GPIO配置gpio _ init structure.gpio _ pin=gpio _ pin _ 9; //PG9端口配置gpio _ init structure.gpio _ mode=gpio _ mode _ out _ PP; //推挽输出gpio _ init structure.gpio _ speed=gpio _ speed _ 50m Hz; gpio_init(gpiog,GPIO_InitStructure ); gpio _ init structure.gpio _ pin=gpio _ pin _ 2; //pa2gpio _ init structure.gpio _ mode=gpio _ mode _ af _ PP; //复用推挽gpio_init(gpioa,GPIO_InitStructure ); gpio _ init structure.gpio _ pin=gpio _ pin _ 3; //pa 3gp io _ init structure.gpio _ mode=gpio _ mode _ in _ floating; //浮动输入gpio_init(gpioa,GPIO_InitStructure ); RCC _ AP B1 periphresetcmd (RCC _ AP B1 per iph _ usar T2,ENABLE ); //重置串行2r cc _ AP B1 periphresetcmd (RCC _ AP B1 per iph _ usar T2,禁用); //停止复位#ifdef EN_USART2_RX //接收///串行配置参数usart _ init structure.usart _ baudrate=bound; //波特率设置; usart _ init structure.usart _ word length=usart _ word length _ 8b; //8位数据长度usart _ init structure.usart _ stop bits=usart _ stop bits _ 1;//一个停止位usart _ init structure.usart _ parity=usart _ parity _ no; //奇偶校验位usart _ init structure.usart _ hardware flow control=usart _ hardware flow control _ none; //无硬件数据流控件usart _ init structure.usart _ mode=usart _ mode _ rx|usart _ mode _ tx; //usart_init的收发(usarT2,USART_InitStructure ); //初始化串行端口//nvic _ init structure.nvic _ IRQ channel=usar T2 _ irqn; //启用串行端口2中断nvic _ init structure.nvic _ irqchannelpreemptionpriority=3//优先级2级nvic _ init structure.nvic _ IRQ //优先级二级nvic _ init structure.nvic _ irqchannelcmd=enable; //启用外部中断通道nvic _ init (nvic _ init structure ); NVIC寄存器usart_itconfig(usarT2,USART_IT_RXNE,ENABLE )要初始化; //打开中断usart _ cmd (usar T2,ENABLE ); //启用串行端口#endifRS485_TX_EN=0; //默认为接收模式} 2.发送字节

//RS485发送len字节.//buf:发送区域开头地址//len:发送字节数(为了与本代码的接收一致,这里建议不要超过64字节(voidRS485_send_data ) ) //设定为发送模式for (t=0; tlen; t//循环传输数据{ while (usart _ getflagstatus (usar T2,USART_FLAG_TC )==RESET ); usart_senddata(usarT2,buf[t] ); } while (usart _ getflagstatus (usar T2,USART_FLAG_TC )==RESET ); RS485_RX_CNT=0; RS485_TX_EN=0; //设为接收模式} 3.读取字节

//接收到的数据长度u8 RS485_RX_CNT=0; voidusarT2_IRQHandler(void ) {u8 res; usart _ get it status (usar T2,USART_IT_RXNE )!=RESET接收数据{RES=usart_receivedata(usarT2 ); //接收到的数据if(RS485_rx_CNT64 ) { RS485 _ rx _ buf [ RS485 _ rx _ CNT ]=RES; //记录接收到的值RS485_RX_CNT; //接收数据添加1 } } }//RS485接收数据//buf:接收缓存起始地址//len:中读取的数据长度voidRS485_receive_data(U8*buf,u8 *len ) *len=0; //默认值为0delay_ms(10; //10ms,若10ms以上连续接收不到一个数据,则接收结束if(rxlen==RS485_rx_cntrxlen ) /接收数据,接收完成(for ) I=0; irxlen; I ) {buf[i]=RS485_RX_BUF[i]; }*len=RS485_RX_CNT; //记录本次数据长度RS485_RX_CNT=0清零} } over

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。