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);
}
}