18F24k22 LOOK up table not working

Discussion in 'Embedded Systems and Microcontrollers' started by Dodgydave, Aug 3, 2013.

  1. Dodgydave

    Thread Starter AAC Fanatic!

    Jun 22, 2012
    5,001
    745
    I am using these pics 18F24K22 and 18F2321 have got the look up table working ok an a 16f722 no problem.

    Right here is the headache when i call the look up table it always returns with the first line of the look up, it wont add thw W to the PCL, and retlw like it should do.


    Even if i pre-load the W with a value it still returns with the first Retlw

    here is the ASM file which works ok on the 16F722

    Code ( (Unknown Language)):
    1.  
    2.  
    3. Show_Units
    4.     movf UNITS,w
    5.     Call Convert                             
    6.     movwf PORTB
    7.     bcf PORTC,5  ; Units mux
    8.     bsf PORTC,6  ; Tens mux            
    9.     bsf PORTC,7  ; Huns mux
    10.     call Delay
    11.     return
    12.  
    13. Convert
    14.                        ;NOTE CHANGE    ADDWF PCL,F TO W  TO MAKE LOOKUP TABLE WORK ALSO ON 18F2321
    15.     ADDWF PCL,f       ; now get dispaly seven segment bits (0= 0n 1= Off)            
    16.     retlw   b'00000011';0           b7=a                         aaaaa           77777
    17.     retlw   b'10011111';1           b6=b                        f     b         2     6
    18.     retlw   b'00100101';2           b5=c                        f     b         2     6
    19.     retlw   b'00001101';3           b4=d                        f     b         2     6
    20.     retlw   b'10011001';4           b3=e                         ggggg           11111
    21.     retlw   b'01001001';5           b2=f                        e     c         3     5
    22.     retlw   b'01000001';6           b1=g                        e     c         3     5
    23.     retlw   b'00011111';7           b0=dp                       e     c         3     5
    24.     retlw   b'00000001';8                                        ddddd           44444
    25.     retlw   b'00011001';9                                                dp            O
    26.                                                           ; Segment bits         PORT bits  
    27.  
    28.  
    29. ***********************************************************************
    30. and this is the ASM for the new pics  NOTE I HAVE TO CHANGE THE
    31.  ADDWF PCL, F TO  ADDWF PCL ,W
    32.  
    33.  or it wont return at all and crashes??
    34. ****************************************************************************
    35. Show_Units
    36.     movf UNITS,w
    37.     Call Convert                             
    38.     movwf PORTB
    39.     bcf PORTC,5  ; Units mux
    40.     bsf PORTC,6  ; Tens mux            
    41.     bsf PORTC,7  ; Huns mux
    42.     call Delay
    43.     return
    44.  
    45. Convert
    46.                        ;NOTE CHANGE    ADDWF PCL,F TO W  TO MAKE LOOKUP TABLE WORK ALSO ON 18F2321
    47.     ADDWF PCL,w       ; now get dispaly seven segment bits (0= 0n 1= Off)            
    48.     retlw   b'00000011';0           b7=a                         aaaaa           77777
    49.     retlw   b'10011111';1           b6=b                        f     b         2     6
    50.     retlw   b'00100101';2           b5=c                        f     b         2     6
    51.     retlw   b'00001101';3           b4=d                        f     b         2     6
    52.     retlw   b'10011001';4           b3=e                         ggggg           11111
    53.     retlw   b'01001001';5           b2=f                        e     c         3     5
    54.     retlw   b'01000001';6           b1=g                        e     c         3     5
    55.     retlw   b'00011111';7           b0=dp                       e     c         3     5
    56.     retlw   b'00000001';8                                        ddddd           44444
    57.     retlw   b'00011001';9                                                dp            O
    58.                                                           ; Segment bits         PORT bits  
    59.  
    60.  
    61.  
    62. EVEN IF I PUT THIS LINE IN
    63. it crashes and hangs because of   ",F"
    64.  
    65. " movlw  .4"
    66.   addwf pcl,f                                                      
    67.  retlw . xxxxxxxx                                                    
    68.  retlw  .xxxxxxxx                                                
    69.   ETC......                                                          
    70.  
    71.  
    72. OR THIS LINE  ,W just returns with first value of retlw?
    73.  
    74. " movlw  .4"
    75.   addwf pcl,w                                                      
    76.  retlw . xxxxxxxx                                                  
    77.  retlw  .xxxxxxxx                                                      
    78.   ETC......                                                          
    79.  
    80.  
    81.  
    82.  
    WHY WONT IT WORK ON THESE NEW 18F SERIES PICS?
     
    Last edited: Aug 3, 2013
  2. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,692
    2,756
    This is a common mistake made when porting from 16f to 18f silicon. An instruction in the 18f uses 2 memory locations, not 1 like the 16f. All instructions start at an even address. If you wish to make a computed jump, you will need to double the value of WREG before adding to PCL.

    As an alternative, consider this:

    http://forum.allaboutcircuits.com/showthread.php?t=56583

    Even better, to better enjoy your 18f experience, learn to use the tblrd instruction. It'll cut your program memory use in half.
     
    Eric007 likes this.
  3. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,692
    2,756
    Read the post I linked to. The answer is in there.
     
  4. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,692
    2,756
    Are you shouting at me?

    Good luck on your project. My help ends here.
     
    tshuck likes this.
  5. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
  6. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,692
    2,756
    Ha! I could write your code 5 different ways in about 2 minutes.

    I'll speak slowly so you understand:

    You. Are. Being. Rude.

    I refuse to reinforce your attitude by acquiescing to your demands.
     
    Eric007 likes this.
  7. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,566
    2,379
    I have just started using the 18f series, but the 18f has a different memory access capability than the 16f.
    For data memory use the LFSR command, load FSR0/FSR1 etc
    for assigned ROM data tables the new commands are easier to use that the 16f RETLW type once you get the hang of it.
    The new ones are:
    TBLRD*
    TBLRD*+
    TBLRD*-
    etc,

    Code ( (Unknown Language)):
    1.  
    2. ;=============================================================================
    3. ;
    4. ;  
    5. ;
    6. ;=============================================================================
    7. ;    Filename:    PIC18g.asm
    8. ;=============================================================================
    9. ;    Demonstrate Create a table and read data in memory transmits to USART
    10. ;    USART routine not shown
    11. ;    Assembled using MPASMWIN V8.9 PICIT-2
    12. ;=============================================================================
    13. ;    Include Files:    p181220.inc    V1.3
    14. ;=============================================================================
    15. ;    PIC18XXX USART
    16. ;=============================================================================
    17.  
    18.         list p=18f1220        ;list directive to define processor
    19.         #include <p18f1220.inc>    ;processor specific definitions
    20.  
    21. ;   Oscillator Selection:
    22.     config    OSC = INTIO2, FSCM = OFF, IESO = OFF                 ;1H
    23.     config  PWRT = ON, BOR = ON, BORV = 27                         ;2L
    24.     config WDTPS = 1,  WDT = OFF                                ;2H
    25.     config STVR = OFF, LVP = OFF, DEBUG = OFF                    ;4L
    26.     config MCLRE = OFF
    27. ;----------------------------------------------------------------------------
    28. ;Constants
    29.  
    30. SPBRG_VAL    EQU    .25        ;set baud rate 9600 for 4Mhz clock
    31.  
    32. ;----------------------------------------------------------------------------
    33. ;Bit Definitions
    34.  
    35.     GotNewData    EQU    0        ;bit indicates new data received
    36.     Mcount    EQU 0x001
    37.     Ncount    EQU 0X000
    38. ;----------------------------------------------------------------------------
    39. ;Variables
    40.  
    41.         CBLOCK    0x000
    42.         count
    43.         Flags            ;byte to store indicator flags
    44.         ENDC
    45.  
    46. ;----------------------------------------------------------------------------
    47. ;This code executes when a reset occurs.
    48.  
    49.         ORG     0x0000        ;place code at reset vector
    50.  
    51. ResetCode:    bra    Main        ;go to beginning of program
    52.  
    53. ;----------------------------------------------------------------------------
    54. ;This code executes when a high priority interrupt occurs.
    55.  
    56.         ORG    0x0008        ;place code at interrupt vector
    57.  
    58. HiIntCode:    ;do interrupts here
    59.  
    60.         reset            ;error if no valid interrupt so reset
    61.  
    62. ;----------------------------------------------------------------------------
    63. ;This code executes when a low priority interrupt occurs.
    64.  
    65.         ORG    0x0018        ;place code at interrupt vector
    66.  
    67. LoIntCode:    ;do interrupts here
    68.  
    69.         reset            ;error if no valid interrupt so reset
    70.  
    71. ;----------------------------------------------------------------------------
    72.  
    73. Main:
    74.         movlw    0x60
    75.         movwf    OSCCON
    76.         bcf        ADCON0, 0
    77.         setf    ADCON1
    78.         clrf    PORTA
    79.         clrf    LATA
    80.         clrf    TRISA
    81. ;Set up for memory access
    82.         bsf        EECON1, 7
    83.         bsf        EECON1, 6
    84. ;Set upf for table read
    85.         movlw    0x10    ;inilialize counter number of charachters
    86.         movwf    count
    87.         movlw    upper msg_table
    88.         movlw    TBLPTRU
    89.         movlw    high msg_table
    90.         movwf    TBLPTRH
    91.         movlw    low    msg_table
    92.         movwf    TBLPRTL
    93. ;access table
    94.  
    95. Advance:
    96.         TBLRD*+    ;Read increment table pointer
    97.         movf   TABLAT, W
    98.         call     Tx_byte
    99.         decfsz    count
    100.         goto advance
    101. ;-----------------------
    102. ;Done
    103. Circle:
    104.         goto     Circle
    105. ;-----------------------
    106. msg_tbl db    0x0a, 0x0d,'T','e','s','t',' ','M','e','s','s'.'a','g','e'
    107. 0x0a,0x0d
    108. ;----------------------
    109. ;transmit subroutine
    110. Tx_byte:
    111.         btfss    PIR1, TXIF
    112.         goto     Tx_byte
    113.         movwf    TXREG
    114.         return
    115. ;---
    116.         end
    117. ;                                            
    118.  
    119.  
    The message table can go anywhere in memory.
    Max.
     
    Last edited: Aug 3, 2013
  8. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,566
    2,379
    Once you set up a table with the db inst, the TBLRD*+ reads the contents of the first location into the TABLAT register and then increments its counter, (*+), the data is then moved from the reg to the WREG and then you do what ever with the data.
    The next time you do the TBLRD*+ it reads the next location, and so on.
    There are ways to read one particular location if needed.
    This is one way.
    Code ( (Unknown Language)):
    1.  
    2. ; Access fourth table loc:
    3. ;output to PORTB
    4.         movlw    0x03    ;define offset
    5.         bcf     STATUS, C    ;prep for math
    6.         addwfc    TBLPTRL, F    ;table pointer = table pointer +W
    7.         clrf    WREG        ;
    8.         addwfc    TBLPTRH, F
    9.         addwfc    TBLPTRU, F
    10.         tblrd*        ;read: no increment of pointer
    11.         movf    TABLAT, W
    12.         clrf    PORTB    ;clear port
    13.         movwf    PORTB    ;output data to port.
    14. Circle:
    15.         goto    Circle
    16.         end
    17.  
    All you do is add an offset to the TBLPTRL
    This is the basic concept you can improve on it probabally.
    I sent an 18f Ebook via email.

    Max.
     
  9. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,566
    2,379
    Compiled and debugged.
    Does now perform OK,
    Code ( (Unknown Language)):
    1.  
    2. ;=============================================================================
    3. ;
    4. ;  
    5. ;
    6. ;=============================================================================
    7. ;    Filename:    PIC18g.asm
    8. ;=============================================================================
    9. ;    Demonstrate Create a table and read data in memory transmits to USART
    10. ;    USART routine not shown
    11. ;    Assembled using MPASMWIN V8.9 PICIT-2
    12. ;=============================================================================
    13. ;    Include Files:    p18F2221.inc    V1.3
    14. ;=============================================================================
    15. ;    PIC18XXX USART
    16. ;=============================================================================
    17.  
    18.         list p=18f2221        ;list directive to define processor
    19.         #include <p18f2221.inc>    ;processor specific definitions
    20.  
    21. ;   Oscillator Selection:
    22.     config    OSC = INTIO2, FCMEN = OFF, IESO = OFF                 ;1H
    23.     config  PWRT = ON, BOR = ON, BORV = 2                         ;2L
    24.     config WDTPS = 1,  WDT = OFF                                ;2H
    25.     config STVREN = OFF, LVP = OFF, DEBUG = OFF                    ;4L
    26.     config MCLRE = OFF
    27. ;----------------------------------------------------------------------------
    28. ;Constants
    29.  
    30. SPBRG_VAL    EQU    .25        ;set baud rate 9600 for 4Mhz clock
    31.  
    32. ;----------------------------------------------------------------------------
    33. ;Bit Definitions
    34.  
    35. GotNewData    EQU    0        ;bit indicates new data received
    36. Mcount    EQU 0x001
    37. Ncount    EQU 0X000
    38. ;----------------------------------------------------------------------------
    39. ;Variables
    40.  
    41.         CBLOCK    0x000
    42.         UNITS
    43.         count
    44.         Flags            ;byte to store indicator flags
    45.         ENDC
    46.  
    47. ;----------------------------------------------------------------------------
    48. ;This code executes when a reset occurs.
    49.  
    50.         ORG     0x0000        ;place code at reset vector
    51.  
    52. ResetCode:    bra    Main        ;go to beginning of program
    53.  
    54. ;----------------------------------------------------------------------------
    55. ;This code executes when a high priority interrupt occurs.
    56.  
    57.         ORG    0x0008        ;place code at interrupt vector
    58.  
    59. HiIntCode:    ;do interrupts here
    60.  
    61.         reset            ;error if no valid interrupt so reset
    62.  
    63. ;----------------------------------------------------------------------------
    64. ;This code executes when a low priority interrupt occurs.
    65.  
    66.         ORG    0x0018        ;place code at interrupt vector
    67.  
    68. LoIntCode:    ;do interrupts here
    69.  
    70.         reset            ;error if no valid interrupt so reset
    71.  
    72. ;----------------------------------------------------------------------------
    73. Main:
    74.         movlw    0x60
    75.         movwf    OSCCON
    76.         bcf        ADCON0, 0
    77.         setf    ADCON1
    78.         clrf    PORTA
    79.         clrf    LATA
    80.         clrf    TRISA
    81. ;Set up for memory access
    82.         bsf        EECON1, 7
    83.         bsf        EECON1, 6
    84. ;------------------------------------------
    85. ; Rest of code here.....
    86. ;----------------------------------------
    87. ;show units routine called when needed with 0-9 in UNITS.
    88. Show_Units:
    89.         movlw    upper Seven_seg
    90.         movwf    TBLPTRU
    91.         movlw    high Seven_seg
    92.         movwf    TBLPTRH
    93.         movlw    low Seven_seg
    94.         movwf    TBLPTRL
    95.        
    96. Digit:
    97.         movf    UNITS, W    ;UNITS contains 0-9
    98.         bcf     STATUS, C    ;prep for math
    99.         addwfc    TBLPTRL, F    ;table pointer = table pointer +W
    100.         clrf    WREG        ;
    101.         addwfc    TBLPTRH, F
    102.         addwfc    TBLPTRU, F
    103.         tblrd*        ;read: no incrment of pointer
    104.         movf    TABLAT, W
    105.         movwf    PORTB    ;output data to port.
    106.         return
    107. ;access table
    108. ;Store digits in memory
    109. Seven_seg db 0x03,0x9f,0x25,0x0d,0x99,0x49,0x41,0x1f,0x01,0x19
    110.         end
    111.  
    112.  
    Working fine on Sim.
    Max.
     
    Last edited: Aug 4, 2013
  10. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,566
    2,379
    Seven_seg is where the 0-9 digits are stored in memory with the 'db' instruction.
    All you need to do is call the Show_Units routine with 0-9 in UNITS and it should return it in WREG.
    TBLPTRx is the where the address of the look up table Seven_seg, 20 bit (3 byte wide memory locations) memory address is stored using movff.
    Max.
     
    Last edited: Aug 3, 2013
  11. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,566
    2,379
    Its all there.
    Go from Show_units to the very end, and this is the routine, nothing more.
    This should replicate your original code?
    It is a subroutine that needs to be called with the segment you want to display (0-9) pre-loaded in a Variable called UNITS
    The routine I show will also output this segement value to the PORTB, but it is returned in W so you can put it where you want.
    So say you call the routine with 0 (zero) in Units, it should return '00000011' as per your original lookup, from your original look up table there is no indication what you did with the value other than outputting to PORTB?
    Max.
     
    Last edited: Aug 4, 2013
  12. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    Try running it through MPLAB's built in simulator. Makes debugging that sort of thing easy. Any reason you don't want to use the much superior table instructions?
     
  13. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    Yeah, right. For custom jobs I accept payment thru PayPal IN ADVANCE of any work for 50% of the total, remainder upon receipt of work.
     
    absf, djsfantasi, joeyd999 and 3 others like this.
  14. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,692
    2,756
    What's hilarious is that it's still wrong, even though he thinks he got it right.

    Karma is a b****.
     
  15. JohnInTX

    Moderator

    Jun 26, 2012
    2,348
    1,029
    Indeed. I wasn't going to comment on DD's 'solution' because you know, what's the point, but... YEP! Kaboom in 3-2-1..
     
  16. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    If you bothered reading through the link on my earilier post the Table instruction is laid out pretty clearly including an example. It's very easy and doesn't have page limitations the old RETW command has. Again learn to use the simulator and this should be a cakewalk.

    Actually the PIC datasheets are not to tough but can be daunting, a good PIC book on the 18F would be worth a read.
     
Loading...