# long delay time with 16f84a

Status
Not open for further replies.

#### fs789

##### Junior Member level 2
can any one help me to obtain along delay time 18 hours with pic16f84a and acrystal of 4MHZ ? i need the time to be accurate.

---------- Post added at 13:05 ---------- Previous post was at 11:36 ----------

---------- Post added at 13:06 ---------- Previous post was at 13:05 ----------

#### Tahmid

Which language and compiler do you use?
In mikroBASIC we could do something like:
Code:
dim x as word
for x = 1 to 60000
delay_ms(1080)
next x

That's a delay of 1.08 seconds 60000 times = 64800s = 1080mins = 18 hours

In mikroC, we'd do something similar.

Hope this helps.
Tahmid.

fs789

### fs789

Points: 2

#### fs789

##### Junior Member level 2
Which language and compiler do you use?
In mikroBASIC we could do something like:
Code:
dim x as word
for x = 1 to 60000
delay_ms(1080)
next x

That's a delay of 1.08 seconds 60000 times = 64800s = 1080mins = 18 hours

In mikroC, we'd do something similar.

Hope this helps.
Tahmid.
i want to use assembly language , mplab program

#### keith1200rs

##### Super Moderator
Staff member
I would use the built in timers to get the lowest frequency then use software counters to count multiples of that time period. Think of it as counting hours, minutes, seconds, hundredths of seconds, for example.

Keith.

fs789

### fs789

Points: 2

#### Tahmid

Hi,
In that case, use the timer as keith suggested to get the longest delay. Calculate how long the delay is and according keep a seconds register that increments and resets accordingly in the process incrementing a minutes and an hours registers. When the value in the hours registers = 18, stop the timer and go on doing whatever you would.

Alternately a simpler method is the delay routine:
Code:
; Delay = 64800 seconds
; Clock frequency = 4 MHz

; Actual delay = 64800 seconds = 64800000000 cycles
; Error = 0 %

cblock
d1
d2
d3
d4
d5
endc

Delay
;64799999988 cycles
movlw	0xA1
movwf	d1
movlw	0x24
movwf	d2
movlw	0x21
movwf	d3
movlw	0x60
movwf	d4
movlw	0x02
movwf	d5
Delay_0
decfsz	d1, f
goto	$+2 decfsz d2, f goto$+2
decfsz	d3, f
goto	$+2 decfsz d4, f goto$+2
decfsz	d5, f
goto	Delay_0

;8 cycles
goto	$+1 goto$+1
goto	$+1 goto$+1

;4 cycles (including call)
return

Hope this helps.
Tahmid.

fs789

### fs789

Points: 2

#### keith1200rs

##### Super Moderator
Staff member
The choice between a delay or a timer depends on whether the processor needs to do anything in the 18 hours. If it does nothing for 18 hours then a delay is fine. If it needs to do anything during the 18 hours then you need to use a timer.

Keith.

fs789

### fs789

Points: 2

#### fs789

