Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

HELP - PIC18 hex to ascii

Status
Not open for further replies.

kkjoker

New Member
I want to write a program to convert the hex to ascii by using mplab.
eg. convert 0xAF to 0x41 and 0x46 (convert number in file register 0x40+i to ascii and save it in 2 file registers- 0x50+2i, 0x50+2i+1, where i= 0,1,2,3,4,.....)
Please HELP! THANKS!

Now, I only know how to convert 0x0? (from 0x40+i to 0x50+i) with this code and how can I change it to satisfy my wants>
CODE as following:

Main: movlw 0x1 ; Initialize array starting from 0x040
movwf 0x40, A
movlw 0x4
movwf 0x41, A
movlw 0xA
movwf 0x42, A
movlw 0xF
movwf 0x43, A
lfsr 0, 0x040
lfsr 1, 0x050
movlw d'4'
movwf COUNT,A

MainLoop: movff POSTINC0, InputConvertHexToASCII
call ConvertHexToASCII
movwf POSTINC1
decfsz COUNT, F, A
bra MainLoop
bra $

ConvertHexToASCII: movlw upper ASCIIList
movwf TBLPTRU
movlw high ASCIIList
movwf TBLPTRH
movlw low ASCIIList
movwf TBLPTRL
movf InputConvertHexToASCII, W, A
addlw 0x01
movwf COUNTSub, A
Loop: tblrd*+
decf COUNTSub, F, A
bnz Loop
movf TABLAT, W ;Result in WREG

return


org 0x000200
ASCIIList db 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46

end
 
Hi kk welcome, got it wrong first post had to clear ,, This is for PIC16 but will get you started.
Edit
Code:
;----------------------------------------------
;       H2A JUMP TABLE

DECODE    MOVF    TEMP,W    ; GET BYTE HEX TO ASCII
    ADDWF    PCL,1    ; JUMP TO CODE
    RETLW    0x30    ; '0'
    RETLW    0x31    ; '1'
    RETLW    0x32    ; '2'
    RETLW    0x33    ; '3'
    RETLW    0x34    ; '4'
    RETLW    0x35    ; '5'
    RETLW    0x36    ; '6'
    RETLW    0x37    ; '7'
    RETLW    0x38    ; '8'
    RETLW    0x39    ; '9'
    RETLW    0x41    ; 'A'
    RETLW    0x42    ; 'B'
    RETLW    0x43    ; 'C'
    RETLW    0x44    ; 'D'
    RETLW    0x45    ; 'E'
    RETLW    0x46    ; 'F'

;------------------------------------------------
; SUB H2A
;------------------------------------------------
H2A    MOVF    TESTD,W    ; GET HEX BYTE  
    MOVWF    TEMP    ; SAVE   
    RRF    TEMP,1  ; > SHIFT
    RRF    TEMP,1  ; > SHIFT
    RRF    TEMP,1  ; > SHIFT
    RRF    TEMP,1  ; > SHIFT
    MOVLW    0x0F    ; 
    ANDWF    TEMP,1    ; TRIM 0x
    CALL    DECODE    ;
    MOVWF    ASCIH    ; ASCII HIGH BYTE
    MOVF    TESTD,W    ; GET BYTE
    MOVWF    TEMP    ; SAVE   
    MOVLW    0x0F    ; 
    ANDWF    TEMP,1    ; TRIM 0x
    CALL    DECODE    ;
    MOVWF    ASCIL    ; ASCII LOW BYTE
    RETURN
;---------------------------------------------------
[|code]
 
Last edited:
Here is a version for the PIC16 instruction set. It is very similar to the PIC18 instruction set so not much will need changing. I don't think I wrote this code myself. I think I saw it used by someone else and I used it.

Code:
;***************************************************************************** 
;
;  Function :  output_hexbyte
;  Outputs the byte in tmpData to LCD
; (tmpData not changed in this routine ) ?
;  Input:  data in tmpData
;
;  Output:  Data displayed
;
;***************************************************************************** 
output_hexbyte
  swapf  tmpData,W
  sublw  0x09
  swapf  tmpData,W
  andlw  0x0F
  btfss  STATUS,DC
  addlw  'A' - .10 - '0'
  addlw  '0'
  call  DRV_LCD ;This is sending the ASCII character to the LCD
  movfw  tmpData
  sublw  0x09
  movfw  tmpData
  andlw  0x0F
  btfss  STATUS,DC
  addlw  'A' - .10 - '0'
  addlw  '0'
  call  DRV_LCD ;This is sending the ASCII character to the LCD
  return

Les.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top