I2c.c

Go to the documentation of this file.
00001 #include "I2c.h"
00002 
00003 
00004 
00005 /***********************
00006    init  TWI
00007 ************************/
00008 I2c::I2c(void) {
00009 
00010   TWSR = 0x00;                            // prescale = 1
00011   TWBR = (unsigned char)(((double)((double)F_CPU / (double)TWI_CLK) - 16L) / 2L);  // 100kHz
00012 
00013 }
00014 
00015 
00016 //**********************************************
00017 //   I2C - start condition
00018 //**********************************************
00019 void I2c::sendStart (void)
00020 {
00021 
00022   TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);    // start condition
00023   while(!(TWCR & (1<<TWINT)));                    // wait for acknowledge
00024 
00025 }
00026 
00027 
00028 //**********************************************
00029 //   I2C - Stop condition 
00030 //**********************************************
00031 void I2c::sendStop (void)
00032 {
00033 
00034   TWCR = (1 << TWINT) | (1 << TWSTO) | (1 << TWEN);    // stop condition =0;
00035 
00036 }
00037 
00038 
00039 //**********************************************
00040 //   I2C - address and read
00041 //**********************************************
00042 void I2c::writeByte (unsigned char _byte)
00043 {
00044 
00045   //TWDR = 0x90;                                  // Byte
00046   TWDR = _byte;                                   // Byte
00047   TWCR = (1 << TWINT) | (1 << TWEN);                   // send
00048   while(!(TWCR & (1<<TWINT)));                    // wait until ready
00049 
00050 }
00051 
00052 //**********************************************
00053 //   I2C - receive one byte (with acknowledge)
00054 //**********************************************
00055 unsigned char I2c::readByte (void)
00056 {
00057 
00058   TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);     // 
00059   while(!(TWCR & (1<<TWINT)));                         // wait till ready
00060   return TWDR;                          // data is in TWDR
00061 
00062 }
00063 
00064 // **********************************************
00065 //   I2C - receive one byte (without acknowledge)
00066 // **********************************************
00067 unsigned char I2c::readByteWithoutAck (void)
00068 {
00069 
00070   TWCR = (1 << TWINT) | (1 << TWEN);              // 
00071   while(!(TWCR & (1<<TWINT)));                         // wait till ready
00072   return TWDR;                          // data is in TWDR
00073 
00074 }
Generated on Fri Oct 8 17:10:07 2010 for Crawling Robot Microcontroller Software by  doxygen 1.6.3