完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
嗨,我在PIC18F4680上有一个主I2C,它从温度传感器读出指令,然后用PIC18F46K22在从模式下向另一个板发送命令。在从奴隶PIC获取信息时,我有一个问题,我正在寻找(我猜)两个可能的答案。所有与温度传感器的I2C通信工作良好,并且从从属PIC写入也很好。因此:1。由于奴隶不能发起通信,所以主人必须轮询它并询问它是否有什么要说的。这就是我现在所拥有的,但是我不能让主人从奴隶那里读书。2。有一个多主总线,在需要的时候让第二个PIC写到总线上(还没有尝试过)。我附加了3个带有主代码和从代码的文件,以及一个由两个板使用define来选择内容的第三个文件。前2个文件大多是MCC生成的代码。当接收到READ_REQUEST时,我写信给从机端的SSP1BUF,我无法在主机端读取该信息。如果我想让他们做多主的话,我真的不知道如何修改主代码以便在“奴隶”条件下接收。先谢谢你,
以上来自于百度翻译 以下为原文 Hi, I have a Master I2C on a PIC18F4680 that reads from a temperature sensor and sends commands to another board with a PIC18F46K22 in Slave mode. I have a problem when getting information back from the slave pic and I'm looking for (I guess) two possible answers. All I2C communication with the temperature sensor works fine, and writing to the Slave PIC also works fine. So: 1. Since a Slave cannot initiate communication, the Master would have to poll it and ask if it has anything to say. This is how I have it now, but I cannot get the Master to read from the Slave. 2. Have a multi-master bus and have the second PIC write to the bus when needed (haven't tried this yet). void I2C1_StatusCallback(I2C1_SLAVE_DRIVER_STATUS i2c_bus_state) { switch (i2c_bus_state) { case I2C1_SLAVE_WRITE_REQUEST: messageForMe = (I2C1_targetAddress == I2C_Address); // Address received, check if it's for me and reset message pointers break; case I2C1_SLAVE_WRITE_COMPLETED: if (messageForMe) { // Receive Byte I2C_Input.Buffer[I2C_Input.inPointer] = I2C1_slaveWriteData; I2C_Input.inPointer = (I2C_Input.inPointer + 1) % BUFFER_SIZE; if (I2C1_slaveWriteData == EOT) messageForMe = NO; } break; case I2C1_SLAVE_READ_REQUEST: // Send Message if (I2C_Output.outPointer != I2C_Output.inPointer) { //MOTOR_REF_LED_ON(); SSP1BUF = I2C_Output.Buffer[I2C_Output.outPointer]; I2C_Output.outPointer = (I2C_Output.outPointer + 1) % BUFFER_SIZE; } else { //MOTOR_REF_LED_OFF(); SSP1BUF = EOT; } break; case I2C1_SLAVE_READ_COMPLETED: // Send Message if (I2C_Output.outPointer != I2C_Output.inPointer) { MOTOR_REF_LED_ON(); SSP1BUF = I2C_Output.Buffer[I2C_Output.outPointer]; I2C_Output.outPointer = (I2C_Output.outPointer + 1) % BUFFER_SIZE; } else { MOTOR_REF_LED_OFF(); SSP1BUF = EOT; } break; default: break; } // end switch(i2c_bus_state) } I'm attaching 3 files with the master and slave code and a 3rd file that is used by both boards with a #define to select stuff. The first 2 files are mostly code generated by MCC. I'm writing to SSP1BUF on the Slave side when a READ_REQUEST is received but I'm not being able to read that information on the Master side. And I'm not really sure how to modify the Master code to receive in a "Slave" condition if I want them to work as multi-master. Any ideas will be very appreciated! Thank you in advance, Attachment(s) I2C_Controller.zip (10.20 KB) - downloaded 53 times |
|
相关推荐
2个回答
|
|
这些问题最有可能发生这种情况。从属密码是错误的。从属设备没有启用N时钟伸缩,或者主BITBUS并且不进行检查。这需要一个字节之间的差距来弥补中断耐心。
以上来自于百度翻译 以下为原文 The issues most likely cases this. The slave code is wrong. The slave does not have n clock stretching enabled , or the master bitbangs and does not check. The requires a short gap between byte to cover interrupt patience. |
|
|
|
你好,谢谢你的回复。嗯,是的,奴隶(或主人)代码可能是错误的,这就是我需要帮助的原因。也就是说,我是个白痴。当从从属器读取时,我忘记增加输入缓冲器的输入指针。缓冲器加载得很好,但是读取函数将(当然)不会更新缓冲区的输出/输出指针。我还更改了读取MAX_PACKET_SIZE(我自己的协议)的字节数,因为一次读取一个会导致丢失偶数字节。我还按照您的建议启用了时钟拉伸,不知道它是否有什么不同。ide工作主从I2C通信代码为PIC18F与协议层的实现来处理消息。应该清理一下,但还行。
以上来自于百度翻译 以下为原文 Hi, thank you for the reply. Well, yes, the slave (or the master) code was probably wrong, that's why I needed help. That said, I'm an idiot. I forgot to increase the input pointer for the input buffer when reading from the slave. The buffer was being loaded fine, but the read function would (of course) not update the buffer's input/output pointers. I also changed reading a MAX_PACKET_SIZE (my own protocol) amount of bytes since reading one at a time was causing me to lose the even ones. I also have enabled clock stretching as you suggested, not sure if it made any difference. Anyway, taking all this into account, the attached files in post 1 provide working Master/Slave I2C communication code for PIC18F with an implementation of a protocol layer to handle messages. It should be cleaned up a bit, but it's fairly ok. |
|
|
|
只有小组成员才能发言,加入小组>>
5250 浏览 9 评论
2037 浏览 8 评论
1958 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3218 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2266 浏览 5 评论
788浏览 1评论
680浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
609浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
685浏览 0评论
582浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-29 00:19 , Processed in 1.150380 second(s), Total 81, Slave 63 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号