Counting Timer0 and Timer1 overflows without any interrupt handler


Closed Thread
Results 1 to 33 of 33

Hybrid View

  1. #1
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default Re: Counting Timer0 and Timer1 overflows without any interrupt handler

    Quote Originally Posted by dw_picbasic View Post
    The following PBP3 code is my first attempt at coding for a frequency counter that can read above 500khz...
    It does work, but its not accurate enough to be considered anything but educational or a fun project.
    Since it's not a problem for the PIC chip, or for PBP ...
    Will there be a Second Attempt?

    I'd hate to interfere with the learning process.

    But I would suggest using the Timer0 input (T0CKI) for the frequency to be measured.
    The Timer0 prescaler has a minimum period of 20nS, which means it can take up to 50Mhz.
    Timer1 (T1CKI) can only take up to 1Mhz with a 4Mhz OSC, or 5Mhz with a 20Mhz OSC.

    Any frequency measurement requires an accurate "Time Base".
    And the easiest way to get that is to use a 32768 Hz crystal on the Timer1 oscillator.
    This way, Timer1 generates the 1 sec window, and Timer0 (and it's overflows) counts the frequency.

    It is a little tricky to read the Timer0 prescaler to get the lowest byte of the count though.
    The prescaler should be 1:256 (the default).

    Oh, and the PICs oscillator should be as fast as possible, so that the program can respond to the hardware flags, which have to be polled.
    The fastest oscillator for the 16F877A is 20 Mhz.

    A simulation works good up to 600Kz. Reading the exact Hz. (actual results will depend on the 32768 crystal tolerance)
    A real chip should go much higher, but the SIM crashes with the higher frequency.

    Name:  FREQy.jpg
Views: 11045
Size:  106.5 KB

    Sometimes you just need to know it can be done ... so you can figure it out on your own.
    DT

  2. #2
    Join Date
    Jan 2007
    Posts
    78


    Did you find this post helpful? Yes | No

    Default Re: Counting Timer0 and Timer1 overflows without any interrupt handler

    Darrel,
    That's an ***AWESOME*** post!!
    It hadn't occurred to me to flip the timers around that way.
    Also, I need to go back to the data sheet and look at the electrical characteristics again for timer0 clock minimum pulse width.
    Per your info about the 20nSec I need to read through that again.
    I definitely want to try it!!
    This will be the first time, for me learning to setup Timer1 with external crystal for time base.
    Perhaps I would do well reading up on how that functionality is used for a standard 12hour clock.

    Also, what simulator are you using?
    It looks totally awesome!!
    Many thanks
    Duane :-]

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default Re: Counting Timer0 and Timer1 overflows without any interrupt handler

    The simulator is Proteus VSM from Labcenter in the UK.
    It's a bit pricey, but does an amazing job. Couldn't do my job without it.

    And I just want to point out how I have RA3 and RA4 tied together in the above schematic.
    It's a very important part of making the freq. counter work.
    DT

  4. #4
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: Counting Timer0 and Timer1 overflows without any interrupt handler

    Thanks Darrel,
    I never could get my head around how to do that before, now I see tmr0 is a counter, tmr1 is the time base.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    Did you find this post helpful? Yes | No

    Default Re: Counting Timer0 and Timer1 overflows without any interrupt handler

    And I just want to point out how I have RA3 and RA4 tied together in the above schematic.
    It's a very important part of making the freq. counter work.
    OK, I'll bite.... Are using RA3 to gate the signal for the T0CKI-pin, hence the seriesresistor on the input?

    /Henrik.

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default Re: Counting Timer0 and Timer1 overflows without any interrupt handler

    The two pins tied together (and the resistor) serve two purposes.

    1. You're right Henrik, since Timer0 on 16F's can't be stopped and started like all the other timers, it's used to gate the incoming signal.

    2. You can't directly read the value in Timer0's prescaler. So after counting pulses for the 1 sec. window, you have to provide additional pulses to the input until the value in TMR0 changes. The number of pulses you add tells you how many counts were already in the prescaler. This gives the low Byte of the final 32-bit value.

    The result is made up of the Timer0 overflows (HighWord), the TMR0 value (HighByte of the LowWord) and the prescaler value (LowByte of the LowWord)
    DT

  7. #7
    Join Date
    Jan 2007
    Posts
    78


    Did you find this post helpful? Yes | No

    Default Re: Counting Timer0 and Timer1 overflows without any interrupt handler

    Hmmmmm.
    I was thinking I was going to have to use a mosfet solid state relay at the input from the frequency source, so that I could pump Timer0 through the remaining clock cycles necessary to raise its flag again.
    I can see how pulling the Timer0 input pin high will disable the incoming clock from the frequency source.
    But I can't see how it could be used to pump the remaining cycles to get Timer0 to its next overflow since there is no way to dis-engage the frequency source signal. The remaining clocking and the frequency source would conflict with one another.

    Additionally, I was wondering if there is another way of pumping Timer0 to overflow, by using an internal software loop rather than doing it through an external clocking pin? I was wondering if a routine of adding 1 to TIMRL register and testing for a TMR1IF flag might work just as well.


    Unless I'm wrong that the intent is to pump Timer0 from an output pin from the PIC?

    Thanks :-]
    Last edited by dw_picbasic; - 7th February 2014 at 18:36.

Similar Threads

  1. Expanding Timer1 counting range
    By sycluap in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 3rd February 2014, 23:00
  2. PIC18 timer0 interrupt handler, still not getting it
    By ImAnEE in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 30th April 2011, 01:25
  3. DT-INTs Interrupt handler question
    By circuitpro in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 8th January 2010, 01:06
  4. Synchronising Timer0 and Timer1
    By manxman in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 10th April 2008, 00:12
  5. 16F690 TIMER0 and TIMER1
    By nickdaprick in forum mel PIC BASIC
    Replies: 2
    Last Post: - 18th April 2007, 15:49

Members who have read this thread : 2

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