I need help to understand this delay code !

Status
Not open for further replies.

Ghalebi

Newbie level 4
Joined
Apr 28, 2006
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,324
Salam everyone,
I am using a PIC16F84A with a 4 MHz oscillator for some purpose .. and someone fed me up with the following code in order to develop a 5 seconds delay ..
Code:
;5 second delay.
DELAY5 
       CLRF TMR0                       ;START TMR0.
LOOPB  
       MOVF TMR0,W                  ;READ TMR0 INTO W.
       SUBLW .160                     ;TIME - 160
       BTFSS STATUS,ZEROBIT  ; Check the diffrence 
       GOTO LOOPB                   ; Difference aint ZERO
       RETLW 0                          ;Time is 160, return.
He supposed that the frequency of the oscillator is 32 KHz .. I need to understand how did he make a relation between his assumed frequency and the number 160 to get the 5 seconds delay ..

The TMR0 is set to 1:256 ..

Any kind of help is appriciated .. thanks in advance
 

Internal Fosc clock is 32 768Hz, prescaler 1:256

MCU clock Fosc/4 = 8192Hz

Output from prescaler is 8192Hz / 256 = 32Hz

Period is T = 1 / f = 1 / 32Hz = 0,03125s

Delay is T x 160 = 0,03125s x 160 = 5s

This code fragment is bad, test for Zero is'nt advisable.
 

    Ghalebi

    Points: 2
    Helpful Answer Positive Rating
Thanx a lot man, so clear ..

but u didnt like the fragment, could u give ur suggestion to improve it ..

Many Thanks
 

Here is code:

Code:
;5 second delay, Fosc 32 768Hz 
DELAY5 
       CLRF TMR0                      ; Clear TMR0. 
LOOPB  
       MOVF TMR0,W                  ; READ TMR0 INTO W. 
       SUBLW .160                     ; TIME - 160 
       BTFSS STATUS,C             ; Check not borrow
       GOTO LOOPB                   ; 
       RETLW 0                         ; Time is 160, return.

It is workable with low prescaler factor too, without infinite loop.
 

thanks again sir,
but whats the difference with checking the Carry bit instead of Zero bit !!

and what about if i am working on Fosc = 4 MHz ..

Regards
 

Code:
;--------- VARIABLE DEFINITIONS
#define RAM_Start 0x20
    CBLOCK RAM_Start
counterLo
counterMi
counterHi
    ENDC
;----------------------------------------------
; Fosc = 4MHz, delay 4,999994s
;----------------------------------------------
 delay5                 ;2  (from call)
    movlw 0x70          ;3
    movwf counterLo     ;4
    movlw 0x10          ;5
    movwf counterMi     ;6
    movlw 0xF9          ;7
    movwf counterHi     ;8
loop
    incf counterLo,F    ;   1        
    btfsc STATUS,Z      ;   3     
    incf counterMi,F    ;   4     
    btfsc STATUS,Z      ;   6
    incf counterHi      ;   7
    btfss STATUS,Z      ;   9
    goto loop           ;   11
    retlw 0x00          ;10
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…