Tuesday, March 24, 2009

Simple TWI / I2C communication with AVR ATMega8 and TC1321 DAC

I wanted to set up and test the D/A-converter TC1321 with a AVR, but had no previous knowledge of TWI. These are my experiences:

To make things easier I used a AVR with built in TWI function. I got a little confused on this subject when I read on AVR Freaks that for example the ATTiny26 had TWI function. Actually the microprosessors with a lowercase "y" in the comparison chart has USI which can be used for TWI. The uppercase "Y" for TWI indicates that the uC has built in TWI functionality. With such uCs you can include the to get some free predefined twi-codes.

To connect to a TWI device you need to connect the SCL and SDA lines to the uC. For ATMega8 it is PC4 (SDA) and PC5 (SCL). You then need to connect each of these via a pullup resistor to the source voltage (5V). For this connection I used two 220 ohm resistors.



So to sum up: I found it easier than I thought to interface the TC1321. Reading through descriptions that others have made describing both reading and writing to a slave (EEPROM etc) was not necessary when the only thing I wanted was to send data.

I found it easier than I had thought to communicate with the TC1321. First you need to set up the SCL-clock rate. You can calculate the TWBR with this formula:
TWBR=(fCPU/fSCL-16)/(2*4^TWPS)

So if you run your uC at fCPU=8MHz and you want fSCL=100kHz and try to use a prescaler TWPS=0 you get TWBR=(8e6/100e3-16)/(2*4^0)=32. So you start by setting:

TWPS=0;
TWBR=32;
To send data to the TC1321 you can use two protocols: 1-byte format (8-bits) and 2-byte format (10-bits). The progress of sending data is made in a sequence like this:

1-byte format:
START
SLA+W
COMMAND
DATA
STOP

2-byte format:
START
SLA+W
COMMAND
DATA
DATA
STOP

In between every sending you check the status codes by looking at TW_STATUS. The SLA+W is actually only the slave adress, 0x90.

Here are some functions that can be used to send data to the TC1321:


So to sum up: I found it easier than I thought to interface the TC1321. Reading through descriptions that others have made describing both reading and writing to a slave (EEPROM etc) was not necessary when the only thing I wanted was to send data.

Sunday, March 01, 2009

Mini review of "The 4-hour work week" by Timothy Ferris




I came across this book as I was looking for material before having a inspirational lecture about mathematics to new high school students. I ended up having a lecture of why you should embrace learning different things and the importance of knowing a little about most things in life. This is actually what Timothy Ferris was looking for when he decided to break out of his 9-5 traditional work as an employer ("slave" in his words) and start managing his own future. He wanted to work less, earn more and spend the extra time and money he had for traveling and enjoying life while he still was young and able to live it.

The book is a blue print on how to [D]efine your goals in life ("dreamlines"), [E]liminate time wasting and start focusing on realizing those goals (by using Pareto's principle and Parkinson's law), [A]utomate your business (by using outsourcing and the wonders of computers) and [L]iberating yourself from current work and the life you are living.

I really liked the book: It gave me a kick in the butt to start focusing on progressing towards my most important goals. Even though the internet business modell is perfect for people having a big home market, many of the tips are quite transferrable to everyone wanting to start up something new.

I am not dreaming about traveling the world throughout my life anymore, but I still would prefer having more time for hobbies and family. I think this book gave me gigant leaps towards achieving this.