Shop OBEX P1 Docs P2 Docs Learn Events
Displaying 3-digit Decimal numbers on LCD from 2 digit Hex — Parallax Forums

Displaying 3-digit Decimal numbers on LCD from 2 digit Hex

TonyTTonyT Posts: 2
edited 2005-09-09 15:03 in BASIC Stamp
Crew:
I am strugling with converting hex FF to a 3-place decimal number to display on a serial LCD using 74HC595 chip. I had some help mastering the 2-digit conversion and properly displaying on the LCD, but the hundreds place is becoming a challenge.

My setup for motor control:

* BS2Pe
* MEMKEY input
* MMB Motor controller
* LCD 2x16 with serial 74HC595 input

Here is a sample code to set the speed and properly display on the LCD the results:

**********************************
Main:
Gosub Check_Key
Gosub LCD_DISPLAY
GOTO MAIN

Check_Key:
KEY_IN = $0
CLOCK_SET = $0
PAUSE 250
KEY_IN = IN3
IF KEY_IN=1 THEN GET_MEMKEY
RETURN

Get_MEMKEY:
SEROUT MEMKEY,Baud24,[noparse][[/noparse]$00] 'Get key number
SERIN MEMKEY,Baud24,15000,MEM_COM,[noparse][[/noparse]MEM_BUFFER]
GOSUB MEMKEY_LOOKUP_Table
KEY_IN = $0
CLOCK_SET = $1
RETURN

MEMKEY_LOOKUP_Table: '(transforms the data correctly from the MEMKEY)
LOOKDOWN MEM_BUFFER,[noparse][[/noparse]$00,$01,$02,$03,$04,$05,$06,$07,$08,$09,$0A,$0B,$0C,$0D,$0E,$0F],MEM_BUFFER
LOOKUP MEM_BUFFER,[noparse][[/noparse]$01,$02,$03,$0A,$04,$05,$06,$0B,$07,$08,$09,$0C,$0D,$00,$0E,$0F],MEM_BUFFER
RETURN

Load_Hundreds_Digit:
MEM_Hundred = MEM_BUFFER.LOWNIB
MEM_BUFFER = $00
RETURN

Load_Tens_Digit:
MEM_Convert.HIGHNIB = MEM_BUFFER.LOWNIB
MEM_BUFFER = $00
RETURN

Load_Ones_Digit:
MEM_Convert.LOWNIB = MEM_BUFFER.LOWNIB
MEM_BUFFER = $00
Response=$00
RETURN

Store_Key_Entry_Hundreds: 'need to add time out
GOSUB Check_Key 'Check and load high nib
IF CLOCK_SET = $1 THEN Load_Hundreds_Digit
GOTO Store_Key_Entry_Hundreds
PAUSE 100
RETURN

Store_Key_Entry_Tens: 'Need to add time out
GOSUB Check_Key 'Check and load high nib
IF CLOCK_SET = $1 THEN Load_Tens_Digit
GOTO Store_Key_Entry_Tens 'was _High
PAUSE 100
RETURN

Store_Key_Entry_Ones: 'was Low
FOR Response = 30 TO 0 '30 second Protection loop if no entry
GOSUB Check_Key 'Check and load low nib
IF CLOCK_SET = $1 THEN Load_Ones_Digit
NEXT
RETURN

Enter_Keypad:
GOSUB Store_Key_Entry_Tens
GOSUB Store_Key_Entry_Ones
GOSUB MEM_BCD_Conversion 'Convert the HEX2 input to 2-place Binary Coded Dec
RETURN

Enter_Keypad_Hundreds
GOSUB Store_Key_Entry_Hundreds
GOSUB Store_Key_Entry_Tens
GOSUB Store_Key_Entry_Ones
GOSUB MEM_Hundred_Conversion 'Convert the HEX3 input to 3-place Binary Coded Dec
RETURN

MEM_BCD_Conversion:
MEM_Buffer = (MEM_Convert.HIGHNIB * 10) + MEM_Convert.LOWNIB 'Convert to BCD
RETURN

MEM_Hundred_Conversion:
MEM_Buffer = (MEM_Hundred *100) + (MEM_Convert.HIGHNIB * 10) + MEM_Convert.LOWNIB 'Convert to 3 Place BCD
RETURN

>> other code here>>

LCD_DISPLAY:
GOSUB LCD_Clr_Display 'added for LCD display
addr = MMB_Speed 'Print Speed on LCD (000 to 255 Dec, $00-$FF Hex)
GOSUB LCDprint 'print it
GOSUB LCD_Shift_L2 'Set LCD for Line 2
addr = MMB_Digits 'Displays "001-255:" on LCD
GOSUB LCDprint 'print it
GOSUB Enter_Keypad_Hundreds 'Convert the HEX2 input to 3-place Binary Coded Dec (?)
Speed = MEM_Buffer 'loads Speed buffer
tempB = Speed 'tempB for LCD display
GOSUB LCD_Shift_L1_13 'Go display 3-digit "Speed" on LCD
RETURN

LCDnum2:
READ Digits + (tempB / 10), char
GOSUB LCDputc
READ Digits + (tempB // 10), char
GOSUB LCDputc
RETURN

LCDnumHEX:
READ Digits + (tempB / 16), char
GOSUB LCDputc
READ Digits + (tempB // 16), char
GOSUB LCDputc
RETURN

*****************
Obviously this is only a few chuncks of the code, but the issue is when I send the data to the display, it comes out as 2-digit hex instead of 3 digit decimal. For the LCDnum2: routine, how do I create a LCDnum3 (?) I tried the following & didnt work:

>>>>>>>>>>>>>>
LCDnum3:
READ Digits + (tempB / 100), char
GOSUB LCDputc
READ Digits + (tempB / 10), char
GOSUB LCDputc
READ Digits + (tempB // 10), char
GOSUB LCDputc
RETURN
>>>>>>>>>>>>>>

Note: The "GOSUB LCDputc" routine is the shift through the the 74HC595

The objective is to display $FF is as " 255 "

Any ideas?

Tony Thorne
tthorne@camano.net

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-09-09 15:03
    It's not too difficult.· Use the dig operator to get the digit value of each position:

    · huns = theValue DIG 2
    · tens = theValue DIG 1
    · ones = theValue DIG 0

    Use those values with LOOKUP (or READ with a DATA table) to get the 7-segment pattern for the display.· Then use SHIFTOUT to push the digits out.· Now, if you want to get fancy and blank leading zeros you can do this: Add an 11th element to your LOOKUP or DATA table that has all bits off.· Then do this:

    · IF (theValue < 100) THEN
    ··· huns = 10
    ··· IF (theValue < 10) THEN
    ···· ·tens = 10
    ··· ENDIF
    · ENDIF

    The 10 index (11th item in table)·will cause the blank LEDs pattern to be loaded.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
Sign In or Register to comment.