Using Real time Counter on timer 2 with 32Khz crystal

  • Sourcy

    Sourcy - 2014-06-30


    If I connect a 32Khz crystal on timer 2 and then define portUSE_TIMER2. Will the RTOS automatically use the 32Khz crystal for timing?

  • Phillip Stevens

    Phillip Stevens - 2014-07-01

    That should work, at least for the ATmega1284p and ATmega2560 devices, without any changes in the code. Both of these have specific Timer2 inputs.

    For the ATmega328p I have not put the necessary #define portUSE_TIMER2 in the code to allow Timer2 to work, because the ATmega328p doesn't have Timer2 available.

    Note that the freeRTOS timing is not equivalent to the MCU timing. You still need to have the fuses set for the MCU crystal/oscillator which is connected to the XTAL pins.

    If you want, it is possible to set the fuses to use the internal RC oscillator to drive the MCU, and an external 32kHz watch crystal attached to TOSC pins to derive real time for freeRTOS. But, this is an option that I've not experimented with. It is quite difficult to derive this configuration using standard Arduino hardware, like the UNO, because it has the 16MHz crystal ocillator soldered on the board, with unsuitable SMD capacitors for a 32kHz crystal.

    Last edit: Phillip Stevens 2014-07-03
  • Sourcy

    Sourcy - 2014-07-03

    I need to have very good timing for the RTOS. I know Atmel data sheet states the timers are real time counters. But I want to use a 32KHz crystal for maximum accuracy.

    Now with that said I think you may have lost me. I am using atMega2560 which is broken on a arduino board. This is temporary while i raise some funds to print my own PCB.

    When you say fuses which fuses are you talking about?

    As far as I understand the MCU uses a 16MHz external crystal on the XTAL pins, which is what I want.
    Then I want the 32KHz crystal to be used for timing of the RTOS, wich is connect to TOSC pins.

    As far as I understand from the code if I attach a 32KHz crystal on timer 2 and then set portUSE_TIMER2. The OS should now be running on the 32KHz crystal. Please correct me if I am wrong.

    Just one other question can the timing library still work if this is the case? As if I want to have a real time clock.

    If I miss understand anything please let me know and could you kindly explain as best as you could.


  • Phillip Stevens

    Phillip Stevens - 2014-07-03

    If you are using ATmega2560 then everything will work as expected.

    The Fuses are used to set the MCU system clock source. Typically an Arduino will use 16MHz low power crystal, although full swing oscillator is advisable if you have an electrical noisy environment. This is nothing to do with the freeRTOS timing.

    If you connect a 32kHz watch crystal (with 6pF or 12pF capacitors depending on the crystal, see the Goldilocks schematics for example of how to do this) to the Timer2 pins, then you can #define portUSE_TIMER2 and then the freeRTOS scheduler will use the watch crystal to deliver a real time accurate (to the accuracy of the crystal, I use 5ppm crystals for example) clock.

    You can then use the time.h functions to set and manage real time. Note that this library concentrates on second resolution, over days, as in a real time clock. But, it doesn't give you microsecond resolution.

    To get microsecond resolution, you'll need to dive into your code and access the ticksRemainingInSec variable to know how much of each Second remains to pass. You'll also know how many total ticks are in each second (portTickRateHz) so that gives you a fraction of a second resolution.

    Alternatively, to get event timers accurate down to the system tick level, you can also use the ttimer.h functions in uIP/sys. Typically, you'd design 256 ticks (interrupts) per second when using the 32kHz crystal on the Timer2 8 bit timer.

    Last edit: Phillip Stevens 2014-07-03
  • Sourcy

    Sourcy - 2014-07-03


    I don't need microSecond res for the timing library. I just thought it would be handy to implement the library later on.



Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks