Continue to Site

# PIC Timer 1 preload value

#### augustinetez

##### Active Member
Would somebody please check my maths for the preload value of Timer1 on a 16F1827 running at 4MHz.

I'm looking for a delay of 5mS, pretty sure I've screwed this up because it's taking a lot longer than 5mS on the hardware.

65536-(delay*clock freq)/(prescaler*4) = 65536-((.005*4000000)/(1*4))=60536 (EC78 HEX)

Thanks

#### Pommie

##### Well-Known Member
I still have CodeTidy (dated 05/22/2008) installed.
I lost that many years ago. Keep thinking I need to write a new one that also does C. So many projects, so little time.

Mike.

#### Pommie

##### Well-Known Member
Out of curiosity I converted the above to C, so three lines in asm become 1 in C.
I.E.
Code:
      banksel  OSCCON
movlw    b'01101000'    ;select 4MHz clock = 1MHz instruction cycle
movwf    OSCCON         ;Set PIC oscillator frequency
becomes
OSCCON=0b01101000;  //4MHz clock

And the whole thing becomes,
Code:
      OSCCON=0b01101000;  //4MHz clock
T1CON=0b00000001;   //Timer 1 on
CCP1CON=0b00001011; //Special Events Trigger
CCPR1=5000;         //period = 5000
while(1){           //loop forever
while(CCP1IF==0); //wait for CCP1IF to be set
;5mS has passed
CCP1IF=0;         //clear it
}                   //end of loop forever
Note, CCPR1L/H can be accessed as a 16 bit variable

Mike.

#### Nigel Goodwin

##### Super Moderator
Trying a version with only spaces,
Code:
setup
banksel  OSCCON
movlw    b'01101000'    ;select 4MHz clock = 1MHz instruction cycle
movwf    OSCCON         ; Set PIC oscillator frequency
banksel  T1CON
movlw    b'00000001'    ;timer 1 on
movwf    T1CON
banksel  CCP1CON
movlw    b'00001011'    ;Special Events Trigger
movwf    CCP1CON
banksel  CCPR1L         ;set timer 1 to repeat
movlw    low(5000)      ;every 500 clock cycles
movwf    CCPR1L         ;equals 5mS
banksel  CCPR1H
movlw    high(5000)
movwf    CCPR1H
loop
banksel  PIR1
btfss    PIR1,CCP1IF    ;wait for interrupt flag
goto     loop           ;to be set
;5mS has passed
bcf       PIR1,CCP1IF   ;clear it
goto      loop

Mike.
Edit, appears it's tabs that cause the problem.
I would presume that it's simply that the forum software is expanding the tabs to a different number of spaces than the original editor did?.

Replies
2
Views
626
Replies
3
Views
2K
Replies
4
Views
2K
Replies
1
Views
559
Replies
4
Views
1K