Generating Square wave based external low power crystal?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Generating Square wave based external low power crystal?

2,363 Views
BasePointer
Contributor II
Hi,
 
I have a 32.768kHz external xtal. I want to generate square wave based this crystal to measure its frequency. I can't measure it directly from EXAL pin due to probe capacitance. I have a high precision frequency counter. Do you know a way to generate a signal based directly this crystal without using internal DCO? I don't want jitter at output.
 
MCU is MC9S08LC60
CW v5.1 C Compiler
 
I tried common outputs of LCD module of the MCU. but no change. I didn't understand why it is not :smileysad:
 
Thank you.
 
Labels (1)
0 Kudos
5 Replies

489 Views
bigmac
Specialist III
Hello,
 
Is your frequency counter capable of measuring low frequencies  to high resolution (e.g. 0.001 Hz), or alternatively, is it capable of period measurement (say 0.1 us resolution, or better)?
 
In either case, you might setup a PWM output of known frequency, and with 50 percent duty cycle.  For frequency measurement, the PWM frequency should be as high as feasible.  For period measurement, you should keep the PWM frequency low for best resolution, e.g. perhaps a 1 Hz frequency, if within the capabilities of the counter.
 
Another method suitable for frequency measurement, is to toggle an output pin within a very tight program loop, with a known number of cycles per loop.  This should be written in assembly code.  The COP timer and all interrupts should remain disabled during the test.
 
LOOP:
  BRSET  0,PTA
  NOP           ; Padding for 50% duty cycle
  NOP
  NOP
  BRCLR  0,PTA
  BRA    LOOP
 
Since the frequency or period measurement should occur over a one second measurement interval, or thereabouts, any jitter should be averaged over this period, and I would generally not expect this to be an issue.
 
If you must directly measure the frequency of the 32 kHz crystal, you will need external buffer components that present a fixed load to the crystal whether or not a counter is connected.
 
This might consist of a pair of HCMOS inverter stages (specifically of the unbuffered type).  The first stage would operate in a quasi-analog fashion, with a high value resistor between input and output (say 4.7M).  The input would be capacitively coupled to one of the oscillator pins (say 10p).  The output of the first stage would connect to the input of the second stage, and the counter would connect to the output of the second stage.  You would still need to take into account the input capacitance of the buffer during the design of the oscillator.
 
Regards,
Mac
 
0 Kudos

489 Views
BasePointer
Contributor II
Hi bigmac,
 
The counter I have is capable 100nHz resolution for 1Hz.
 
I can't use timer modules of the LC60. Because their time base is all DCO based in FEE mode. I can't define clock base directly as external crystal.
 
Code:
CLKSB:CLKSA TPM Clock Source to Prescaler Input0:0 No clock selected (TPMx disabled)0:1 Bus rate clock (BUSCLK)1:0 Fixed system clock (XCLK)1:1 External source (TPMxCLK)1,2

In the MCU, there are two module that uses directly external crystal. First is RTI and the second is LCD.
RTI module has no output, it just generate interrupt. LCD module has output that has frequency External Clock Frequency / [(1+CLKADJ)*8*(2^LCLK)*2]. The final frequency with this way could be 128Hz directly based external crystal. My tests show me it is varying and I don't know why it is.
 
I'm new at Frescale HCS08 family, I don't know much about its assembly language and didn't work it before. I think the code you suggested should work in FBE mode to disable DCO. I can also use directly timer module in this mode. But my entire application can't run at this frequency. So this is not suitable way for me.
 
Thank you,
BP
0 Kudos

489 Views
peg
Senior Contributor IV
Hi BP,
 
Why can't you just write a programme that just has an empty main loop and an RTI ISR that simply toggles an output? This would then provide a signal that is in direct proportion to the crystal frequency.
Shouldn't matter how you write the code, the delay (from RTI to output toggle) should be the same everytime.
 
0 Kudos

489 Views
BasePointer
Contributor II
Hi,
 
I write the code below. It directly toggles PTC2/TPM1CH0 pin with compare output at about 4096Hz.
The counter tell me 4096.0208(+5.1ppm) - 4096.0217(+5.3ppm) as its frequency. There are still a small jitter?
 
If I measure it directly EXTAL pin with a probe 10:1, 10pF, it says 32767.745Hz (-7.78ppm)
 
 
Code:
  DisableInterrupts;    SOPT1 = 0b01110011;  SOPT2 = 0b10000000;    ICGC2 = 0;  ICGC1 = 0b00110001;  // FBE mode, fbus=16.768KHz    TPM1MOD = 1;  TPM1C0V = 0;  TPM1C0SC = 0b00010100; // toggle at Fext / 8 -> 32768/8=4096Hz  TPM1SC = 0b00001000;    while(1);
 
This is a kind of RTC application and I need to calibrate every products to max 2ppm. The real application code use FEE mode. Do you have a calibration method for mass production? I don't like this two program method.
 
Thank you,
BP.
0 Kudos

489 Views
bigmac
Specialist III
Hello BP,
 
I think there are a few points that need to be considered.
 
You are using a crystal type originally intended for use in wrist watches, that when worn, are in a fairly "constant temperature" environment.  The frequency drift with temperature change can be significantly higher for this type of low frequency crystal, than for a higher frequency.  I presume your product may be subject to considerably wider temperature variation, and you could be talking multiples of 10 ppm.
 
The crystal frequency will drift with time, due to aging.  Look at the data for the crystal you are using, but I suspect it is unlikely to be less than 1 ppm for the first 12 months.
 
Jitter of the oscillator output will have little effect on a "normal" RTC application, where resolution is one second.  The average frequency is what needs to be measured.  When you measure the frequency, you should be aware of the averaging period for the measurement setup, and choose a period in the region of one second, for any fine adjustment - for coarse adjustment the measurement period could be less.
 
For a factory adjustment procedure, there is probably little point in attempting to adjust finer than about 0.5-1.0 ppm.  With your suggested figure of 2 ppm, and a measured jitter of  approximately 0.2 ppm, you should be able to easily adjust to this accuracy.  Just ignore any variation of the least significant digit of the counter reading.
 
The change of frequency (13 ppm) is the sort of figure to be expected with the capacitive loading by the oscilloscope probe.  If you want to measure crystal frequency directly, you would require the hardware buffer approach, as previously described.
 
If you don't want to add the hardware, you will need the special firmware specifically for the measurement.  One possibility is to commence output of the calibration signal (the code you have posted would be suitable), whenever a special frequency calibrate mode is initiated.  This would then continue until the next reset, when normal operation would resume.
 
Regards,
Mac
 
0 Kudos