Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

MPLAB SIM Problem

Status
Not open for further replies.

zhaniko93

New Member
Hey I am new in PIC-s and I have compiled a little program which simply puts logical 1 on porta's first pin. Here's the code:
Code:
list P=16F72
include "c:\PIC\_inc\P16F72.inc"
__config _RC_OSC & _WDT_OFF & _CP_OFF
;============
; Declarations:
porta equ 05
org 1FF
goto Start
org 0
;============
; Subroutines:
Init clrf porta ; resets Port A
movlw b'0000' ; RA0: LED, RA1-3: not connected
bsf STATUS, RP0
movwf TRISA
bcf STATUS, RP0
retlw 0
;============
; Program Start:
Start call Init ; sets up inputs and outputs
Main bsf porta, 0 ; turns on LED
goto Main ; loops back to Main
END
When I try to debug it in MPLAB SIM, it loops in subroutine forever(i.e. it performs Init clrf porta, and everything before retlw 0 then it doesn't jump to Main, it goes back up to Init clrf porta and it continues forever), when I tried to simulate in Proteus,. it Gave error: Stack underflow executing RETLW instruction. whats wrong? what am i doing incorrectly? P.S. sry for bad english :)
 
Set up tris first then the port high or low.
Put the program into a chip and save yourself all the frustration. I haven't used MPLAB for 10 years.
You can get all your results and debugging by providing a halt or outputting to a LED or producing a beep or a beep-beep to let you now when the micro passses a certain point in a program.
 
Sims have their uses Colin, I personally find the MPLAB sim a bit limited though. He should give the OSHONSOFT free trial a go. It's got virtual switches, LEDs & LCDs and motors and shows all the registers by name, allows for direct register alteration when stepping and Break on the 'value' in a register as opposed to code location.

Here is working code that I have tested and reorganized a bit.

Code:
	list		p=16f72		; list directive to define processor
	#include	<P16F72.inc>		; processor specific variable definitions
	

	__config _RC_OSC & _WDT_OFF & _CP_OFF

;	PORTA EQU 0x05 


	org 0x00
	goto Start
	
; Program Start:
Start 	call Init ; sets up inputs and outputs
Main 	bsf porta, 0 ; turns on LED
	goto Main ; loops back to Main
; Subroutines:
Init 	clrf porta ; resets Port A
	movlw b'0000' ; RA0: LED, RA1-3: not connected
	bsf STATUS, RP0
	movwf TRISA
	bcf STATUS, RP0
	retlw 0

	END
 
Last edited:
Code:
; PROGRAM FUNCTION:The state of an LED is toggled every second and a
; buzzer sounds for one second every five seconds.
 list P=16F72
 include "c:\PIC\_inc\P16F72.inc"
 __config _XT_OSC & _WDT_OFF & _CP_OFF
;============
; Declarations:
 porta equ PORTA
 portb equ PORTB
 Mark30 equ 08
 Post80 equ 09
 _5Second equ 0A
 org 1FF
 goto Start
 org 0
;============
; Subroutines:
 Init clrf porta ; resets inputs and outputs
 clrf portb ;
 bsf STATUS, RP0
 movlw b'00000' ; RA0: LED, RA1-3: not connected
 movwf TRISA
 movlw b'00000000' ; RB0: buzzer, RB1-7: not connected
 movwf TRISB
 bcf STATUS, RP0
 movlw b'00000111' ; sets up timing register
 option
 movlw d'30' ; sets up marker
 movwf Mark30 ;
 movlw d'80' ; sets up first postscaler
 movwf Post80 ;
 movlw d'5' ; sets up 5 seconds counter
 movwf _5Second ;
 retlw 0
;============
; Program Start:
 Start call Init
 Main movfw Mark30 
 subwf TMR0, w 
 btfss STATUS, Z 
 goto Main 
 movlw d'30'  
 addwf Mark30, f 
 decfsz Post80, f 
; if(Post80 != 0)	{
 goto Main 
 movlw d'80' 
 movwf Post80 
 comf porta, f 
 bcf portb, 0 
 decfsz _5Second, f 
 goto Main 
 bsf portb, 0
 movlw d'5' 
 movwf _5Second ;
 goto Main ; loops back to start
 END

gives errors:----------------------------------------------------------------------
Release build of project `C:\PIC\Double Timing\time_.disposable_mcp' started.
Language tool versions: MPASMWIN.exe v5.35, mplink.exe v4.35
Thu Jun 24 18:15:48 2010
----------------------------------------------------------------------
Clean: Deleting intermediary and output files.
Clean: Done.
Executing: "C:\Program Files\Microchip\MPASM Suite\MPASMWIN.exe" /q /p16F72 "time_.asm" /l"time_.lst" /e"time_.err"
Warning[207] C:\PIC\DOUBLE TIMING\TIME_.ASM 9 : Found label after column 1. (porta)
Warning[207] C:\PIC\DOUBLE TIMING\TIME_.ASM 10 : Found label after column 1. (portb)
Warning[207] C:\PIC\DOUBLE TIMING\TIME_.ASM 11 : Found label after column 1. (Mark30)
Warning[207] C:\PIC\DOUBLE TIMING\TIME_.ASM 12 : Found label after column 1. (Post80)
Warning[207] C:\PIC\DOUBLE TIMING\TIME_.ASM 13 : Found label after column 1. (_5Second)
Warning[207] C:\PIC\DOUBLE TIMING\TIME_.ASM 19 : Found label after column 1. (Init)
Message[302] C:\PIC\DOUBLE TIMING\TIME_.ASM 23 : Register in operand not in bank 0. Ensure that bank bits are correct.
Message[302] C:\PIC\DOUBLE TIMING\TIME_.ASM 25 : Register in operand not in bank 0. Ensure that bank bits are correct.
Warning[224] C:\PIC\DOUBLE TIMING\TIME_.ASM 28 : Use of this instruction is not recommended.
Warning[219] C:\PIC\DOUBLE TIMING\TIME_.ASM 30 : Invalid RAM location specified.
Warning[219] C:\PIC\DOUBLE TIMING\TIME_.ASM 32 : Invalid RAM location specified.
Warning[207] C:\PIC\DOUBLE TIMING\TIME_.ASM 38 : Found label after column 1. (Start)
Warning[207] C:\PIC\DOUBLE TIMING\TIME_.ASM 39 : Found label after column 1. (Main)
Warning[219] C:\PIC\DOUBLE TIMING\TIME_.ASM 39 : Invalid RAM location specified.
Warning[219] C:\PIC\DOUBLE TIMING\TIME_.ASM 44 : Invalid RAM location specified.
Warning[219] C:\PIC\DOUBLE TIMING\TIME_.ASM 45 : Invalid RAM location specified.
Warning[219] C:\PIC\DOUBLE TIMING\TIME_.ASM 49 : Invalid RAM location specified.
Executing: "C:\Program Files\Microchip\MPASM Suite\mplink.exe" /p16F72 "time_.o" /z__MPLAB_BUILD=1 /o"time_.cof" /M"time_.map" /W /x
MPLINK 4.35, Linker
Copyright (c) 1998-2010 Microchip Technology Inc.
Errors : 0

Loaded C:\PIC\Double Timing\time_.cof.
----------------------------------------------------------------------
Release build of project `C:\PIC\Double Timing\time_.disposable_mcp' succeeded.
Language tool versions: MPASMWIN.exe v5.35, mplink.exe v4.35
Thu Jun 24 18:15:50 2010
----------------------------------------------------------------------
BUILD SUCCEEDED

whhat "Invalid RAM location specified" does that mean? why invalid?
 
Upon inspecting the start of available GPR memory on the oshonsoft sim for the 16f72, I observe that the first available address is 0x20,second is 0x21...... until 0xBF.
Be explicit with your addressing (0x05 and not 05, for portability) and use locations for GPR starting from 0x20.

Why do u have an org statement after your goto Start? And try to do some indenting, mixing Call names and code is tuff to read. Did u observe the sample I posted earlier? It's a lot easier to read.
 
OK now everything is fine 10x u very much. only 1 thing left:
when simulating with proteus, everything is all right but when simulating with PIC Simulator IDE, when program comes to command " comf porta, f", first time every bit is set to 1 and on second route, when the command is executed, only fourth byte of PORTA is set to 0, all others are left to 1. Why? Here's the code:
Code:
 list P=16F72
 include "c:\PIC\_inc\P16F72.inc"
 __config _XT_OSC & _WDT_OFF & _CP_OFF
;============
; Declarations:
 porta equ PORTA
 portb equ PORTB
 Mark30 equ 0x20
 Post80 equ 0x21
 _5Second equ 0x22
 org 1FF
 goto Start
 org 0
;============

; Program Start:
 Start 
	call Init
 Main 
	movfw Mark30 ; 
 	subwf TMR0, w ; 
 	btfss STATUS, Z 
 	goto Main
	 decfsz Post80, f 
	 goto Main
	 movlw d'80' 
	 movwf Post80 
	 comf porta, f 
	 bcf portb, 0 ; portb.0 = 0;
	 decfsz _5Second, f 
	 goto Main 
	  bsf portb, 0 
	  movlw d'5' 
	  movwf _5Second ;
	  goto Main ; loops back to start

; Subroutines:
 Init 
	clrf porta ;
	 clrf portb ;
	 bsf STATUS, RP0
	 movlw b'00000' 
	 movwf TRISA
	 movlw b'00000000' 
	 movwf TRISB
	 bcf STATUS, RP0
	 movlw b'00000111' 
	 option
	 movlw d'30'
	 movwf Mark30 
	 movlw d'80' 
	 movwf Post80 
	 movlw d'5' 
	 movwf _5Second ;
	 retlw 0
;============


 END
 
Use the microcontroller view in the Pic Simulator, it will show you that only RA4 is not an A/D port. Thus when u read in PORTA to complement it u are Reading a bunch of zeros except for Ra4. The 16f72 datasheet explains the adcon1 bit settings.
You have not set the Adcon1 register to specify digital inputs for Porta, default is analog. Add the following lines just before the <bcf STATUS, RP0> in your init routine

movlw b'00000111'
movwf adcon1 ; set portA to digital.

The 16f72 is a bit large and complicated to be used as a starter PIC. U must read its datasheet as it has some more advanced capabilities.
 
Last edited:
Can you tell me the easyest PIC16 one? Becouse another problem appeared and I can't solve it on my own.
btfss portb, 3 ; tests push button, skip if pressed
goto LEDOff ; push button isn’t pressed so turns
bsf porta, 0
when I change port-s third pin to 1 in Simulator IDE, it doesnt turn porta 0 on. why?
 
Last edited:
Post the code so i can see.

is this what u have?

btfss portb,3 ; skip next statement if bit is set.
goto LEDoff
bsf porta,0 ; set bit 0 if portb,3 is 1.

Well, in your Tris statement u have setup portb as all outputs. U have to configure the input (rb3) as an input before u can use it as one.
Next thing, porta,0 will go high if portb,3 is high. usually a switch when depressed means a pin goes low. Lastly, u should check to see if portb,3 offers a pull up setting. In order to have reliable switching states for a switch input the input line must have pull up capability or a physical pull up resistor is required in circuit.

When debugging a sim u must use break points and step through while studying key registers like the w reg and the status reg upon each instruction. That will show u what is happening or not happening as u expect. Also have the breakpoint manager active in the oshonsoft pic simulator to follow the instructions as u step thru.

As a newbie testbed I suggest the pic kit 2 LPC with the 16f690 PIC. You get lessons to work through, and u can program the pic from the PC directly and see things happen on the LEDS etc.
 
Last edited:
I an only simulating, not doing on real microprocessors. I changed versdion to 16f690 and still not working (same problem with PIC Simulator IDE) CODE:
Code:
; PROGRAM FUNCTION: If a push button is pressed an LED is turned on.
 list P=16F690
 include "c:\PIC\_inc\P16F690.INC"
 __config _XT_OSC & _WDT_OFF & _CP_OFF
 ;============
 ; Declarations:
 porta equ 05
 portb equ 06
 org 1FF
 goto Start
 org 0

 ; Program Start:
 Start call Init
 Main btfss portb, 4 ; tests push button, skip if pressed
 goto LEDOff ; push button isn’t pressed so turns
 ; LED off
 bsf porta, 0 ; push button is pressed so turns
 ; LED on
 goto Main ; loops back to Main
 LEDOff bcf porta, 0 ; turns LED off
 goto Main ; loops back to Main

;============
; Subroutines:
 Init clrf porta ; resets inputs and outputs
 clrf portb 
 bsf STATUS, RP0
 movlw b'000000' ; RA0: LED, RA1-3: not connected
 movwf TRISA
 movlw b'11111111' ; RB0: push button, RB1-7: N/C
 movwf TRISB
 bcf STATUS, RP0
 retlw 0
 ;============


 END
 
Ok, perhaps you should play with the 16F84 and then the 16f628 and then the 16f690 in that order.

here is the code u need to make the 16f690 work, again you did not set the ports to digital.

Code:
Init clrf porta ; resets inputs and outputs
 	clrf portb 
 	bsf STATUS, RP0 ; switch to bank 1
 	movlw b'000000' ; RA0: LED, RA1-3: not connected
 	movwf TRISA
	movlw b'11111111' ; RB0: push button, RB1-7: N/C
 	movwf TRISB
	bsf status,rp1 ; switch to bank 3
	bcf STATUS, RP0 ; switch to bank 2
	clrf ansel ; make porta digital
	clrf anselh ; make portb digital
	bcf status, rp1 ; switch to bank 0
 	retlw 0
;============

Let me point out something that is not obvious.

The physical pins on the MCU are referenced to either an input buffer an output buffer or an analog unbuffered state. An MCU like the 16f84 has no analog so you have only input or output states to set. IT is possible to write to an output buffer while a pin is set as an input. You don't overwrite the pin as it's set as an input. When u switch the pin to an output it will THEN output the value in the output buffer.

Ok, so if u write to a pin that is set as an input the value u write sits in the unaccessed output buffer. If u try to read back that pin u get ONLY the INPUT value of the pin, not what u just placed in the output buffer. make the pin an output and u can write and read the output values of the pin IF the hardware does not force the pin to a different state.

Now the PIC simulator IDE shows u the MCU port output buffers and special registers in the regular simulator window. THIS is not the INPUT.

If u wish to simulate an input to the MCU u must enable the microcontroller view and then toggle the state of the input pin by clicking on the 'T' . If the pin has an 'A' next to it the pin is set as an analog input, and you have to use the ANSEL/H register to switch it as I did in the code. Note that pins which are outputs have no option to toggle in the microcontroller view. The output of the pins is shown and u can see that the porta,0 does go high (as coded) when the portb,4 is set as high.

Lastly, which ever PIC you want to use please read the section on the datasheet about the ports, you appear to not want to do that, but u must as a learner.
 
There isn't P16F84 in my proteus library. where can I add it from? P.S. 10x mosaic I understood and made corrections to everything :) thanks
There is only 16F84A not 16F84. by the way, are they the same?
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top