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.

Problem with delay code!

Status
Not open for further replies.

Roger_NO

Member
Hi!

I have a problem with delay routines! tryed many, but my led just wont flash!
this is the last code i tryed on my PIC, but the led is constant on.

I have used the PicLoops Delay loopsV2.2 program in this example, but also tryed the Delaycode generator @ https://www.golovchenko.org/cgi-bin/delay

non of witch i got to work!

in this code the delay time should be 1s according to the program!

what am i doing wrong?



Code:
#include    <p16f877a.inc>  ; processor specific variable definitions
     
__CONFIG _DEBUG_OFF&_CP_ALL&_WRT_HALF&_CPD_ON&_LVP_OFF&_BODEN_OFF&_PWRTE_ON&_WDT_OFF&_HS_OSC

;--------------------------CONSTANTS----------------------------------------

CounterA  equ 	08h
CounterB  equ 	09h
CounterC  equ	0Fh


;---------------------------port define-----------------------------------

 ORG     00H                     ;the address of entrance program
 BSF     STATUS,RP0          ;select BANK1
 MOVLW   b'00000000'        ;set RA0-5 to outputs
 MOVWF   TRISC                  
 BCF     STATUS,RP0          ;select BANK0                 
;--------------------------program------------------------------------------

start
 bsf	 PORTC,0		      ;RC0 high
 call	 Delay 
 bcf	 PORTC,0	   	      ;RC0 low
 call 	 Delay
 goto	 start				     
 



;-------------------------subroutine delay--------------------------------
 
Delay
 movlw	D'6'
 movwf	CounterC
 movlw	D'19'
 movwf	CounterB
 movlw	D'173'
 movwf	CounterA
loop		
 decfsz	CounterA,1
 goto	loop
 decfsz	CounterB,1
 goto	loop
 decfsz	CounterC,1
 goto	loop
 retlw	0
end
 
Last edited:
It looks like you're assigning variables to addresses which are occupied by special function registers...

Mike
 
ok, so witch registers should i turn to!20H and up? but the program itself is ok?

thanks for any reply!

Roger:)
 
Last edited:
ok, so which registers should i turn to? 20H and up?
Look in Section 2.0 of your datasheet. Figure 2.3 on Page 17 is a very good Register File Map that shows exactly what memory is free for you to use. There's free RAM in each bank that you can use.

To set up your variables, use cblock, like this:
Code:
	cblock	0x20
	d1,d2,d3,speed,speed2,temp
	endc

	org	0x000
	...

The number after cblock tells the assembler where to start putting variables. As Mike-K8LH says, stay out of the SFR's or you can expect your code to do nothing at all. :p Each variable is one byte. There are ways to do multi-byte vars too.

If, for neatness or other reason, you don't want them all on one line, you can do this kind of thing too:
Code:
	cblock	0x20
	d1,d2,d3,speed,speed2,temp
	var1,var2,var3,var4,var5
	endc
Note no commas at end of rows.

but the program itself is ok?
I haven't looked at it too much detail, but a quick cursory look seems ok. Unless you tell us your oscillator speed there's no way to tell you whether that delay is the correct length or not. When programming in assembler I often just use the to save time.

OH! Also, when you're posting source code, be sure to either click the # icon before pasting your code, or type [code] before your code and [/code] after it. These tags tell the web-site software to leave the formatting (indentation, etc.) of your source code alone (see my two code blocks above and one below). Makes it MUCH more readable and makes it much more likely that other people will actually look at it and try to help you. If it's an unformatted unreadable mess a lot of people won't bother looking at it.

Here's your original code, cleaned up and with a cblock. I haven't tested it, but it appears correct. Note the use of banksel directives instead of the (IMHO) mildly cryptic RP0 bit twiddling some people use.
Code:
	#include <p16f877a.inc>
	__CONFIG _HS_OSC & _LVP_OFF & _WDT_OFF

	cblock	0x20
	CounterA,CounterB,CounterC
	endc

	org	0x000
	banksel	TRISC		;bank 1
	movlw	b'00000000'	;portc all outs
	movwf	TRISC
	banksel	PORTC		;bank 0

start	bsf	PORTC,0		;RC0 high
	call	delay
	bcf	PORTC,0		;RC0 low
	call	delay
	goto	start

delay	movlw	0x06
	movwf	CounterC
	movlw	0x13
	movwf	CounterB
	movlw	0xad
	movwf	CounterA
loop	decfsz	CounterA,1
	goto	loop
	decfsz	CounterB,1
	goto	loop
	decfsz	CounterC,1
	goto	loop
	return

	end
 
Last edited:
Thank you so much guys, the delay routine worked perfect! Now my led flashes:)
Next time i'm gonna use # when i paste my code. My clock fq is 4MHz btw. I liked the banksel, from now on i'm going to use that!

As you probably understant, i am new at programming pic. And im just wondering! when you use 0x06 etc. is that the regestry adress. if so, what is that in hex.
 
Last edited:
The 6 is a value you are moving into a register and decrementing for your counter delay.

6 in hex is still 6. Hex is base16 so you have 0 1 2 3 4 5 6 7 8 9 A B C D E F 10 11, etc.
 
And im just wondering! when you use 0x06 etc. is that the regestry adress. if so, what is that in hex.
No. Putting the 0x prefix only means that the number that follows is hex. I use it out of habit. In Motorola/Freescale chips you usually use a $ prefix. Some people use an h suffix (your original code did). That's allowed. Just be consistent.

When I'm using a number less than 0x0a, or 0-9, the 0x is not necessary. It's just an automatic habit for me to put the hex prefix on it.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top