Menu

How do I use external crystal on 16F18426

Help
2019-01-15
2019-01-23
  • Jim giordano

    Jim giordano - 2019-01-15

    This is another Noob question, thanks for your patience.

    I'm trying to use a one second timer by attaching a clock crystal to timer1.
    This works perfectly on a 16F324 chip, but not on the 16F826 and I can't see what I'm missing.

    The interupt is never getting called. It's the exact same circuit, just pop one chip out and the other in.

    '#chip 16F18324,32
    '#define chipno 1
    #chip 16F18426,32
    #define chipno 2
    
    #config RSTOSC=HFINT32, MCLRE=OFF
    #config FEXTOSC=OFF, CLKOUTEN=OFF ', WRT=OFF, CPD=on
    
    DIR PortA.4 IN  ' 32k crystal
    DIR PortA.5 IN  ' 32k crystal
    
    dir PortA.2 out  ' pulse led once per second
    InitTimer1 ExtOsc,0  ' 32768 crystal time keeper
    
    On Interrupt Timer1Overflow Call CheckClock
    
    ClearTimer 1
    StartTimer 1
    
    do
      if DoClock then
        DoClock=0
        PulseOut PortA.2,100 ms  ' pulse led
      end if
    loop
    
    sub CheckClock
      TMR1H = 128 'TMR1H = 0x80    ' set up for one second delay
      DoClock=1   ' time to blink led
    end sub
    
     

    Last edit: Jim giordano 2019-01-15
    • Anobium

      Anobium - 2019-01-16

      These are different microcontroller families therefore there is more of a setup required for the 16f18426... T1CK1. This part requires PPS where the other parts may have a T1CK1 default port assignment.

      T1CKIPPS = 0x05;   //RA5->TMR1:T1CKI;
      

      But, you should always add PPS for both parts and I would ensure you understand the need for the port DIRection.

      Anobium

       
      • Jim giordano

        Jim giordano - 2019-01-16

        Evan-
        I simplified the program as much as possible. I used all the default ports so I wouldn't have to mess with PPS to present the smallest code I could for people to look at. I'm not sure what you were referring to when you said "ensure you understand the need for the port DIRection", any enlightment will be appreciated.

         
  • David Stephenson

    I think the T1CKIPPS is for a clock pulse on a particular pin and not for an external crystal. The external clock crystal has to be on A.4 and A.5.

    You need to set the TMR1CLK register to b'00000111' to select the secondary oscillator(SOCS) directed to timer1. Then set T1CON.0=1 to activate timer 1.

     
  • David Stephenson

    Oops! I got the chip number wrong (that above is for the 16F18426).
    For the 18324 you just need to set the T1CON=b'10001001'
    To check if the timer is working, but the crystal is not oscillating change to
    T1CON=b'11001001' this will select the LFINTOSC (at 31.25 kHz)

     
  • Jim giordano

    Jim giordano - 2019-01-16

    David-
    You were right the first time, it was meant for the 16F18426 :)

    All it took was TMR1CLK=b'00000111' after doing the InitTimer1.

    I printed out TMR1CLK after doing the InitTimer1 and it was set to 3 (b'00000011') which is HFINTOSC rather than SOSC.

    I couldn't find anywhere in the chipdata file where the clock sources for the timers are indicated. Perhaps there's something I should be using rather ExtOsc.

     
  • Jim giordano

    Jim giordano - 2019-01-23

    There is a new timer.h file available that fixes the problem for both the 16F1826 and 16F18324 familys. It is no longer necessary to manually set TMR1CLK.

    InitTimer1 SOSC,0 or InitTimer1 ExtOsc,0 is now working properly :)

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.