Squirrels have fuzzy tails.
0 Members and 1 Guest are viewing this topic.
#define REGISTER 0x00 #define DS1307_ADDR 0x68 #define BUFFSIZE 0x08 int bytes[8]; int i; rprintfInit(uart1SendByte); // Set up rprintf to be USB i2cInit(); //initialize I2C interface i2cSetBitrate(100); //set I2C transaction bit rate to 100 kHz i2cSendStart(); //Send I2C start condition in Master mode i2cSendByte(DS1307_ADDR | 0x00); //Send (address|R/W) combination or a data byte over I2C i2cSendByte(REGISTER); i2cSendStart(); //Send I2C start condition in Master mode i2cSendByte(DS1307_ADDR | 0x01); //Send (address|R/W) combination or a data byte over I2C for (i=0;i<8;i++) { i2cReceiveByte(1); bytes[i] = i2cGetReceivedByte(); } i2cSendStop(); //Send I2C stop condition in Master mode for (i=0;i<8;i++) { rprintf("%d.",bytes[i]); }
I do not have access to an o-scope until Monday, but I'd like to get this working over the weekend anyways.
32.768kHz Crystal ConnectionICCA – SCL clocking at max frequency = 100kHz
i2cSetBitrate(100); //set I2C transaction bit rate to 100 kHz
#define REGISTER 0x00 #define TARGET_ADDR 0x68 int bytes[7]; int i; int buffer; rprintfInit(uart1SendByte); // Set up rprintf to be USB i2cInit(); //initialize I2C interface i2cSetBitrate(1); //set I2C transaction bit rate to 100 kHz buffer = 0; i2cMasterSend(TARGET_ADDR, 0x01, &buffer); i2cMasterReceive(TARGET_ADDR, 8, &bytes[0]); for (i=0;i<8;i++) { rprintfu08(bytes[i]); rprintfChar('.'); } rprintf("\r"); delay_ms(1500);
00.00.00.00.01.00.1C.00.00.00.00.00.01.00.1C.00.00.00.00.00.01.00.1C.00.
buffer = 0;i2cMasterSend(TARGET_ADDR, 0x01, &buffer);
Look at how I did my compass code:http://www.societyofrobots.com/robotforum/index.php?topic=8393.0
STA-SLA+W-DATA-STO 0= 0 1= 0 2= 0 3= 0 4= 0 5= 0
void i2cMasterSendDiag(u08 deviceAddr, u08 length, u08* data){ // this function is equivalent to the i2cMasterSendNI() in the I2C library // except it will print information about transmission progress to the terminal // disable TWI interrupt cbi(TWCR, TWIE); // send start condition i2cSendStart(); i2cWaitForComplete(); rprintf("STA-"); // send device address with write i2cSendByte( deviceAddr&0xFE ); i2cWaitForComplete(); rprintf("SLA+W-"); // send data while(length) { i2cSendByte( *data++ ); i2cWaitForComplete(); rprintf("DATA-"); length--; } // transmit stop condition // leave with TWEA on for slave receiving i2cSendStop(); while( !(inb(TWCR) & BV(TWSTO)) ); rprintf("STO"); // enable TWI interrupt sbi(TWCR, TWIE);}void control(void) { #define REGISTER 0x00 #define TARGET_ADDR 0x68 int bytes[7]; int i; int temp; u08 in[6]; //6 bytes = MSB heading, LSB heading, MSB pitch, LSB pitch, MSB roll, LSB roll int buffer; rprintfInit(uart1SendByte); // Set up rprintf to be USB i2cInit(); //initialize I2C interface i2cSetBitrate(5); //set I2C transaction bit rate to 5 kHz i2cMasterSendDiag(0x68,0x01,0x00); delay_ms(10); i2cMasterReceive(0x68, 6, &in[0]); //read 6 byte data from slave to master for (i=0;i<6;i++) { rprintf("%d= %d ",i,in[i]); } delay_ms(1500);
START ADDR REGISTER DATA STOP SENT!! START ADDR REGISTER START ADDR STOP0 = 104 1 = 209 2 = 162 3 = 69 4 = 69 5 = 139 6 = 22 7 = 45
i2cInit(); //initialize I2C interface i2cSetBitrate(5); //set I2C transaction bit rate to 5 kHz delay_ms(10); // disable TWI interrupt cbi(TWCR, TWIE); // START i2cSendStart(); //Send I2C start condition in Master mode i2cWaitForComplete(); rprintf("START"); // I2C ADDRESS OF SLAVE i2cSendByte(TARGET_ADDR&0xFE); //Send (address|R/W) combination or a data byte over I2C i2cWaitForComplete(); rprintf(" ADDR"); // I2C DEVICE REGISTER i2cSendByte(REGISTER); i2cWaitForComplete(); rprintf(" REGISTER "); // DATA BYTE i2cSendByte(0x00); i2cWaitForComplete(); rprintf(" DATA "); // STOP i2cSendStop(); //Send I2C stop condition in Master mode rprintf(" STOP "); rprintf(" SENT!! "); delay_ms(10); while(1) { // START i2cSendStart(); //Send I2C start condition in Master mode i2cWaitForComplete(); rprintf("\rSTART"); // I2C SLAVE ADRESS i2cSendByte(TARGET_ADDR&0xFE); //Send (address|R/W) combination or a data byte over I2C i2cWaitForComplete(); rprintf(" ADDR"); // REGISTER i2cSendByte(REGISTER); i2cWaitForComplete(); rprintf(" REGISTER "); // START i2cSendStart(); //Send I2C start condition in Master mode i2cWaitForComplete(); rprintf("START "); // I2C SLAVE ADDRESS i2cSendByte(TARGET_ADDR&0xFF); //Send (address|R/W) combination or a data byte over I2C i2cWaitForComplete(); rprintf(" ADDR "); // DATA BYTES for (i=0;i<8;i++) { i2cReceiveByte(1); bytes[i] = i2cGetReceivedByte(); } // STOP i2cSendStop(); //Send I2C stop condition in Master mode rprintf("STOP\r"); for (i=0;i<8;i++) { rprintf("%d = %d ",i,bytes[i]); } delay_ms(1500); }
2khz : 0 = 104 1 = 162 2 = 69 3 = 22 4 = 90 5 = 180 6 = 104 7 = 162 5 khz: 0 = 104 1 = 209 2 = 162 3 = 69 4 = 69 5 = 139 6 = 22 7 = 45 10 khz : 0 = 104 1 = 162 2 = 69 3 = 22 4 = 90 5 = 180 6 = 104 7 = 162 100khz : 0 = 104 1 = 209 2 = 209 3 = 162 4 = 69 5 = 69 6 = 139 7 = 22
To make things wierder, when I change the frequency , I get different readings. But notice how the numbers are pretty much the same, just in a different order. Interestingly Byte 0 always remains 104 ( decimal value of the I2C slave's address)Quote2khz : 0 = 104 1 = 162 2 = 69 3 = 22 4 = 90 5 = 180 6 = 104 7 = 162 5 khz: 0 = 104 1 = 209 2 = 162 3 = 69 4 = 69 5 = 139 6 = 22 7 = 45 10 khz : 0 = 104 1 = 162 2 = 69 3 = 22 4 = 90 5 = 180 6 = 104 7 = 162 100khz : 0 = 104 1 = 209 2 = 209 3 = 162 4 = 69 5 = 69 6 = 139 7 = 22