Are there simulation problems in MPLab with delays?

Ronald Cotton says:

What is wrong with the following code? It is supposed created a 2 sec delay; however, when I simulate the code in MPLAB version 5.2, the code causes the program to lock up around 65ms or so.

I can successfully generate a 0.5 second delay by changing PostScaler value to 4 instead of 16.

When I program the 2 second delay program into the PIC, the LEDs that I have to PORTB stay on; however, when I program the 0.5 second delay into the chip, the LEDs on PORTB blink every 0.5 seconds.




;**********************************************************************
;   This file is a basic code template for assembly code generation   *
;   on the PICmicro PIC16C74A. This file contains the basic code      *
;   building blocks to build upon.                                    *  
;                                                                     *
;   If interrupts are not used all code presented between the ORG     *
;   0x004 directive and the label main can be removed. In addition    *
;   the variable assignments for 'w_temp' and 'status_temp' can       *
;   be removed.                                                       *                        
;                                                                     *
;   Refer to the MPASM User's Guide for additional information on     *
;   features of the assembler (Document DS33014).                     *
;                                                                     *
;   Refer to the respective PICmicro data sheet for additional        *
;   information on the instruction set.                               *
;                                                                     *
;   Template file assembled with MPLAB V3.99.18 and MPASM V2.15.06.   *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Filename:      4720Lab2.asm                                      *
;    Date: 01/24/2001                                                 *
;    File Version:    1                                               *
;                                                                     *
;    Author: Ronald J Cotton                                          *
;    Company:                                                          *
;                                                                     * 
;                                                                     *
;**********************************************************************
;                                                                     *
;    Files required:                                                  *
;                                                                     *
;                                                                     *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Notes:                                                           *
;                                                                     *
;                                                                     *
;                                                                     *
;                                                                     *
;**********************************************************************


        list      p=16c74b            ; list directive to define processor
        #include <p16c74b.inc>        ; processor specific variable definitions

        __CONFIG   _CP_OFF & _WDT_ON & _BODEN_ON & _PWRTE_ON & _XT_OSC

; '__CONFIG' directive is used to embed configuration data within .asm file.
; The lables following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word.





;***** VARIABLE DEFINITIONS
w_temp          EQU     0x20            ; variable used for context saving 
status_temp     EQU     0x21            ; variable used for context saving
Channel         EQU     0X22            ; Channel Variable
Counter         EQU     0X23            ; Counter
DelayValue      EQU     0x24            ; Delay
Info            EQU     0x25
DelayTemp       EQU     0x26            ; variable used for context saving

        cblock          0x27            ; Beginning unused RAM

                AdValue:8               ;
        endc








;**********************************************************************
                ORG     0x000                   ; processor reset vector
                clrf    PCLATH                  ; ensure page bits are claared
                goto    main                    ; go to beginning of program


                ORG     0x004                   ; interrupt vector location
                movwf   w_temp                  ; save off current W register contents
                movf    STATUS,w                ; move status register into W register
                bcf     STATUS,RP0              ; ensure file register bank set to 0
                movwf   status_temp             ; save off contents of STATUS register
                movf    DelayValue,w            ; move status register into W register
                movwf   DelayTemp               ; save off current DelayValue contents
        

        



                btfsc   PIR1,TMR2IF             ; Handler for Timer2
                call    IntTimer2               ; Interrupt Handler for Timer2
                
                bcf     STATUS,RP0              ; ensure file register bank set to 0
                movf    status_temp,w           ; retrieve copy of STATUS register
                movwf   STATUS                  ; restore pre-isr STATUS register contents
                movf    DelayTemp,W             ; retrieve copy of delay register
                movwf   DelayValue              ; restore pre-isr DelayValue register
                swapf   w_temp,f
                swapf   w_temp,w                ; restore pre-isr W register contents
                retfie                          ; return from interrupt
IntTimer2:              
                bcf     PIR1,TMR2IF             ; Must Clear Int flag for Timer2
                decf    Counter,F               ; Decrease Counter
                return



main:
                bcf     STATUS,RP0              ; 
                movlw   0x31                    ; 
                movwf   Counter                 ;

                call    InitTimer2              ;

                movlw   0xc0                    ;
                movwf   INTCON                  ;

                movlw   0x4A                    ; Prescaler=16, PostScaler=16
                movwf   T2CON                   ; Since OSC = 4Mhz timer = 1uS
                bsf     T2CON,TMR2ON            ; Start Timer

MainLoop:
                call    FiveSec
                call    LED
                goto    MainLoop

;*********************************************************************************************
;*      Everything in this section represents either initialization code or                  *
;*      helper functions.                                                                    *
;*                                                                                           *
;*                      Ronald J.Cotton                                                      *
;*                      02/02/2001                                                           *
;*********************************************************************************************          


FiveSec:
                btfss   Counter,7               ;
                goto    FiveSec
                movlw   0x32                    ;
                addwf   Counter,F               ; Put Counter back to 50
                return


LED:
                comf    PORTB                   ; Toggle PORTB
                return

InitTimer2:
                bcf     STATUS,RP0              ; Ensure Reg Bank 0
                clrf    T2CON                   ; Stop Timer2, Pre=1:1, Post=1:1
                clrf    TMR2                    ; Clear Timer2 register
                clrf    PIR1                    ; Clear Interrupt Flag register
                bsf     STATUS,RP0              ; Bank1
                clrf    PIE1                    ; Disable all Peripheral Interrupts
                bsf     PIE1,TMR2IE             ; Enable only Timer2
                movlw   0xf9                    ; Timeout Value = 249
                movwf   PR2                     ;

                clrf    TRISB                   ; Make PortB output
                bcf     STATUS,RP0              ; Back to Bank0
                movlw   0xff                    ; All on
                movwf   PORTB                   ; Initialize LEDS to be on
                return

                
                END                             ; directive 'end of program'


Scott Lee Says:

I see _WDT_ON in the config but there are no clrwdt instructions so I'm sure that is a problem when you try to use the 2 second delay on real HW.

As for MPLAB simulation, yes it seems to pause the execution after some short (in human terms) period of execution. In my experience it isn't reseting the device or anything -- it's like it just decides to insert a break point.

Questions: