2009年8月9日 星期日

dePIC24F I2C Master 程式

#include "../hfiles/p24fj64ga006.h"
#include "../hfiles/pin.h"

/**********************************************************

**********************************************************/
void main(void)
{
ConfigIntI2C();
OpenI2C();

unsigned char temp1,temp2;
//WP=0; /*write protection*/

temp1=write_I2C(0X1F0,0X35); //(unsigned int I2C_addr, unsigned char I2C_data);

temp2=read_I2C(0X1F0); //(unsigned int I2C_addr);
//WP=1;
}
/**********************************************************

**********************************************************/
unsigned char write_I2C(unsigned int I2C_addr, unsigned char I2C_data)
{
StartI2C();
while(I2C1CONbits.SEN==1);

MasterWriteI2C(0xa2); /*device address*/
while(I2C1STATbits.TRSTAT);
if(I2C1STATbits.ACKSTAT==1) return 0;

MasterWriteI2C((I2C_addr>>8)&0XFF); /*high 8 bit data address*/
while(I2C1STATbits.TRSTAT);
if(I2C1STATbits.ACKSTAT) return 0;

MasterWriteI2C((I2C_addr)&0XFF); /*low 8 bit data address*/
while(I2C1STATbits.TRSTAT);
if(I2C1STATbits.ACKSTAT) return 0;

MasterWriteI2C(I2C_data); /*low 8 bit data*/
while(I2C1STATbits.TRSTAT);
if(I2C1STATbits.ACKSTAT) return 0;

StopI2C();
while(I2C1CONbits.PEN);
return 1;
}
/**********************************************************

**********************************************************/
unsigned char read_I2C(unsigned int I2C_addr)
{
unsigned char temp;

StartI2C();
while(I2C1CONbits.SEN);

MasterWriteI2C(0xa2); /*DEVICE address,write*/
while(I2C1STATbits.TRSTAT);

MasterWriteI2C((I2C_addr>>8)&0XFF); /*device high 8 bit data address*/
while(I2C1STATbits.TRSTAT);

MasterWriteI2C(I2C_addr& 0XFF); /*device low 8 bit data address*/
while(I2C1STATbits.TRSTAT);

RestartI2C();
while(I2C1CONbits.RSEN);

MasterWriteI2C(0xa3); /*device address,read*/
while(I2C1STATbits.TRSTAT);

temp=MasterReadI2C();

NotAckI2C();

StopI2C();
while(I2C1CONbits.PEN);

return temp;
}
/**********************************************************

**********************************************************/
unsigned char MasterReadI2C(void)
{
I2C1CONbits.RCEN = 1;
while(I2C1CONbits.RCEN);

I2C1STATbits.I2COV = 0;

return(I2C1RCV);
}
/**********************************************************

**********************************************************/
char MasterWriteI2C(unsigned char data_out)
{
I2C1TRN = data_out;

if(I2C1STATbits.IWCOL) /* If write collision occurs,return -1 */
{
I2C1STATbits.IWCOL=0;return -1;
}
else
{
return 0;
}
}
/**********************************************************

**********************************************************/
void AckI2C(void)
{
I2C1CONbits.ACKDT = 0;
I2C1CONbits.ACKEN = 1;
}
/**********************************************************

**********************************************************/
void ConfigIntI2C(void)
{
/*
SI2CIF = 0; // clear the MI2C & SI2C Interrupts
MI2CIF = 0;

SI2CIP0 = 0; // set the SI2C priority
SI2CIP1 = 0; // set the SI2C priority
SI2CIP2 = 0; // set the SI2C priority
MI2CIP0 = 0; // set the MI2C priority
MI2CIP1 = 0; // set the MI2C priority
MI2CIP2 = 0; // set the MI2C priority
SI2CIE = 0; // enable/disable the SI2C Interrupt
MI2CIE = 0; // enable/disable the MI2C Interrupt
*/
}
/**********************************************************

**********************************************************/
void NotAckI2C(void)
{
I2C1CONbits.ACKDT = 1;
I2C1CONbits.ACKEN = 1;
}
/**********************************************************

**********************************************************/
void OpenI2C(void)
{
I2C1BRG = 27; /*BAUD= 100khz,at 11.0592mhz fcy*/
I2C1CONbits.I2CEN=1;
}
/**********************************************************

**********************************************************/
void RestartI2C(void)
{
I2C1CONbits.RSEN = 1; /* initiate restart on SDA and SCL pins */
}
/**********************************************************

**********************************************************/
void StartI2C(void)
{
I2C1CONbits.SEN = 1; /* initiate Start on SDA and SCL pins */
}
/**********************************************************

**********************************************************/
void StopI2C(void)
{
I2C1CONbits.PEN = 1; /* initiate Stop on SDA and SCL pins */
}
/**********************************************************

**********************************************************/