如何使用MAX20340通过电源线发送和接收数据

max20340简化了通过电源线发送和接收数据的过程。任何具有 i 的系统2c主站可以在供电的同时发送和接收数据。本文讨论如何编写软件以在主站和从站之间发送和接收数据。它还提供了示例代码片段以加速开发。
介绍
本文说明如何使用max20340双向直流电力线通信管理ic发送和接收数据。该文件描述了用于将数据传输到单个从站或多个从站的主从固件。还包括示例代码片段。
通信概述
在最简单的配置中,主机仅通过两个物理触点连接到单个从站:plc和接地。主机和从机各有自己的微控制器,通过i与各自的max20340通信。2c 发送和接收数据。
图1.主站和单个从站之间通信接口的简化框图。
传输数据
max20340简化了电力线数据传输。首先,要传输的数据是通过i写入的2c 到三个 8 位传输数据字节寄存器。然后,plc传输位通过i2c 指定要传输的字节数。一旦写入 plc 传输位,传输就开始了。
最多写入三个字节的数据以传输到传输数据字节寄存器(tx_data0、tx_data1和寄存器tx_data2 0x0d、0x0e、0x0f)。
将 plc 传输位 (tx[1:0]) 写入plc_com_ctrl寄存器以触发最多三个字节的传输。写入 tx[1:0] 字段的值指定要传输的字节数。(plc_com_ctrl在登记册0x09中)。
图2.流程图显示了使用max20340通过电源线传输数据的步骤。
接收数据
接收从另一个max20340发送的数据也非常简单。断言“新数据”中断,指定接收数据时的字节数。然后,可以通过i2c在三个8位接收数据字节寄存器中读取接收的数据。
等待新数据中断(new_data1i并在寄存器2x0b中new_data0i)。
从接收数据字节寄存器(寄存器0x1、2x0 和 10x0)中读取最多三个字节的接收数据(rx_data11、rx_data0 和寄存器中的rx_data12)。
图3.流程图显示了使用max20340通过电源线接收传输数据的步骤。
主/从通信流程
在典型应用中,主从应用处理器遵循主/从通信顺序并检查传输错误。
max20340配置为主站,启动所有plc通信。配置为从机的max20340只能在主站向其发送数据后向主站发送数据。主站传输数据后,启动定时器等待从站响应。如果计时器过期,则在主站上断言plc_tmri中断,表示从机没有响应。在发送任何新数据之前,主站应等待从站响应或超时。
主机和从站还应检查plc_tx_erri和plc_rx_erri中断指定的传输错误。
例子
这些实现示例假设主从应用处理器的输入引脚配置为中断,连接到max20340的中断引脚。调用“wait_for_interrupt()”会检查此引脚的状态并等待其为低电平。此功能的实现是特定于平台的。调用“i2c_write_register(...)”和“i2c_read_register(...)”可启动max8中20340位寄存器的读写,其实现也特定于平台。
假设在每个示例中都已取消屏蔽所有相关中断。其他一些实现通过 i 轮询中断寄存器2c 而不是取消屏蔽中断并使用中断引脚。
这些示例假设所有传输均以默认通信设置发送:24μs通信频率、288ma plc灌电流、奇偶校验和10ms rx等待定时器。这些设置取决于应用程序。
将数据传输到单个从站
在本例中,plc 主站连接到单个 plc 从站(图 1)。主站传输三个字节,从站以一个字节响应。主机应仅在处于“从站发现充电”状态(fsm_stat = 0b110)时尝试传输数据。
主传输
主固件遵循以下基本结构,在设置中断掩码后传输三个字节:
将一组三个字节加载到 tx_data0-2 寄存器中。
通过在plc_com_ctrl寄存器中写入 tx[1:0] 位来发送三个字节。
(可选)等待 newdata1/2、tmr_err、tx_err 或 rx_err 中断。
或者,如果收到 newdata1/2 中断,则从rx_data寄存器读取从属响应。
从属接收
设置中断掩码后,从属固件遵循以下基本结构,等待来自主站的数据,并可选择在主站的等待计时器到期之前做出响应:
等待 rx_err 或 new_data1/2 中断。
如果收到 newdata1/2 中断,则从 rx_data0-2 个寄存器读取数据字节。
(可选)将响应加载到 txdata0 寄存器中。
(可选)通过在plc_com_ctrl寄存器中写入 tx[1:0] 位来发送一个字节。
主传输固件示例代码片段
主固件的代码片段如下所示。在此示例中,数据字节、0x55、0x5a和0xa5由主站传输。此代码旨在用作辅助开发的参考,不能按原样使用。
/*load a set of three bytes into tx_data0-2 registers and write all three bytes at once (i2c auto-incrementation of register address)*/uint8_t data_bytes[3];data_bytes[0] = 0x55;data_bytes[1] = 0x5a;data_bytes[2] = 0xa5;i2c_write_register(ao23_mast_addr, tx_data0_reg, data_bytes, 3); /*send three bytes by writing 0b11 to the tx[1:0] bits in the plc_com_ctrl register (all other bits [7:2] are application dependent)*/i2c_write_register(ao23_mast_addr, plc_com_ctrl_reg, 0x97); /*optional: wait for newdata, tmr_err, tx_err, or rx_err interrupt (timeout should be added in typical system)*/wait_for_interrupt();/*optional: check if interrupt was caused by slave response (newdata1/2)*/uint8_t plc_int_buf[1];i2c_read_register(ao23_mast_addr, plc_irq_reg, plc_int_buf, 1);bool new_data = (plc_int_buf[0] & 0x06) != 0;/*optional: if newdata interrupt, read single-byte slave response (response should be processed in typical system)*/if(new_data){ uint8_t slave_resp_buf[1]; i2c_read_register(ao23_mast_addr, rx_data0_reg, slave_resp_buf, 1);}  
从站接收固件示例代码片段
a snippet from the slave firmware is shown below. the slave responds to the received data by transmitting a single byte: 0x55 in this example. this code is meant to be used as a reference to assist development and cannot be used as is./*wait for rx_err or new_data1/2 interrupt*/wait_for_interrupt();/*check if interrupt caused by received data (newdata1/2)*/uint8_t plc_int_buf[1];i2c_read_register(ao23_slav_addr, plc_irq_reg, plc_int_buf, 1);bool new_data = (plc_int_buf[0] & 0x06) != 0;/*if data received, read data and optionally respond*/if(new_data){ /*read all three data bytes from rx_data0-2 registers (i2c auto-incrementation of register address) (received data should be processed in typical system)*/ uint8_t data_reg_buf[3]; i2c_read_register(ao23_slav_addr, rx_data0_reg, data_reg_buf, 3); /*optional: load response into txdata0 register*/ i2c_write_register(ao23_slav_addr, tx_data0_reg, 0x55); /*optional: send one byte by writing the tx[1:0] bits in the plc_com_ctrl register (all other bits [7:2] are application dependent)*/ i2c_write_register(ao23_slav_addr, plc_com_ctrl_reg, 0x95);} 将数据传输到两个从站
在本例中,主机向两个连接的从站之一传输三个字节(图 4)。
图4.主站和两个从站之间通信接口的简化框图。
每个从机都配置了不同的rsel电阻值,以确保它们具有唯一的plc从机地址。目标接收方的 plc 从地址被添加到第一个传输字节的 msb 中。
从站以一个字节响应,并在响应字节的msb中包含自己的plc从地址。
有关本实现示例中使用的plc从站寻址的更多详细信息,请参阅本文档的“多个从站注意事项”部分。
主传输
主固件遵循以下基本结构,在设置主等待计时器和中断掩码后传输三个字节:
将一组三个字节加载到 tx_data0-2 寄存器中(23 位中只有 24 位包含要发送的数据)。一位,即tx_data0的msb,包含接收方的plc从地址)。
通过在plc_com_ctrl寄存器中写入 tx[1:0] 位来发送三个字节。
(可选)等待 newdata1/2、tmr_err、tx_err 或 rx_err 中断。
或者,如果收到 newdata1/2 中断,则从rx_data寄存器读取从属响应。
从属接收
设置中断掩码后,从属固件遵循以下基本结构,等待来自主站的数据,并可选择在主站的等待计时器到期之前做出响应:
等待 rx_err 或 new_data1/2 中断。
如果收到 newdata1/2 中断,则从 rx_data0-2 寄存器读取数据字节。
读取dev_status1寄存器的ps_add位。
检查rx_data0的 msb。如果 msb 不等于 ps_add,请忽略接收到的数据。
(可选)将响应加载到 txdata0 寄存器中(7 位中只有 8 位包含要发送的数据)。一位,即 tx_data0 的 msb,包含 ps_add 位)。
(可选)通过在plc_com_ctrl寄存器中写入 tx[1:0] 位来发送一个字节。
主传输固件示例代码片段
主固件的代码片段如下所示。在此示例中,数据字节0x55、0x5a和0xa5由主站传输。数据以plc从站地址“1”传输到从站。因此,发送的实际数据是0xd5、0x5a和0xa5(plc 从地址添加到字节 1 的 msb 中)。此代码旨在用作辅助开发的参考,不能按原样使用。
/*load a set of three bytes into tx_data0-2 registers and write all three bytes at once (i2c auto-incrementation of register address)*/uint8_t data_bytes[3];data_bytes[0] = 0xd5; /*this byte contains 7 bits of actual data, msb is recipient ps_add*/data_bytes[1] = 0x5a;data_bytes[2] = 0xa5;i2c_write_register(ao23_mast_addr, tx_data0_reg, data_bytes, 3); /*send three bytes by writing 0b11 to the tx[1:0] bits in the plc_com_ctrl register (all other bits [7:2] are application dependent)*/i2c_write_register(ao23_mast_addr, plc_com_ctrl_reg, 0x97); /*optional: wait for slave response interrupt (timeout should be added in typical system)*/wait_for_interrupt();/*optional: check if interrupt caused by slave response (newdata1/2)*/uint8_t plc_int_buf[1];i2c_read_register(ao23_mast_addr, plc_irq_reg, plc_int_buf, 1);bool new_data = (plc_int_buf[0] & 0x06) != 0;/*optional: if newdata interrupt, read slave response (response should be processed in typical system)*/if(new_data){ uint8_t slave_resp_buf[1]; i2c_read_register(ao23_mast_addr, rx_data0_reg, slave_resp_buf, 1);} 从站接收固件示例代码片段
从固件的代码片段如下所示。从站通过发送单个字节来响应接收到的数据:在本例中0x55。该从站的plc从站地址为“1”。因此,发送的实际数据是0xd5(plc从地址被添加到字节1的msb)。此代码旨在用作辅助开发的参考,不能按原样使用。
/*wait for rx_err or new_data1/2 interrupt*/wait_for_interrupt();/*check if interrupt caused by received data (newdata1/2)*/uint8_t plc_int_buf[1];i2c_read_register(ao23_slav_addr, plc_irq_reg, plc_int_buf, 1);bool new_data = (plc_int_buf[0] & 0x06) != 0;/*if data received, read data, check if intended recipient, and optionally respond*/if(new_data){ /*read all three data bytes from rx_data0-2 registers (i2c auto-incrementation of register address) (received data should be processed in typical system)*/ uint8_t data_reg_buf[3]; i2c_read_register(ao23_slav_addr, rx_data0_reg, data_reg_buf, 3); /*read ps_add bit of the dev_status1 register*/ uint8_t slave_addr_buf[1]; i2c_read_register(ao23_slav_addr, dev_status1_reg, slave_addr_buf, 1); uint8_t ps_add = slave_addr_buf[0] & 0x01; /*check if msb of rx_data0 (intended recipient address) equals ps_add of this slave*/ uint8_t = recipient_plc_addr = (data_reg_buf[0] & 0x80) >> 7; bool is_recipient = (ps_add == recipient_plc_addr); if(is_recipient){ /*optional: load response into txdata0 register. this byte contains 7 bits of actual data, msb is the slave's own ps_add*/ i2c_write_register(ao23_slav_addr, tx_data0_reg, 0xd5); /*optional: send one byte by writing the tx[1:0] bits in the plc_com_ctrl register (all other bits [7:2] are application dependent)*/ i2c_write_register(ao23_slav_addr, plc_com_ctrl_reg, 0x95); }} 多个从站注意事项
max20340主机可以连接任意数量的从机。plc上的最大从站数量仅受主站可以提供的最大充电电流(1.2a)和plc上的总电容的限制。
应使用用户定义的寻址方案,以防止当plc主站与两个或多个plc从机接口时,多个从站尝试同时响应。plc从地址在传输过程中不会自动使用,尽管rsel电阻可用于为从机分配具有唯一plc从地址的从机。
当主站传输数据时,所有连接的从站都会接收数据,无论其配置的plc从站地址如何。当 plc 主站打算仅将数据包发送到其中一个 plc 从站时,应在数据字节中嵌入唯一标识预期接收方的地址。用户可以灵活地将地址分配给任何数据字节位。
rsel 设置的 plc 从地址仅为 1 位。因此,它仅在寻址最多两个从站时才有用。如果连接了两个以上的plc从站,则必须使用更多位传输的数据字节来唯一地寻址所有从站。
所有plc从站接收相同的数据。因此,每个从站的应用处理器都应该从传输的数据中提取地址位,并将其与自己的唯一地址进行比较,以确定它是否是预期的接收者。然后,预期的从站相应地处理数据,而其他从站则简单地丢弃数据。
双从机寻址示例
假设有两个连接的从站,第一个具有plc从地址“0”,第二个具有plc从地址“1”(基于rsel值并存储在dev_status1寄存器的ps_add位中)。用户选择将目标接收方的plc从地址编码为第一个字节的最高有效位。
主机向第一个从站发送 23 位数据,如下所示:
字节 1: '0xxxx' 字节 2: 'xxxx'
字节 3: 'xxxxxx'
其中x是23位数据,字节1的msb包含第一个从站的plc从地址('0')。
主机向第二个从设备发送23位数据,如下所示:
字节 1: '1xxxx' 字节 2: 'xxxx'
字节 3: 'xxxxxxx'
其中x是23位数据,字节1的msb包含第二个从站的plc从地址(“1”)。
当主站传输此数据时,两个从站都将接收数据。
当从站接收数据时,如果字节1的msb与配置的plc从地址匹配,则此数据适用于它,并且应该处理数据。 如果msb与配置的plc从地址不匹配,则此数据不适用于它,它应该简单地丢弃/忽略数据。
如果预期的接收者从站响应主站,则所有其他从站也会收到响应。在具有多个从站的从站响应期间,预期的接收方从站自己的plc从站地址应嵌入到响应的数据字节中。这可确保其他从站丢弃/忽略预期的从站对主站的响应。
请注意,这不是必需的,plc协议中没有内置从站寻址。如果用户应用程序不要求两个从站接收不同的数据,并且从属机从不尝试同时响应主站,则唯一地址不需要嵌入到数据字节中。如果未使用从站寻址方案,则两个从站都接收主站发送的数据。如果任何从站响应主站,则所有其他从站将此响应视为接收到的数据。如果两个从站尝试同时响应,则会发生通信错误。适当的从站寻址方案可以防止此类错误。


远程预付费电能管理系统在星湖101广场的应用
Pembroke公司推出用于目标识别的光电短波红外传感器
立洋光电谱写乡村振兴帮扶新篇章 烽火通信获全国普法先进单位称号
基于STC12C5A60S2的变频恒压供水系统
半导体制造:跟随还是超越摩尔定律
如何使用MAX20340通过电源线发送和接收数据
移动固态硬盘有缺点,但瑕不掩瑜!仍是最受畅销移动存储产品
游戏手机如何选择,教你几招入手一款性能好机
内部与外部看门狗定时器的比较
无线射频技术设计的无人值守病房监护系统
CCLINK IE转Profinet网关配置通讯配置案例
统信容器云管理平台—有雀 支持基础资源和应用统一管理
DS31256 HDLC控制器的配置步骤—桥接模式
一次消谐装置应用功能的简单介绍
大电流电路板布局的基础知识
数字化转型时代,企业必须确保其风险才能面向未来
成都首家无人超市亮相,引数千吃瓜群众围观,开门俩小时门禁系统被刷崩!
Marvell收购Cavium的背后,只是为了“抱团取火”
无纺布干态落絮测试仪的主要组成部分介绍
中国未来5-10年光纤需求量巨大,注重竞争力优先