Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[AVR] How to use RTC in microcontrollers?

Status
Not open for further replies.

abdi110

Member level 2
Joined
Mar 5, 2015
Messages
52
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Activity points
382
Could any one please let me know how to use RTC in AVR?
I would like to have a calendar in my program in order to my program or a part of it be executed in a certain date. Is it possible? and How?
 
Last edited:

srizbf

Advanced Member level 5
Joined
Apr 3, 2010
Messages
1,995
Helped
417
Reputation
840
Reaction score
328
Trophy points
1,363
Activity points
10,985

KlausST

Super Moderator
Staff member
Joined
Apr 17, 2014
Messages
19,475
Helped
4,306
Reputation
8,617
Reaction score
4,262
Trophy points
1,393
Activity points
129,131
Hi,

After power loss the date and time is lost.
Therefore i use external rtc with backup battery.

******
Internal rtc:
Isr for a fixed isr frequency. First counter for parts of second (depends on isr frequency)
Then counter 0...59 for seconds (on every counter reset)
Then counter 0..59 for minutes(on every second counter reset)
Then counter 0..23 for hours ( on every minutes reset)
...
And so on.
Take care withmonth, because they have variabke number of days.
Other things to take care: leap year, dalight saving...

Klaus
 

hobbyckts

Advanced Member level 1
Joined
Mar 12, 2015
Messages
481
Helped
88
Reputation
176
Reaction score
85
Trophy points
28
Activity points
2,457
Make sure the RTC is having the back-up battery supply.
 

KlausST

Super Moderator
Staff member
Joined
Apr 17, 2014
Messages
19,475
Helped
4,306
Reputation
8,617
Reaction score
4,262
Trophy points
1,393
Activity points
129,131
Hi,

Make sure the RTC is having the back-up battery supply.

This would mean to supply the whole AVR with a backup battery.
But the AVR maybe needs 100 time the current of a dedicated RTC IC.
An thus the battery needs 100 times the capacity of an RTC IC battery.

Using the internal RTC in my eyes makes sense:
* If the AVR is continously supplied with power. (stove, microwave ofen, hifi equippment ..)
* or if the AVR has the posibility to adjust its intenal RTC after power loss (internet, DCF77...)

Klaus
 

hobbyckts

Advanced Member level 1
Joined
Mar 12, 2015
Messages
481
Helped
88
Reputation
176
Reaction score
85
Trophy points
28
Activity points
2,457
I meant to use the external RTC with battery backup to provide the timing correct
 

abdi110

Member level 2
Joined
Mar 5, 2015
Messages
52
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Activity points
382
Hi,



This would mean to supply the whole AVR with a backup battery.
But the AVR maybe needs 100 time the current of a dedicated RTC IC.
An thus the battery needs 100 times the capacity of an RTC IC battery.

Using the internal RTC in my eyes makes sense:
* If the AVR is continously supplied with power. (stove, microwave ofen, hifi equippment ..)
* or if the AVR has the posibility to adjust its intenal RTC after power loss (internet, DCF77...)

Klaus
Thanks every one
I decided to use an external RTC IC because of the power loss case.
 

bigdogguru

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,274
Trophy points
1,413
Location
Southwest, USA
Activity points
62,510
I decided to use an external RTC IC because of the power loss case.

In that case, you may want to consider an RTC like the Maxim DS3234, there are breakout board readily available for the developer and open source libraries for AVR and Arduino.

Maxim DS3234 Datasheet

lib-ds3234, A library for interfacing the DS3234 (SPI) RTC to an AVR microcontroller.

Arduino DeadOn RTC – DS3234 wiring example and tutorial.

SparkFun DeadOn RTC Breakout - DS3234



Several device manufacturers offer RTCs along with Maxim, Microchip, NXP, ST, TI and others:


Maxim Real-Time Clocks (RTC) ICs


Microchip Real-Time Clock/Calendar

NXP Real-Time Clocks (RTC)

ST Real-Time Clock (RTC) ICs

TI Real-time Clocks (RTC)



BigDog
 

abdi110

Member level 2
Joined
Mar 5, 2015
Messages
52
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Activity points
382
So many thanks dear friend for your complete; information I was thinking about ds1307 now I can consider both to select one.
Thank you.

- - - Updated - - -

Could you please help me about the battery Circuit? I mean the battery charger circuit and type of battery which is good for this purpose.

- - - Updated - - -

Based on the data sheet the battery is connected directly to the chip. I wanted to ask if the battery charger is implemented in the chip? Or I have to make it externally?
 

hobbyckts

