mplab sim says running.....

Status
Not open for further replies.

jeremygaughan

New Member
Hi, I'm starting like most people doing a blinky program. I am running some code through the mplab sim to see if my program runs properly and to get a feel for the commands. When the program gets to my delay loop it seems to hit a snag. The sim says running.... and doesn't ever make any progress. Here is my code I imagine I am using the wrong registers for my count1 and count 2, or I have overlooked something else. Thanks for taking a look at my code.

list p=16f628a
#include <p16f628a.inc>
__config _CP_OFF & _WDT_OFF & _PWRTE_ON &
_BODEN_OFF &_INTRC_OSC_NOCLKOUT & _MCLRE_ON &
_LVP_OFF
ERRORLEVEL -302
;*****Set up the Constants****

STATUS equ 03h ;Address of the STATUS register
TRISA equ 85h ;Address of the tristate register for port A
PORTA equ 05h ;Address of Port A
COUNT1 equ 9Ah ;First counter for our delay loops
COUNT2 equ 9Bh ;Second counter for our delay loops

;****Set up the port****

bsf STATUS,5 ;Switch to Bank 1
movlw 00h ;Set the Port A pins
movwf TRISA ;to output.
movlw 85h ;set value of counter
movwf 9Ah ;move to counter folder
movlw 85h ;set value of counter2
movwf 9Bh ;move value to counter folder
bcf STATUS,5 ;Switch back to Bank 0
;****Turn the LED on****

Start
movlw 02h ;Turn the LED on by first putting it
movwf PORTA ;into the w register and then on the port

call Delay

movlw 00h ;Turn the LED off by first putting it
movwf PORTA ;into the w register and then on the port

call Delay

goto Start ;go back to Start and turn LED on again

Delay

Loop1
decfsz COUNT1,1 ;This second loop keeps the LED
goto Loop1 ;turned off long enough for us to
decfsz COUNT2,1 ;see it turned off
goto Loop1
return

;****End of the program****

end
 
You've used the include file so you don't have to redefine the I/O
Try using
bsf STATUS,RP0 ;bank1

I've got to run for lunch, but will look at your code when I get back.
Also try looking at the Hello World poster on my site (download section)
 
hi jeremy,
Dont forget the 'sloooow' execution speed of the program when running in the simulator.

Your delays will seem like forever!.

Use a Variable and some conditional statements in the program delays.

