PIC 上的基本二进制转换失败
我有以下代码,转换 dec。到二进制(存储为 int)并且它在 C::B 中运行得很好:
for(i=0;i<8;i++)
{
carry = start_value%2;
tab[bla] = carry;
bla++;
start_value = start_value/2;
}
for(i=bla; i>0; i--)
{
binary = binary + tab[i]*pow(10,i);
}
但是当我尝试使用 MikroC 在 PIC18F4550 上执行相同操作时,它失败了! 我已经包含了“pow”并尊重我使用的变量的声明。不管怎样,输出都是错误的(例如-62053而不是110110)。
感谢您的关注:) 祝你今天过得愉快
I have the following code, converting dec. to binary (stored as an int) and it runs just fine in C::B :
for(i=0;i<8;i++)
{
carry = start_value%2;
tab[bla] = carry;
bla++;
start_value = start_value/2;
}
for(i=bla; i>0; i--)
{
binary = binary + tab[i]*pow(10,i);
}
But when I try to do the same on a PIC18F4550 with MikroC it just fails !
I have included 'pow' and respected the declarations for the variables I use. No matter what, the output is wrong (-62053 instead of 110110 for instance).
Thanks for taking a look into it :)
Have a nice day
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
检查此 PIC 架构的
int
类型 (sizeof(int)
) 的大小。例如,如果int
是 16 位,则它可以容纳的最大值是 65535(对于unsigned int
)或从 -32768 到 32767(对于int
)。问题可能是您正在计算的值对于您正在使用的类型来说太大了。Check the size of an
int
type (sizeof(int)
) for this PIC architecture. For example, if anint
is 16 bit, the maximum value it can hold is 65535 (for anunsigned int
) or from -32768 to 32767 (for anint
). Probably the problem is that the value you are calculating is too big for the type you are using.您应该从
i = bla - 1
开始第二个循环,因为它比二进制数字多增加了一次。也可能是tab[]
未初始化为 0。第二个循环应包含第一个选项卡元素: tab[0] :
You should start the second loop from
i = bla - 1
since it was incremented one more time than there binary digits. Also it could be thattab[]
isn't initialized to 0.And the second loop shall comprise the first tab element : tab[0] :
要从二进制数字数组中获取十进制值,您必须将它们乘以 2 的适当幂,而不是 10。假设 pow() 就是它看起来的样子,pow(base, exponent),您需要使用改为 pow(2,i)。
To get a decimal value from an array of binary digits, you have to multiply them by the appropriate powers of 2, not of 10. Assuming pow() is what it appears to be, pow(base, exponent), you need to use pow(2,i) instead.
jmpcm:
在我的 PIC18F 上,int 是 16 位(2 个字节)。
但问题是代码在我的计算机上运行良好,但在 PIC 上运行不佳,而我使用的变量具有相同的大小。
菲利普:
我有一种感觉,你关于桌子没有清理干净的说法可能是对的!但还不能测试它。
珍妮·品达:
不,我正在存储一个需要在 int 中进行交互的位数组(需要节省 RAM 空间),因此我们仍在最终存储中使用 base10。
全部 :
我用一个定义了所有位的结构来摆脱它:
所以我可以将 1 位存储在一组 8 位中,并在主要计算中将其视为 int 并在 Nx1 矩阵中访问它。
尽管如此,还是感谢您的帮助!
jmpcm :
An int is 16 bits (2 bytes) on my PIC18F.
But the problem is that the code runs fine on my computer but not on the PIC, whereas variables I use have the same size.
philippe :
I have a feeling you might be right about the table not being cleared ! Can't test it yet though.
Jeanne Pindar :
No. I am storing an array of bits that I need to interact with in an int (need to save RAM space), so we're still working base10 on final storing.
All :
I managed my way out of it with a structure for which I defined all the bits :
So I can store 1 bit in a group of 8 bits and treat it as an int in main calculations and access it in a Nx1 matrix.
Thanks for your help nonetheless !