Jump to content

Very accurate timer

Recommended Posts

Hello all together,


I need  a very accurate timer for a midi clock. For that reason I need a timer, that is giving me for every beat 24 beats back. At hte same time, I need a serial connection with 31250baud. My question is now, how do I get a very, very accurate timer. The timer quarz is normaly 32768, and this is maybe a little bit to slow, becaus I want my bpm with one digit after the point like 121,1bpm. I  have a MSP430G2553. Can I connect a 1MHz or even a 4MHz cristal to the

Link to post
Share on other sites

This device will not directly drive a high freq crystal, but there are a number of options as an external source can be used for internal timing:


a) Use an external oscillator or clock generator module. Benefit: a programmable module will give you exactly the frequency you want (within error limit) Drawback: additional components


B) calibrate the SCO to the 32KHz crystal. This will remain stable for a reasonable time, and the recalibration can be done periodically if needed. The DCO is rated at about 6% over the operating range of the device, but is quite stable over moderate term as long as temp and power supply are also fairly stable. In many cases, the calibration is more than stable enough for long term use.


c) Use the 32KHz clock for the beat timing, and use the DCO for serial, deriving the serial parameters by comparison against the crystal.


d) if you can count on the other device to talk first, use the crystal for beat timing, use the DCO for serial timing, and sync the serial to the other devices bitrate. This only works if you know you can count on the timing from the other end and you can force the other device to transmit first. I don't recall if the 2553 supports this in hardware, but it is pretty easy to set up in software.




Of these, I would probably use © as a low cost, easy choice. The serial clock can be tuned quite tightly (fraction of a clock cycle), and the calibration is pretty straightforward in software. I think one or another of the application notes deals with this, but if not, and you need a hand on this, many people here can outline methods.


That said, I have used the factory calibration values for the DCO with good success for serial timing. 8-bit serial needs stability of approx 3% to avoid losing bit frame registration (3% in opposing directions at opposite ends of a link is 6% total, which is about where the last bit frame will be out of timing bounds), which is reasonable for the DCO over most of the qualified operating range, though it is risky.

Link to post
Share on other sites

@@sven222 121.1 bpm is not particularly fast for the 32KHz crystal.


Every second a timer clocked by the 32Khz crystal will count 32,768 times.


We can work out how many clock cycles the timer will run for to achieve a 121.1bpm output.


121.1 [beats per minute] / 60 = 2.018 [beats per second]

Clock cycles = 32768 [cycles per second] / 2.018 [cycles] = 16,235.177 cycles.


Now we can't set a timer to 0.177 cycles. so we'll round down. CCR = 16,235.


Now rounding introduces error, lets see how big this error is by working backwards.

32,768 / 16,235 = 2.01835 Hz

2.01835 * 60 = 121.1013 bpm


(121.1 - 121.1013) / 121.1 = -0.0000109 = -10ppm (This is in the range of the crystal frequency specification)


I'm not sure if you would like an even more accurate timer than this.

(Keep in mind that if you use a faster clock source for the timer Module then the timer will be overflowing multiple times during your period, you will need to account for this in software)


Also please correct me if my maths is wrong, This was all "back of the envelope" stuff

Link to post
Share on other sites

Thank you Greeeg and enl.

Variant c sounds good, I have to have a look in this direction and find out how this will work. Thanks for the advices. To the "back of the envelope math" that is how I always calculate my stuff. The problem is, that midi does not need one impulse per beat, it needs 24. So I do the same calculation:


121.1 bpm * 24  / 60 = 48.44Hz


Clock cycles:

32768 / 48,44 = 676,46 cycles round down means for CCR = 676


Now calculate the bpm we give out:

Now we have in Hz = 32768 /676 = 48.47Hz

48.47 * 60 / 24 = 121.18bpm


(121.1 - 121.18) / 121.1 = 0,00066 = - 660 ppm


If I do the calculation with 121.2bpm the error is 1.3

Link to post
Share on other sites

Your error calc isn't correct. At 676,46 counts nominal and 676 counts actual, the error is at most half of (1/676)*100, which is 0,0014*100, or 0,14%, for a max error of 0,07% (this matches your calculation). At 121,2bpm, the error will be much less, as the actual rate with a count of 676 is 121,18. You will have a 0,1% worst case error at a freq of roughly 180bpm. At your 121bpm range, you have an accuracy of approximately +/-0.1bpm.


If you need more accurate than this, you can jitter the count in a manner analogous to the way the DCO clock and the clock divider for the serial module do. In fact, if it was me (which it is not) I probably would. I would have the timer interrupt at count, and reset the terminal count in the interrupt handler. Each time, rotate a 16 bit word with the appropriate bits set for the nearest 1/16th of a count. Mask the low order bit and add that to the count so when there is a '0' to the low order bit, the count is the for higher BPM, when a '1' it is a cycle longer for the lower exact BPM. This will get your error to less than 0,01% (100ppm) for all rates up to approximately 1000BPM. The max error will be less than one half period of the highest audible tone (ok, a bit more than 1/2 period if the listener happens to be a newborn, or a cat, or a dog). This is probably sufficient for most music applications.


Link to post
Share on other sites


I do not understand completely what you would do. I think it is because of my lack of english and my not that big programming skills. But I try do figure it out.

My idea would have been now, that I constantly try to adjust the DCO with the crystal. And then I can do the serial signal and the bpm with 4MHz and both should ba allright.  





Link to post
Share on other sites

Shouldn't need to constantly adjust. Unless you expect major changes in operating conditions (supply voltage, temperature) during operation, you should only need to check the calibration at startup. Stability of the oscillator is sufficient for the serial clock if conditions don't change.


You can do both serial and BPM with the DCO, but I would use the crystal for BPM (using a TimerA) and DCO for serial, myself. A couple percent variation is OK for serial if the DCO drifts a little, and the crystal is much, much more than fine for the BPM, and much more stable.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...