##### Junior Member level 2
thank you very much
please what do you mean by the term ($+1) ?? because i am newer in assemply language. #### keith1200rs ##### Super Moderator Staff member fs789 ### fs789 Points: 2 Helpful Answer Positive Rating #### Tahmid ##### Advanced Member level 5 Hi,$ represents the current instruction.
So,
Code:
here: goto $ means Goto current instruction, in other words, stay on that line$+1 means next instruction.
$+2 means the instruction after the next one$-1 means next instruction
$-2 means the instruction before the previous one eg. Code: here: bsf portb,1 bsf portb,2 goto$+2
bsf portb,3
bsf portb,4
goto $-5 Here, the lines are executed and then goes to "goto$+2", ie, it jumps to "bsf portb,4" then goes to "goto $-5" ie 5 lines before, ie "bsf portb,1" Hope this helps. Tahmid. fs789 ### fs789 Points: 2 Helpful Answer Positive Rating #### fs789 ##### Junior Member level 2 Hi, In that case, use the timer as keith suggested to get the longest delay. Calculate how long the delay is and according keep a seconds register that increments and resets accordingly in the process incrementing a minutes and an hours registers. When the value in the hours registers = 18, stop the timer and go on doing whatever you would. Alternately a simpler method is the delay routine: Code: ; Delay = 64800 seconds ; Clock frequency = 4 MHz ; Actual delay = 64800 seconds = 64800000000 cycles ; Error = 0 % cblock d1 d2 d3 d4 d5 endc Delay ;64799999988 cycles movlw 0xA1 movwf d1 movlw 0x24 movwf d2 movlw 0x21 movwf d3 movlw 0x60 movwf d4 movlw 0x02 movwf d5 Delay_0 decfsz d1, f goto$+2
decfsz	d2, f
goto	$+2 decfsz d3, f goto$+2
decfsz	d4, f
goto	$+2 decfsz d5, f goto Delay_0 ;8 cycles goto$+1
goto	$+1 goto$+1
goto	$+1 ;4 cycles (including call) return Hope this helps. Tahmid. can you please explain for me how you calculated number of cycles and the value of delay?? #### Tahmid ##### Advanced Member level 5 fs789 ### fs789 Points: 2 Helpful Answer Positive Rating #### fs789 ##### Junior Member level 2 thank you very much can you please help me with this code?? it is simple delay loop,((i think about 585225 cycles)) is it true?? the problem is , the output port(B0) is flickering !!!!!!!!!! ido not know the problem?? PROCESSOR 16f84a ; Defining processor #include "p16f84a.inc" ; Microchip INC database __CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC ; frequency 4MHZ org 0x00 goto first first bsf 03,5 bsf 81,7; ;81 option reg. clrf 85 ; 85 and 86 are trisa, trisb clrf 86 ; 03 is status bcf 03,5 movlw .255 movwf 0f clrf 05 ; 05 port a and 06 port b clrf 06 delay bsf 03,0 movlw d'255' movwf 0c movwf 0e movwf 0d decfsz 0c,f goto$-1
bsf 03,0
decfsz 0d,f
goto $-1 bsf 03,0 decfsz 0e,f goto$-1
bsf 03,0
decfsz 0f,f
goto delay
bsf 06,0
end

Last edited:

#### Tahmid

Hi,

From "first bsf 03,5" to end, it's 588271 cycles. Use the MPLAB stopwatch tool to find it out.

Hope this helps.
Tahmid.

---------- Post added at 16:30 ---------- Previous post was at 14:49 ----------

Hi,
You added the include file, so you can use the names of the SFR instead of the locations. eg:
Code:
first bsf 03,5
can now be written as
Code:
first bsf STAUS, 5
or
Code:
first bsf STAUS, RP0

Code:
clrf 85 ; 85 and 86 are trisa, trisb
can be written as
Code:
clrf TRISA

You just need to be careful that the names of the SFR are written in capital letters.

Hope this helps.
Tahmid.

fs789

### fs789

Points: 2

#### fs789

##### Junior Member level 2
ok. thank you
is my code has any mistakes??
and why when i connect pic in real application, the output seems flickering??not stable

#### Tahmid

Hi,
I haven't gone through the code, but there maybe other reasons the output is flickering.
Most common I would say is inappropriate supply line filtering. Very near the PIC power pins(VDD and VSS) place a 0.1uF ceramic capacitor and a 100uF electrolytic along the power line. This should get rid of spikes/noise problems that can affect the PIC.
Then, check if your oscillator is placed near the PIC. Having the oscillator very far away and connecting it to the PIC may also result in unwanted behaviour.

I'll see the code to see if it's ok.

Hope this helps.
Tahmid.

fs789

### fs789

Points: 2

#### fs789

##### Junior Member level 2
HI,
i have checked the connection of circuit,oscillator,capacitors,and every thing is ok.
but the problem of fkickering is still happening!!!!!!!!!!

#### Tahmid

This is the corrected version of your code:
Code:
PROCESSOR 16f84a ; Defining processor
#include "p16f84a.inc" ; Microchip INC database
__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC
; frequency 4MHZ
org 0x00
goto first

