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.

PIC 16F676 - problem with data table

Status
Not open for further replies.

pebe

Member
I am writing a program to move a stepping motor in accordance with a variable voltage input to the A/D. The idea is that I 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?
 
Mike. I am converting 0-255 to 0-160, and the conversion values are non linear.

Eng1. Thanks, I'll read up on it.
 
Large lookup tables, particularly in the 16F range with all the paging, can be a real problem.

Suggest you look at the Microchip Forum and search under ' lookup tables ' for some help and to see the example code there.

Microchip Technology User Forums
 
Yes, the problem turned out to be one of page boundaries.

I ended up filtering out the case where ADRESH = 0, and passing that straight to the motor, then decrementing other values to pass to the table for 1-255.

That allowed the 255 RETLW steps and the ADDWF instruction to be contained within the page 200 to 2FF. Problem solved.

Thanks, everyone.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top