teensy 3.0 RTC experiments

Status
Not open for further replies.

manitou

Senior Member+
I hooked up a 32khz crystal to my teensy 3.0. Crystal is spec'd at 20ppm, and no external load capacitors are required. In an earlier post,
http://forum.pjrc.com/threads/17515-teensy-3-crystal-freq-check-with-NTP-and-GPS
I measured the freqeuncy of the core crystal to be within -5 ppm of GPS or (NTP host). So I wrote a little sketch to use the RTC pulse-per-second interrupt to collect micros() and check the frequency of the 32khz crystal against the core crystal.

Code:
// teensy RTC  32khz  
// hardware/teensy/cores/teensy3/pins_teensy.c   ch 39
// after program changes affecting RTC regs, recompile, and cycle power

volatile static unsigned long ticks,us;
static unsigned long us0=0;

void rtc_seconds_isr(void) {
	if (us0 == 0) us0 = micros();
      else {
        ticks++;
        us = micros() - us0;
    }
}

void setup() {
	Serial.begin(9600);
	while(!Serial);
//	rtc_compensate(-100);
//  RTC_TCR =0x0101 ;  // compensate  1 tick is 30ppm 0x0001
//  RTC_CR = 0x3500;    // capacitance
	RTC_IER = 0x10;    //TSIE
	NVIC_ENABLE_IRQ(IRQ_RTC_SECOND);
	Serial.println(RTC_SR,HEX);
	Serial.println(RTC_CR,HEX);
	Serial.println(RTC_IER,HEX);
	Serial.println(RTC_TCR,HEX);
}

void loop() {
	unsigned long t;
        int ppm, tprev=0;
        char str[64];


        ppm = 1000000*ticks - us;
        ppm = 1.e6 * ppm/ (float)us;
        sprintf(str,"%d %d %f %d",ticks,ticks-tprev,us*1.e-6,ppm);
        Serial.println(str);

	t = rtc_get();
	Serial.println(t);
        Serial.println(RTC_TCR,HEX);
	delay(2000);
}

The first tests showed the 32khz crystal to be with 4 ppm of the teensy.

The teensy library has routines to set,get, and adjust the RTC (rtc_set(), rtc_set(), rtc_compensate()).

The teensy ARM processor has various internal load capacitors than can be configured. Looking at the RTC hardware registers, the 16pf and 4pf internal capacitors are configured. Setting other values in the RTC_CR you can vary the load capacitance from 0 to 30 pf and that affects the frequency of the 32khz crystal.
Code:
capacitance pf     ppm
  0                     226
  2                     176
  4                     137
  8                      83
 16                      22
 20                       4   default
 22                      -3
 28                     -22
 30                     -26

The processor has a calibration register (rtc_compensate()) that you can make small (less than 1ppm) to large adjustments to the RTC frequency. By trying various values in rtc_compensate you can observe changes in the ppm reported by the sketch. With the ARM's internal temperature sensor (analogRead(38)) and converting to Celsius, one could use rtc_compensate() to adjust the RTC frequency to compensate for changes in temperature.
 
Status
Not open for further replies.
Back
Top