Take the 2-minute tour ×
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It's 100% free, no registration required.

On my 16F628A's I want to iterate of a predefined sequence of values. How do I do this?

In C, I would do something like this:

char vals = {22, 10, 34, 16};

char id = 0;

port = vals[id]; id++

It's the 'vals[id]' that I haven't figured out.

share|improve this question
    
I do not have time for a little to look this up, but I believe the reason they started their own C compiler support with the PIC18 is because they are the first pic to support indirect addressing and a number of other features that C likes to use. –  Kortuk Jul 17 '10 at 17:13
    
@Kortuk: Sorry, that's just wrong. All PICs have indirect addressing capability. Even the smallest have a single FSR register. The PIC 18 has three FSRs, the enhanced PIC 16 two, and any register can be used as a pointer in the 16 bit parts. –  Olin Lathrop Oct 31 '12 at 13:41
    
@OlinLathrop then I was told incorrectly by someone, I can say I never verified that myself on the PIC16. I thought the PIC18 had full functional support. I had been told on the PIC16 you had to load the address in one register and pull the value from another, making pointers quite inefficient beyond using one/two. –  Kortuk Oct 31 '12 at 15:18
    
@Kortuk: Putting the address in one register and then reading another is how indirect addressing works on all the 8 bit PICs, including the PIC 18. The difference is that the PIC 18 has three such indirect pointers with some pre/post inc/dec capability. The enhances PIC 16 has two indirect pointers with full pre/post inc/dec capability, and the older PIC 16 and PIC 10 and 12 have a single pointer with no auto inc/dec capability. –  Olin Lathrop Oct 31 '12 at 15:57
    
@OlinLathrop Well I would appreciate a thorough answer here, I appreciate the information. –  Kortuk Oct 31 '12 at 16:26
show 1 more comment

2 Answers 2

up vote 7 down vote accepted

There are no indirect memory access instructions on midrange pics, instead you put the address of the memory location you want to access into the FSR register. And then the value is accessible at the INDF register.

See section 4.4 of the datasheet for your chip. http://ww1.microchip.com/downloads/en/devicedoc/40044f.pdf

share|improve this answer
    
It was right there under my nose. Thank you –  mmccoo Jul 17 '10 at 19:21
add comment

If you want a PIC16F628A to do something like

char vals = {22, 10, 34, 16};
int main(void){
    char id = 0;
    while(1){
        PORTB = vals[id];
        id++;
        id = id & 3;
    };
}

one option is to compile your C code with a C compiler. (When compiling for such chips, the compiler will use the FSR and INDF registers exactly as John Burton explained). My understanding is that SDCC is the only open-source C compiler that supports the PIC16F628A and other "14-bit" PICmicro chips.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.