Hi
Sorry for delay in updating this code, but finally I have something that works and is tested, extensively. The code snippet below is just that a snippet, its what allows you to configure the MPC3421 and read a value from it into the ADCReading variable.
You can use this as a basis to setup your own routines, etc. It hasn't actually taken me all this time to figure this out its just I'm very busy and completely forget to post it here until now.
Anyway hope it will help someone somewhere.
Russ
| Code: |
// set up the I2C and MCP3421 using the peripheral library
// 400kHz using 40MHz clock
OpenI2C1(0x8000, 92);
StopI2C1();
while(I2C1CONbits.PEN);
// set the 3421 into 16bit mode
StartI2C1();
while(I2C1CONbits.SEN);
if (MasterWriteI2C1(0xD0) == 0) {
// wait for data to end transmission
while(I2C1STATbits.TRSTAT);
// wait for ack
while(I2C1STATbits.ACKSTAT);
// set into continuous mode 16bit - 8V/V
MasterWriteI2C1(0x18);
while(I2C1STATbits.TRSTAT);
while(I2C1STATbits.ACKSTAT);
StopI2C1();
while(I2C1CONbits.PEN);
}
StartI2C1();
while(I2C1CONbits.SEN);
// write Slave address
if (MasterWriteI2C1(0xD1) == 0) {
// wait for the data to be written
while(I2C1STATbits.TRSTAT);
// wait for ACK
while(I2C1STATbits.ACKSTAT);
// now read 3 bytes of data
if (MastergetsI2C1(3, i2c_data, 1000) == 0) {
// successful read so convert the result
// i2c_data[0:1] form the 16 bit result
// i2c_data[2] = config byte
if ((i2c_data[2] & 0x80) == 0) {
// new data available
ADCReading = (i2c_data[0] << 8) | i2c_data[1];
}
}
StopI2C1();
while(I2C1CONbits.PEN);
}
|
|