CONTENTS

Home
Updates
Software
Electronics
Android / iOS
Videos
Music
Resume
Contact



HTTPS VERSION


The MSP430 DCO (And A Thermocouple)

Posted: March 14, 2015

Introduction

This project started out as a simple project to connect a thermocouple to an MSP430 so the data can be displayed on a mobile device. This ended up as more of an experiment on the MSP430G2553's digitally controlled oscillator (DCO). The problem with the DCO is it's not an accurate clock source which isn't even consistent between different chips. So my goals for this project ended up being:

  • Using software on the chip itself, figure out how fast the DCO really is.
  • Auto adjust baud divisor based on DCO speed.
  • Create a chart of all the DCO settings (further down on this page).
  • Compare difference in the DCO speed from two microcontrollers with the same part number.
  • Test the DCO during a temperature change (in a freezer).
  • Prove Bluetooth and BLE signals can travel through the walls a refrigerator.

I posted my source code to do the calibration at the bottom of this page. Other people may find it useful for their own projects for adjusting their baud rates based on the DCO speed.

I did another project with a similar DCO issue a few months ago: MSP430 VGA with Java. This one used an MSP430F5529 to generate a VGA signal originally with the DCO and the MSP430's built in 32.768kHz crystal calibration which did create a VGA signal, but with a ton of jitter. I ended up fixing it with a real 25MHz crystal.

Related Projects @mikekohn.net

Thermometers: Thermometers, Clock Thermometer, Bluetooth Thermometer, MSP430 DCO, E-Ink Thermometer

Pictures

MSP430G2553 with Thermocouple and BLE

This is the thermocouple circuit with the RedBearLab BLE-Mini and a K type thermocouple.

Video

This video shows on the left an Android phone connected over Bluetooth 2.0 to my older DS18B20 Bluetooth thermometer and on the right an iPod touch connected over BLE to the newer thermocouple circuit. I put both circuits in the freezer to show that the signal is strong enough to go through a refrigerator's wall, that the DCO remains constant, and the thermocouple reacts a lot faster. https://youtu.be/EIMklPfZ-XQ

The Thermocouple

I'll start with the thermocouple since this is really the most boring part of this project. The parts I used were:

The RedBearLab BLE-Mini wants a UART connection to the micro at 57600 baud which kind of becomes a challenge with the MSP430 DCO. The baud rate divisor is calculated by clock source / baud. For example: 16000000MHz / 9600 baud = 1666 (0x0682):

  mov.b #0x82, &UCA0BR0
  mov.b #0x06, &UCA0BR1

Since there is no guarantee when setting the DCO that you'll get the frequency you think you're getting, what I've done in the past while using the MSP430's UART is... well first of all I wouldn't run anything above 9600 baud. Then I would calibrate my code for the chip I'm using by looking at an output pin of on an oscilloscope, figuring out how far off my DCO is from that, and adjusting my baud rate constant for this specific chip. If I blew the chip and needed to program a new one, I had to recalibrate my code again.

I actually did think about using an Atmel ATtiny2313 (one of my other favorite chips) for this project with a 20MHz crystal or even just using a built in clock source which is still much more accurate than the DCO, but I really like the MSP430's debugging and programming tools. Also worth noting: according to the msp430x2xx user's guide, the baud rate generator can use a 32.768kHz crystal to get up to 9600 baud, but I haven't tried this yet.

The video above shows this circuit connected over BLE to an iPod touch next to my older thermometer project connected with Bluetooth 2.0. I put both in the freezer proving that the Bluetooth and BLE signals are strong enough to go through my refrigerator walls and that the the DCO appears to be stable during this temperature change. Also, it's interesting to note how much faster the thermocouple reacted to the temperature change vs. the DS18B20.

The DCO

In order to figure out the speed of the DCO in software I connected a 32.768kHz crystal to Xin/Xout of my MSP430. I set up Timer A and Timer B so that Timer A interrupts every 512 cycles and gets its clock source from the DCO. Timer B interrupts every 32768 cycles and gets its clock source from the 32768 Hz crystal. Therefore Timer B should interrupt once a second and tell how many DCO interrupts there were in that interval. Multiplying the number of DCO interrupts by 512 (aka, binary shift left by 9) should tell how many CPU ticks there were, maybe off by 511 cycles or so.

I wrote some code that would do this multiplication saving the result into 4 bytes of memory and then dividing this number by the baud rate I want. In the thermocouple circut I did this at the top of the program once.

In order to set the DCO speed, there are two constants I use: DCO_x (DCO speed) and RSEL_x (range select):

  ;; Set MCLK to 16 MHz with DCO 
  mov.b #DCO_4, &DCOCTL
  mov.b #RSEL_15, &BCSCTL1
  mov.b #0, &BCSCTL2

There is also a modulator selection, which gives a frequency between two DCO frequencies. For example in my case where RSEL=15 / DCO=4 I get 14.754MHz and RSEL=15 / DCO=5 I get 15.981MHz. Using the modulator (a value between 0 and 32) I should be able to get some frequencies in between DCO_4 and DCO_5. I've never used this setting before.

