# PIC12F1822 program counter jumping around during debug and release programming

I am having the most awful trouble getting things up and running with my PIC12F1822.

I'm using MPLAB 8.86, an ICD3 and have the chip, in a DIP package set up in a breadboard on my desk. As far as I can see, the connections are all reliable and okay. Things were working okay until yesterday evening and I've not moved/touched anything.

The schematic looks like this:

Before I write any code which does any input based on switches or gets data from serial, I just want to twiddle some LEDs so I know everything is working.

Here's my code :

 list           p=12F1822      ; list directive to define processor
#include <p12F1822.inc> ; processor specific variable definitions
__CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
__CONFIG _CONFIG2, _WRT_OFF & _PLLEN_OFF & _STVREN_OFF & _BORV_19 & _LVP_OFF

#DEFINE        LED1    PORTA,.2        ;Status LED connected to Port A bit 1

D1      EQU    0x7D    ;temp register, used during delay
D2      EQU    0x7E     ;temp register, used during delay
D3      EQU    0x7F
ORG     0x0000            ; processor reset vector
GOTO    START
;----------------------------------------------------------------------------;
;DELAY ROUTINE, FORCES THE PIC TO WASTE TIME FOR 0.1Seconds AT 16MHz;
DELAY
MOVLW  0X35
MOVWF  D1
MOVLW  0XE0
MOVWF  D2
MOVLW  0X01
MOVWF  D3
DELAY_0
DECFSZ D1, F
GOTO   $+2 DECFSZ D2, F GOTO$+2
DECFSZ D3, F
GOTO   DELAY_0
;4 CYCLES
GOTO   $+1 GOTO$+1
;4 CYCLES (INCLUDING CALL)
RETURN

;------------------------------------------
START
;DEVICE CONFIG STUFF
BANKSEL        TRISA
MOVLW  B'00000000'     ;EVERYTHING ELSE IS OUTPUT
MOVWF  TRISA
BANKSEL        OSCCON
MOVLW  B'01111010'     ;PLL DISABLED, 16MHZ CLOCK SPEED, INTERNAL CLOCK
MOVWF  OSCCON
BANKSEL        ANSELA
CLRF   ANSELA          ;ALL PINS AS DIGITAL
MOVLW  B'00001111'
MOVWF  ADCON1          ;ALSO TURNS OFF ANALOG THINGS
BANKSEL        CM1CON0 CLRF    CM1CON0         ;ALL COMPARATORS OFF
BANKSEL        PORTA
CLRF   PORTA           ;CLEAR EVERYTHING ON PORT A BEFORE WE START, EXIT WITH BANK0 SELECTED
LOOP
BCF    LED1
CALL   DELAY
BSF    LED1
CALL   DELAY
GOTO   LOOP
END


Nothing too exciting.

When I make this and then program it in in release mode I just get one LED stuck on.

When I make this in debug mode and then try to step through with the debugger/ICD3/MPLAB - I can't reliably get the code to run.

Specifically, the program counter seems to be jumping around all over the place.

To my understanding, when you reset the MCU using the yellow button in MPLAB, the program should go to the top and then wait for you to either run or step through - like this:

The location of the PC is indicated by the green arrow - mine keeps showing up in places which are not the start! It shows up in all manner of places e.g.

This makes debugging a pain - and - indicates that something is really wrong.

I have no idea what - has anyone else come across anything like this?

It isn't limited to this code snippet either. In [This Example] after a reset the PC jumps in at BANKSEL OSCCON (line 138) and when running seems get get stuck inside AUXHELLOWORLD (i.e. the LED keeps flashing forever).

Any insight you might have would be most appreciated Best D

• Have you tried removing the resistor and LED attached to PGC ? – Rocketmagnet Aug 10 '12 at 14:16
• yes, to no avail – mynameisdev Aug 10 '12 at 19:33

I used an assembly template located here from Bill Howl on this website and wrapped your delay routine around it.

I payed particular attention to the port initialization routine example given on the microchip datasheet pg 124 Example 12-1 Initializing port A. The BANKSEL PORTA just before the delay routine and port toggling kicks off is crucial! Doesn't appear to have the problem your are experiencing with the program counter. Simulated for 806 seconds... And no issues. Sometimes it helps to clean the project code up by clicking project, clean.

Give this source code a try

Your second piece of code is causing stack overflow errors
CORE-E0001: Stack over flow error occurred from instruction at 0x000037
CORE-E0001: Stack over flow error occurred from instruction at 0x000037
CORE-E0001: Stack over flow error occurred from instruction at 0x000037
CORE-E0001: Stack over flow error occurred from instruction at 0x000038

This is because you are missing the return statement in the AUXHELLOWORLD block.
When your code starts it would cause an infinite never-ending loop of these two calls (initialise and auxhelloworld) each time adding to the stack and never getting popped back. Eventually the stack will be overwhelmed and lose track of where it is supposed to return causing strange code behaviour and program counter to jump about.

START
CALL INITIALISE
CALL AUXHELLOWORLD

Try the template! Really you should learn c code and download the microchip or Hitech c compiler. It will make your life much easier... You won't go back to assembly!
Furthermore you can also incorporate assembly in c code, if you really needed the specialised code.

I was able to compile and simulate this code without issues in MPLAB v8.84, with no program counter jumpiness, in both debug and release mode. I can see the port A bit toggling just fine with MPLAB sim.

Did you try toggling LATA instead of PORTA?

Did you specify MPASM to build absolute code?

This program for the 12F1822 which also flashes an LED connected to RA2 works OK on a PCB of mine:

;flasher.asm
;simple program for PIC12F1822 to flash LED on RA2 (pin 5)

list      p=12f1822 ;list directive to define processor
#include "p12f1822.inc" ;processor specific variable definitions

errorlevel -302

__CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON & _BOREN_OFF
__CONFIG _CONFIG2, _LVP_OFF & _PLLEN_ON

;-----------------------------------------------------------------
;defines
;-----------------------------------------------------------------

#define LED LATA2   ; (pin 5)

;-----------------------------------------------------------------
;variables
;-----------------------------------------------------------------
cblock      0x70        ;same bank as LATA
Counter1
Counter2
endc

;------------------------------------------------------------------
;initialisation
;------------------------------------------------------------------

org 0

banksel OSCCON
movlw   b'01110000'     ;HFINTOSC (32 MHz with PLL enabled)
movwf   OSCCON
banksel OSCSTAT
goto    $-1 btfss OSCSTAT,PLLR ;PLL ready? goto$-1
banksel TRISA
bcf     TRISA,LED       ;LED output
banksel LATA

;-----------------------------------------------------------------------
;main program loop
;-----------------------------------------------------------------------

loop:
bsf     LATA,LED    ;LED off
call    dly
bcf     LATA,LED    ;LED on
call    dly
goto    loop

dly:
movlw   0xFF
movwf   Counter1
dly1:
movlw   0xFF
movwf   Counter2
dly2:
decfsz  Counter2,F
goto    dly2
decfsz  Counter1,F
goto    dly1
return

end