PICcoder.co.uk Forum  


::post new topic::
Re:Getting ADC result
Date: 2008/06/16 10:22 By: RWillis Status: Visitor  
 
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(0x800092); 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(3i2c_data1000) == 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); }

reply | quote


      Topics Author Date
    thread link
Getting ADC result
rwillis 2007/04/27 13:27
    thread link
thread linkthread link Re:Getting ADC result
dariog 2007/05/01 14:32
    thread link
thread linkthread linkthread link Re:Getting ADC result
rwillis 2007/05/03 12:05
    thread link
thread linkthread linkthread linkthread link Re:Getting ADC result
dariog 2007/05/15 10:51
    emo
thread linkthread linkthread linkthread linkthread link Re:Getting ADC result
RWillis 2008/06/16 10:22
::post new topic::