#include "config.h"
#define MCP3421_WRITE_ADDR 0xD0
#define MCP3421_READ_ADDR 0xD1
#define FM24C02_WRITE_ADDR 0xA0
#define FM24C02_READ_ADDR 0xA1
/* I2C初始化配置 */
void Init_I2C(void)
{
TRISB |= 0x50;
SSPSTAT = 0x80;
SSPCON = 0x38;
SSPCON2 = 0x00;
//SSPCON3 = 0x80;
SSPADD = 0x09; //(FOSC/((SSPADD+1)*4))
}
/* I2C启动信号 */
static void I2C_Start(void)
{
SSP1IF = 0;
SSPCON2bits.SEN = 1;
while(!SSP1IF);
SSP1IF = 0;
}
/* I2C重新启动信号 */
static void I2C_Restart(void)
{
SSP1IF = 0;
SSPCON2bits.RSEN = 1;
while(!SSP1IF);
SSP1IF = 0;
}
/* I2C停止信号 */
static void I2C_Stop(void)
{
SSP1IF = 0;
SSPCON2bits.PEN = 1;
while(!SSP1IF);
SSP1IF = 0;
}
void I2C_WriteByte(u8 uAddr, u8 uDat)
{
I2C_Start();
SSP1BUF = FM24C02_WRITE_ADDR;
while(!SSP1IF);
SSP1IF = 0;
SSP1BUF = uAddr;
while(!SSP1IF);
SSP1IF = 0;
SSP1BUF = uDat;
while(!SSP1IF);
SSP1IF = 0;
I2C_Stop();
}
u8 I2C_ReadByte(u8 uAddr)
{
u8 uData;
I2C_Start();
SSP1BUF = FM24C02_WRITE_ADDR;
while(!SSP1IF);
SSP1IF = 0;
SSP1BUF = uAddr;
while(!SSP1IF);
SSP1IF = 0;
I2C_Restart();
SSP1BUF = FM24C02_READ_ADDR;
while(!SSP1IF);
SSP1IF = 0;
SSPCON2bits.RCEN = 1;
while(!SSP1IF);
uData = SSP1BUF;
while(!SSP1IF);
SSP1IF = 0;
SSPCON2bits.ACKDT = 1;
SSPCON2bits.ACKEN = 1;
while(!SSP1IF);
SSP1IF = 0;
I2C_Stop();
return (uData);
}
u16 I2C_MCP3421_ReadADValue(u8 u_config)
{
u8 adc_h = 0,adc_l = 0,adc_val = 0;
u16 adc_value = 0;
I2C_Start(); //启动I2C
SSP1BUF = MCP3421_WRITE_ADDR; //写模式
while(!SSP1IF);
SSP1IF = 0;
SSP1BUF = u_config; //写配置位
while(!SSP1IF);
SSP1IF = 0;
I2C_Stop(); //停止I2C
I2C_Start(); //启动I2C
SSP1BUF = MCP3421_READ_ADDR; //读模式
while(!SSP1IF);
SSP1IF = 0;
//第一个字节
SSPCON2bits.RCEN = 1; //使能接收数据
while(!SSP1IF);
adc_h = SSP1BUF;
while(!SSP1IF);
SSP1IF = 0;
SSPCON2bits.ACKDT = 0; //发送ACK应答位
SSPCON2bits.ACKEN = 1;
while(!SSP1IF);
SSP1IF = 0;
//第二个字节
SSPCON2bits.RCEN = 1;
while(!SSP1IF);
adc_l = SSP1BUF;
while(!SSP1IF);
SSP1IF = 0;
SSPCON2bits.ACKDT = 0;
SSPCON2bits.ACKEN = 1;
while(SSP1IF==0);
SSP1IF = 0;
//第三个字节
SSPCON2bits.RCEN = 1;
while(!SSP1IF);
adc_val = SSP1BUF;
while(!SSP1IF);
SSP1IF = 0;
SSPCON2bits.ACKDT = 0;
SSPCON2bits.ACKEN = 1;
while(SSP1IF==0);
SSP1IF = 0;
//第四个字节
SSPCON2bits.RCEN = 1;
while(!SSP1IF);
adc_val = SSP1BUF;
while(!SSP1IF);
SSP1IF = 0;
SSPCON2bits.ACKDT = 0;
SSPCON2bits.ACKEN = 1;
while(SSP1IF==0);
SSP1IF = 0;
//第五个字节
SSPCON2bits.RCEN = 1;
while(!SSP1IF);
adc_val = SSP1BUF;
while(!SSP1IF);
SSP1IF = 0;
SSPCON2bits.ACKDT = 1;
SSPCON2bits.ACKEN = 1;
while(SSP1IF==0);
SSP1IF = 0;
I2C_Stop();
adc_value = (adc_h<<8)+adc_l;
return (adc_value);
}
#if 0
void IIC_WriteByte(uchar addr,uchar dat)
{
SSP1IF = 0;
SSPCON2bits.SEN = 1;
delay_us(10);
while(SSP1IF==0);
SSP1IF = 0;
SSPBUF = 0xa0;
while(SSP1IF==0);
SSP1IF = 0;
SSPBUF = addr;
while(SSP1IF==0);
SSP1IF = 0;
SSPBUF = dat;
while(SSP1IF==0);
SSP1IF = 0;
SSPCON2bits.PEN = 1;
while(SSP1IF==0);
SSP1IF = 0;
delay_ms(1);
}
uchar IIC_ReadByte(uchar addr)
{
uchar temp = 0;
SSP1IF = 0;
SSPCON2bits.SEN = 1;
while(SSP1IF==0);
SSP1IF = 0;
SSPBUF = 0xa0;
while(SSP1IF==0);
SSP1IF = 0;
SSPBUF = addr;
while(SSP1IF==0);
SSP1IF = 0;
SSPCON2bits.RSEN = 1;
while(SSP1IF==0);
SSP1IF = 0;
SSPBUF = 0xa1;
while(SSP1IF==0);
SSP1IF = 0;
SSPCON2bits.RCEN = 1;
while(SSP1IF==0);
//SSPIF = 0;
temp = SSPBUF;
// ACKEN = 1;
while(SSP1IF==0);
SSP1IF = 0;
SSPCON2bits.ACKDT = 1;
SSPCON2bits.ACKEN = 1;
while(SSP1IF==0);
SSP1IF = 0;
SSPCON2bits.PEN = 1;
while(SSP1IF==0);
SSP1IF = 0;
return temp;
}
#endif
|