Difference between revisions of "LPC1768: RTC Programming"
m |
|||
(118 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
[[category: LPC1768 Tutorials]] | [[category: LPC1768 Tutorials]] | ||
− | + | In this tutorial we will be discussing about the internal RTC of LPC1768. We will see what are the different registers associated with the RTC but we will mainly focus on basics registers required to use RTC. Later we will see how to use Explore Embedded library for RTC.<br><br><br> | |
− | |||
=RTC Registers= | =RTC Registers= | ||
+ | The RTC includes a number of registers. We will concentrate some of the basic required registers. | ||
+ | {| class="table table-striped table-hover table-condensed table-bordered" | ||
+ | |-class="info" | ||
+ | |Register|| Description | ||
+ | |- | ||
+ | | CCR || Clock Control Register | ||
+ | |-class="active" | ||
+ | |colspan= '2'|'''Consolidated Time Registers''' | ||
+ | |- | ||
+ | |CTIME 0|| Consolidated Time Register 0 | ||
+ | |-class="active" | ||
+ | |CTIME 1|| Consolidated Time Register 1 | ||
+ | |- | ||
+ | |CTIME 2|| Consolidated Time Register 2 | ||
+ | |-class="active" | ||
+ | |colspan = '2'|'''Time Counter Registers''' | ||
+ | |- | ||
+ | |SEC || Seconds Counter | ||
+ | |-class="active" | ||
+ | |MIN || Minutes Register | ||
+ | |- | ||
+ | |HOUR || Hours Register | ||
+ | |-class="active" | ||
+ | |DOM || Day of Month Register | ||
+ | |- | ||
+ | |DOW || Day of Week Register | ||
+ | |-class="active" | ||
+ | |DOY || Day of Year Register | ||
+ | |- | ||
+ | |MONTH || Months Register | ||
+ | |-class="active" | ||
+ | |YEAR || Years Register | ||
+ | |- | ||
+ | |CALIBRATION || Calibration value Register | ||
+ | |} | ||
+ | <br><br><br><br> | ||
− | + | =RTC Register Configuration= | |
− | + | Lets see how to configure these registers. | |
− | {| class=" | + | {| class="table table-striped table-hover table-condensed table-bordered" |
− | + | |-class="info" | |
+ | |CCR | ||
|- | |- | ||
|31:5|| 4 || 3:2|| 1|| 0 | |31:5|| 4 || 3:2|| 1|| 0 | ||
− | |- | + | |-class="active" |
| - || CCALEN || - || CTCRST || CLKEN | | - || CCALEN || - || CTCRST || CLKEN | ||
|} | |} | ||
+ | =====CCR ( Clock Control Register )===== | ||
+ | The Clock Control Register register controls the operation of the clock divide circuit. | ||
'''Bit 0 – CLKEN : Clock Enable'''<br> | '''Bit 0 – CLKEN : Clock Enable'''<br> | ||
+ | This bit is written with one to enable time counters and written with zero to disable time counters. | ||
'''Bit 1 - CTCRST : CTC Reset'''<br> | '''Bit 1 - CTCRST : CTC Reset'''<br> | ||
+ | When this bit is set to one, the internal oscillator divider are reset. | ||
− | '''Bit 3:2 ''' | + | '''Bit 3:2 - Reserved'''<br> |
+ | These bits must be set to zero for normal RTC operation. | ||
− | '''Bit 4 - CCALEN''' <br> | + | '''Bit 4 - CCALEN : Calibration counter enable''' <br> |
+ | To disable calibration counter and reset it to zero, write one to this bit. | ||
+ | When written zero, the counter is enabled and starts counting | ||
− | '''Bit 31:5 ''' | + | '''Bit 31:5 - Reserved'''<br><br><br><br><br> |
=====Time Counter Register ===== | =====Time Counter Register ===== | ||
− | {| class=" | + | The time value consists of the eight counters and there is separate register associated with each counter. To update the RTC, new values should be written to these registers. |
− | + | {| class="table table-striped table-hover table-condensed table-bordered" | |
+ | |-class="info" | ||
+ | |Time Counter Registers | ||
|- | |- | ||
− | | | + | |Register|| Description || Minimum Value || Maximum Value |
+ | |-class="active" | ||
+ | |SEC || Seconds value|| 0 || 59 | ||
|- | |- | ||
− | | | + | |MIN || Minutes value|| 0 || 59 |
+ | |-class="active" | ||
+ | |HOUR || Hours value|| 0 || 23 | ||
|- | |- | ||
− | | | + | |DOM || Day of Month value|| 1 || 28,29,30 or 31(depending on the month) |
+ | |-class="active" | ||
+ | |DOW || Day of Week value|| 0 || 6 | ||
|- | |- | ||
− | | | + | |DOY || Day of Year value|| 1 || 365 (366 for Leap Year) |
+ | |-class="active" | ||
+ | |MONTH || Month value|| 1 || 12 | ||
|- | |- | ||
− | | | + | |YEAR || Year value|| 0 || 4095 |
− | + | |}<br><br> | |
+ | |||
+ | ===== Consolidated Time Registers ===== | ||
+ | |||
+ | The values of the Time Counters can optionally be read in a consolidated format which | ||
+ | allows the programmer to read all time counters with only three read operations. | ||
+ | |||
+ | There are 3 Consolidated Time Registers. The minimum and maximum values of the various fields in these registers are same as the respective Time Counter Register. | ||
+ | |||
+ | These registers are read only. New values should be written to Time Counter Registers. | ||
+ | |||
+ | |||
+ | {| class="table table-striped table-hover table-condensed table-bordered" | ||
+ | |-class="info" | ||
+ | |Consolidated Time Register 0 ( CTIME0 ) | ||
+ | |--class="active" | ||
+ | | 31:27 || 26:24 || 23:21 || 20:16 || 15:14 || 13:8 || 7:6 || 5:0 | ||
|- | |- | ||
− | | | + | | Reserved || Day of Week || Reserved || Hours || Reserved || Minute || Reserved || Second |
− | | | + | |} |
− | | | + | |
− | |- | + | |
− | | | + | {| class="table table-striped table-hover table-condensed table-bordered" |
+ | |-class="info" | ||
+ | |Consolidated Time Register 1 ( CTIME1 ) | ||
+ | |--class="active" | ||
+ | | 31:27 || 27:16 || 15:12 || 11:8 || 7:5 || 4:0 | ||
|- | |- | ||
− | | | + | | Reserved || Year || Reserved || Month || Reserved || Day of Month |
|} | |} | ||
− | =Steps for | + | |
− | =Code | + | {| class="table table-striped table-hover table-condensed table-bordered" |
+ | |-class="info" | ||
+ | |Consolidated Time Register 2 ( CTIME2 ) | ||
+ | |--class="active" | ||
+ | | 31:12 || 11:0 | ||
+ | |- | ||
+ | | Reserved || Day of Year | ||
+ | |}<br><br><br> | ||
+ | |||
+ | =Steps for Using RTC= | ||
+ | === Initialize RTC === | ||
+ | * Disable RTC clock using CCR ( Clock Control Register ) | ||
+ | * Reset clock using CCR register | ||
+ | * Enable RTC calibration in RTC Calibration Register | ||
+ | * Enable the clock for RTC using CCR register | ||
+ | |||
+ | === Set Date and Time === | ||
+ | As we saw previously, there are separate Time Counter registers for each time parameter hour, min, sec and same is the case date. We just need to copy the required values to these registers. | ||
+ | |||
+ | === Read Date and Time=== | ||
+ | The values of date and time can be read from associated Time Counter registers. | ||
+ | Alternately, date and time can also be read from Consolidated Time registers. | ||
+ | |||
+ | We will use Time Counter registers for both reading and writing.<br><br><br><br> | ||
+ | |||
+ | =Code Example= | ||
===Example 1 === | ===Example 1 === | ||
− | ===Using Explore Embedded | + | |
+ | In this example we will see how to use lpc1768 inbuilt RTC. The time and date will be displayed continuously on 2x16 LCD. | ||
+ | |||
+ | <html> | ||
+ | <script src="https://gist.github.com/SaheblalBagwan/08ad5b3998d8d3a98fd03fc784db7c22.js"></script> | ||
+ | </html> | ||
+ | <br><br><br><br> | ||
+ | |||
+ | ===Using Explore Embedded Libraries=== | ||
+ | |||
+ | Now we will discuss how to use Explore Embedded Libraries for LPC1768 inbuilt RTC. | ||
+ | Refer the LCD tutorial for interfacing the 2x16 lcd. | ||
+ | |||
+ | <html> | ||
+ | <script src="https://gist.github.com/SaheblalBagwan/7e34857b78d611d996a32cadf1467d14.js"></script> | ||
+ | </html> | ||
+ | <br><br><br><br> | ||
+ | |||
+ | =Connection= | ||
+ | Do the connections as shown the below image and flash the compiled code. | ||
+ | [[File:LPC1768_RTC.jpg]] | ||
+ | |||
+ | |||
+ | = Downloads= | ||
+ | Download the complete project folder from the below link: | ||
+ | https://codeload.github.com/ExploreEmbedded/Explore-Cortex-M3-LPC1768-Stick-DVB-14001/zip/master<br><br> | ||
+ | |||
{{DISQUS}} | {{DISQUS}} |
Latest revision as of 17:50, 19 July 2016
In this tutorial we will be discussing about the internal RTC of LPC1768. We will see what are the different registers associated with the RTC but we will mainly focus on basics registers required to use RTC. Later we will see how to use Explore Embedded library for RTC.
Contents
RTC Registers
The RTC includes a number of registers. We will concentrate some of the basic required registers.
Register | Description |
CCR | Clock Control Register |
Consolidated Time Registers | |
CTIME 0 | Consolidated Time Register 0 |
CTIME 1 | Consolidated Time Register 1 |
CTIME 2 | Consolidated Time Register 2 |
Time Counter Registers | |
SEC | Seconds Counter |
MIN | Minutes Register |
HOUR | Hours Register |
DOM | Day of Month Register |
DOW | Day of Week Register |
DOY | Day of Year Register |
MONTH | Months Register |
YEAR | Years Register |
CALIBRATION | Calibration value Register |
RTC Register Configuration
Lets see how to configure these registers.
CCR | ||||
31:5 | 4 | 3:2 | 1 | 0 |
- | CCALEN | - | CTCRST | CLKEN |
CCR ( Clock Control Register )
The Clock Control Register register controls the operation of the clock divide circuit.
Bit 0 – CLKEN : Clock Enable
This bit is written with one to enable time counters and written with zero to disable time counters.
Bit 1 - CTCRST : CTC Reset
When this bit is set to one, the internal oscillator divider are reset.
Bit 3:2 - Reserved
These bits must be set to zero for normal RTC operation.
Bit 4 - CCALEN : Calibration counter enable
To disable calibration counter and reset it to zero, write one to this bit.
When written zero, the counter is enabled and starts counting
Bit 31:5 - Reserved
Time Counter Register
The time value consists of the eight counters and there is separate register associated with each counter. To update the RTC, new values should be written to these registers.
Time Counter Registers | |||
Register | Description | Minimum Value | Maximum Value |
SEC | Seconds value | 0 | 59 |
MIN | Minutes value | 0 | 59 |
HOUR | Hours value | 0 | 23 |
DOM | Day of Month value | 1 | 28,29,30 or 31(depending on the month) |
DOW | Day of Week value | 0 | 6 |
DOY | Day of Year value | 1 | 365 (366 for Leap Year) |
MONTH | Month value | 1 | 12 |
YEAR | Year value | 0 | 4095 |
Consolidated Time Registers
The values of the Time Counters can optionally be read in a consolidated format which allows the programmer to read all time counters with only three read operations.
There are 3 Consolidated Time Registers. The minimum and maximum values of the various fields in these registers are same as the respective Time Counter Register.
These registers are read only. New values should be written to Time Counter Registers.
Consolidated Time Register 0 ( CTIME0 ) | |||||||
31:27 | 26:24 | 23:21 | 20:16 | 15:14 | 13:8 | 7:6 | 5:0 |
Reserved | Day of Week | Reserved | Hours | Reserved | Minute | Reserved | Second |
Consolidated Time Register 1 ( CTIME1 ) | |||||
31:27 | 27:16 | 15:12 | 11:8 | 7:5 | 4:0 |
Reserved | Year | Reserved | Month | Reserved | Day of Month |
Consolidated Time Register 2 ( CTIME2 ) | |
31:12 | 11:0 |
Reserved | Day of Year |
Steps for Using RTC
Initialize RTC
- Disable RTC clock using CCR ( Clock Control Register )
- Reset clock using CCR register
- Enable RTC calibration in RTC Calibration Register
- Enable the clock for RTC using CCR register
Set Date and Time
As we saw previously, there are separate Time Counter registers for each time parameter hour, min, sec and same is the case date. We just need to copy the required values to these registers.
Read Date and Time
The values of date and time can be read from associated Time Counter registers. Alternately, date and time can also be read from Consolidated Time registers.
We will use Time Counter registers for both reading and writing.
Code Example
Example 1
In this example we will see how to use lpc1768 inbuilt RTC. The time and date will be displayed continuously on 2x16 LCD.
Using Explore Embedded Libraries
Now we will discuss how to use Explore Embedded Libraries for LPC1768 inbuilt RTC. Refer the LCD tutorial for interfacing the 2x16 lcd.
Connection
Do the connections as shown the below image and flash the compiled code.
Downloads
Download the complete project folder from the below link:
https://codeload.github.com/ExploreEmbedded/Explore-Cortex-M3-LPC1768-Stick-DVB-14001/zip/master