完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
当为PIC24FJ256GB206做一些Bang-I2C代码时,我意外地将一个错误从我自己(通过计时器工作)改为内置的编译器时引入了一个bug。这是我在复制粘贴过程中最后得到的结果:我没有注意到错误,同时我做了很多其他的改变。然后,当我测试了一个更大的函数,它使用了I2C功能几个级别时,代码就不再工作了。我的问题是:1)为什么C30编译器不把这个标记为一个错误?2)如果这不是一个错误,那么有人能告诉我它会写什么吗?正确的线是这个(并且当改变到这个我的代码开始正常工作):
以上来自于百度翻译 以下为原文 When working on some bit-bang i2c code for a PIC24FJ256GB206 I accidentally introduced a bug when changing a delay call from my own (working via timers) to the compiler built in. This is what I ended up with during the copy-paste: __delay_us(1)(100); I did not notice the error and I was doing a lot of other changes at the same time. Then when I tested a larger function that used the i2c functionality a few levels away the code no longer worked. My questions here are: 1) Why did not the C30 compiler flag this as an error? 2) If it is not an error, then could someone please tell me what it will do as written? The correct line is this (and when changed to this my code started working properly again): __delay_us(100); |
|
相关推荐
13个回答
|
|
哦,当然,这有道理!(愚蠢的我不记得宏观!)
以上来自于百度翻译 以下为原文 Oh sure, that makes sense! (stupid of me not remembering about the macro!) |
|
|
|
这不是编译器中的一个bug,它并不是一个错误,它不能被修改,因为“(100)”只是一个与Y.E.DelayuUS无关的表达式。C不要求语句在单独的行上,而您所拥有的是同一行上的两个语句:γ-DelayuUS(US)计算为复合语句{……一些代码…}和(100);是表达式语句。所以你有:{……一些代码…}(100),相当于……一些代码…(100);当然,(100);什么也不做。
以上来自于百度翻译 以下为原文 This is not a bug in the compiler, and it's not a bug in __delay_us, and it can't be "fixed" by changing __delay_us, because "(100)" is just an expression that has nothing to do with __delay_us. C does not require statements to be on separate lines, and what you have is two statements on the same line: __delay_us(us) evaluates to the compound statement { ...some code... } and (100); is an expression statement. So what you have is: { ...some code... } (100); which is equivalent to ...some code... ; (100); and of course the (100); does nothing. |
|
|
|
当然,寺庙是正确的。这里是宏的定义:& lt;编辑& gt;如果宏没有括号和分号定义,它仍然应该工作,并且将为此情况生成一个错误。
以上来自于百度翻译 以下为原文 Of course @jtemples is correct. Here is the definition of the macro: #define __delay_us(d) { __delay32( (unsigned long) (((unsigned long long) d)*(FCY)/1000000ULL)); } |
|
|
|
永远记住,C是包含无限量的绳子的语言,不需要额外收费。
以上来自于百度翻译 以下为原文 Always remember, C is the language that includes an infinite amount of rope at no extra charge. |
|
|
|
…一个刽子手套索(我上传一个图像,但访问被拒绝)。
以上来自于百度翻译 以下为原文 ... a hangmans noose (I'd upload an image but Access is Denied). |
|
|
|
严肃地说,宏不应该用括号和分号来定义吗?
以上来自于百度翻译 以下为原文 Seriously though, shouldn't the macro be defined without the braces and semicolon? #define __delay_us(d) __delay32( (unsigned long) (((unsigned long long) d)*(FCY)/1000000ULL)) |
|
|
|
|
|
|
|
嗯,它不是通常的“安全措施”,当它被用于复合语句或类似的时候?(我也不喜欢他们……)
以上来自于百度翻译 以下为原文 Well, isn't it the usual "safety measure" for when it's used in compound statements or alike? (I don't like them either...) |
|
|
|
我不认为这是个问题,因为γ-Delay32()定义为:
以上来自于百度翻译 以下为原文 I don't see that is a problem because __delay32() is defined as: extern void __delay32(unsigned long cycles); |
|
|
|
谢谢,那么我相信我的观点是:1)我想要的是:但是我得到了:(100)是一个法律声明,除了我不知道为什么编译器没有抱怨失踪;在这两个声明之间?2)允许把这样的东西放入任何代码中:它将被接受C代码但不做任何事情…我的代码停止工作的原因必须是因为延迟比预期的短100倍。它现在正在工作,我的问题是编译器没有帮助的原因。我发现了。主要是PASCAL开发人员,我对C缺乏检查感到不安。很容易射出一只脚……但是,在任何情况下,如果我忘记在使用它们之前声明我的变量,那就好像是C30,这很好!
以上来自于百度翻译 以下为原文 Thanks, then I believe my take from this is: 1) what I wanted was: __delay_us(100); but I got instead: __delay_us(1) (100); where (100) is a legal statement, except I am wondering why the compiler did not complain about the missing ; between the two statements? 2) It is allowed to put something like this into the code anywhere: (100); and it will be accepted C code but does not do anything... The reason my code ceased to work must be because the delay became 100 times shorter than intended. It is now working and my question was really why the compiler did not help me spot it. Being primarily a Pascal developer I am uncomfortable with the C lack of checking. Easy to shoot ones foot.... However it seems like C30 in any case errors out if I forget to declare my variables before using them, that is good! |
|
|
|
因为宏计算为{代码…这在语法上是一个语句,就像……代码…;是一个语句。
以上来自于百度翻译 以下为原文 Because the macro evaluates to { ...code... } which syntactically is a statement, just like ...code...; is a statement. |
|
|
|
对于像这样的单个语句宏,我通常不会麻烦用括号包装它。但我不认为C30的操作有什么“错误”,虽然更典型的是,如果你想包装宏,它是用“do{…代码…”完成的。}(0)“(在结束时没有分号)。这也会引发宏错误,因为宏调用缺少分号。
以上来自于百度翻译 以下为原文 For a single-statement macro like that, I usually wouldn't bother wrapping it in braces. But I wouldn't say there's anything "wrong" with what C30 does, though more typically if you want to wrap a macro, it's done with "do { ...code... } while (0)" (no semicolon at the end). That would have also triggered a syntax error due to the missing semicolon on the macro invocation. |
|
|
|
同意。也许Microchip应该编辑宏定义。
以上来自于百度翻译 以下为原文 Agree. Perhaps Microchip should edit that macro definition. |
|
|
|
只有小组成员才能发言,加入小组>>
5243 浏览 9 评论
2035 浏览 8 评论
1955 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3208 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2258 浏览 5 评论
779浏览 1评论
672浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
598浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
682浏览 0评论
579浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-24 22:20 , Processed in 1.547940 second(s), Total 102, Slave 86 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号