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.

Simulation Problem

Status
Not open for further replies.

Kane2oo2

New Member
hi guys
i've just tried writing a simple program to flash some LED's On/Off

Code:
STATUS	equ	03
PORTA	equ 05
PORTB	equ	06
TRISA	equ	85
TRISB	equ	86
RP0		equ	05
CNTR	equ	0C
CNTR1	equ	0D

;Program

START	org		0
		bsf		STATUS,RP0
		movlw	b'00000000'
		movwf	TRISA
		movlw	b'00000000'
		movwf	TRISB
		bcf		STATUS,RP0
MAIN	movlw	b'00000000'
		movwf	PORTB
		movlw	b'00000011
		movwf	CNTR
LOOP	DECFSZ	CNTR,0
		GOTO	LOOP
		movlw	b'11111111'
		movwf	PORTB
		movlw	b'00000011'
		movwf	CNTR1
LOOP2	DECFSZ	CNTR1,0
		GOTO	LOOP2
		

		END

i cant see whats wrong, but when i use PIC simulator IDE it just seems to get stuck in the first loop, and looking at the memory address ...the value doesnt decrease by 1 (it doesnt decrease at all)

is it the simulator...or is there something simple i just cant see?

thanks
Kane
 
ah sorry ...i know the problem...how silly am i ..... (no dont answer that) :p

the DECFSZ f,d ...i set the d to 0 instead of 1

Kane
 
sorry another question now...

how does this give a 500ms delay?


Code:
Delay500		clrf DelayL		; clear DelayL to 0
		clrf DelayM		; clear DelayM to 0
		movlw 3h			; set DelayH to 3
		movwf DelayH
Wait1		decfsz DelayL		; subtract 1 from DelayL
		goto Wait1		; if not 0, goto Wait1
		decfsz DelayM		; subtract 1 from DelayM
		goto Wait1		; if not 0, goto Wait1
		decfsz DelayH		; subtract 1 from DelayH
		goto Wait1		; if not 0, goto Wait1
		return			; finished the delay

i dont undetstand, the first 2 lines set the DelayL and delayM to 0...so how can they subtract 1 in line 5?
and the same for DelayM

how can i calculate a delay and how many loops i need to do?
say i have a 4mhz clock. that means the pic will do 4mill instructions a second? So to get a delay of 1s i would have to loop 4 million times?

:?

Kane
 
Hi,
If you have a look at :
http://www.piclist.com
you will find a delay generator there that produces code for any delay you want.
I also found a neat little program that you can download that also produces the code for delay loops. Yhis can be downloaded from:
**broken link removed**
Cheers,
Barry.
 
I also found a neat little program that you can download that also produces the code for delay loops. Yhis can be downloaded from:
**broken link removed**

thanks m8 that was perfect

Kane
 
Kane2oo2 said:
sorry another question now...

how does this give a 500ms delay?


Code:
Delay500		clrf DelayL		; clear DelayL to 0
		clrf DelayM		; clear DelayM to 0
		movlw 3h			; set DelayH to 3
		movwf DelayH
Wait1		decfsz DelayL		; subtract 1 from DelayL
		goto Wait1		; if not 0, goto Wait1
		decfsz DelayM		; subtract 1 from DelayM
		goto Wait1		; if not 0, goto Wait1
		decfsz DelayH		; subtract 1 from DelayH
		goto Wait1		; if not 0, goto Wait1
		return			; finished the delay

i dont undetstand, the first 2 lines set the DelayL and delayM to 0...so how can they subtract 1 in line 5?
and the same for DelayM

how can i calculate a delay and how many loops i need to do?
say i have a 4mhz clock. that means the pic will do 4mill instructions a second? So to get a delay of 1s i would have to loop 4 million times?

:?

Kane
a register with 00h in it .. minus 1 ..SHOULD be FFh with the borrow flag set..
 
Kane2oo2 said:
ah sorry ...i know the problem...how silly am i ..... (no dont answer that) :p

the DECFSZ f,d ...i set the d to 0 instead of 1

Kane

That's one excellent reason to use the include file that MicroChip provide, rather than using your own equates at the beginning of the program (which seems totally pointless?). You then simply use 'W' or 'F' instead of the less informative '0' or '1', and you have all the 'official' registers and bits all correctly equated to their proper places.
 
Barry said:
Hi,
If you have a look at :
http://www.piclist.com
you will find a delay generator there that produces code for any delay you want.
I also found a neat little program that you can download that also produces the code for delay loops. Yhis can be downloaded from:
**broken link removed**
Cheers,
Barry.
nice call Barry .. this is a link to the actual code generator..
 
today is really my first day that i've actually started to program pics.
and so i have quite a few questions (as you can see) so rather than start a new topic i will just add another question in here.

this time...
i have 2 almost identical programs....1 works whereas the other doesnt.

the first one i wrote myself and with this...all the LED's came on...but it didnt seem to loop...they just stayed on.

So with the second one...i copied the delay routine from a help file ...and changed nothing else (but this one works) .... they all use the same routine ... just in the one i created they have different names.
Could this be anything to do with it?

Below are the 2 files:

File 1:
Code:
	Title "Sequence"

;5 LEDs flash up and down
;BIT LOW = LED ON
;BIT HIGH = LED OFF
;Declarations

