从PIC发送浮动到Raspberry Pi

时间:2015-01-09 00:56:55

标签: raspberry-pi pic uart

我试图通过uart将pic 16f877a(easypic4开发板)的浮动值发送到raspberry pi。

mikroc代码

AValue = 4.88 * ADC_Read(2)
ptr = (insigned char *)&AValue;
    for (i=0; i < sizeof(AValue);
        UART1_Write(*(ptr+1)), i++);
UART1_Write(0x0a);
delay_ms(100)


import serial, time, struct
from pprint import pprint


ser = serial.Serial("/dev/ttyAMA0", 9600)

ser.write(raw_input("enter char: "))

while True:

    count = 0
    AValue = []
    for ch in ser.read():
        if ch == "\n":
            AValue = []
            time.sleep(0.1)

    while count < 4:
        for ch in ser.read():
            AValue.append(ch)
            count += 1

    flt =struct.unpack("<f",str("".join(AValue)))
    pprint (flt)enter code here

在raspberry pi上的python shell中输出。正如你所看到的那样,当你移动底池时,值会发生变化,但只有零值是正确的。实际上甚至不为零,因为第一个值应该是1 * 4.88

(0.0,)
(-1.1472824864025526e-35),
(-3.2123910193243324e-35),
(-4.405564851100735e-35),
(-3.0045950051756514e-32),

1 个答案:

答案 0 :(得分:0)

小心,Microchip使用自定义浮点格式。

在这里,您可以看到4997,12号码

的两种翻译
0F6h, 028h, 09Ch, 045h    ; IEEE Real       = 4997,12
0F6h, 028h, 01Ch, 08Bh    ; Microchip Real  = 4997,12

enter image description here

有关阅读的更多信息:http://ww1.microchip.com/downloads/en/AppNotes/00575.pdf

编辑:添加x86 asm转换rutines ...

procedure AnsiSingleToMFormat(var Data: single);
asm
  mov       cx,[eax + 2]
  rcl       cl,1
  rcl       ch,1
  rcr       cl,1
  mov       [eax + 2],cx
end;

procedure MFormatToAnsiSingle(var Data: single);
asm
  mov       cx,[eax + 2]
  rcl       cl,1
  rcr       ch,1
  rcr       cl,1
  mov       [eax + 2],cx
end;