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.

  • \$\begingroup\$ 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. \$\endgroup\$ – Kortuk Jul 17 '10 at 17:13
  • \$\begingroup\$ @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. \$\endgroup\$ – Olin Lathrop Oct 31 '12 at 13:41
  • \$\begingroup\$ @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. \$\endgroup\$ – Kortuk Oct 31 '12 at 15:18
  • \$\begingroup\$ @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. \$\endgroup\$ – Olin Lathrop Oct 31 '12 at 15:57
  • \$\begingroup\$ @OlinLathrop Well I would appreciate a thorough answer here, I appreciate the information. \$\endgroup\$ – Kortuk Oct 31 '12 at 16:26

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


If you want a PIC16F628A to do something like

char vals = {22, 10, 34, 16};
int main(void){
    char id = 0;
        PORTB = vals[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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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