first bsf 03,5
bsf 81,7; ;81 option reg.
clrf 85 ; 85 and 86 are trisa, trisb
clrf 86 ; 03 is status
bcf 03,5
movlw .255
movwf 0f
clrf 05 ; 05 port a and 06 port b
clrf 06
delay bsf 03,0
movlw d'255'
movwf 0c
movwf 0e
movwf 0d
decfsz 0c,f
goto $-1 bsf 03,0 decfsz 0d,f goto$-1
bsf 03,0
decfsz 0e,f
goto $-1 bsf 03,0 decfsz 0f,f goto delay bsf 06,0 goto$
end

The error here is after ~589ms, the microcontroller turns PORTB0 on and then the program ends, causing the microcontroller to jump to start and restart everything including all initializations. This is undesirable. What you want is to make the microcontroller sit idle and not end the program so that it does not restart.
That's why before end, I made the microcontroller endlessly loop there, such that it never restarts again. It is done by placing:
Code:
goto $ before end. A more organised and easy to read and understand version of your code would be such: Code: PROCESSOR 16f84a ; Defining processor #include "p16f84a.inc" ; Microchip INC database __CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC ; frequency 4MHZ CBLOCK 0X0C COUNT,D1,D2,D3,D4 ENDC org 0x00 goto first first clrf PORTA clrf PORTB bsf STATUS,RP0 bsf OPTION_REG,7; ;81 option reg. clrf TRISA ; 85 and 86 are trisa, trisb clrf TRISB ; 03 is status bcf STATUS,RP0 movlw .255 movwf COUNT delay bsf STATUS,0 movlw .255 movwf D1 movwf D3 movwf D2 decfsz D1,f goto$-1
bsf 	STATUS,0
decfsz 	D2,f
goto 	$-1 bsf STATUS,0 decfsz D3,f goto$-1
bsf 	STATUS,0
decfsz 	D4,f
goto 	delay

bsf 	PORTB,0
goto	$end This makes it easy to understand your code. I hope you understand the differences. Hope this helped. Tahmid. ---------- Post added at 17:25 ---------- Previous post was at 17:24 ---------- I have simulated both the codes and they are working properly. fs789 ### fs789 Points: 2 Helpful Answer Positive Rating #### fs789 ##### Junior Member level 2 THANK YOU VERY MUCH but now ,assume i want adelay time 18 hours. the code is generated as follows : is this time is very accurate??? if use this this delay every day,what do yoy expect the error ,(accumulating error) in ayear?? the code is as follows: ; Delay 64 800 000 000 machine cycles ; Duration of the delay 18 hour ; Frequency of the pulsing oscillator 4 MHZ movlw .157 movwf Reg_1 movlw .14 movwf Reg_2 movlw .113 movwf Reg_3 movlw .3 movwf Reg_4 movlw .6 movwf Reg_5 decfsz Reg_1,F goto$-1
clrwdt
decfsz Reg_2,F
goto $-4 decfsz Reg_3,F goto$-6
decfsz Reg_4,F
goto $-8 decfsz Reg_5,F goto$-.10
nop
nop

#### Tahmid

Hi,
The 18 hour delay is perfect, but what would likely make the circuit inaccurate, would be crystal frequency, deviation over time and temperature and more importantly how much time you are losing by calling the delay and doing other stuff.

Hope this helps.
Tahmid

#### fs789

##### Junior Member level 2
Hi,
The 18 hour delay is perfect, but what would likely make the circuit inaccurate, would be crystal frequency, deviation over time and temperature and more importantly how much time you are losing by calling the delay and doing other stuff.

Hope this helps.
Tahmid
OK, THANK YOU .
IF ALL THESE FACTORS( TEMPERATURE,crystal frequency, deviation over time......) HAPPENED: WHAT IS THE DEGREE OF ACCURACY DO YOU THINK WE WILL HAVE IN THIS SITUATION??
IS IT ACCEPTABLE ??

Status
Not open for further replies.