PIC 16F676 - problem with data table

Discussion in 'Embedded Systems and Microcontrollers' started by pebe, May 16, 2009.

  1. pebe

    Thread Starter AAC Fanatic!

    Oct 11, 2004
    I am writing a program to move a stepping motor in accordance with a variable voltage input to the A/D. The idea is to use a table to convert the 0-255 from the high byte of the A/D, to the number of steps that the motor needs.

    I am using a pot giving 0v to Vdd into the A/D for testing. Without the conversion table, the program works perfectly.

    But I am having a problem with the data table – something I haven’t used before. I have given it the label ‘table’ and located it at the end of memory at address 2F0h as recommended in a tutorial I read. The table was called from address 059h.

    The program worked for the first couple of degrees rotation of the pot. Advancing the pot any further crashed the program (it seemed that the stack got corrupted and somehow diverted operation into a different part of the program).

    I checked everything and eventually I decided to move the table to address 07Ah, between the end of the main program and the subs. There, it was OK up to 50% pot rotation. Further rotation of the pot crashed the program.

    I have now moved the table toward the start of the program at 013h. That has moved the main program down so the sub is called from 161h. Now it works happily up to within a couple of degrees of the top, then crashes again.

    So it seems that the position of the table in memory has something to do with the problem. These are snippets of the program I used:

    movfw ADRESH ‘get high byte from A/D
    call table ‘get conversion
    movwf ADnew ‘enter new table value

    table ‘sub
    addwf PCL,1 ‘add A/D value to PC
    retlw .0
    retlw .1
    retlw .1
    retlw .2
    etc. for total of 256 retlw instructions

    If I miss out the 'call' instruction, the program works fine. Where have I gone wrong, or what have I missed?
  2. DonQ

    AAC Fanatic!

    May 6, 2009
    I don't usually work with assembly in PICs, but I think you may be forgetting about setting the bank bits before you use instructions that cross the bank boundaries. This explanation works to explain most of the symptoms you describe. In most "C" compilers, this is handles for you so my expertise with this is somewhat limited.

    Are you aware of the banked memory in the PICs?
  3. pebe

    Thread Starter AAC Fanatic!

    Oct 11, 2004
    Yes, it turned out to be a question of crossing page boundaries.

    I filtered out the case where ADRESH = 0, and passed that straight to the motor. Then decremented remaining values giving a table of 1 to 255.

    That allowed me to get the table and the ADDLW insruction on a single page. That solved the problem

    Thanks for your help.