PIC Microcontroller RTC Views

Discussion in 'Embedded Systems and Microcontrollers' started by dhruv1128, Jun 28, 2014.

  1. dhruv1128

    Thread Starter New Member

    Jun 28, 2014
    6
    0
    Hello Everyone,

    I want to manufacture a Datalogger using PIC microcontroller. I am logging the data every minute and storing in a SD card along with the timestamp. The temperature of the surrounding environment would be around 50 *C. So, what would be better for generating timestamp :

    1. To generate the timestamp using the timers of the pic controller internally through coding.

    2. To use RTC modules like DS1307, DS3231 externally connected to the circuit.

    Any other options are welcomed.

    Hope for a reply

    Thank you
     
  2. JohnInTX

    Moderator

    Jun 26, 2012
    2,345
    1,026
    Its easy enough to generate an interrupt driven system tick using TMR2/PR2 of most PICs. You'll need an accurate oscillator (crystal controlled). Usually the period is on the order of 1-10ms. Just count interrupts to seconds and count minutes and hours from there.

    You also can use TMR1's internal oscillator with an external 32KHz watch crystal to generate (slower) tiks. This setup can be configured to keep time using battery backup (the PIC sleeps while the timer keeps running) during periods of power off.

    Using the PIC internal timers will require some additional programming but that's offset by not having to buy and program the interface for an external RTC.

    There are many threads here on just that subject. Poke around.

    Have fun.
     
    dhruv1128 likes this.
  3. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    Some PICs have integrated RTCC now such as PIC32.

    Of course they must kept powered on all the time. Using a module with battery can be easier.

    If power supply is secure, then of course using the PIC for RTCC is better.

    Or both things, loading from RTCC module, then using the 512 Hz test signal to update the clockkeeping.
     
    dhruv1128 likes this.
  4. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,804
    832
    The_RB (AAC member) has a page in his site that describes how to accurately time using a PIC. Take a look here
     
    dhruv1128 likes this.
  5. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,388
    497
    Don't forget leap years.
     
    dhruv1128 likes this.
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,387
    1,605
    While some PICs come with an internal RTC they are not the easiest thing to use, not if you wish to hold the time date during power outages.

    My choice when doing this was to use an external RTC to keep the time.

    The reasons are multiple in choosing the PCF2129A to do this:
    - very accurate time
    - it has it's own battery backup, so set time today and use it for a long long time.
    - has internal crystal, one less part to install, plus defined strays for increased accuracy.
    - at about $3USD in single quantities is less then half the cost of alternatives.
     
    dhruv1128 and absf like this.
  7. NorthGuy

    Active Member

    Jun 28, 2014
    603
    121
    The internal PIC's RTCC combined with a sleep mode consumes very little current and doesn't require any external parts except for the watch crystal. You only need to design your battery backup in such a way that PIC always has power.

    The added benefit is that you can easily wake up at any time if you need to. For example, you can wake up when a button is pressed and turn on the main power. You can also turn off the main power without losing memory state.
     
    dhruv1128 likes this.
  8. Potato Pudding

    Well-Known Member

    Jun 11, 2010
    684
    92
    I also doubt that adding a coin cell battery backup to your PIC w/RTCC peripheral will be more complicated than adding an external RTCC device.

    The simplest form of staggered supply isolation is a pair of low Vf diodes dropping power to the PIC's decoupling Capacitors. One for main power and another for the backup batter(y/ies). The Main power must be higher voltage than the backup to make this work. You need to have an onboard comparator in the PIC, connected and coded to drop it into sleep mode whenever the main power drops below the backup coin cell voltage.
     
    dhruv1128 likes this.
  9. sirch2

    Well-Known Member

    Jan 21, 2013
    1,008
    351
    The advantage of an external RTC is that you can happily power down or reset the MCU (power failure, watchdog or whatever) without loosing the actual time if you use the RTC battery backup.
     
    dhruv1128 likes this.
  10. NorthGuy

    Active Member

    Jun 28, 2014
    603
    121
    PIC's RTCC module will function through the reset without any problems, at least on the MCUs that I looked at.

    As to the total lost of all power (including backup), it'll kill time with any RTC, be it external or internal one.
     
    dhruv1128 likes this.
  11. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    these RTC modules often have a piggy back battery, even rechargeable lithium.
     
    dhruv1128 likes this.
  12. embpic

    Member

    May 29, 2013
    187
    3
    USE RTC then it can be external or internal. but don't use interrupt driven. bcoz u want time stamp then when u cut off power then this time will lost so better way use RTC with Battery.
     
    dhruv1128 likes this.
  13. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    I think there will already be a lot of coding for operating the SD card and FAT file system, power down code, and obviosuly the sensor logging code etc so the tiny bit of added code to make minutes, and count hours:mins is pretty trivial. So personally I would do it in the PIC.

    Regarding the months:days, I would just use linear days ie;
    days:hours:mins
    where days is a number from 0 to 65536 (assuming you use a 16bit var for "days"). That saves a lot of issues with months:days and gives you easy and reliable code.
     
    dhruv1128 likes this.
  14. dhruv1128

    Thread Starter New Member

    Jun 28, 2014
    6
    0
    Hello,

    Thank You for your reply.

    Is PCF2129A accurate when working environment is around 55*C. Orr else can you suggest me some good RTC modules ?
     
  15. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,387
    1,605
    The accuracy is spec'ed in section 13.2 of the data sheet at ±5 ppm up to 60°C. A conventional tuning fork crystal is off 30-30 ppm at the same temperature.

    I could suggest modules with similar performance (made by Maxim) but at a much higher price ($9.06 USD single quantity), or modules that don't perform as well (every module with an external crystal), but I wouldn't.

    If you want to look for yourself, here's a list of 2,184 devices. With full knowledge of your needs you may find a device that better suits your custom needs.

    I did this using Microchip's file system library code. I'm not sure where this is in their current app library, but the old one is there and it is very simple to work with. I would base my app on the sample "Device - Mass Storage - SD Card reader" project which can read/write the SC card from a PC, add in the file system libraries. The app could read/write the card, and you have the options of either removing the card or just using s USB cable to read out the data.
     
    Last edited: Jun 29, 2014
    dhruv1128 likes this.
Loading...