[En-Nut-Discussion] TWI / I2C and DS1307
Douglas Pearless
Douglas.Pearless at pearless.co.nz
Thu Feb 3 10:34:16 CET 2005
Thanks to the people who have sent me code...
I have persevered with the TWI code that is part of NUTO/S
I have:
#define RTC_I2C_ADDRESS 0x68 // this is first 7 bits the address of the
// remembering it gets shifted one bit to the left to make it 0xD0
//for the maxim DS1307 RTC
...
TwInit(128);
fprintf(uart0, "\n\rTwInit RC = %d\n", TwMasterError());
fflush(uart0);
TWI_speed = 100000;
TwIOCtl(TWI_SETSPEED,&TWI_speed);
fprintf(uart0, "\n\rTWI_SETSPEED RC = %u\n", TwMasterError());
fflush(uart0);
// init RTC
initRTC((u_char)RTC_I2C_ADDRESS);
fprintf(uart0, "\n\rinitrtc RC = %d\n", TwMasterError());
fflush(uart0);
NutDelay(2000);
getRTCtime((u_char)RTC_I2C_ADDRESS);
fprintf(uart0, "\n\rgetRTCtime RC = %d\n", TwMasterError());
fflush(uart0);
and
int readRTC(u_char sla,u_int addr,void *rxxdata,u_short len)
{
u_char adata[1];
adata[0] = (u_char) addr;
return TwMasterTransact(sla, adata, 1,rxxdata, len, 0);
}
int writeRTC(u_char sla,u_int addr,void *txdata,u_short len)
{
u_char data[17];
data[0] = (u_char)(addr);
memcpy(&data[1],txdata,len);
return TwMasterTransact(sla, data,len+1, 0, 0, 0);
}
int initRTC(u_char sla)
{
/* DS1307
*/
u_char data[12]; //yes an array of 1 is redundant, but I want
consistency
int rc;
data[0] = 0b00000000; //bit =0 to start oscillator, zero minutes
data[1] = 0b00000000; // zero minutes
data[2] = 0b00100000; //24hour mode, 8pm
data[3] = 0b00000100; //day 6 (Saturday)
data[4] = 0b00000001; //1st
data[5] = 0b00000001; //January
data[6] = 0b00000101; //2005
data[7] = 0b00010000; //turn on the square wave
data[8] = 'A';
data[9] = 'B';
data[10]= 'C';
data[11]='D';
rc = writeRTC(sla,0,data,12);
return rc;
}
int getRTCtime(u_char sla)
{
u_char data[12];
int rc;
data[0] = 0b00000000;
data[1] = 0b00000000;
data[2] = 0b00000000;
data[3] = 0b00000000;
data[4] = 0b00000000;
data[5] = 0b00000000;
data[6] = 0b00000000;
data[7] = 0b00000000;
data[8] = 0b00000000;
data[9] = 0b00000000;
data[10] = 0b00000000;
data[11] = 0b00000000;
rc = readRTC(sla,0,&data,12);
return rc;
}
On UART0 I get:
TwInit RC = 0
TWI_SETSPEED RC = 0
initrtc RC = 0
getRTCtime RC = 0
and in AVRStudio, the getRTCtime always returns "data" as being:
data[0] = 0x00
data[1] = 0x80
data[2] = 0x00
data[3] = 0x82
data[4] = 0x88
data[5] = 0x0C
data[6] = 0x40
data[7] = 0x80
data[8] = 0x20
data[9] = 0x00
data[10] = 0x12
data[11] = 0x00
I can see pulses on the SDA and SCL lines, but I always get the same garbage
back.
Note that data[8] through data[11] are RAM locations.
Any ideas??
Cheers Douglas
--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.8.2 - Release Date: 28/01/2005
More information about the En-Nut-Discussion
mailing list