00001
00006 #include "twi_basics.h"
00007 #include <util/twi.h>
00008
00012 #define TW_CONF_MASK ((1<<TWWC) | (1<<TWEN) | (1<<TWIE) | (1<<1) )
00013
00014 #define TW_SEND_CMD(cmd) TWCR = (TWCR & TW_CONF_MASK ) | (cmd)
00015
00016
00019 void wait()
00020 {
00021 while (!(TWCR & (1<<TWINT)));
00022 }
00023
00028 twi_status twi_start()
00029 {
00030 TW_SEND_CMD( (1<<TWINT) | (1<<TWSTA) );
00031 wait();
00032 return TW_STATUS;
00033 }
00034
00040 twi_status twi_send(uint8_t data)
00041 {
00042 TWDR = data;
00043 TW_SEND_CMD( (1<<TWINT) );
00044 wait();
00045 return TW_STATUS;
00046 }
00047
00054 twi_status twi_receive(uint8_t * where,uint8_t ack)
00055 {
00056 if(ack)
00057 TW_SEND_CMD((1<<TWINT)|(1<<TWEA));
00058 else
00059 TW_SEND_CMD(1<<TWINT);
00060 wait();
00061 *where=TWDR;
00062 return TW_STATUS;
00063 }
00064
00068 void twi_stop()
00069 {
00070 TW_SEND_CMD((1<<TWINT)|(1<<TWSTO));
00071 while ((TWCR & (1<<TWSTO)));
00072 }