As with nearly all cool electronic devices that I buy, this one too came from the foreign lands and was off eBay (http://goo.gl/jbcKE6 – Link will expire as with all eBay links)!
One of the features that I’ve wanted to have in my watch that I’d actually find really useful is the ability to have a compass. While my phone offers a compass too, having one mounted on my wrist is just so much cooler so as a bit of a tester, I bought this board (£1.16 at time of purchase) and decided to hook it up to my STM32F0 discovery board. It was also a good way for me to learn the I2C protocol as I’d never used it before.
I’ve always thought that I2C was a really cool protocol having all devices in an open drain configuration and only eve requiring two wires. Some devices can even be powered by the pullup resistors on the I2C bus themselves which is even cooler!
Having become relatively educated in the SPI protocol, I thought that many of the parameters would be the same for I2C but how wrong I was. Firstly most of I2C seems to be sequential flag checking, looking for acknowledge bits and so on. After searching the internet for STM32F0 examples, I couldn’t find any at all which was a bit annoying! So in the end, I dived straight in and after a frustrating hour or so, I got my first magnetometer reading through!
The HMC5883L (http://goo.gl/XMYQ31), is really useful as it essentially contains a loop buffer containing all of the read values and the address of this buffer increments with every read meaning that you only need to initialize the register address at register 03, then execute 6 I2C reads to get all XYZ values. Each XYZ read is 12bit and is transmitted as a high and low pair of bits. These bits are in the two’s compliment form and can be easily concatenated with 1 shift by 8 bits and an OR.
I’m yet to get a proper compass application written but that will be soon to follow! My next IC to play with will be the LSM303 which integrates a magnetometer and an accelerometer. Once I figure this IC out, I’ll hopefully be incorporating it into the watch so I can have accelerometer activated wakeup!
If I ever get round to learning to Github, I’ll upload my code for this!
Update: For a more useful implementation of the compass, visit my new post: https://hsel.co.uk/2014/08/13/stm32f0-mini-tutorial-using-the-i2c-peripheral-to-communicate-with-a-hmc5883l-digital-compass-ic/