Continue to Site

# Debugging of INTERRUPT routine in MPLAB SIM

Status
Not open for further replies.

#### BlackOps

##### Full Member level 5
mplab sim interrupts

Hello, i am debugging simple program, which turns on/off led every period of time. eround 2 seconds. and i want to measure the exact delay when the LED is On or Off (by calculation on paper it is 2.031616 seconds)

i am doing it on PIC16F88 chip, and using MPLAB SIM for debugging.

but during debug, the cursor goes to the main program loop after the setup routine, and never jumps to interrupt... So, the debugger doesnt make interrupt event occur, how can i fully debug it with MPLAB SIM?

here is code:
Code:
;***********************************************************************************
;
;
;
;
;***********************************************************************************

#include <p16F88.inc>

__CONFIG	_CONFIG1, _CP_OFF & _CCP1_RB0 & _DEBUG_ON & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_ON & _PWRTE_ON & _WDT_OFF & _INTRC_IO

status_temp
w_temp
count
ENDC

START
goto	SETUP

;***********************************************************************************
; START OF THE INTERRUPT SERVICE ROUTINE
;***********************************************************************************
INTERRUPT
movwf	w_temp
swapf	STATUS, w
movwf	status_temp;

decfsz	count, f
goto	INTEXIT
movlw 	b'10000000'
xorwf 	PORTB,f
movlw 	0x1F
movwf	count

INTEXIT
bcf INTCON, TMR0IF

swapf status_temp, w
movwf STATUS
swapf w_temp, f
swapf w_temp, w

retfie
;***********************************************************************************
; END OF THE INTERRUPT SERVICE ROUTINE
;***********************************************************************************

;***********************************************************************************
; START OF THE SETUP ROUTINE (RUNS ONLY ONCE)
;***********************************************************************************
SETUP
banksel	OPTION_REG
movlw	0xc7
movwf	OPTION_REG

banksel	TRISB
movlw	b'00000000'
movwf	TRISB

banksel PORTB
clrf	PORTB

banksel OSCCON
movlw 	b'01100000' ; should be 4 mhz
movwf 	OSCCON

banksel	TMR0
clrf	TMR0

banksel	INTCON
clrf	INTCON

movlw	0x1f
movwf	count

movlw	0xe0
movwf	INTCON
;***********************************************************************************
; END OF THE SETUP ROUTINE
;***********************************************************************************

;###################################################################################
; MAIN PROGRAM START
;###################################################################################
MAINLOOP
nop
nop
nop
goto MAINLOOP
;###################################################################################
; MAIN PROGRAM END
;###################################################################################

END

#### andre_luis

##### Super Moderator
Staff member
mplab sim logic analyzer

BlackOps,

Debuggers that are not JTAG, don't work with interrupts and assincronous events.

+++

mplab interrupts

#### creative_35

##### Full Member level 1
mplabsim tmr0

what crystel you are using?
if you want a 2.031616 second delay with the above routine, you have to use a crystel of 129.032 Mhz crystel wich is not possible with a pic.
your code will jump to interrupt routine exactly after tmr0 overflow and if you want to watch the exact time there is a stop watch in the debugmenu. open it and run the code. open debugger/settings/animation and set the animation speed as 1 msec so that it will run with an average speed.
Put a breakpoint in interrupt routine and run the code you will get exact time.

#### BlackOps

##### Full Member level 5
mplab sim

well i have remaked code a little, now it works fine.
creative_35 what are you talking about 129Mhz crystal??? why??? i am using just 4Mhz internal oscillator, and i get 2.031616 seconds as i see... because i did the simulation in PROTEUS and used its COUNTER tool, to count how many seconds LED will be on... it showed exactly this result. (the one thing is strange when program simulation began in proteus counter tool already had some little time on it...but when subtracted this time from the one i got after led is on, i got exactly 2.031616 secs)

here is the calculation, correct me if i am wrong:

1. 4mhz Oscillator, after divide by 4 is 1Mhz instruction clock

2. 1Mhz is divided in prescaler by 256, and the frequency fed to TMR0 register is 1Mhz/256 = 0.00390625 Mhz, so the TMR0 increments every 1/0.00390625Mhz = 256us

3. TMR0 has 256 possible values so it will interrupt every 256 * 256us = 65536us

4. count variable in setup routine is assigned value 31 (DEC), so interrupt routine decrements it and turns on/off led for: 31 * 65536us = 2.031616 seconds.