Advanced Member level 1
Joined
Mar 12, 2015
Messages
481
Helped
88
Reputation
176
Reaction score
85
Trophy points
28
Activity points
2,457
For DS1307 you need to connect the battery externally and regarding the battery charger circuit you need to use rechargeable battery for that first so select that first. Then will work charging circuit
 

    V

    Points: 2
    Helpful Answer Positive Rating

bigdogguru

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,274
Trophy points
1,413
Location
Southwest, USA
Activity points
62,510
So many thanks dear friend for your complete; information I was thinking about ds1307 now I can consider both to select one.

One advantage the DS3234 has over the DS3107 is the DS3234 has an integrated temperature compensated crystal oscillator (TCXO) and crystal, therefore no external crystal is required. The DS3234 is quite a bit more stable, ±2ppm, than the DS3107, enabling it to retain a more accurate time.

Could you please help me about the battery Circuit? I mean the battery charger circuit and type of battery which is good for this purpose.

Based on the data sheet the battery is connected directly to the chip. I wanted to ask if the battery charger is implemented in the chip? Or I have to make it externally?

Typically these types of devices utilize a small coin lithium cell, like the CR1225. The DS3234 only draws a few uA, an average of 1.0uA to 3.0uA, while on battery backup, therefore the life of a small lithium cell is quite reasonable.

Of course you could, utilize a recharge lithium based cell, however you would need to implement an external charging circuit into the design and I'm not sure a cost/benefit analysis would justify it. Some of the Maxim RTCs do incorporate a trickle charger into their design, DS12R885/DS12R887 and DS1340, I'm sure there are other manufactures offering similar devices.

Power Considerations for Accurate Real-Time Clocks

Trickle Charging Lithium Batteries with the ISL1208 and ISL1209 RTC Devices


Selecting a Backup Source for Real-Time Clocks

Battery Charging and Management Solutions

The above appnote details some of the power considerations.

What is the specific end application?

BigDog
 

    V

    Points: 2
    Helpful Answer Positive Rating

KlausST

Super Moderator
Staff member
Joined
Apr 17, 2014
Messages
19,475
Helped
4,306
Reputation
8,617
Reaction score
4,262
Trophy points
1,393
Activity points
129,131
Hi,

i´d avoid rechargable batteries for clock backup power supply.

* the clock need only a few uA of current.
* a lithium coin cell lasts for years
* rechargable batteries often have a bigger self discharge than those coin cells
* if you use rechargabe batteries and need to recharge it - lets say - every three month, it is likely you forget them to charge.
* as long as your application is powered it needs no current from the battery

Klaus
 

hobbyckts

Advanced Member level 1
Joined
Mar 12, 2015
Messages
481
Helped
88
Reputation
176
Reaction score
85
Trophy points
28
Activity points
2,457
Hi,

i´d avoid rechargable batteries for clock backup power supply.

* the clock need only a few uA of current.
* a lithium coin cell lasts for years
* rechargable batteries often have a bigger self discharge than those coin cells
* if you use rechargabe batteries and need to recharge it - lets say - every three month, it is likely you forget them to charge.
* as long as your application is powered it needs no current from the battery

Klaus

Then in that case we can use a simple coin battery as a backup supply for RTC DS1307.
 

bigdogguru

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,274
Trophy points
1,413
Location
Southwest, USA
Activity points
62,510
As both Klaus and I have alluded to, a simple coin lithium cell should be able to effectively provide backup power to a RTC for quite a long time. The following appnote should provide insight into predicting cell life in a typical RTC backup application:

Lithium Coin-Cell Batteries: Predicting an Application Lifetime


BigDog
 

abdi110

Member level 2
Joined
Mar 5, 2015
Messages
52
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Activity points
382
One advantage the DS3234 has over the DS3107 is the DS3234 has an integrated temperature compensated crystal oscillator (TCXO) and crystal, therefore no external crystal is required. The DS3234 is quite a bit more stable, ±2ppm, than the DS3107, enabling it to retain a more accurate time.



Typically these types of devices utilize a small coin lithium cell, like the CR1225. The DS3234 only draws a few uA, an average of 1.0uA to 3.0uA, while on battery backup, therefore the life of a small lithium cell is quite reasonable.

Of course you could, utilize a recharge lithium based cell, however you would need to implement an external charging circuit into the design and I'm not sure a cost/benefit analysis would justify it. Some of the Maxim RTCs do incorporate a trickle charger into their design, DS12R885/DS12R887 and DS1340, I'm sure there are other manufactures offering similar devices.

Power Considerations for Accurate Real-Time Clocks

Trickle Charging Lithium Batteries with the ISL1208 and ISL1209 RTC Devices


