Program Counter, Stack

Program Counter

“The program counter (PC) specifies the address of the instruction to fetch for execution.” (direct quote from the data sheet 😉 ) The Program Counter is responsible to execute instructions according to the order of the program you compiled. Program execution starts from the line 00h. As you already expect “goto” instruction and the interrupts changes the normal order of execution. These will force the program counter to jump to the specified line. The SFR’s related are PCL and PCLATH.

You can use the org directive to specify where the program starts and to specify specific lines.

Normally you put,

org    00

At the beginning of the program.

The Stack

All 16 series PIC microcontrollers have 8 level stack. It’s some sort of memory that remembers where were you before. It can remember up to 8 levels.

You can call a subroutine inside a subroutine. “The stack allows a combination of up to 8 program calls and interrupts to occur. The stack contains the return address from this branch in program execution.” which means you can call a nested loop (subroutines) of maximum 8 levels. “After the stack has been PUSHed eight times, the ninth push overwrites the value that was stored from the first push. The tenth push overwrites the second push (and so on).” Now you already lost the first and the second locations. So when programming you must keep in mind how many levels you have already created in a subroutine. Otherwise this can lead to a non responsive program which does nothing but keeps executing last parts of the nested loop created by the programmer. In high level languages, recursive functions can cause stack overflows. You may need to change the structure of your program when you encounter one.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: