Hi,
I am trying to use the I2C interface for the first time and am unsure of my code. I am using the GT16A as a master and am attempting to read data from the MAX1069 14 bit ADC IC. Below is my I2C initialising function and the read function. I am not getting the results I expected and I am unsure why. Any help will be greatly appreciated! edit->Turns out I forgot the pull-up resistors, but I still can't get the ADC to function correctly..
int receiveIIC (void) // Thermistor ADC - MAX1069 { byte dataADC[2] ; int result; unsigned char MAX_delay = 100; unsigned int count; IICC_IICEN = 1; // IIC enabled IICC_TX = 1; // Set Transmit IICC_MST = 1; // Set START // Send address. LSB decides on R or W while(IICS_TCF == 0); IICD = 0x61; // Address -> 0110000, LSB for receive -> 1 = 0x61 // Address -> 0110000, LSB for receive -> 0 = 0x60 for (count=0;count<MAX_delay;count++) asm("nop"); while(IICS_TCF == 0); IICC_TX = 0; dataADC[0] = IICD; // Receive first byte for (count=0; count<MAX_delay;count++) asm("nop"); while(IICS_TCF == 0); IICC_TXAK = 1; // Send No Acknowledge dataADC[1] = IICD; // receive second byte for (count=0; count<MAX_delay;count++) asm("nop"); while(IICS_TCF == 0); IICC = 0x80; result = dataADC[0] << 8; result += dataADC[1]; return result; }
Hello Oliver!
How is the project going?
We'd like to know if this suggestion was helpful throughout your problem, would you share with us?
Best regards,
Monica.
Hi Hobson,
There is a AN to show you how to use IIC in hcs08, hope it is useful.
http://cache.freescale.com/files/microcontrollers/doc/app_note/AN3291.pdf
And just take a look at your code, I think there are three problems.
1. You didn't check ACK from slave.
2. There are some problems after transimiting slave address
3. result = dataADC[0] << 8; I don't think that is a good practice to shift like that. dataADC[0] is a 8-bit data, your practice is depending on compiler's implicit cast. the good practice is to do cast by ourselves. like that:
result = (int)dataADC[0] << 8;
Hope my answers is helpful.
.
So I started to attempt to write the I2C send routine that will communicate with a 14 bit, AD5647R DAC. I have encountered a problem where the high bit for the data is missing the front 2 zeroes after I split the incoming value. I have done this as shown below. Any ideas? Also still haven't managed to get the read function to work... edit->It is working with the code below now
void sendIIC (int output)
{
byte dataDAC[2];
unsigned char MAX_delay = 100;
unsigned int count;
dataDAC[0] = (output>>8) & 0x00FF; // High
dataDAC[1] = (output) & 0x00FF; // Low
IICC_IICEN = 1; // IIC enabled
IICC_TX = 1; // Set Transmit
IICC_MST = 1; // Set START
// Send address. LSB decides on R or W
while(IICS_TCF == 0);
IICD = 0x1E; // Address -> 0001111, LSB for receive -> 0
for (count=0; count<MAX_delay;count++) asm("nop");
// Send Command byte
while(IICS_TCF == 0);
IICD = 0x1F; // Command: R->0 ,S->0 ,Command->011 (Write to and update DAC n) ,DAC Address->111 (Both DACs)
for (count=0; count<MAX_delay;count++) asm("nop");
// Send high data byte
while(IICS_TCF == 0);
IICD = dataDAC[0]; // Send the high bits
for (count=0; count<MAX_delay;count++) asm("nop");
// Send low data byte
while(IICS_TCF == 0);
IICD = dataDAC[1]; // Send the low bits
for (count=0; count<MAX_delay;count++) asm("nop");
IICC = 0x80;
}
Hi Hobson,
The send function's problem is just like receving function. you didn't wait slave's ACK. and at the last, you'd better polling IICS_TCF to confirm dataDAC[1]'s transfer is completed.
IICD = dataDAC[1]; // Send the low bits
for (count=0; count<MAX_delay;count++) asm("nop");
while(IICS_TCF == 0);
IICC = 0x80;