Selecting a Backup Source for Real-Time Clocks

Battery Charging and Management Solutions

The above appnote details some of the power considerations.

What is the specific end application?

BigDog

So many thanks dear friend your information is very useful for me. So I use a non rechargeable battery and therefore no need to any charging circuit.
My customer asks me to make a circuit for him to work as a trial sample (for his end customer) and after a exact time his customer pay him the cost of circuit otherwise the circuit stops working. So I need a calendar in the circuit in order that stop the circuit after an exact date.
Thank you very much again.

- - - Updated - - -

Hi,

i´d avoid rechargable batteries for clock backup power supply.
.

* the clock need only a few uA of current.
* a lithium coin cell lasts for years
* rechargable batteries often have a bigger self discharge than those coin cells
* if you use rechargabe batteries and need to recharge it - lets say - every three month, it is likely you forget them to charge.
* as long as your application is powered it needs no current from the battery

Klaus
Thanks dear friend so I will use a non rechargeable battery it is easier and witj more advantages thanks
 

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
523
Trophy points
1,393
Activity points
0
Using timer interrupts make a software clock. Also use eeprom to store number of days used. Read this value into day variable on start up. Create delays as required like 180 days, 360 days... When that much days are over then the device stops functioning.

No need for an RTC if clock is used for trial time expire check. I always do such things. When I give some .hex file for testing I always give code which works for 30 days. Days count is stored in eeprom. The device can be erased and reprogrammed and used for another x days.

Code:
if(days < TRIAL_LIMIT) {
         //do everything
}
 

abdi110

Member level 2
Joined
Mar 5, 2015
Messages
52
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Activity points
382
Using timer interrupts make a software clock. Also use eeprom to store number of days used. Read this value into day variable on start up. Create delays as required like 180 days, 360 days... When that much days are over then the device stops functioning.

No need for an RTC if clock is used for trial time expire check. I always do such things. When I give some .hex file for testing I always give code which works for 30 days. Days count is stored in eeprom. The device can be erased and reprogrammed and used for another x days.

Code:
if(days < TRIAL_LIMIT) {
         //do everything
}
Thanks I do not know how to store some thing in eeprom for example in at mega 8
 
Last edited:

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
523
Trophy points
1,393
Activity points
0
If you can use mikroC PRO AVR Compiler then I can give you an example of software timer setting trial time for the device.
 

bigdogguru

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,274
Trophy points
1,413
Location
Southwest, USA
Activity points
62,510
Thanks but what happens after a power loss or after a turning and off? I think in power loss and resetting the AVR everything goes to first and therefore the time days variable will be set to zero and never reaches to the time limit so the user can turn off and on the system and everything will start from the beginning isn't it?

Actually Milan suggested storing the day count in EEPROM which is nonvolatile, the stored data is retained regardless of power availability. If the day count was stored in SRAM, you would be correct, once power is removed from the device all data, variables, etc, stored in SRAM are lost.

The difficulty maybe detecting repeated removal of the battery from the RTC, thereby resetting it back to a default date. You may need to store the last known date and time of the RTC in EEPROM as well, if the date or time suddenly reverts to the past, then RTC has been reset and a proper course of action can be taken, like expiring the demo. You would need to regularly update the stored date and time with a routine that is scheduled to run at certain intervals.

What AVR will you be using in your design? Disregard, an ATMega 8, which has 512 bytes of EEPROM storage.

BigDog
 

abdi110

Member level 2
Joined
Mar 5, 2015
Messages
52
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Activity points
382
If you can use mikroC PRO AVR Compiler then I can give you an example of software timer setting trial time for the device.
Thanks Milan but I use code vision could you please help me on this compiler?

- - - Updated - - -

Actually Milan suggested storing the day count in EEPROM which is nonvolatile, the stored data is retained regardless of power availability. If the day count was stored in SRAM, you would be correct, once power is removed from the device all data, variables, etc, stored in SRAM are lost.

The difficulty maybe detecting repeated removal of the battery from the RTC, thereby resetting it back to a default date. You may need to store the last known date and time of the RTC in EEPROM as well, if the date or time suddenly reverts to the past, then RTC has been reset and a proper course of action can be taken, like expiring the demo. You would need to regularly update the stored date and time with a routine that is scheduled to run at certain intervals.

What AVR will you be using in your design? Disregard, an ATMega 8, which has 512 bytes of EEPROM storage.

BigDog
So thanks
Based on what Milan said do I need the RTC yet?
I think I have to change the AVR to one with more eeprom memory. How much memory is proper in your opinion for this application?
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top