Menu

Need help using 32k watch crystal as main oscilator on 16F19346

Help
2019-10-19
2019-11-25
1 2 > >> (Page 1 of 2)
  • Jim giordano

    Jim giordano - 2019-10-19

    Does this sound familiar? Well this is a total noob type question on the simplest of setup, nothing tricky like the other thread.

    I'm using a standard watch crystal across pins 2 and 3 (RA4 and RA5) and two 15pf caps to ground in the standard configuration. I know the crystal and caps are good as I took them from a working circuit using them across the same pins, but to drive timer1. I put an led from rc0 for an indicator.

    I wrote this test program-

    #option explicit
    #chip 16F18346,.032768
     ' tell GCB frequency in MHZ for delay and pcm routines
    
    #config  MCLRE=OFF
    #config FEXTOSC=LP
    #config RSTOSC=EXT1X  ' should not be needed for these tests
    

    ' Test 1

    do
          rc0=1
          wait 100 s
          rc0=0
          wait 100 s
    loop
    

    ' this routine results in the 10 changes in 2.6 seconds
    ' clearly something is wrong with the #chip definition line.

    'Test 2

    dim cnt as integer
        dim state as byte
        cnt=0
        state=0
        '  note in this loop it will do many ops to get to 32000
        '    so in reality should take several seconds per change
        do
              cnt=cnt+1
              if cnt>32000 then  ' should be much longer than a second
                  if state=0 then
                        state=1
                  else
                        state=0
                  end if
                  rc0=state
                  cnt=0
              end if
        loop
    

    'This procedure again gives about 10 changes in 2.6 seconds so the clock is running much faster than 32k.

    So....
    I clearly don't know what to put in for the #chip and the fextosc options.

    What am I not seeing here??????

     

    Last edit: Jim giordano 2019-10-19
  • Jim giordano

    Jim giordano - 2019-10-19

    p.s.-

    I just pulled the crystal out and ran the circuit and got the exact same results, so it isn't using the crystal at all that I can see.

     
  • David Stephenson

    As I found with the 16F18346 the OSCCON1 bits are not correctly set in GCB.
    This is what I found was needed (and maybe the oscillator enable).
    osccon1=b'01110000' 'select fextosc
    oscen.7=1 'enable extosc

    I also found what you did that some other oscillator was selected. On other chips the selected oscillator is often the internal low power which runs at 31.25 kHz so it can seem like the clock crystal is working (but a bit slow).

     
  • Jim giordano

    Jim giordano - 2019-10-19

    Yes, the primary purpose of this thread is to get GCB working the way it is meant to be. Using the simplest of programs, setting up all these things properly as designed.

     
  • David Stephenson

    I suppose it is inevitable in a project in of this kind that some rarely used features would not be supported. I've taken to doing the strange things the hard way (looking at the datasheet and seeing which registers need to be written to). Fortunately you can mix GCB with ASM and write to registers (as long as the compiled code does not overwrite them - can be a problem with the ADC)then it all works fine.

     
  • Anobium

    Anobium - 2019-10-19

    search for 16f19156 the chip with the rtcc. The thread should give the insights to resolve.

     
  • Jim giordano

    Jim giordano - 2019-10-19

    Hi Evan-

    If you are talking about the thread https://sourceforge.net/p/gcbasic/discussion/579126/thread/8ea6a50e/?limit=25#4a2e, then I couldn't find anything to address the problem as presented above.

    There is so much unrelated and unuseful stuff in there that it might as well be written in C for all I could use it.

    This is precisely why I started this thread. To get a simple solution that isn't all balled up with RTCC, VBat, SOSC, and a ton of other unrelated stuff.

    Avoid confuscation!

    This problem has nothing to do with RTCC or anything else in that thread!!!!!

     
  • mkstevo

    mkstevo - 2019-10-22

    Now I would have considered a 32k watch crystal to be exactly an RTCC. I thought RTCC indicated Real Time Clock Crystal? Which I presumed you meant with your "Watch Crystal"? Your application may not be using it to measure "Real Time", but the PIC may well be treating it as such?

     
  • Jim giordano

    Jim giordano - 2019-10-22

    The 16f19156 has a built-in RTCC (real time clock/calendar), which the 16f183xx series does not. Thus demonstrating the unnecessary obfuscation.

    And appologies for the tone of my previous post. Just really frustrated here. I'm currently tracing the GCB source code to see where the problem lies.

     
  • Anobium

    Anobium - 2019-10-22

    @jim. I am looking at this now. :-)

     
  • Anobium

    Anobium - 2019-10-22

    Please try this. I have documented the code.

    #chip 16F18346,.032768
    #config RSTOSC = SOSC
    
        'NOSC SOSC NDIV 1
        OSCCON1 = 0x30
        'CSWHOLD may proceed SOSCPWR Low power SOSCBE crystal oscillator
        OSCCON3 = 0x00
        'LFOEN disabled ADOEN disabled SOSCEN enabled EXTOEN disabled HFOEN disabled
        OSCEN = 0x08
        'HFFRQ 16_MHz
        OSCFRQ = 0x06
        'HFTUN 0
        OSCTUNE = 0x00
    
     
  • Jim giordano

    Jim giordano - 2019-10-22

    Using that code and running the test:

    do
    rc0=1
    wait 1 s
    rc0=0
    wait 1 s
    loop

    it takes about 1.3 seconds between each change of led state. So not quite right yet.

     
  • David Stephenson

    I still get confused by the distinction (or lack of) between the SOSC and the LF oscillators.
    Is it that SOCS can be used for peripheral only (and the CPU clock if you want) and LF has to be used for the CPU clock(but can also be directed to peripherals).
    I got caught out today by a chip (16F18855) that has separate pins for the two functions (LF is on A.6 and A.7 and the SOSC is on C.0 and C.1).
    I think we were also wondering why GCB does not automatically set the OSCCON registers based on the oscillator choosen in the configuration.

     

    Last edit: David Stephenson 2019-10-22
  • David Stephenson

    Jim, I'm not sure that WAIT will be that accurate when running at 32768 Hz.
    I've used my code to feed into timer1 and produce an interrupt every minute and it is exact (I'm getting better than 1 sec/month agreement - with calibration).
    On these chips there is a "failsafe oscillator" which runs at about 1 MHz when the selected oscillator cannot be found.

     
  • Jim giordano

    Jim giordano - 2019-10-22

    I couldn't think of an easy way to test without involving a timer, which will be the next can of worms after this first step is fixed.

     
  • Anobium

    Anobium - 2019-10-23

    @Jim. Let us test and interrupt to resolve the 1s count issue.


    Using the internal OSC and then using the SOSC as the clock source example. I used one of the demos and just changed the chip. Tested on 18446 - works nicely.

    #chip 16F18346,32                'Specify the Device you are compiling for
        #option Explicit                 'This option ensures that all variables are dimensioned in the user program.  See http://gcbasic.sourceforge.net/help/__option_explicit.html
    
        #define LED0 porta.2             'Set a constant to refer to an LED.
        dir LED0 out                     'Set LED as an output
    
        InitTimer1 SOSC,0                ' 32768 crystal time keeper
        On Interrupt Timer1Overflow Call CheckClock
        SetTimer 1, 0x8000                'TMR1H = 0x80    ' set up for one second delay.
        StartTimer 1
    
        do
        loop
    
        sub CheckClock
    
          SetTimer 1, 0x8000              'TMR1H = 0x80    ' set up for one second delay
    
          LED0=!LED0
        end sub
    
    
        'Optimise code
        #define USE_Timer0 false
        #define USE_Timer2 false
        #define USE_Timer3 false
        #define USE_Timer4 false
        #define USE_Timer5 false
        #define USE_Timer6 false
    

     
  • Anobium

    Anobium - 2019-10-23

    Same code but use the SOSC as the main clock also. At little tweak from the code posted above. I just changed the frequency and the clock source.

    Just checked with scope... every 1s

        #chip 16F18346,.032768           'Specify the Device you are compiling for
        #option Explicit                 'This option ensures that all variables are dimensioned in the user program.  See http://gcbasic.sourceforge.net/help/__option_explicit.html
        #config RSTOSC = SOSC            'Specify clock source
    
        #define LED0 porta.2             'Set a constant to refer to an LED.
        dir LED0 out                     'Set LED as an output
    
        InitTimer1 SOSC,0                ' 32768 crystal time keeper
        On Interrupt Timer1Overflow Call CheckClock
        SetTimer 1, 0x8000                'TMR1H = 0x80    ' set up for one second delay.
        StartTimer 1
    
        do
        loop
    
        sub CheckClock
    
          SetTimer 1, 0x8000              'TMR1H = 0x80    ' set up for one second delay
    
          LED0=!LED0
        end sub
    
    
        'Optimise code
        #define USE_Timer0 false
        #define USE_Timer2 false
        #define USE_Timer3 false
        #define USE_Timer4 false
        #define USE_Timer5 false
        #define USE_Timer6 false
    
     

    Last edit: Anobium 2019-10-23
  • David Stephenson

    I've moved to a different chip (I needed more pins). This works, notice I am using the LP oscillator (not the SOSC).
    I have got it running a LED 4x7 segment display and in night-time setting the batteries should last about 7 years (2xAA). I've had trouble with interrupts in the past so I prefer to read the interrupt flag directly.

    #chip 16lf18855, .032768
    #config fextosc=lp,rstosc=ext1x, boren=off,lpboren=off,zcd=off
    #config mclre_off, WDTE=off, CLKOUTEN_OFF, CP_OFF, WRT_OFF, LVP_OFF, CPD_OFF
    'lets make sure everything is off as this is running on batteries
    
    osccon1=b'01110000' 'select fextosc
    
    'pmd0-5 setting?Do I need to do this?
    pmd0=b'01111111'
    pmd1=b'11111101'
    pmd2=b'11011111'
    pmd3=b'11111111'
    pmd4=b'11111111'
    pmd5=b'11111111'
    nop
    nop
    nop
    t1clk=b'00000001' 'select fosc/4 as clk source
    
    t1con=b'00110001'' 1:8 and enable
    trisa=b'11000000''set crystal pins as input
    trisb=b'00000000'
    trisc=b'00000000'
    intcon.6=1 'peie - enable interrupts
    pie4.0=1 'timer1 overflow interrupt on
    pir4.0=0 'timer1 interrupt flag off
    tmr1h=b'00010000'
    tmr1l=b'00000000'
    do
    if pir4.0=1 then
    pir4.0=0
    kminute=kminute+1
    if kminute=60 then kminute=0:khour=khour+1
    if khour=13 then khour=1
    loop
    
     

    Last edit: David Stephenson 2019-10-23
    • Anobium

      Anobium - 2019-10-23

      @David.. can you port the code I posted to 18855? Let us to keep a common solution.

       
  • David Stephenson

    I had some difficulties getting the SOSC to work consistently on the 18855 and it kept defaulting to the 1MHz failsafe. it might have saved a bit of current using the SOSC, as I could have used SLEEP and kept TIMER1 going (but it would have only saved about 5 uA).
    The problem with testing your code is I will have to shift the clock crystal to different pins and I've got the project boxed up at the moment doing a long term stability/readability test.

     
    • Anobium

      Anobium - 2019-10-23

      @David. Not sure what you mean.

      On the 16lf18855 the SOSC1 and SOSC2 are on RC0 and RC1 respectively. So, these are fixed and the code I posted would work. How do you change the SOSC1 and SOSC2 clock port assignments (ready to learn here).

      The LED is just a simple code change... to whatever port you are using.

      Config:

      Using fextosc=lp is a good setting. I like that. But, using ext1x meant you had to set the External Clock set to LP. I am not sure the different power consumption between fextosc=lp,rstosc=ext1x and rstosc=sosc but from a user perspect the latter is easier to understand.

      I think the rest of the config is set expliciity by Great Cow BASIC as follows, so not needed: This extract is from the chipdata.def (shown below is a new resource we will publish within the next release).

      [16LF18855]
      CP_OFF
      LVP_OFF
      WDTE_OFF
      MCLRE_OFF
      CLKOUTEN_OFF
      **RSTOSC_HFINT32**  set by the demo code above to SOSC
      FEXTOSC_OFF
      
       
  • Jim giordano

    Jim giordano - 2019-10-23

    Evan-

    That works perfectly.
    I'm flabergasted.
    This is the obvious, simple, straightforward way to do this.
    I can't believe in everything I tried, I didn't try this exact setup.
    Thank you for your patience.

     
    • Anobium

      Anobium - 2019-10-23

      Pleasure. When I work this stuff out. I create a demo. That way... I forget about it until I need it again.

      obvious, simple, straightforward way to do this.

      It is when you see it working. :-)

       
  • David Stephenson

    Sorry I did not get back on this one I got the osc to work so I carried on with the other stuff.
    I included an alarm and I also was experimenting with left justifying the hours (as I am using a full 4-digit-7seg LED display) and I think I like it better than the normal right justified.
    Piezo sounders are strange things the one I was using was almost silent until it was put in a cavity to match its natural frequency now it is LOUD.

    The LP osc is connected to A.6 and A.7 so these are different pins.
    The LP oscillator HAS to feed the processor and will stop during sleep, the socs can feed the timers and/or the processor and it will keep running during sleep saving power and still keeping time, but when the display is active the processor needs to run anyway (it's complicated)

     
  • Jim giordano

    Jim giordano - 2019-11-24

    Evan-
    We are back to this one again after unexpected results in another circuit.

    Here is the last program you entered above, with an insert by me.
    I added another led at pin rb7 for another test.
    The insert runs through a for loop 3000 times and switches the led state.
    Looking at the asm file, there are 16 instruction generated for the FOR loop.
    So by my calculations, it should take 3000 times 16 clock cycles between led changes or roughly 48000/32768 seconds = about 1.5 seconds. The led flashes too fast to see, and I would have to change to loop to execute 100000 times to get the one second flash rate. Clearly the chip is not running at 32k.

       #chip 16F18346,.032768           'Specify the Device you are compiling for
        #option Explicit                 'This option ensures that all variables are dimensioned in the user program.  See http://gcbasic.sourceforge.net/help/__option_explicit.html
        #config RSTOSC = SOSC            'Specify clock source
    
        #define LED0 porta.2             'Set a constant to refer to an LED.
        dir LED0 out                     'Set LED as an output
    
        InitTimer1 SOSC,0                ' 32768 crystal time keeper
        On Interrupt Timer1Overflow Call CheckClock
        SetTimer 1, 0x8000                'TMR1H = 0x80    ' set up for one second delay.
        StartTimer 1
    
    
    ' code added by jim to test instruction rate
    dim nn as word
    #define led1 portb.7
    dir led1 out
    led1=1  ' test clock by instruction can execute in a second or so
    do  ; flash led
      for nn=1 to 3000:next  ; loop should take about 16 clock cycles / nn
      led1=!led1             ; = 48000 ticks = change about every 1.5 seconds.
    loop
    ' end added code
    
    
        do
        loop
    
        sub CheckClock
    
          SetTimer 1, 0x8000              'TMR1H = 0x80    ' set up for one second delay
    
          LED0=!LED0
        end sub
    
    
        'Optimise code
        #define USE_Timer0 false
        #define USE_Timer2 false
        #define USE_Timer3 false
        #define USE_Timer4 false
        #define USE_Timer5 false
        #define USE_Timer6 false
    

    So any ideas to get the chip to run at 32K?

     

    Last edit: Jim giordano 2019-11-24
1 2 > >> (Page 1 of 2)

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.