完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
大家好,我正在尝试读取和处理我的TCP/IP演示应用程序中的I2C数据。我使用了plib示例,它工作得很好(独立运行)。当我在演示应用程序中尝试做同样的操作时,它不工作。下面是我正在使用的代码:UINT8 i2cData[10];UINT8 i2cbyte[10];UINT16 up,low;UINT16 up1,low1,val;读取的数据存储在i2cbyte中。//变量操作up=i2cbyte[0];low=i2cbyte[1];up1=i2cbyte[0]<<6;low1=i2cbyte[1]>>2;val=up1|low1;示例值:i2cbyte[0]=0x0d和i2cbyte[1]=0xb9问题在于没有变量操作代码正在工作。我已经附加了调试器部分的屏幕截图。什么会引起这个问题?这可能是因为内存问题吗?而且,变量在某些情况下似乎具有相同的地址(在调试器中,也在PIC中看到)。应注意的是,该代码独立运行良好,只在演示应用程序中显示问题。
以上来自于百度翻译 以下为原文 Hi all, I am trying to read and process i2c data in my tcp/ip demo app. I have used the plib example and it works well (stand alone). When trying to do the same in my demo app, its not working. Below is the code that I am using : UINT8 i2cData[10]; UINT8 i2cbyte[10]; UINT16 up,low; UINT16 up1,low1,val; The data read is stored in i2cbyte. // variable manipulation up = i2cbyte[0]; low = i2cbyte[1]; up1 = i2cbyte[0] << 6; low1 = i2cbyte[1] >> 2; val = up1 | low1; Example value : i2cbyte[0] = 0x0d and i2cbyte[1] = 0xb9 The problem is that none of the variable manipulation code is working. I have attached a screen shot of the debugger section. What could be causing this problem ? Could this be due to memory issue ? Also, the variables seems to have same address in certain case (seen in the debugger,also in the pic). Due note that this code works fine standalone and shows issue only in demo app. Attached Image(s) |
|
相关推荐
17个回答
|
|
监视窗口中的那些“地址”非常奇怪,即,重复……可能是监视/调试器问题(我几乎不相信MPLAB X…)您的代码听起来足够好了
以上来自于百度翻译 以下为原文 Those "addresses" in the watch window are quite strange, i.e. repeated... Could it be a Watch/debugger issue (I hardly trust MPLAB X...) Your code sounds good enough |
|
|
|
你使用这些变量了吗?如果没有,因为它们没有被标记为“易失性”,编译器可以自由地优化对它们的访问。
以上来自于百度翻译 以下为原文 Are you using these variables anywhere yet? If not, as they have not been marked "volatile", the compiler is free to optimise away any accesses to them. |
|
|
|
@qhb我不理解你的问题。如果没有初始化变量(up、low、up1、low1或val),那么就是你要求的。
以上来自于百度翻译 以下为原文 @qhb I didnt understand your question. I havent initialised variables ( up,low,up1,low1 or val), if thats what you asked for. |
|
|
|
在你发布的代码片段之后,你的代码对这些变量做了什么吗?如果没有,那么如果程序一开始就不存在,那么对程序的输出没有影响,所以编译器可能决定根本不写入。这是使用“优化编译器”可能混淆的副作用。它不需要做无意义的活动。
以上来自于百度翻译 以下为原文 Does your code do anything with these variables after the snippet of code you posted? If not, then it makes no difference to the output of the program if they never existed in the first place, so the compiler may decide not to write to them at all. That is a possible confusing side effect of using an "optimising compiler". It doesn't have to do pointless activities. |
|
|
|
在此之后,我将“Var”返回到主代码(如图片中)。我应该在变量上标记变量吗?我怎样才能解决这个问题?
以上来自于百度翻译 以下为原文 I am returning "val" to the main code (as in the picture) after this. Should I mark volatile on the variables ? How can I solve this ? |
|
|
|
在调试时,可以尝试向所有变量添加“volatile”,这样就可以在监视窗口中看到所有中间值。
以上来自于百度翻译 以下为原文 You could try adding "volatile" to all your variables just while you are debugging, so you can see all the intermediate values in the watch window. It shouldn't be necessary once you have it working right. |
|
|
|
这就是问题所在。我不希望它在我的集成代码中工作。:(
以上来自于百度翻译 以下为原文 Thats the problem. I havent goten it to work in my integrated code. :( |
|
|
|
谢谢,添加了挥发性的工作和调试器显示正确的值……这是正确的方法吗?添加挥发性是否以任何方式影响代码(因为我多次调用函数)?
以上来自于百度翻译 以下为原文 @qhb Thanks, adding volatile works and debugger shows correct values.... Is this the right way to do it? Does adding volatile affect the code in any way (as I am calling the function multiple times) ? |
|
|
|
|
|
|
|
易失性会使代码稍微慢一些,因为它必须创建每个中间值,而不是最有效的方法。如果只对“val”变量进行操作,您还会看到相同的结果吗?
以上来自于百度翻译 以下为原文 Volatile will make the code a little slower, as it must create each of the intermediate values, rather than do it the most efficient way. If you only do it to the "val" variable, do you still see the same result? |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
由于您没有提到您使用的是哪个编译器,我想知道这里是否有“整数提升”问题。此代码只能在编译器自动将8位值转换为16位之后才能工作。这是“自动整数提升”。请尝试将其更改为:
以上来自于百度翻译 以下为原文 As you haven't mentioned which compiler you're using, I'm wondering if there is an "integer promotion" issue here. This code up1 = i2cbyte[0] << 6; can only work if the compiler automatically converts the 8 bit value into 16 bit before doing the shift. This is "automatic integer promotion". Try changing it to: up1 = (UINT16)i2cbyte[0] << 6; |
|
|
|
|
|
|
|
在完成调试之后,移除该易失性。此用途仅用于调试。通常只需要在中断和主代码之间共享变量。
以上来自于百度翻译 以下为原文 You remove the volatile after you are done debugging. This use is just for debugging. Usually Volatile is only needed for variable shared between interrupts and the main code. |
|
|
|
所以,当程序不能正常工作时,您可能会对您所获得的内容更加犹豫不决。尝试一次只使一个变量不稳定,以尝试缩小问题的范围。您发现编译器错误的可能性很小,但是更有可能出现其他问题。
以上来自于百度翻译 以下为原文 So You could be a little more fothcoming about what you are getting when it doesn't work. Try just making one variable at a time volatile, to try to narrow down where the problem is. There is a tiny probability you have found a compiler bug, but it's much more likely something else is going on. |
|
|
|
只有小组成员才能发言,加入小组>>
4824 浏览 9 评论
1829 浏览 8 评论
1747 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
2954 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2059 浏览 5 评论
460浏览 1评论
1111浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
362浏览 0评论
263浏览 0评论
LAN9252使用SQI通信,进行数字复位后读BYTE_TEST异常
1797浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-4-24 11:08 , Processed in 1.557496 second(s), Total 92, Slave 75 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号