完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
我使用PIC18F26K20并将其设置为主模式。阅读数据表,我很困惑,什么是真正开始I2C传输和以什么顺序进行。一页说启动一个传输,我必须通过设置SSPCON2寄存器的SEN位来生成一个启动条件。另一个页面说,我只是向SSPBUF寄存器写了一个值。如果我必须设置SEN启动,那么我什么时候编写SSPBUF?
以上来自于百度翻译 以下为原文 I'm using a pic18F26k20 and have it set to master mode. Reading through the data sheet, I'm confused as to what actually starts the I2C transmission and in what order to proceed. One page says to start a transmission I have to generate a start condition by setting the SEN bit of the SSPCON2 register. Another page says I simply write a value to the SSPBUF register. If I do have to set SEN to start, then when do I write SSPBUF? |
|
相关推荐
6个回答
|
|
我强烈建议不要使用中断来启动。当然,在非中断代码中查询SSPIF标志,但是不要在中断服务例程中执行这一切。您必须熟知如何在中断中实现状态机来完成它。
以上来自于百度翻译 以下为原文 I would strongly recommend NOT using interrupts to start with. Sure, poll the SSPIF flag in non-interrupt code, but don't try to do this all inside an interrupt service routine. You have to be well acquainted with how to implement state machines to do it in an interrupt. |
|
|
|
事实上,我希望我没有给人实际使用ISR的印象,我只是建议使用国旗。
以上来自于百度翻译 以下为原文 Indeed, I hope I didn't give the impression to actually use the ISR, I was suggesting just using the flag. |
|
|
|
我不太清楚这里的问题。难道我不需要检查一下中断标志,看看我什么时候需要写缓冲区吗?这是我的ISR的片段,显示我的意思:
以上来自于百度翻译 以下为原文 I'm not quite sure what the problem is here. Don't I need to check the interrupt flags to see when I need to write the buffer? Here's a snippet from my ISR to show what I mean: if (SSPIE && SSPIF) { if (needToSendI2C_Address)//start condition just completed { i2c_write(i2c_address) needToSendI2C_Address = FALSE; }else { if (needToSendI2C_Data) { i2c_write(i2c_data) needToSendI2C_Data = FALSE; } } SSPIF = 0; //clear flag } |
|
|
|
我不太清楚这里的问题。难道我不需要检查一下中断标志,看看我什么时候需要写缓冲区吗?下面是我的ISR中的一个片段来说明我的意思:你通常不想在ISR内部做这些事情,通常你想尽可能少地在ISR中花费时间。你可以知道何时使用中断标志(不是中断本身)或SS来加载缓冲器。当启动传输时,发送启动条件,等待SEN清除(当启动条件结束时清除),然后加载缓冲器。然后,您可以等待BF清除(当缓冲区中的数据已经完成发送时清除)。然后,您将检查ACK状态位,以确定从设备是否确认数据,并且如果您想发送更多字节,则再次加载缓冲区。或者,如果你不想发送更多的数据,发送一个停止条件出来(笔)。
以上来自于百度翻译 以下为原文 I'm not quite sure what the problem is here. Don't I need to check the interrupt flags to see when I need to write the buffer? Here's a snippet from my ISR to show what I mean: if (SSPIE && SSPIF) { if (needToSendI2C_Address)//start condition just completed { i2c_write(i2c_address) needToSendI2C_Address = FALSE; }else { if (needToSendI2C_Data) { i2c_write(i2c_data) needToSendI2C_Data = FALSE; } } SSPIF = 0; //clear flag } You usually won't want to do these things inside the ISR itself, generally you want to spend as little time inside an ISR as you possibly can. You can tell when to load the buffer by using either the interrupt flag (not the interrupt itself) or the SSP status flags. I.E. When starting a transmit, send the start condition, wait for SEN to clear (it clears when the start condition is finished) then load the buffer. You can then wait for BF to clear (cleared when data in the buffer has finished sending). You would then check the ACK status bit to determine if the slave device acknowledged the data and load the buffer again if you want to send more bytes. Or if you don't want to send any more data, send a stop condition out (PEN). |
|
|
|
我不太清楚这里的问题。难道我不需要检查一下中断标志,看看我什么时候需要写缓冲区吗?你所做的是实现一个粗糙的状态机,但是你忽略了各种需要的东西。也就是说,你没有检查SLAVE返回的ACK/NACK状态。你不允许有一个以上的数据字节。你不允许从从属设备读取(这会触发相同的数据。在交易结束时,你不会发送一个停止条件。所有这些事情在非中断代码中都更容易调试。注意我说的“开始”。一旦你有了所有的逻辑排序,并了解如何外围工程,当然,处理它在一个中断服务,如果你需要同时做其他事情。如果你不去,就别去打扰了。
以上来自于百度翻译 以下为原文 I'm not quite sure what the problem is here. Don't I need to check the interrupt flags to see when I need to write the buffer? What you have done is to implement a crude state machine, but you've left out various required things. i.e. You're not checking the ACK/NAK status returned by the slave You're not allowing for more than one data byte. You're not allowing for reading from the slave device (which will trigger the same interrupt) You're not sending a STOP condition at the end of the transaction. All these things are easier to debug in non-interrupt code. Note I said "to start with". Once you have all the logic sorted, and understand how the peripheral works, sure, handle it inside an interrupt service if you need to to doing other things at the same time. Don't bother if you don't. |
|
|
|
更新:我用一个Switter case语句编写了一个状态机,I2C看起来工作正常。你没有检查SLAVEE返回的ACK/NACK状态。你不允许有一个以上的数据字节。你不允许从从属设备读取(这会触发相同的中断)。在事务结束时不发送停止条件。检查,检查,不检查,检查。
以上来自于百度翻译 以下为原文 Update: I made a state machine with a switch case statement and I2C seems to be working fine. You're not checking the ACK/NAK status returned by the slaveCheck, check, not checked, check. |
|
|
|
只有小组成员才能发言,加入小组>>
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 02:31 , Processed in 1.345746 second(s), Total 85, Slave 69 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号