So next I took the thermocouple source code and stripped it down so it was just the DCO calibration code. I programmed the chip to cycle through all DCO/RSEL combinations that it could (some ended up not working because the clock speed was too low to generate an accurate baud rate and I ended up with baud barf) and output the frequency results over UART. A Python script collects all the data and builds an HTML table which I posted below:

DCO_0DCO_1DCO_2DCO_3DCO_4DCO_5DCO_6DCO_7
RSEL_0-0.096MHz0.102MHz0.110MHz0.119MHz0.130MHz0.142MHz0.157MHz
RSEL_10.112MHz0.119MHz0.127MHz0.137MHz0.148MHz0.161MHz0.177MHz0.196MHz
RSEL_20.155MHz0.165MHz0.176MHz0.189MHz0.205MHz0.223MHz0.245MHz0.271MHz
RSEL_30.219MHz0.233MHz0.249MHz0.268MHz0.290MHz0.315MHz0.346MHz0.383MHz
RSEL_40.300MHz0.319MHz0.342MHz0.367MHz0.396MHz0.432MHz0.473MHz0.524MHz
RSEL_50.428MHz0.456MHz0.487MHz0.523MHz0.566MHz0.615MHz0.674MHz0.745MHz
RSEL_60.588MHz0.626MHz0.669MHz0.718MHz0.775MHz0.843MHz0.924MHz1.021MHz
RSEL_70.834MHz0.886MHz0.946MHz1.016MHz1.096MHz1.190MHz1.303MHz1.440MHz
RSEL_81.181MHz1.255MHz1.339MHz1.436MHz1.549MHz1.680MHz1.838MHz2.031MHz
RSEL_91.710MHz1.815MHz1.935MHz2.074MHz2.233MHz2.421MHz2.646MHz2.919MHz
RSEL_102.459MHz2.607MHz2.776MHz2.971MHz3.195MHz3.459MHz3.779MHz4.165MHz
RSEL_113.061MHz3.250MHz3.464MHz3.714MHz4.004MHz4.338MHz4.742MHz5.256MHz
RSEL_124.191MHz4.441MHz4.727MHz5.066MHz5.468MHz5.930MHz6.460MHz7.100MHz
RSEL_135.833MHz6.169MHz6.538MHz6.963MHz7.478MHz8.118MHz8.911MHz9.891MHz
RSEL_148.422MHz8.944MHz9.543MHz10.217MHz11.008MHz11.895MHz12.920MHz14.140MHz
RSEL_1511.744MHz12.369MHz13.048MHz13.813MHz14.754MHz15.981MHz17.551MHz19.405MHz

I ran the exact same firmware on another MSP430G2553 chip and got these quite different results:

DCO_0DCO_1DCO_2DCO_3DCO_4DCO_5DCO_6DCO_7
RSEL_0-----0.126MHz0.139MHz0.154MHz
RSEL_1--0.124MHz0.133MHz0.144MHz0.157MHz0.172MHz0.191MHz
RSEL_20.152MHz0.162MHz0.173MHz0.186MHz0.201MHz0.219MHz0.240MHz0.266MHz
RSEL_30.214MHz0.228MHz0.243MHz0.262MHz0.283MHz0.308MHz0.338MHz0.375MHz
RSEL_40.296MHz0.315MHz0.337MHz0.361MHz0.391MHz0.425MHz0.467MHz0.518MHz
RSEL_50.418MHz0.445MHz0.475MHz0.511MHz0.552MHz0.601MHz0.659MHz0.729MHz
RSEL_60.583MHz0.621MHz0.664MHz0.712MHz0.769MHz0.836MHz0.915MHz1.012MHz
RSEL_70.825MHz0.878MHz0.937MHz1.006MHz1.085MHz1.180MHz1.290MHz1.426MHz
RSEL_81.175MHz1.250MHz1.334MHz1.431MHz1.543MHz1.675MHz1.830MHz2.021MHz
RSEL_91.712MHz1.818MHz1.938MHz2.077MHz2.235MHz2.423MHz2.643MHz2.915MHz
RSEL_102.485MHz2.636MHz2.806MHz3.002MHz3.227MHz3.492MHz3.807MHz4.194MHz
RSEL_113.134MHz3.326MHz3.544MHz3.796MHz4.088MHz4.425MHz4.824MHz5.339MHz
RSEL_124.279MHz4.534MHz4.823MHz5.166MHz5.573MHz6.039MHz6.560MHz7.194MHz
RSEL_135.985MHz6.328MHz6.697MHz7.122MHz7.639MHz8.289MHz9.081MHz10.065MHz
RSEL_148.906MHz9.461MHz10.076MHz10.777MHz11.586MHz12.483MHz13.490MHz14.741MHz
RSEL_1512.489MHz13.126MHz13.811MHz14.623MHz15.662MHz16.993MHz18.599MHz20.444MHz

Schematic
Thermocouple schematic

Source code
dco_calibrate.asm

Source code assembles with naken_asm.

Copyright 1997-2018 - Michael Kohn