STATUS	equ 0x03
PORTA	equ	0x05
PORTB	equ	0x06
TRISA	equ	0x85
TRISB	equ	0x86
RP0		equ	0x05
LPL		equ	0x0C
LPM		equ	0x0D
LPH		equ	0x0E

;Main Program

START	org 0x00			;start address
		BSF	STATUS,RP0		;set status to page1
		movlw	0x0F		;load b'1111'
		movwf	TRISA		;move b'1111' to trisa - set all to inputs
		movlw	0x00		;load b'0000000'
		movwf	TRISB		;move b'00000000' to trisb - set all to outputs
		BCF	STATUS,RP0		;return to page0

BEGIN	movlw	0x00
		movwf	PORTB
		CALL	DoDelay

		movlw	b'11111110'
		movwf	PORTB
		CALL	DoDelay

		movlw	b'11111101'
		movwf	PORTB
		CALL	DoDelay

		movlw	b'11111011'
		movwf	PORTB
		CALL	DoDelay

		movlw	b'11110111'
		movwf	PORTB
		CALL	DoDelay

		movlw	b'11101111'
		movwf	PORTB
		CALL	DoDelay
		GOTO	BEGIN

DoDelay	CLRF	LPL
		CLRF	LPM
		movlw	0x06
		movwf	LPH
again	DECFSZ	LPL
		GOTO	again
		DECFSZ	LPM
		GOTO	again
		DECFSZ	LPH
		GOTO	again


		END


File 2: the edited one (working)

Code:
	Title "Sequence"

;5 LEDs flash up and down
;BIT LOW = LED ON
;BIT HIGH = LED OFF
;Declarations

STATUS	equ 0x03
PORTA	equ	0x05
PORTB	equ	0x06
TRISA	equ	0x85
TRISB	equ	0x86
RP0		equ	0x05
DelayL	equ	0x0C
DelayM	equ	0x0D
DelayH	equ	0x0E

;Main Program

START	org 0x00			;start address
		BSF	STATUS,RP0		;set status to page1
		movlw	0x0F		;load b'1111'
		movwf	TRISA		;move b'1111' to trisa - set all to inputs
		movlw	0x00		;load b'0000000'
		movwf	TRISB		;move b'00000000' to trisb - set all to outputs
		BCF	STATUS,RP0		;return to page0

BEGIN	movlw	0x00
		movwf	PORTB
		CALL	Delay200

		movlw	b'11111110'
		movwf	PORTB
		CALL	Delay200

		movlw	b'11111101'
		movwf	PORTB
		CALL	Delay200

		movlw	b'11111011'
		movwf	PORTB
		CALL	Delay200

		movlw	b'11110111'
		movwf	PORTB
		CALL	Delay200

		movlw	b'11101111'
		movwf	PORTB
		CALL	Delay200
		GOTO	BEGIN

Delay200		clrf DelayL		
		clrf DelayM		
		movlw 1h			
		movwf DelayH
Wait1		decfsz DelayL		
		goto Wait1		
		decfsz DelayM		
		goto Wait1		
		decfsz DelayH		
		goto Wait1		
		return

sorry about all these questions :p

Kane
 
yes ...i realised that now ... i've changed it...but still something strange is happening

instead of the lights scrolling across (like they do with file2) ...they all just blink on and off

:?
 
hmmmm interesting.....

i just rewrote the subroutine using lowercase instructions....and it worked :?

although the call statements are in capital...they seem to work fine.

oh well i guess i'll just stick to lowercase from now on

Thanks
Kane
 
Kane ya Assembler is case sensitive..
Barry that site is 8) ..
i just used a half second delay on my robo car .. lol..and it works..!!
now all i have to do is transform the pascal code to assembler , to drive the steppers..
 
would this code work to debounce a switch?

Code:
START	bsf	STATUS,RP0
		movlw	0x01
		movwf	TRISA
		movlw	0x00
		movwf	TRISB
		bcf	STATUS,RP0

MAIN	movlw	0x00
		movwf	PORTB
			
GETPORT	movlw	PORTA
		movwf	PORTSTATUS1
		btfss	PORTSTATUS1,0
		goto	GETPORT
		call	DELAY10MS
		
		movlw	PORTA
		movwf	PORTSTATUS2
		btfss	PORTSTATUS2,0
		goto	GETPORT
		
		movlw	0xFF
		xorwf	PORTB,1
		goto 	GETPORT
			

DELAY10MS	clrf	DelayL
			movlw	0x03
			movwf	DelayM
LOOP		decfsz	DelayL,1
			goto	LOOP
			decfsz	DelayM,1
			goto	LOOP
			return

is a delay 0f 10ms enough?

Kane
 
do you really need to debounce the switch at all ?
if you are using the int input then yes..i would recomend it..
but you seem to be polling the port .. i may be mistaken
 
no im not using the interrupt.

but if i didnt debounce then when its first pressed the lights would go off ... but then it will get back to checking the port again before the switch would have time to settle? so the lights might come back on again.


but anyway ... even if i used the delay in a circuit that needed debouncing ...would a delay of 10 ms do it?

thanks
Kane
 
Kane2oo2 said:
no im not using the interrupt.

but if i didnt debounce then when its first pressed the lights would go off ... but then it will get back to checking the port again before the switch would have time to settle? so the lights might come back on again.


but anyway ... even if i used the delay in a circuit that needed debouncing ...would a delay of 10 ms do it?

Try looking at my tutorials!.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top