Jump to content

Displaying 7bit signed value


jackchaos

Recommended Posts

I'm using MIOS_AIN_Pin7bitGet() to get an unsigned 7 bit value (0-127) from my pots.

Some of the pots need to handle parameters as signed values -63 to +63 where center is 0.

I'm able to transmit the correct values over MIDI by:


parameter += 0x40;

parameter &= 0x7f;

However, I'm having trouble displaying the values correctly on the LCD.

Positive values are showing up fine, but as I turn into the negative range, the value is displayed as if it were unsigned (because 'parameter' is unsigned).

How can I display an unsigned char as a signed 7bit value?

Also, the MIOS_LCD_PrintBCD2 functions are not printing leading zeros eg " 6" instead of "06".

Can anyone suggest a straight forward way of printing the zero?

Regards

Edited by jackchaos
Link to comment
Share on other sites


; --------------------------------------------------------------------------
;; print dec value -64..63
CS_MENU_PRINT_PMDec7
movwf TMP1
movlw ' ' ; space or "-"?
btfss TMP1, 6; (if value <= 0x3f (6th bit cleared), print "-"
movlw '-'
call MIOS_LCD_PrintChar

CS_MENU_PRINT_PMDec7_WO_Sign
movf TMP1, W ; calc: 0x40-value
sublw 0x40
btfsc WREG, 7 ; negate value if negative to get absolute value
negf WREG, ACCESS
goto MIOS_LCD_PrintBCD2 ; and print it
[/code] The same in C:
[code]
if( value < 64 ) {
MIOS_LCD_PrintChar('-');
MIOS_LCD_PrintBCD2(64-value);
} else {
MIOS_LCD_PrintChar(' ');
MIOS_LCD_PrintBCD2(value-64);
}
The same for MIOS32:

MIOS32_LCD_PrintFormattedString("%3d", (int)value - 64);
[/code]
[code]
; --------------------------------------------------------------------------
;; print dec value patted with 0
CS_MENU_PRINT_Dec000
clrf MIOS_PARAMETER1
call MIOS_HLP_Dec2BCD
movf MIOS_PARAMETER2, W
call MIOS_LCD_PrintHex1
movf MIOS_PARAMETER1, W
goto MIOS_LCD_PrintHex2
The same in C:

MIOS_HLP_Dec2BCD(value);
MIOS_LCD_PrintHex1(MIOS_PARAMETER2);
MIOS_LCD_PrintHex2(MIOS_PARAMETER1);
[/code] The same for MIOS32:
[code]
MIOS32_LCD_PrintFormattedString("%03d", value);

Best Regards, Thorsten.

Link to comment
Share on other sites

I'm a bit lost on the second example, but I'll take your word for it. I'll try it tonite when I get home.

The details are explained here:

http://www.ucapps.de/cmios8_fun.html#MIOS_HLP_Dec2BCD

MIOS_LCD_BCD* uses MIOS_HLP_Dec2BCD internally, and removes the leading zeroes by spaces.

If you want to output leading zeroes, just print the original BCD code (binary coded digits, each digit is coded in 4 bits) with MIOS_LCD_PrintHex2 (to print the rightmost binary coded digits) resp. MIOS_LCD_PrintHex1 (to print a single (the leftmost) binary digit)

I changed the C example code (removed the bitshifting), so that it matches with the example given in the documentation.

Best Regards, Thorsten.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...