That is when running in sim mode, it skips [dosn't compile] the delays.

When you post your code 'select it' and add the CODE quotes from the menu bar, the source code will keep its format.

EDIT: another quick look,, you are using register addresses that are in Bank1 [for the counters] also these are defined registers, its not the ideal way.
Use General Registers 0x20 up, see Figure 4.2 on the datasheet.
 
Last edited:
wow that was a rush. I didn't think I would be that happy to see the program finally work. I used the suggestions that guys submitted and it works perfectly. I'm almost ready to program this chip. Thanks again and here is the code revised.
Code:
	list p=16f628a
#include <p16f628a.inc>
		__config  	_CP_OFF & _WDT_OFF & _PWRTE_ON & _BODEN_OFF &_INTRC_OSC_NOCLKOUT & _MCLRE_ON & _LVP_OFF
		ERRORLEVEL -302

COUNT1         equ       20h     ;First counter for our delay loops
COUNT2         equ       21h     ;Second counter for our delay loops 
		     
    bsf           STATUS,RP0      ;Switch to Bank 1
    movlw       00h                  ;Set the Port A pins
    movwf      TRISA               ;to output.
    bcf           STATUS,RP0      ;Switch back to Bank 0
 
    movlw	07h		     ;turn off comparators
    movwf	CMCON

Start           

    movlw      02h                   ;Turn the LED on by first putting it
    movwf      PORTA              ;into the w register and then on the port 

    call          Delay 

    movlw      00h                  ;Turn the LED off by first putting it
    movwf      PORTA             ;into the w register and then on the port 

    call          Delay 

    goto        Start               ;go back to Start and turn LED on again 

Delay
    movlw      85h                  ;set value of counter
    movwf      20h	            ;move to counter folder
    movlw      03h	            ;set value of counter2
    movwf      21h	            ;move value to counter folder
Loop1
    decfsz      COUNT1,1          ;This second loop keeps the LED
    goto        Loop1                ;turned off long enough for us to
    decfsz      COUNT2,1          ;see it turned off
    goto        Loop1          
    return 

    end
 
Last edited:
Nicely done.

Since you've defined your variables COUNT1, COUNT2 you can call them by name.

so
Code:
 movwf 21h
can be
Code:
 movwf COUNT2

Tip1 for variables (when you have a few)
Code:
 cblock 0x20  ; start of variable space same as CBLOCK 21h
COUNT1
COUNT2
x
y
z
endc

Tip2 to reserve a block within a cblock (useful for FSR, or VAR+1, VAR+2)
Code:
 COUNT:4  ; block of four COUNT
 
Last edited:
I've made a few modifications. I like your advice to use variables. I found that turning on a few lights is be about the same a turning on one light just repeating more of the same program. So I made a little stoplight program and a small stoplight with some leds and a capacitor / resistor for the clock. If you have anymore advice I really appreciate it. Thanks again for the guidance.
**broken link removed**
**broken link removed**
Code:
	list p=16f628a
#include <p16f628a.inc>
		__config  	_CP_OFF & _WDT_OFF & _PWRTE_ON & _BODEN_OFF &_INTRC_OSC_NOCLKOUT & _MCLRE_ON & _LVP_OFF
		ERRORLEVEL -302

	cblock     20h               ;establish variables
	x,y,z
	endc						      
		     
    bsf        STATUS,RP0        ;Switch to Bank 1
    movlw      00h               ;Set the Port A pins
    movwf      TRISA             ;to output.
    bcf        STATUS,RP0        ;Switch back to Bank 0
 
    movlw	   07h				 ;turn off comparators
    movwf	   CMCON

Start           

	movlw     01h                ;Turn the red LED on by first putting it
    movwf     PORTA              ;into the w register and then on the port 

    call      Delay 
	call      Delay
	
    movlw     00h                ;Turn all LEDs off by first putting it
    movwf     PORTA              ;into the w register and then on the port 

  	movlw	  02h 				 ;Turn on green led
	movwf	  PORTA				 ;
	
	call      Delay
	call      Delay
	
	movlw	  00h				 ;turn off all leds
	movwf	  PORTA
	
	movlw	  04h				 ;turn on yellow led
	movwf	  PORTA
	
	call      Delay
	
	movlw	  00h				 ;turn off all leds
	movwf	  PORTA
	
    goto      Start              ;go back to Start and turn LED on again 

Delay
    movlw     03h	             ;set value of counter2
    movwf	  y    	             ;assign to varible y
Loop2	
	movlw	  10h                ;set value of counter
    movwf     x     	         ;assign to varible x

Loop1

    decfsz    x,1                ;This second loop keeps the LED
    goto      Loop1              ;on for a moment
    decfsz    y,1              
    goto      Loop2             
    return 

    end
 
hi jeremy,

If you use the MPLAB simulator to step thru your program, you may find 'conditional assembly' useful on many occasions
when writing programs.

Have edited your delay subr and added two lines near the top.

It will run in the simulator as it is now.
By changing the Sim1 to '0' or '1' you will see the effect, after you assemble/build, as you step thru the program.

I would suggest that you give the CBlock variables more meaningful names, x,y,z single character will be difficult to keep track
of in your head.

eg: del_cnt1, del_cntr2, asc_bfr3 etc...

Code:
	list p=16f628a

	#include <p16f628a.inc>
	__config  	_CP_OFF & _WDT_OFF & _PWRTE_ON & _BODEN_OFF &_INTRC_OSC_NOCLKOUT & _MCLRE_ON & _LVP_OFF
	ERRORLEVEL -302

;add these two lines of text
	Variable Sim1 
Sim1 SET .1	;if '1' its running in the Simulator, else '0'

;

	cblock     20h               ;establish variables
	x,y,z
	endc						      

;Your Source here...........

;test only
lp1: call Delay
	goto lp1
;Running a program with long delays in the Simulator can be 'slow'
;by adding a Variable say, Sim1 and setting it to either '0' or '1' and then
;testing that Variable during assembly will direct the assembler to
;generate code that gives a shortened delay for Simulation 
;or the required delay for a PIC.
;
;
Delay:
	if Sim1 == 0x01		;assembler conditional assembly
	movlw 	.1		;this is for the Sim, just 1 loop
	else
    	movlw     03h	        ;set value of counter2
	endif

    	movwf	  y    	        ;assign to varible y
Loop2:
	if Sim1 == 0x01
	movlw 	.1		;this is for the Sim, just 1 loop
	else	
	movlw	  10h           ;set value of counter
	endif

    	movwf     x  	         ;assign to varible x

Loop1

    	decfsz    x,1                ;This second loop keeps the LED
    	goto      Loop1              ;on for a moment
    	decfsz    y,1              
    	goto      Loop2             
    	return 



    	end
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…