完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
你好,我想把一个32位的数字转换成ASCII字符,这样我就可以把它们发送到Twitter。我使用CP2102模块,PIC18F47 K40,XC8 V1.44和RealTalm。第一个数字被正确计算。作为输入,我用4294967295作为输出,在timeNI上得到424497 66 40i,也尝试了PIC。16F1939也有同样的结果。当我写16位时,它工作得很好。我的代码有什么问题?
以上来自于百度翻译 以下为原文 Hello I am trying to convert a 32 bits number into ASCII characters, so that I can send them to the therminal. I use a CP2102 module, Pic18F47K40 , XC8 v1.44 and Realterm. The first digits are calculated correct. as input I use 4294967295 as output on the therminal I get 4294976640 I also tried the PIC16F1939 with the same result. When I write it 16 bits it works fine. What is wrong on my code? while(1) { uint32_t binair_4, binair4; binair4 = 4294967295; binair_4 = binair4; uint8_t n10e8 = (binair_4 / 10e8); binair_4 = binair_4 - (n10e8 * 10e8); uint8_t n10e7 = (binair_4 / 10e7); binair_4 = binair_4 - (n10e7 * 10e7); uint8_t n10e6 = (binair_4 / 10e6); binair_4 = binair_4 - (n10e6 * 10e6); uint8_t n10e5 = (binair_4 / 10e5); binair_4 = binair_4 - (n10e5 * 10e5); uint8_t n10e4 = (binair_4 / 10e4); binair_4 = binair_4 - (n10e4 * 10e4); uint8_t n10e3 = (binair_4 / 10e3); binair_4 = binair_4 - (n10e3 * 10e3); uint8_t n10e2 = (binair_4 / 10e2); binair_4 = binair_4 - (n10e2 * 10e2); uint8_t n10e1 = (binair_4 / 10e1); binair_4 = binair_4 - (n10e1 * 10e1); uint8_t n10e0 = (binair_4 / 10e0); binair_4 = binair_4 - (n10e0 * 10e0); uint8_t n1 = binair_4; if (binair4 >= 10e8 ) send_character_to_TX(n10e8 + '0'); if (binair4 >= 10e7 ) send_character_to_TX(n10e7 + '0'); if (binair4 >= 10e6 ) send_character_to_TX(n10e6 + '0'); if (binair4 >= 10e5 ) send_character_to_TX(n10e5 + '0'); if (binair4 >= 10e4 ) send_character_to_TX(n10e4 + '0'); if (binair4 >= 10e3 ) send_character_to_TX(n10e3 + '0'); if (binair4 >= 10e2 ) send_character_to_TX(n10e2 + '0'); if (binair4 >= 10e1 ) send_character_to_TX(n10e1 + '0'); if (binair4 >= 10e0 ) send_character_to_TX(n10e0 + '0'); send_character_to_TX(n1 + '0'); send_character_to_TX(0x0D); //new line} __delay_ms(500); } |
|
相关推荐
13个回答
|
|
|
|
|
|
我试着建议DarioGI改变数字,并为N10E1/2/3等制作UTIT32 T,这不是工作。
以上来自于百度翻译 以下为原文 I tried suggestion of DarioG I changed the numbers and made uint32t for the n10e1/2/3 etc, thats not working. from KTrenholm : this is working uint32_t number = 1234567890; char OutputArray[15] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; sprintf(OutputArray,"%lur",number); for (n=0;n<11;n++) {send_character_to_TX(OutputArray[n] );} |
|
|
|
不,他说将浮点数10E8、10E7等改为整数100000、UL、100000、0L等,也可以从QHB读取POST。您尝试过ULTOAL(),它应该比StastFf()使用更少的资源吗?
以上来自于百度翻译 以下为原文 No, he said to change the floating point numbers 10e8, 10e7, etc. to integers 100000000uL, 10000000uL, etc. Also, read the post from Qhb. Have you tried ultoa(), which should use less resource than sprintf()? |
|
|
|
我仍然想知道为什么我最初编写的代码不起作用。编译器不能进行这样的计算吗?我会尝试这个数字,但是这个数字有不同的长度,感谢荷兰的所有回复。
以上来自于百度翻译 以下为原文 I still wondering why the code I wrote first is not working. Is the compiler not capable to do such calculations? I will try ultoa() still the number have different lenghts, thanks for all the replies Ep from the Netherlands |
|
|
|
你看过回复了吗?是否更改了常数的格式,以便编译器使用正确的值(整数)?
以上来自于百度翻译 以下为原文 DId you read the responses? Have you change the format of the constants so the compiler is using the correct values (integers)? |
|
|
|
编译器正在按照你告诉它做的去做。你告诉它在32位整数上做24位浮点运算。事实上,你没有得到预期的结果不是编译器的错误。
以上来自于百度翻译 以下为原文 The compiler is doing exactly what you told it to do. You told it to do 24-bit floating point math on 32-bit integers. The fact that you're not getting the results you expect is not the compiler's fault. |
|
|
|
我告诉过你为什么在第七章中,你的代码是错误的,这是因为C对使用不同大小的变量进行算术的理解不好。这意味着什么?你能举个例子吗?
以上来自于百度翻译 以下为原文 I told you why in post#7. It is your code that is faulty, due to a bad understanding of how C does arithmetic using different sized variables. What does this mean? Can you give an example? |
|
|
|
不要把它归咎于编译器。这就是达里奥和Qhb所说的:你们这样做了吗?
以上来自于百度翻译 以下为原文 Don't blame it on the compiler. This is what Dario and Qhb said to do: uint8_t n10e8 = (binair_4 / 1000000000uL); binair_4 = binair_4 - (n10e8 * 1000000000uL); uint8_t n10e7 = (binair_4 / 100000000uL); binair_4 = binair_4 - (n10e7 * 100000000uL); uint8_t n10e6 = (binair_4 / 10000000uL); binair_4 = binair_4 - (n10e6 * 10000000uL); uint8_t n10e5 = (binair_4 / 1000000uL); binair_4 = binair_4 - (n10e5 * 1000000uL); uint8_t n10e4 = (binair_4 / 100000uL); binair_4 = binair_4 - (n10e4 * 100000uL); uint8_t n10e3 = (binair_4 / 10000uL); binair_4 = binair_4 - (n10e3 * 10000uL); uint8_t n10e2 = (binair_4 / 1000u); binair_4 = binair_4 - (n10e2 * 1000u); uint8_t n10e1 = (binair_4 / 100u); binair_4 = binair_4 - (n10e1 * 100u); uint8_t n10e0 = (binair_4 / 10u); binair_4 = binair_4 - (n10e0 * 10u); uint8_t n1 = binair_4; Did you do this: ultoa(OutputArray,number,10); |
|
|
|
试试这个函数:& lt;编辑& gt;按1~0注释更新代码。
以上来自于百度翻译 以下为原文 Try this function: void ultodecASCII(char * output, unsigned long value) { unsigned char v[ 10 ]; unsigned char vindex; vindex = 0; if (output) { do { v[ vindex++ ] = '0' + (value % 10); value = value / 10; } while(value); do { *output++ = v[ --vindex ]; } while(vindex); *output = 0; } } Code updated as per 1and0 comments. |
|
|
|
要修复的项:将V[]数组的大小增加到11个10,从第二个DO while循环中移除“’”,并且NULL终止输出字符串。
以上来自于百度翻译 以下为原文 Items to fix:
|
|
|
|
达里奥和QHB,代码现在工作,我已经写了你的号码。但是我从来没有用过U和ULY谢谢你。
以上来自于百度翻译 以下为原文 Dario and Qhb, The code is working now, I had written the numbers as you asked. but I never used the u and ul thank you |
|
|
|
|
|
|
|
现在你明白了。真正的问题是,你明白为什么这样做,而你的原始代码不?如果没有,请再次读取该线程中的所有帖子。
以上来自于百度翻译 以下为原文 Now that you see it works. The real question is do you understand why this works and your original code does not? If not, read all the posts in this thread again. |
|
|
|
只有小组成员才能发言,加入小组>>
5447 浏览 9 评论
2144 浏览 8 评论
2052 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3315 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2356 浏览 5 评论
954浏览 1评论
814浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
791浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
833浏览 0评论
我是Microchip 的代理商,有PIC16F1829T-I/SS 威廉希尔官方网站 问题可以咨询我,微信:A-chip-Ti
529浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-4-20 03:24 , Processed in 1.596957 second(s), Total 102, Slave 85 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191