Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
LinkBack Thread Tools Display Modes
Old 21st October 2004, 12:36 PM   (permalink)
Default Simulation Problem

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
Kane2oo2 is offline  
Old 21st October 2004, 12:44 PM   (permalink)
Default

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
Kane2oo2 is offline  
Old 21st October 2004, 01:03 PM   (permalink)
Default

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
Kane2oo2 is offline  
Old 21st October 2004, 01:25 PM   (permalink)
Default

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:
http://www.mnsi.net/~boucher/picloops.html
Cheers,
Barry.
Barry is offline  
Old 21st October 2004, 02:00 PM   (permalink)
Default

Quote:
I also found a neat little program that you can download that also produces the code for delay loops. Yhis can be downloaded from:
http://www.mnsi.net/~boucher/picloops.html
thanks m8 that was perfect

Kane
Kane2oo2 is offline  
Old 21st October 2004, 02:22 PM   (permalink)
Default

Quote:
Originally Posted by Kane2oo2
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..
williB is offline  
Old 21st October 2004, 03:33 PM   (permalink)
Default

Quote:
Originally Posted by Kane2oo2
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.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Old 21st October 2004, 04:29 PM   (permalink)
Default

Quote:
Originally Posted by Barry
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:
http://www.mnsi.net/~boucher/picloops.html
Cheers,
Barry.
nice call Barry .. this is a link to the actual code generator..

http://www.piclist.com/cgi-bin/delay.exe
williB is offline  
Old 21st October 2004, 05:41 PM   (permalink)
Default

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
Kane2oo2 is offline  
Old 21st October 2004, 06:05 PM   (permalink)
Default

first off i see that your first program has no return statement..
williB is offline  
Old 21st October 2004, 06:11 PM   (permalink)
Default

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

:?
Kane2oo2 is offline  
Old 21st October 2004, 06:30 PM   (permalink)
Default

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
Kane2oo2 is offline  
Old 21st October 2004, 07:03 PM   (permalink)
Default

Hello williB,
Thanks. Was in rather a hurry when I posted. :lol:
Cheers,
Barry.
Barry is offline  
Old 21st October 2004, 08:06 PM   (permalink)
Default

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..
williB is offline  
Old 22nd October 2004, 04:06 PM   (permalink)
Default

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
Kane2oo2 is offline  
Reply

Bookmarks

Thread Tools
Display Modes





All times are GMT. The time now is 07:53 AM.


Electronic Circuits  |  Learning Electronics
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.

eXTReMe Tracker