PIC 上的基本二进制转换失败

发布于 2024-10-03 13:29:17 字数 422 浏览 0 评论 0原文

我有以下代码,转换 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一袭白衣梦中忆 2024-10-10 13:29:19

检查此 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 an int is 16 bit, the maximum value it can hold is 65535 (for an unsigned int) or from -32768 to 32767 (for an int). Probably the problem is that the value you are calculating is too big for the type you are using.

小伙你站住 2024-10-10 13:29:19

您应该从 i = bla - 1 开始第二个循环,因为它比二进制数字多增加了一次。也可能是 tab[] 未初始化为 0。

第二个循环应包含第一个选项卡元素: tab[0] :

for(i = bla - 1; i >= 0; i--)
{
    binary += tab[i] * pow(10, i);
}

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 that tab[] isn't initialized to 0.

And the second loop shall comprise the first tab element : tab[0] :

for(i = bla - 1; i >= 0; i--)
{
    binary += tab[i] * pow(10, i);
}
画离情绘悲伤 2024-10-10 13:29:19

要从二进制数字数组中获取十进制值,您必须将它们乘以 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.

琉璃梦幻 2024-10-10 13:29:19

jmpcm:
在我的 PIC18F 上,int 是 16 位(2 个字节)。
但问题是代码在我的计算机上运行良好,但在 PIC 上运行不佳,而我使用的变量具有相同的大小。

菲利普:
我有一种感觉,你关于桌子没有清理干净的说法可能是对的!但还不能测试它。

珍妮·品达:
不,我正在存储一个需要在 int 中进行交互的位数组(需要节省 RAM 空间),因此我们仍在最终存储中使用 base10。

全部 :
我用一个定义了所有位的结构来摆脱它:

struct {
unsigned int bit0: 1;
unsigned int bit1: 1;
...

所以我可以将 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 :

struct {
unsigned int bit0: 1;
unsigned int bit1: 1;
...

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 !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文