32.768kHz external crystal for TMR1 time base and internal 4MHz oscillator


Closed Thread
Results 1 to 9 of 9
  1. #1
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891

    Default 32.768kHz external crystal for TMR1 time base and internal 4MHz oscillator

    Hi there,

    I'd like to have my external 32.768kHz crystal as time base onTMR1 and, in the same time, run the internal oscillator at 4MHz.

    Thoretically, this simple code hereunder should blink my LED only when the TMR1IF overflow flag is set.

    But...

    This code works erratically:
    - sometimes the program starts, sometimes not
    - commenting the fuses will mostly make the program start almost always
    - physically removing the 32.768kHz crystal from my breadboard will change nothing => when the program works, the LED still blinks?!

    Code:
    ' ====== FUSES ====================================================================================
    ' PIC 16F690
    @ __Config _FCMEN_ON &_IESO_OFF &_CPD_OFF &_WDT_OFF &_BOR_OFF &_CP_OFF &_PWRTE_OFF &_MCLRE_OFF &_LP_OSC 
    
    ' ====== INTERRUPT SERVICE ROUTINE ================================================================
    GOTO INIT: ' Just to place ISR at top of program
    DISABLE
    ISR: 'if TMR1IF is set (TMR1 has overflown)
        toggle PORTB.6 'LED
        PIR1.0 = 0 ' clear TMR1IF overflow flag
        resume
    ENABLE
    
    ' ====== INITIALIZE ===============================================================================
    INIT:
    PAUSE 1000 'circuit settle time
    
    ' ====== DEFINES ==================================================================================
    DEFINE OSC 4
    
    ' ====== REGISTERS ================================================================================
    '             76543210
    OPTION_REG = %10000000 ' Pull-Ups disabled
    OSCCON     = %01100000 ' Internal RC set to 4Mhz
    ANSEL      = %00000000 ' Analog inputs Channels 0 to 7
    ANSELH     = %00000000 ' Analog inputs Channels 8 to 11
    INTCON     = %11000000 ' INTerrupts CONtrol: GIE is ON, PEIE is ON 
    T1CON      = %00111001 ' Timer1 OSC enabled, Timer1 enabled, presc.1:8
    PIE1       = %00000001 ' Enable TMR1IF overflow flag
    
    ' ====== PROGRAM ==================================================================================
    ON INTERRUPT GOTO ISR
    
    MAIN:
        goto MAIN:
    END
    Setting the FCMEN to OFF will never let the program run which lets me think the 32.768Khz crystal doesn't actually run at all. But le LED blinks?!

    What am I missing?
    Roger

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,383


    Did you find this post helpful? Yes | No

    Default Re: 32.768kHz external crystal for TMR1 time base and internal 4MHz oscillator

    TMR1CS:
    Timer1 Clock Source Select bit




    T1CON = %00111011 ' Timer1 OSC enabled, Timer1 enabled, presc.1:8

    Warning I'm not a teacher

  3. #3
    Join Date
    May 2013
    Location
    australia
    Posts
    2,383


    Did you find this post helpful? Yes | No

    Default Re: 32.768kHz external crystal for TMR1 time base and internal 4MHz oscillator

    plus config needs to enable internal osc
    Code:
     __config  _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOD_ON & _IESO_ON & _FCMEN_ON
    Warning I'm not a teacher

  4. #4
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891


    Did you find this post helpful? Yes | No

    Default Re: 32.768kHz external crystal for TMR1 time base and internal 4MHz oscillator

    I've modified the code according to the answers.

    But with or without the 32,768kHz crystal, the LED will always blink.

    As per datasheet, I have enabled LP oscillator. Why should I set the internal oscillator instead please?

    Same question for T1CON.TMR1CS; why should I set this one?

    Code:
    ' ====== FUSES ====================================================================================
    ' PIC 16F690
     __config  _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOD_ON & _IESO_ON & _FCMEN_ON
    
    ' ====== INTERRUPT SERVICE ROUTINE ================================================================
    GOTO INIT: ' Just to place ISR at top of program
    DISABLE
    ISR: 'if TMR1IF is set (TMR1 has overflown)
        toggle PORTB.6 'LED
        PIR1.0 = 0 ' clear TMR1IF overflow flag
        resume
    ENABLE
    
    ' ====== INITIALIZE ===============================================================================
    INIT:
    PAUSE 1000 'circuit settle time
    
    ' ====== DEFINES ==================================================================================
    DEFINE OSC 4
    
    ' ====== REGISTERS ================================================================================
    '             76543210
    OPTION_REG = %10000000 ' Pull-Ups disabled
    OSCCON     = %01100000 ' Internal RC set to 4Mhz
    ANSEL      = %00000000 ' Analog inputs Channels 0 to 7
    ANSELH     = %00000000 ' Analog inputs Channels 8 to 11
    INTCON     = %11000000 ' INTerrupts CONtrol: GIE is ON, PEIE is ON 
    T1CON      = %00111011 ' Timer1 OSC enabled, Timer1 enabled, presc.1:8
    PIE1       = %00000001 ' Enable TMR1IF overflow flag
    
    ' ====== PROGRAM ==================================================================================
    ON INTERRUPT GOTO ISR
    
    MAIN:
        goto MAIN:
    END
    Roger

  5. #5
    Join Date
    May 2013
    Location
    australia
    Posts
    2,383


    Did you find this post helpful? Yes | No

    Default Re: 32.768kHz external crystal for TMR1 time base and internal 4MHz oscillator

    As per datasheet, I have enabled LP oscillator. Why should I set the internal oscillator instead please?
    if I understand correctly your intent is to use the intosc @4mhz for cpu clock and the ext osc @32khz for timer1 clk source

    so to use the HFINTOSC @4mhz config= _INTRC_OSC_NOCLKOUT [no clk out assumed]
    osccon= $60
    Same question for T1CON.TMR1CS; why should I set this one?
    to use the ext osc for timer1

    you must enable the ext osc
    bit 3 T1OSCEN: LP Oscillator Enable Control bit
    and
    select ext osc as timer clock source
    bit 1 TMR1CS: Timer1 Clock Source Select bit

    so t1con= %00111011 with 8:1 prescale and timer on
    Warning I'm not a teacher

  6. #6
    Join Date
    May 2013
    Location
    australia
    Posts
    2,383


    Did you find this post helpful? Yes | No

    Default Re: 32.768kHz external crystal for TMR1 time base and internal 4MHz oscillator

    the data sheet says that T1OSCEN should be enable and the user should delay for a suitable time before timer is enabled

    I don't have a 16f690 handy to test with
    Warning I'm not a teacher

  7. #7
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891


    Did you find this post helpful? Yes | No

    Default Re: 32.768kHz external crystal for TMR1 time base and internal 4MHz oscillator

    Thanks a lot Richard,

    The code hereunder works

    BTW, I had to change the config settings (fuses) since you might have copied them for another PIC and generated errors while compiling. Please let me know if I should take care to change some of them.

    For the test, I removed the 32,768kHz crystal while running () and the LED stops blinking which is correct.

    Code:
    ' ====== FUSES ====================================================================================
    ' PIC 16F690
    @ __config _INTRC_OSC_NOCLKOUT &_WDT_OFF &_PWRTE_OFF &_MCLRE_OFF &_CP_OFF &_CPD_OFF &_BOR_OFF &_IESO_OFF &_FCMEN_OFF
    
    ' ====== INTERRUPT SERVICE ROUTINE ================================================================
    GOTO INIT: ' Just to place ISR at top of program
    DISABLE
    ISR: 'if TMR1IF is set (TMR1 has overflown)
        toggle PORTB.6 'LED
        PIR1.0 = 0 ' clear TMR1IF overflow flag
        TMR1H  = 0
        TMR1L  = 0
        resume
    ENABLE
    
    ' ====== INITIALIZE ===============================================================================
    INIT:
    PIR1.0 = 0 ' clear TMR1IF overflow flag
    TMR1H  = 0
    TMR1L  = 0
    
    ' ====== DEFINES ==================================================================================
    DEFINE OSC 4
    
    ' ====== REGISTERS ================================================================================
    '             76543210
    OPTION_REG = %10000000 ' Pull-Ups disabled
    OSCCON     = %01100000 ' Internal RC set to 4Mhz
    ANSEL      = %00000000 ' Analog inputs Channels 0 to 7
    ANSELH     = %00000000 ' Analog inputs Channels 8 to 11
    INTCON     = %11000000 ' INTerrupts CONtrol: GIE is ON, PEIE is ON 
    T1CON      = %00111011 ' Timer1 OSC enabled, Timer1 enabled, presc.1:8
    PIE1       = %00000001 ' Enable TMR1IF overflow flag
    
    ' ====== PROGRAM ==================================================================================
    PAUSE 1000 'circuit settle time
    
    ON INTERRUPT GOTO ISR
    
    MAIN:
        goto MAIN:
    END
    Roger

  8. #8
    Join Date
    May 2013
    Location
    australia
    Posts
    2,383


    Did you find this post helpful? Yes | No

    Default Re: 32.768kHz external crystal for TMR1 time base and internal 4MHz oscillator

    BTW, I had to change the config settings (fuses) since you might have copied them for another PIC and generated errors while compiling. Please let me know if I should take care to change some of them.
    config was generated by meCONFIG for pbp3
    Warning I'm not a teacher

  9. #9
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891


    Did you find this post helpful? Yes | No

    Default Re: 32.768kHz external crystal for TMR1 time base and internal 4MHz oscillator

    Quote Originally Posted by richard View Post
    config was generated by meCONFIG for pbp3
    I have 2.6 so this may be why there is a change
    Roger

Similar Threads

  1. 1 x external crystal oscillator for multiple PICs
    By harryweb in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 27th November 2013, 15:05
  2. Replies: 12
    Last Post: - 21st July 2012, 21:27
  3. question about pic16f877a and 32.768khz crystal
    By amenoera in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 17th October 2009, 10:38
  4. First time with external oscillator - 16F877a Not working
    By financecatalyst in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 15th October 2009, 18:14
  5. 16f877A and tmr1 external crystal question
    By comwarrior in forum General
    Replies: 3
    Last Post: - 13th July 2009, 01:40

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts