完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
我想在这里外交点。在使用基础服务库时,MCC生成的函数UARTXXSTATUSOGET()不是一个硬错误,而是看起来很坏的C代码。下面是生成的代码。看看你能不能告诉什么是错误的。对于那些不熟悉的,U1STA是一个位图16位寄存器。寄存器的内容以枚举类型返回。UHHH,这不是枚举类型是如何使用的。C对数据类型进行快速宽松,允许编译此代码,但这是坏的风格。C++编译器会将此标记为一个错误。这个函数看起来是两个不同的方向,所以我不会就如何纠正代码提出一个建议。开发人员需要选择一个方向并对其进行正确的编码。边注:此函数的标题中的示例代码不编译。
以上来自于百度翻译 以下为原文 I'm trying to be diplomatic here. Not a hard bug but bad looking C code for the function UARTx_StatusGet() generated by MCC when using the Foundation Services Library. Here is the generated code. See if you can tell what is wrong. /* Product Revision : Foundation Services Library - pic24-dspic-pic32mm : v1.26 Device : PIC24FJ1024GB610 */ typedef enum { /* Indicates that Receive buffer has data, at least one more character can be read */ UART1_RX_DATA_AVAILABLE /*DOM-IGNORE-BEGIN*/ = (1 << 0) /*DOM-IGNORE-END*/, /* Indicates that Receive buffer has overflowed */ UART1_RX_OVERRUN_ERROR /*DOM-IGNORE-BEGIN*/ = (1 << 1) /*DOM-IGNORE-END*/, .... /* more bit values defined */ } UART1_STATUS; // And the function itself UART1_STATUS UART1_StatusGet (void) { return U1STA; } For those not familiar, U1STA is a bit-mapped 16 bit register. The contents of the register is being returned as an enumerated type. Uhhh, that's not how enumerated types are used. C plays fast and loose with data types and allows this code to compile but this is bad style. A C++ compiler would flag this as an error. The function appears to be going in two different directions so I won't make a suggestion as to how to correct the code. Developer needs to pick a direction and code it properly. Side note: The example code in the header for this function does not compile. |
|
相关推荐
11个回答
|
|
不好看?不,你最好戴眼镜。试图找到一些关于“高级C技术”的书。虽然看起来很奇怪,但在编写安全的、可检查的代码时,枚举比定义常量有一些优势。
以上来自于百度翻译 以下为原文 Bad looking? No - you'd better get glasses. Of try to find some book about "Advanced C Techniques". While it seems to look weird for you, enums have some advantages over #define'd constants when it comes to writing secure, checkable code. |
|
|
|
你到底关心什么?C++所做的任何事情都不相关,这就是C.。
以上来自于百度翻译 以下为原文 What exactly are you concerned about? Whatever C++ would do isn't relevant; this is C. |
|
|
|
看起来不错。我们会选择UART1A StasuStTyt作为类型。
以上来自于百度翻译 以下为原文 Looks ok. Would have prefered UART1_STATUS_t _t for types |
|
|
|
我看到的问题是在函数声明:uART1IONEUTAR1STATUSOGET(空){返回U1STA;},我读到的是,函数UnAtAR1STATUSOGET返回Type UARTAR1STATUS的变量。UART1X状态是枚举类型,所以返回的变量应该有一个值,该值是Type的成员。E枚举类型。因为函数返回U1STA寄存器的内容,这不一定是真的,因为寄存器中的其他位。所以对我来说,这个函数有一个bug。当我尝试在循环中使用函数时,我发现了这一点,测试了(UART1YSTATUSER()==UART1Y-TXIORT),并且它从未实现。只有通过深入研究代码,我才意识到发生了什么。我猜想,另一张海报所提到的“高级”技术类似于(C++中的n=类型Cn= const无符号int)。我已经看到了,并且用了EnUM常数。但我从未见过它扩展到变量,其中枚举类型变量与未签名int完全相同,取任何适合的值。对我来说,这违背了使用EnUM类型的目的。我举了两个风格指南,找不到任何提到这个“先进”的技术。总之,如果有一个程序员圈认为EnUM变量可以保持任何值是可以的,我会把它存入我的头脑中作为参考。关于在“IF”语句之后放置括号的问题,我们有足够的论证。不必为此争论。
以上来自于百度翻译 以下为原文 The problem I see is around the function declaration: UART1_STATUS UART1_StatusGet (void) { return U1STA; } I read this as saying that the function UART1_StatusGet returns a variable of type UART1_STATUS. UART1_STATUS is an enumerated type so the variable returned should have a value that is a member of the enumerated type. Because the function returns the contents of the U1STA register, this is not necessarily true because of the other bits in the register. So to me, the function has a bug. I found this out when I tried using the function in a loop, testing for (UART1_STATUS() == UART1_TX_COMPLETE) and it never went true. Only by digging into the code did I realize what happened. I'm guessing that the "advanced" technique that another poster referred to is something like (enum type in C == const unsigned int in C++). And I've seen that and used enum constants that way myself. But I've never seen it extended to the variables where an enum type variable was treated exactly the same as an unsigned int, taking any value that would fit. To me, that defeats the purpose of using an enum type. I pulled up a couple of style guides and couldn't find any mention of this "advanced" technique. Anyway, if there is a circle of programmers that thinks it is ok for enum variables to hold any value, I'll file that away in my head for reference. We have enough arguments about where to place the braces after an "if" statement. No need to argue about this one. |
|
|
|
C中的枚举最终只是一个“int”值的集合。它没有任何额外的特殊属性。当然,你不喜欢代码,但是它并不是一个错误或错误,它会被这样使用。你还没有显示整个枚举,但是我猜想里面有一个“1 & lt;lt;15”,这是未定义的行为。我还假设U1STA是一个未签名的int,它被返回语句转换为一个已签名的int。
以上来自于百度翻译 以下为原文 An enum in C is ultimately just a collection of "int" values. It doesn't have any additional special properties. You're certainly free to not like the code, but it's not a bug or an error for it to be used that way. There are a couple of things that might be real issues with that code, though. You haven't shown the entire enum, but I suspect there is a "1 << 15" in there, which is undefined behavior. And I also assume that U1STA is an unsigned int which is being converted to a signed int by the return statement. |
|
|
|
你自己说:U1STA是一个有点映射的16位寄存器。它有16个可能的比特组合,而不是1个你正在测试的组合。(UART1YSTATUSES()= = UART1OXTXIORT)不起作用,因为U1STA有其他可能被设置或清除的比特。使用一个掩码:如果(U1Stand & U1StudioUrxDaAd掩码)如果(U1Stand & lt;and lt;U1StasUrxDaaPasn)如果(UART1YSTATUSES()和1和lt;& UART1OTXXO完成)EnUM仅仅是已经存在于您的芯片的XC16头文件中的位置。
以上来自于百度翻译 以下为原文 You said it yourself: U1STA is a bit-mapped 16 bit register. It has 16 possible combinations of bits not 1 that you are testing for. (UART1_STATUS() == UART1_TX_COMPLETE) That won't work because U1STA has other bits that may be set or cleared. For the above to work all the other 15 bits in UxSTA would have to be zero. Use a mask: if (U1STA & U1STA_URXDA_MASK) if (U1STA & 1 << U1STA_URXDA_POSN) if (UART1_STATUS() & 1 << UART1_TX_COMPLETE) The enum is just the positions that already exist in the xc16 header file of your chip. |
|
|
|
使用一些编译器,可以在不匹配时使用指向枚举的指针获取警告:基于GCC的编译器将在此警告,但XC8不会。这有点笨拙,它对生成的代码没有帮助。
以上来自于百度翻译 以下为原文 With some compilers, you can get a warning by using pointers to enums when there's a mismatch:void main(void) { enum color { red, green }; enum shape { circle, square }; enum color color, *c_ptr; enum shape shape, *s_ptr; s_ptr = &shape; /* ok */ c_ptr = &shape; /* warning */ }gcc-based compilers will warn here, but XC8 will not. It's a bit awkward, and it doesn't help with generated code. |
|
|
|
当然,这要归功于文档。您需要被告知函数返回的是有位掩码值,而不是简单枚举。
以上来自于百度翻译 以下为原文 Surely it comes down to documentation. You need to be told that the function is returning a bitmasked value, not a simple enum. |
|
|
|
没有,“圆”,EnUM可以包含任何值。卷轴放置是个人喜好的问题。我认为你需要提高你的编程技巧,然后告诉我们U1STA是什么。
以上来自于百度翻译 以下为原文 There is no, "circle", enum can contain any value. Curly braces placement is a matter of personal preference. I think you need to up your programming skills before informing us what U1STA is. |
|
|
|
在这个疯狂的讨论中,我有点晚了……而XC8 1.45还没有确定EnUM变量/常量只对同一枚举类型的其他检查(其他编译器更成熟——这样做),使用枚举是很好的做法。TE(数据大小可能根据“必要性”而不同),拥有“未命名”枚举成员是完全合法的。(考虑EnUM声明仅是一个命名符号的列表,如“x定义,但具有改进的属性”)小提示:如果编译器不支持枚举类型检查,则使用一个额外的检查器,例如doxGEN。刚好DoxEngEngEngin保存了一段时间,通过发现我已经(系统地)将一种枚举值分配给另一种枚举变量。互换了2个变量。在任何人有机会注意到错误的回复之前改变了这个。找到一些bug。然后重新比较一些枚举变量与一个命名符号:如果你不知道你在做什么,你可能想解密我的签名。
以上来自于百度翻译 以下为原文 I'm a bit late in this wild discussion ... While XC8 1.45 is not yet up to ascertain the enum variables/constants are asserted to/checked only vs. others of the same enum type (other compilers are more mature - doing so), it is good practice to use enums. And no - while an enum might occupy only a byte (data size may vary depending on "necessity"), it is completely legal to have "unnamed" enum members. (Consider the enum declaration simply a list of named symbols - like #define, but with improved properties.) Small hint: if your compiler does not support enum type checking, use an additional checker - e.g. doxygen. Just rcently doxygen saved some time by uncovering that I had (systematically) assigned enum values of one kind to an enum variable of another kind. Had interchanged 2 variables. Changed this back prior anybody had a chance of noticing the error resp. finding some bug. And re comparing some enum variable vs. one of the named symbols: if you don't know what you're doing, you might want to decipher my signature. |
|
|
|
这些构造的预期用途如下:这当然不是一个错误。它是好是坏是另一回事。在这种情况下,最好的建议是这样做——而不是挖掘别人的代码,写自己的。你总是按照你喜欢的方式去做,你会永远快乐。编写一次并在所有的PIC24项目中重复使用。其实很简单。
以上来自于百度翻译 以下为原文 The intended use of these constructions is as follows: if (UART1_StatusGet() & UART1_RX_DATA_AVAILABLE) { } This certainly is not a bug. Whether it is good or bad is another story. In such situations, the best advise is this - rather than digging into other people's code, write your own. You will always have it done the way you prefer and you will always be happy. Write it once and reuse in all your PIC24 projects. Very simple, actually. |
|
|
|
只有小组成员才能发言,加入小组>>
4803 浏览 9 评论
1820 浏览 8 评论
1739 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
2947 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2050 浏览 5 评论
440浏览 1评论
1103浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
353浏览 0评论
256浏览 0评论
LAN9252使用SQI通信,进行数字复位后读BYTE_TEST异常
1770浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-4-10 14:04 , Processed in 1.343951 second(s), Total 83, Slave 67 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号