Continue to Site

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.

PIC Timer 1 preload value

Status
Not open for further replies.

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 :oops: 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.
 
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?.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top