Electronic – PIC12F1822 program counter jumping around during debug and release programming

assemblydebuggingpic

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:

Test circuit design

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  
         BANKSEL        ADCON1  
         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  
         ;BLINK THAT LED LIKE YOUR LIFE DEPENDED ON IT  
         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:

One

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.

Two

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

Best Answer

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. enter image description here
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.