PIC16F88 Timer1 (RTC)

Discussion in 'Embedded Systems and Microcontrollers' started by ATM, Feb 18, 2010.

  1. ATM

    Thread Starter Member

    Oct 8, 2009
    31
    0
    Hey, i'm in the process of creating a Real Time Clock using the Timer1 register of a PIC16F88. I will be using a external 32.768KHz crystal to pulse the register.
    I've done a little maths and figured that I need to preload the TMR1H and TMR1L registers with the value 32768 in order to achieve a 1 second count until overflow.

    I have wrote the following code below in SourceBoost:

    I have wrote the Interrupt code under the "void isr" heading, now I need this code to be called every time the Timer1 register overflows i.e after every second. I just dont know how to implement this. I've tried just calling the interrupt from the main code but once inside the interrupt code the program never leaves it, but I need the interrupt code to be constantly running in the background whilst the main code runs.

    Any help is greatly appreciated, thanks.
     
  2. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    If you read through the datasheet for that device, it is all right there....

    Here is the way you would implement an RTC with interrupt using timer1 :
    Code ( (Unknown Language)):
    1. RTCinit BANKSEL TMR1H
    2. MOVLW 0x80 ; Preload TMR1 register pair
    3. MOVWF TMR1H ; for 1 second overflow
    4. CLRF TMR1L
    5. MOVLW b’00001111’ ; Configure for external clock,
    6. MOVWF T1CON ; Asynchronous operation, external oscillator
    7. CLRF secs ; Initialize timekeeping registers
    8. CLRF mins
    9. MOVLW .12
    10. MOVWF hours
    11. BANKSEL PIE1
    12. BSF PIE1, TMR1IE ; Enable Timer1 interrupt
    13. RETURN
    14. RTCisr BANKSEL TMR1H
    15. BSF TMR1H, 7 ; Preload for 1 sec overflow
    16. BCF PIR1, TMR1IF ; Clear interrupt flag
    17. INCF secs, F ; Increment seconds
    18. MOVF secs, w
    19. SUBLW .60
    20. BTFSS STATUS, Z ; 60 seconds elapsed?
    21. RETURN ; No, done
    22. CLRF seconds ; Clear seconds
    23. INCF mins, f ; Increment minutes
    24. MOVF mins, w
    25. SUBLW .60
    26. BTFSS STATUS, Z ; 60 seconds elapsed?
    27. RETURN ; No, done
    28. CLRF mins ; Clear minutes
    29. INCF hours, f ; Increment hours
    30. MOVF hours, w
    31. SUBLW .24
    32. BTFSS STATUS, Z ; 24 hours elapsed?
    33. RETURN ; No, done
    34. CLRF hours ; Clear hours
    35. RETURN ; Done
    This was all copied from the datasheet, you would be amzed in how much you can learn about a device when you read its documents :)

    My .02
     
  3. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    You can not have your interrupt code to be constantly running in the background. But the the timer will run constantly. Then the timer overflows a interrupt will be generated . This will force the CPU to step out of the main code can execute the ISR code segment. Then the ISR code is done it will return to the main code at the point there it was interrupted.
     
  4. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    You can set the upper timer bit during the interrupt routine so it will overflow at 32768.
     
  5. ATM

    Thread Starter Member

    Oct 8, 2009
    31
    0
    I've been through the data sheet. I'm writing the code in C, (see above..) as i've never studied assembler language so I don't know how to follow that code, and I'd rather not just copy and paste something without understanding how it works.

    In my actually code im unclear as how to make the program execute the interrupt code once the T1 reg has overflowed.
     
    Last edited: Feb 19, 2010
  6. ATM

    Thread Starter Member

    Oct 8, 2009
    31
    0
    Yea, thats what I have done.

     
  7. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234

    The code will execute on its own when the interrupts occurs, you have to enable this interrupt and place the code in the interrupt vector, and you also have to set the config options to enable the Timer1 interrupt, and configure the timer for counter mode with external clock source so it will trigger the interrupt when its count overflows......
     
    ATM likes this.
  8. ATM

    Thread Starter Member

    Oct 8, 2009
    31
    0
    Could you elaborate on this please, I already understand everything else, forgive my naivety. The thing i'm unsure with is, how does the program know which segment of code is the interrupt code. Surely just naming it 'ISR' is not enough.
     
  9. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234


    That all depends on the compiler you are using, by placing it in a code segment starting with void ISR() the compiler knows to place this part of the code in the Interrupt Service Routine..... I have not personally used source boost but in Microchips C compiler you would write the code like this....

    Code ( (Unknown Language)):
    1.  
    2. void __ISR( _RTCC_VECTOR,ipl1) RTCCInterrupt( void)
    3. {
    4.    //code goes here for the RTCC interrupt for the Pic32MX
    5.     mRTCCClearIntFlag();    //Clear Interrupt Flag
    6.    
    7. }//RTCC Interrupt
    8.  
    check the compilers help files to see how it handles ISR code.....
     
  10. ATM

    Thread Starter Member

    Oct 8, 2009
    31
    0
    Looking into it now, thanks for your help.
     
Loading...