PIC Timer 1 preload value

augustinetez

Well-Known Member
Most Helpful 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
 
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.
 
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?.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…