[General] MCP3425 Interface code

Status
Not open for further replies.

ajit_nayak87

Member level 5
Joined
Oct 30, 2017
Messages
86
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Activity points
981
I am trying to Interface PIC 18F24k40 with MCP3425 . I am using Mplab v 3.61 with C8 compiler v1.5.
Here part of code. Where i am trying to read I2C value and convert value into voltage and Display.
With below code i could see change in values continously. But like to Know how i2C read value can be converted to use further for reading Precision value. I m trying to monitor Temp parameter of j,k ,rtd with precision value.



Code:
void IdleI2C1( void ) 
{
     unsigned int I2c_timeout;
	while ( ( SSP1CON2 & 0x1F ) | ( SSP1STATbits.R_W ) )
    {
       I2c_timeout++;
       if(I2c_timeout>4000)
       {
           I2c_timeout=0;
           return;
       }
        
    } 
        
	
}



void Write_MCP3425()
 {
    
    SSP1CON2bits.SEN=1;
    while(1==SSP1CON2bits.SEN);
    SSP1BUF =0B11010000; 
     IdleI2C1(); 
    
      if((CONTROLREG & 0x04) == 0x04)						
	           SSP1BUF =0x89;					// gain 2
	     else
             SSP1BUF =0x8B;	
          
     
     IdleI2C1(); 
     SSP1CON2bits.PEN = 1;        // Initiate Stop condition
    while (1==SSP1CON2bits.PEN);                // Wait for Stop condition to complete    
    
    x=SSP1BUF;
     
 }

void Read_MCP3425()
{
   SSP1CON2bits.RSEN =1;
 while(1==SSP1CON2bits.RSEN); 
IdleI2C1();  
 SSP1BUF =0b1101000;
IdleI2C1();  
 SSP1BUF =0X89;
 SSP1CON2bits.RSEN =1;
 while(1==SSP1CON2bits.RSEN);
IdleI2C1();  
 SSP1BUF =0b1101001;
IdleI2C1(); 
 SSP1CON2bits.RCEN =1;
while(1==SSP1CON2bits.RCEN);
 while (1==SSP1STATbits.BF); 
 ADCValue[0]=SSP1BUF;
IdleI2C1(); 
 
  while (1==SSP1STATbits.BF); 
 ADCValue[1]=SSP1BUF;

 ADCResult = (ADCResult << 8) | ADCValue[1];
	ADCaverage += ADCResult;
    
 IdleI2C1();
SSP1CON2bits.ACKDT = 1;      // Prepare to send NACK            
SSP1CON2bits.ACKEN = 1;      // Initiate NACK to EEPROM
 SSP1CON2bits.PEN=1;
while(1== SSP1CON2bits.PEN);
 
 
 
    
}


void Blink_Count()
{
    
   if(PIR0bits.TMR0IF == 1)
   { 
       PIR0bits.TMR0IF =0;
       count=count+1;
       if(count>=30)
       {
          Dissect(ADCaverage);
           Display();          
                           
       }
       
   }
    
}


void main()

{

while (1)
    {
       Read_MCP3425();
             
        Delay(5000);
        
        
    }
}
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…