and as u c i used 4mhz crystal...not 129...

and the stopwatch which u was talking about just show me the increment by 1us every instruction cycle, after the setup routine it jumps to infinite looop in the main program loop, so the stop watch increments infinitely..

anyway thanks

Can anyone show or explain to me how to use Logic Analyser in MPLAB SIM, it doesnt seem to work...or show some diagramm? thanks

Added after 4 hours 47 minutes:

well, now i want to add some value to TMR0, to get another value.
for example, now i must get 0.7936 second delay

again comes the calculation:

DELAY = (4 * PRESCALER * (256 - TMR0) * count) / Crystal

if TMR0 will have 156 as initial value, then it will count 100 times. So:

DELAY = (4 * 256 * (256 - 156) * 31) / 4 000 000 = 0.7936 seconds

but when i tested its delay in PROTEUS i got: 0.777883 that is wrong, WHY? have any idea?

i also know that during the write to TMR0 it takes 2 instruction cycles, so i did this both in the SETUP routine and in the INTERRUPT:

movlw .158
movwf TMR0

so, where is the problem?

thanks

and plz can anybody answer to my previous post about Logic Analyzer??

#### shyounessi

##### Newbie level 1
mplab what interrupt

you can change its proper intruppt flag register manully
i think PIR1 register can be modified in View menu and so you can
intruppt program.

#### creative_35

##### Full Member level 1
debug interrupt mplab

BlackOps said:
well i have remaked code a little, now it works fine.
creative_35 what are you talking about 129Mhz crystal??? why??? i am using just 4Mhz internal oscillator, and i get 2.031616 seconds as i see... ??

Yes, you are right. I didn't notice your count variable. I just calculated the time to occur an interrupt. sorry.

#### BlackOps

##### Full Member level 5
mplab sim inerrupt goto start

I still cannot use the Logic Analyser in MPLAB SIM debugger! and cannot find any info on it, help files contain little information about it. did anyone use it? i add channels, then do run. then open logic analyser window, and dont see any graphs! help me plz with this!

well i have changed this program to get EXACT 1 second delay! but it doesnt work correct... here is source code:
Code:
;***********************************************************************************
;
;
;
;
;***********************************************************************************

#include <p16F88.inc>

__CONFIG	_CONFIG1, _CP_OFF & _CCP1_RB0 & _DEBUG_ON & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_ON & _PWRTE_ON & _WDT_OFF & _INTRC_IO

status_temp
w_temp
count
ENDC

START
goto	SETUP

;***********************************************************************************
; START OF THE INTERRUPT SERVICE ROUTINE
;***********************************************************************************
INTERRUPT

movwf	w_temp
swapf	STATUS, w
movwf	status_temp;

decfsz	count, f
goto	INTEXIT
movlw 	b'10000000'
xorwf 	PORTB,f
movlw 	.5000
movwf	count
movlw	.158
movwf	TMR0

INTEXIT

bcf INTCON, TMR0IF

swapf status_temp, w
movwf STATUS
swapf w_temp, f
swapf w_temp, w

retfie
;***********************************************************************************
; END OF THE INTERRUPT SERVICE ROUTINE
;***********************************************************************************

;***********************************************************************************
; START OF THE SETUP ROUTINE (RUNS ONLY ONCE)
;***********************************************************************************
SETUP
banksel	OPTION_REG
;	movlw	0xc7
movlw	0xc0
movwf	OPTION_REG

banksel	TRISB
movlw	b'00000000'
movwf	TRISB

banksel PORTB
clrf	PORTB

banksel OSCCON
movlw 	b'01100000' ; should be 4 mhz
movwf 	OSCCON

banksel	INTCON
clrf	INTCON

movlw	.5000
movwf	count

movlw	0xe0
movwf	INTCON

movlw	.158
movwf	TMR0
;***********************************************************************************
; END OF THE SETUP ROUTINE
;***********************************************************************************

;###################################################################################
; MAIN PROGRAM START
;###################################################################################
MAINLOOP
nop
nop
nop
goto MAINLOOP
;###################################################################################
; MAIN PROGRAM END
;###################################################################################

END

from calculation i must get:

1 second = (4 * 2 * 100 * 5000) / 4 000 000

but program doesnt work normal....i dont get 1sec delay, what is problem?