3.1 RTC.1: The Real Time Clock (RTC) does not work reliably within the
temperature specification
Introduction:
The RTC is a set of counters for measuring time when system power is on, and optionally
when it is off. The RTC is clocked by a separate 32 kHz oscillator that produces a 1 Hz
internal time reference. The RTC is powered by its own power supply pin, VBAT, which
can be connected to a battery, externally tied to a 3 V supply, or left floating. The RTC can
operate over temperature range from 40 C to 85 C.
Problem:
The RTC does not work reliably within the temperature specification.
Work-around:
None.
My own LPC1768 board with RTC didn't work, too. So here's my how to check RTC:
1. Don't connect anything to RTCX1-In (PIN16). Even my Tektronix 10x Voltage
probe can stop RTC.
2. Connect a 10x scope probe to RTCX2-Out (PIN18). If RTC isn't working you will
see a voltage of about 2.5V. If it's working you will see a 0.2V 32kHz
oscillation at 0.4V. So don't expect a huge amplitude.
3. VBAT shouldn't be a problem at all (connect it to VCC if it's not connected
to a battery)
4. If the crystal isn't working, change the caps. If this doesn't help, change
the crystal. My favorite is a TC26 crystal.
int main(void)
{
RTCInit();
RTCStart();
NVIC_EnableIRQ( RTC_IRQn) ;
}
/*********** ********* ********* ********* ********* ********* ********* ********* ***
* rtc.c: Realtime clock C file for NXP LPC17xx Family Microprocessors
*
* Copyright(C) 2009, NXP Semiconductor
* All rights reserved.
*
* History
* 2009.05.26 ver 1.00 Prelimnary version, first Release
*
************ ********* ********* ********* ********* ********* ********* ********* **/
/*********** ********* ********* ********* ********* ********* ********* ********* ********* *******
Includes
************ ********* ********* ********* ********* ********* ********* ********* ********* ******/
#include "lpc17xx.h"
#include "type.h"
#include "stdio.h"
#include "rtc.h"
RTCTime relogio_atual;
/*********** ********* ********* ********* ********* ********* ********* ********* ***
** Function name: RTC_IRQHandler
**
** Descriptions: RTC interrupt handler
**
** parameters: None
** Returned value: None
**
************ ********* ********* ********* ********* ********* ********* ********* **/
void RTC_IRQHandler (void)
{
LPC_RTC->ILR |= ILR_RTCCIF; //reset pending int flag
relogio_atual = RTCGetTime() ; //read RTC;
}
/*********** ********* ********* ********* ********* ********* ********* ********* ***
** Function name: RTCInit
**
** Descriptions: Initialize RTC timer
**
** parameters: None
** Returned value: None
**
************ ********* ********* ********* ********* ********* ********* ********* **/
void RTCInit( void )
{
// Enable CLOCK into RTC
LPC_SC->PCONP |= (1 << 9);
//If RTC is stopped, clear STOP bit.
if ( LPC_RTC->RTC_AUX & (0x1<<4) )
{
LPC_RTC->RTC_AUX |= (0x1<<4);
}
//--- Initialize registers ---
LPC_RTC->AMR = 0;
LPC_RTC->CIIR = 1; //INT every 1 sec;
LPC_RTC->CCR = 0;
return;
}
/*********** ********* ********* ********* ********* ********* ********* ********* ***
** Function name: RTCStart
**
** Descriptions: Start RTC timer
**
** parameters: None
** Returned value: None
**
************ ********* ********* ********* ********* ********* ********* ********* **/
void RTCStart( void )
{
/*--- Start RTC counters ---*/
LPC_RTC->CCR |= CCR_CLKEN;
LPC_RTC->ILR = ILR_RTCCIF;
}
/*********** ********* ********* ********* ********* ********* ********* ********* ***
** Function name: RTCStop
**
** Descriptions: Stop RTC timer
**
** parameters: None
** Returned value: None
**
************ ********* ********* ********* ********* ********* ********* ********* **/
void RTCStop( void )
{
/*--- Stop RTC counters ---*/
LPC_RTC->CCR &= ~CCR_CLKEN;
}
/*********** ********* ********* ********* ********* ********* ********* ********* ***
** Function name: RTC_CTCReset
**
** Descriptions: Reset RTC clock tick counter
**
** parameters: None
** Returned value: None
**
************ ********* ********* ********* ********* ********* ********* ********* **/
void RTC_CTCReset( void )
{
LPC_RTC->CCR |= CCR_CTCRST;
}
/*********** ********* ********* ********* ********* ********* ********* ********* ***
** Function name: RTCSetTime
**
** Descriptions: Setup RTC timer value
**
** parameters: None
** Returned value: None
**
************ ********* ********* ********* ********* ********* ********* ********* **/
void RTCSetTime( RTCTime Time )
{
LPC_RTC->SEC = Time.RTC_Sec;
LPC_RTC->MIN = Time.RTC_Min;
LPC_RTC->HOUR = Time.RTC_Hour;
LPC_RTC->DOM = Time.RTC_Mday;
LPC_RTC->DOW = Time.RTC_Wday;
LPC_RTC->DOY = Time.RTC_Yday;
LPC_RTC->MONTH = Time.RTC_Mon;
LPC_RTC->YEAR = Time.RTC_Year;
}
/*********** ********* ********* ********* ********* ********* ********* ********* ***
** Function name: RTCSetAlarm
**
** Descriptions: Initialize RTC timer
**
** parameters: None
** Returned value: None
**
************ ********* ********* ********* ********* ********* ********* ********* **/
void RTCSetAlarm( RTCTime Alarm )
{
LPC_RTC->ALSEC = Alarm.RTC_Sec;
LPC_RTC->ALMIN = Alarm.RTC_Min;
LPC_RTC->ALHOUR = Alarm.RTC_Hour;
LPC_RTC->ALDOM = Alarm.RTC_Mday;
LPC_RTC->ALDOW = Alarm.RTC_Wday;
LPC_RTC->ALDOY = Alarm.RTC_Yday;
LPC_RTC->ALMON = Alarm.RTC_Mon;
LPC_RTC->ALYEAR = Alarm.RTC_Year;
}
/*********** ********* ********* ********* ********* ********* ********* ********* ***
** Function name: RTCGetTime
**
** Descriptions: Get RTC timer value
**
** parameters: None
** Returned value: The data structure of the RTC time table
**
************ ********* ********* ********* ********* ********* ********* ********* **/
RTCTime RTCGetTime( void )
{
RTCTime LocalTime;
LocalTime.RTC_ Sec = LPC_RTC->SEC;
LocalTime.RTC_ Min = LPC_RTC->MIN;
LocalTime.RTC_ Hour = LPC_RTC->HOUR;
LocalTime.RTC_ Mday = LPC_RTC->DOM;
LocalTime.RTC_ Wday = LPC_RTC->DOW;
LocalTime.RTC_ Yday = LPC_RTC->DOY;
LocalTime.RTC_ Mon = LPC_RTC->MONTH;
LocalTime.RTC_ Year = LPC_RTC->YEAR;
return ( LocalTime );
}
/*********** ********* ********* ********* ********* ********* ********* ********* ***
** Function name: RTCSetAlarmMask
**
** Descriptions: Set RTC timer alarm mask
**
** parameters: Alarm mask setting
** Returned value: None
**
************ ********* ********* ********* ********* ********* ********* ********* **/
void RTCSetAlarmMask( uint32_t AlarmMask )
{
/*--- Set alarm mask ---*/
LPC_RTC->AMR = AlarmMask;
}
/*********** ********* ********* ********* ********* ********* ********* ********* ***
** End Of File
************ ********* ********* ********* ********* ********* ********* ********* ***/
/*********** ********* ********* ********* ********* ********* ********* ********* ***
* rtc.h: Header file for NXP LPC17xx Family Microprocessors
*
* Copyright(C) 2009, NXP Semiconductor
* All rights reserved.
*
* History
* 2009.05.27 ver 1.00 Prelimnary version, first Release
*
************ ********* ********* ********* ********* ********* ********* ********* ***/
typedef struct {
uint32_t RTC_Sec; /* Second value - [0,59] */
uint32_t RTC_Min; /* Minute value - [0,59] */
uint32_t RTC_Hour; /* Hour value - [0,23] */
uint32_t RTC_Mday; /* Day of the month value - [1,31] */
uint32_t RTC_Mon; /* Month value - [1,12] */
uint32_t RTC_Year; /* Year value - [0,4095] */
uint32_t RTC_Wday; /* Day of week value - [0,6] */
uint32_t RTC_Yday; /* Day of year value - [1,365] */
} RTCTime;
#define IMSEC 0x00000001
#define IMMIN 0x00000002
#define IMHOUR 0x00000004
#define IMDOM 0x00000008
#define IMDOW 0x00000010
#define IMDOY 0x00000020
#define IMMON 0x00000040
#define IMYEAR 0x00000080
#define AMRSEC 0x00000001 /* Alarm mask for Seconds */
#define AMRMIN 0x00000002 /* Alarm mask for Minutes */
#define AMRHOUR 0x00000004 /* Alarm mask for Hours */
#define AMRDOM 0x00000008 /* Alarm mask for Day of Month */
#define AMRDOW 0x00000010 /* Alarm mask for Day of Week */
#define AMRDOY 0x00000020 /* Alarm mask for Day of Year */
#define AMRMON 0x00000040 /* Alarm mask for Month */
#define AMRYEAR 0x00000080 /* Alarm mask for Year */
#define PREINT_RTC 0x000001C8 /* Prescaler value, integer portion,
PCLK = 15Mhz */
#define PREFRAC_RTC 0x000061C0 /* Prescaler value, fraction portion,
PCLK = 15Mhz */
#define ILR_RTCCIF 0x01
#define ILR_RTCALF 0x02
#define CCR_CLKEN 0x01
#define CCR_CTCRST 0x02
#define CCR_CLKSRC 0x10
#define IDEAL_RTC_FREQ 32768000 //in mHz (milli Hz)
#define MAX_DELTA_FREQUENCY _VALUE (IDEAL_RTC_FREQ / ((1 << CALIBRATION_ VALUE_SIZE) - 1))
#define CALIBRATION_ VALUE_SIZE 17
#define CALDIR(n) (n << 17) //1 = Backward, 0 = Forward
#define CCALEN (1<<4)
void RTC_IRQHandler (void);
void RTCInit( void );
void RTCStart( void );
void RTCStop( void );
void RTC_CTCReset( void );
void RTCSetTime( RTCTime );
RTCTime RTCGetTime( void );
void RTCSetAlarm( RTCTime );
void RTCSetAlarmMask( uint32_t AlarmMask );
/*********** ********* ********* ********* ********* ********* ********* ********* ***
** End Of File
************ ********* ********* ********* ********* ********* ********* ********* ***/
Here are some troubleshooting tips:
Here is some code correctly initializing the RTC from NXP:
Code:int main(void) { RTCInit(); RTCStart(); NVIC_EnableIRQ( RTC_IRQn) ; } /*********** ********* ********* ********* ********* ********* ********* ********* *** * rtc.c: Realtime clock C file for NXP LPC17xx Family Microprocessors * * Copyright(C) 2009, NXP Semiconductor * All rights reserved. * * History * 2009.05.26 ver 1.00 Prelimnary version, first Release * ************ ********* ********* ********* ********* ********* ********* ********* **/ /*********** ********* ********* ********* ********* ********* ********* ********* ********* ******* Includes ************ ********* ********* ********* ********* ********* ********* ********* ********* ******/ #include "lpc17xx.h" #include "type.h" #include "stdio.h" #include "rtc.h" RTCTime relogio_atual; /*********** ********* ********* ********* ********* ********* ********* ********* *** ** Function name: RTC_IRQHandler ** ** Descriptions: RTC interrupt handler ** ** parameters: None ** Returned value: None ** ************ ********* ********* ********* ********* ********* ********* ********* **/ void RTC_IRQHandler (void) { LPC_RTC->ILR |= ILR_RTCCIF; //reset pending int flag relogio_atual = RTCGetTime() ; //read RTC; } /*********** ********* ********* ********* ********* ********* ********* ********* *** ** Function name: RTCInit ** ** Descriptions: Initialize RTC timer ** ** parameters: None ** Returned value: None ** ************ ********* ********* ********* ********* ********* ********* ********* **/ void RTCInit( void ) { // Enable CLOCK into RTC LPC_SC->PCONP |= (1 << 9); //If RTC is stopped, clear STOP bit. if ( LPC_RTC->RTC_AUX & (0x1<<4) ) { LPC_RTC->RTC_AUX |= (0x1<<4); } //--- Initialize registers --- LPC_RTC->AMR = 0; LPC_RTC->CIIR = 1; //INT every 1 sec; LPC_RTC->CCR = 0; return; } /*********** ********* ********* ********* ********* ********* ********* ********* *** ** Function name: RTCStart ** ** Descriptions: Start RTC timer ** ** parameters: None ** Returned value: None ** ************ ********* ********* ********* ********* ********* ********* ********* **/ void RTCStart( void ) { /*--- Start RTC counters ---*/ LPC_RTC->CCR |= CCR_CLKEN; LPC_RTC->ILR = ILR_RTCCIF; } /*********** ********* ********* ********* ********* ********* ********* ********* *** ** Function name: RTCStop ** ** Descriptions: Stop RTC timer ** ** parameters: None ** Returned value: None ** ************ ********* ********* ********* ********* ********* ********* ********* **/ void RTCStop( void ) { /*--- Stop RTC counters ---*/ LPC_RTC->CCR &= ~CCR_CLKEN; } /*********** ********* ********* ********* ********* ********* ********* ********* *** ** Function name: RTC_CTCReset ** ** Descriptions: Reset RTC clock tick counter ** ** parameters: None ** Returned value: None ** ************ ********* ********* ********* ********* ********* ********* ********* **/ void RTC_CTCReset( void ) { LPC_RTC->CCR |= CCR_CTCRST; } /*********** ********* ********* ********* ********* ********* ********* ********* *** ** Function name: RTCSetTime ** ** Descriptions: Setup RTC timer value ** ** parameters: None ** Returned value: None ** ************ ********* ********* ********* ********* ********* ********* ********* **/ void RTCSetTime( RTCTime Time ) { LPC_RTC->SEC = Time.RTC_Sec; LPC_RTC->MIN = Time.RTC_Min; LPC_RTC->HOUR = Time.RTC_Hour; LPC_RTC->DOM = Time.RTC_Mday; LPC_RTC->DOW = Time.RTC_Wday; LPC_RTC->DOY = Time.RTC_Yday; LPC_RTC->MONTH = Time.RTC_Mon; LPC_RTC->YEAR = Time.RTC_Year; } /*********** ********* ********* ********* ********* ********* ********* ********* *** ** Function name: RTCSetAlarm ** ** Descriptions: Initialize RTC timer ** ** parameters: None ** Returned value: None ** ************ ********* ********* ********* ********* ********* ********* ********* **/ void RTCSetAlarm( RTCTime Alarm ) { LPC_RTC->ALSEC = Alarm.RTC_Sec; LPC_RTC->ALMIN = Alarm.RTC_Min; LPC_RTC->ALHOUR = Alarm.RTC_Hour; LPC_RTC->ALDOM = Alarm.RTC_Mday; LPC_RTC->ALDOW = Alarm.RTC_Wday; LPC_RTC->ALDOY = Alarm.RTC_Yday; LPC_RTC->ALMON = Alarm.RTC_Mon; LPC_RTC->ALYEAR = Alarm.RTC_Year; } /*********** ********* ********* ********* ********* ********* ********* ********* *** ** Function name: RTCGetTime ** ** Descriptions: Get RTC timer value ** ** parameters: None ** Returned value: The data structure of the RTC time table ** ************ ********* ********* ********* ********* ********* ********* ********* **/ RTCTime RTCGetTime( void ) { RTCTime LocalTime; LocalTime.RTC_ Sec = LPC_RTC->SEC; LocalTime.RTC_ Min = LPC_RTC->MIN; LocalTime.RTC_ Hour = LPC_RTC->HOUR; LocalTime.RTC_ Mday = LPC_RTC->DOM; LocalTime.RTC_ Wday = LPC_RTC->DOW; LocalTime.RTC_ Yday = LPC_RTC->DOY; LocalTime.RTC_ Mon = LPC_RTC->MONTH; LocalTime.RTC_ Year = LPC_RTC->YEAR; return ( LocalTime ); } /*********** ********* ********* ********* ********* ********* ********* ********* *** ** Function name: RTCSetAlarmMask ** ** Descriptions: Set RTC timer alarm mask ** ** parameters: Alarm mask setting ** Returned value: None ** ************ ********* ********* ********* ********* ********* ********* ********* **/ void RTCSetAlarmMask( uint32_t AlarmMask ) { /*--- Set alarm mask ---*/ LPC_RTC->AMR = AlarmMask; } /*********** ********* ********* ********* ********* ********* ********* ********* *** ** End Of File ************ ********* ********* ********* ********* ********* ********* ********* ***/ /*********** ********* ********* ********* ********* ********* ********* ********* *** * rtc.h: Header file for NXP LPC17xx Family Microprocessors * * Copyright(C) 2009, NXP Semiconductor * All rights reserved. * * History * 2009.05.27 ver 1.00 Prelimnary version, first Release * ************ ********* ********* ********* ********* ********* ********* ********* ***/ typedef struct { uint32_t RTC_Sec; /* Second value - [0,59] */ uint32_t RTC_Min; /* Minute value - [0,59] */ uint32_t RTC_Hour; /* Hour value - [0,23] */ uint32_t RTC_Mday; /* Day of the month value - [1,31] */ uint32_t RTC_Mon; /* Month value - [1,12] */ uint32_t RTC_Year; /* Year value - [0,4095] */ uint32_t RTC_Wday; /* Day of week value - [0,6] */ uint32_t RTC_Yday; /* Day of year value - [1,365] */ } RTCTime; #define IMSEC 0x00000001 #define IMMIN 0x00000002 #define IMHOUR 0x00000004 #define IMDOM 0x00000008 #define IMDOW 0x00000010 #define IMDOY 0x00000020 #define IMMON 0x00000040 #define IMYEAR 0x00000080 #define AMRSEC 0x00000001 /* Alarm mask for Seconds */ #define AMRMIN 0x00000002 /* Alarm mask for Minutes */ #define AMRHOUR 0x00000004 /* Alarm mask for Hours */ #define AMRDOM 0x00000008 /* Alarm mask for Day of Month */ #define AMRDOW 0x00000010 /* Alarm mask for Day of Week */ #define AMRDOY 0x00000020 /* Alarm mask for Day of Year */ #define AMRMON 0x00000040 /* Alarm mask for Month */ #define AMRYEAR 0x00000080 /* Alarm mask for Year */ #define PREINT_RTC 0x000001C8 /* Prescaler value, integer portion, PCLK = 15Mhz */ #define PREFRAC_RTC 0x000061C0 /* Prescaler value, fraction portion, PCLK = 15Mhz */ #define ILR_RTCCIF 0x01 #define ILR_RTCALF 0x02 #define CCR_CLKEN 0x01 #define CCR_CTCRST 0x02 #define CCR_CLKSRC 0x10 #define IDEAL_RTC_FREQ 32768000 //in mHz (milli Hz) #define MAX_DELTA_FREQUENCY _VALUE (IDEAL_RTC_FREQ / ((1 << CALIBRATION_ VALUE_SIZE) - 1)) #define CALIBRATION_ VALUE_SIZE 17 #define CALDIR(n) (n << 17) //1 = Backward, 0 = Forward #define CCALEN (1<<4) void RTC_IRQHandler (void); void RTCInit( void ); void RTCStart( void ); void RTCStop( void ); void RTC_CTCReset( void ); void RTCSetTime( RTCTime ); RTCTime RTCGetTime( void ); void RTCSetAlarm( RTCTime ); void RTCSetAlarmMask( uint32_t AlarmMask ); /*********** ********* ********* ********* ********* ********* ********* ********* *** ** End Of File ************ ********* ********* ********* ********* ********* ********* ********* ***/
Hope this helps in your endeavor.
it should be around 0.9 and 0.7v ............
is there any capacitors for the crystal???
The reports I've read indicate the problem is very batch oriented. Some of the mbeds, which are LPC1768 based, had several problems including the RTC, while other units worked fine. I'll still keep an eye out for other workarounds or tips.
what happens if you dont use any capacitors... even then it should work..... did you try replacing the crystal oscillator..... can you measure the frequency at those point using a multimeter or oscilloscope.....
Problem:
The RTC does not work reliably within the temperature specification.
Work-around:
None.
You know things are bad when NXP's Errata sheet states:
NXP announced that sometime this year they should have the problem solved with a new die. Of course this doesn't help all the people who have already purchased units of the previous design.
do you get any oscillations at those pins. Do you have a physical connection between crystal pin and port pins...
What is the exact date and lot or batch code on the LPC1768 chip? I know someone else who experienced a the same problem. I believe he has a sheet with defective lots, I have him check it for you.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?