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.

TIME DELAYs for PIC16F84A

Status
Not open for further replies.

aldrinsi

New Member
just a newbie in microcontroller, could somebody pls help me how to compute the time delay using looping statement in PIC16F84A?
 

trennonix

New Member
this site has an app in which you enter the time to wait, and the freq of the oscillator
it will generate asm code that you can paste in your program and you can call it at will

the 16F84A is obselete, try the 16F628A it's better and almost half the price (2.5$)
 

be80be

Well-Known Member
Like this it's the easy way
Code:
; Delay = 0.5 seconds
; Clock frequency = 4 MHz

; Actual delay = 0.5 seconds = 500000 cycles
; Error = 0 %

	cblock
	d1
	d2
	d3
	endc

Delay
			;499994 cycles
	movlw	0x03
	movwf	d1
	movlw	0x18
	movwf	d2
	movlw	0x02
	movwf	d3
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	Delay_0

			;2 cycles
	goto	$+1

			;4 cycles (including call)
	return
 

Electronworks

New Member
The way it is generally done is with a timer as follows:

If you are working with a 4MHz clock, and have 4 clocks per instruction cycle, your timer will update every 1us. If your counter overflows after 256 counts, you preload a value as an offset then look for the counter to overflow.

If you want to measure 100us, load in a value of 156 into the counter, so you have 100 counts left (before it overflows). 100 counts is 100us.

If you need a longer time, use the prescaler. A prescaler of 8 means your counter increments every 8us, so in the case above you overflow after 800us.

You can then either look to see when your counter is zero (it has overflowed), or set an interrupt to be generated on overflow and detect the overflow that way.

If you want really slow timing, either call the loop many times, or use a slower clock (like a 32kHz crystal)
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top