調(diào)用的代碼
???????????? case C_READ+C_I2C:
???????????????? return_buf[2] = comm_buf[4];??? //
???????????????? read(comm_buf[3],comm_buf[4],&return_buf[3]);
???????????????? break;
????????????????
???????????? case C_WRITE+C_I2C:
???????????????? return_buf[2] = 0;
???????????????? write(comm_buf[3],comm_buf[4],&comm_buf[5]);
???????????????? break;
?主I2C的代碼
?#define CHIP_ADDR 0x78
?unsigned char cread(uchar addr);
?void cwrite(uchar addr,uchar dat);
?void read(uchar addr, uchar num, uchar * buf);
?void write(uchar addr, uchar num,uchar * buf);
?void i2c_start(void);
?void i2c_stop(void);
?uchar WaitAck(void);
?void SendNotAck(void);
?void SendtAck(void);
?void delay_5us(void);
?void delay(unsigned char num);
?void i2c_writ2(unsigned char dat);
?uchar i2c_rea2(bit ack);
?/***********************************************
???? 總線啟動(dòng)函數(shù)
?***********************************************/
?void i2c_start()
?{
???? SCL=1;delay_5us();
???? SDA=1;delay_5us();
???? SDA=0;delay_5us();delay_5us();
???? SCL=0;delay_5us();
?}
?/***********************************************
???? 總線停止函數(shù)
?***********************************************/
?void i2c_stop()
?{
???? SCL=0;
???? SDA=0;delay_5us();
???? SCL=1;delay_5us();
???? SDA=1;delay_5us();
?}
?/***********************************************
???? I2C總線發(fā)送數(shù)據(jù)時(shí)的ACK應(yīng)答
?***********************************************/
?uchar WaitAck(void)
?{
???? uchar errtime=255;? //因故障接收方無ACK 超時(shí)值為255
???? SDA=1 ;delay_5us();
???? SCL=1 ;delay_5us();
???? while(SDA)
???? {
???????? if (--errtime == 0)
???????? {
???????????? i2c_stop();
???????????? return 0;
???????? }
???? }
???? SCL=0;
???? return 1;
?}
?/***********************************************
???? I2C總線接收數(shù)據(jù)時(shí)的NO ACK應(yīng)答
?***********************************************/
?void SendNotAck(void)
?{
???? SDA=1 ;
???? delay_5us();
???? SCL=1 ;
???? delay_5us();
???? SCL=0;
?}
?/***********************************************
???? I2C總線接收數(shù)據(jù)時(shí)的ACK應(yīng)答
?***********************************************/
?void SendtAck(void)
?{
???? SDA=0 ;
???? delay_5us();
???? SCL=1 ;
???? delay_5us();
???? SCL=0;
?}
?/***********************************************
???? 寫1個(gè)字節(jié)的函數(shù)
?***********************************************/
?void i2c_writ2(uchar dat)
?{
???? uchar i;
???? for(i=0;i<8;i++)
???? {
?//??????? SDA=1;
???????? if((dat&0x80)==0x00) SDA=0;
???????? else SDA=1;
???????? SCL=1;delay_5us();SCL=0;
???????? dat<<=1;
???? }
???? SDA=1;
???? SCL=1;delay_5us();
???? SCL=0;delay_5us();
?}
?/***********************************************
???? 讀當(dāng)前1字節(jié)數(shù)據(jù)函數(shù)
?***********************************************/
?unsigned char cread(uchar addr)
?{
???? unsigned char i;
???? i2c_start();
???? i2c_writ2(CHIP_ADDR);??????? // Write
???? i2c_writ2(addr);
???? i2c_start();??????????????? // Start
???? i2c_writ2(CHIP_ADDR+1);??????? // Read
???? i = i2c_rea2(0);
???? i2c_stop();
???? delay(10);
???? return(i);
?}
?/***********************************************
???? 寫當(dāng)前1字節(jié)數(shù)據(jù)函數(shù)
?***********************************************/
?void cwrite(uchar addr,uchar dat)
?{
???? i2c_start();
???? i2c_writ2(CHIP_ADDR);??????? // Write
???? i2c_writ2(addr);
???? i2c_writ2(dat);
???? i2c_stop();
???? delay(30);
?}
?/***********************************************
???? 讀數(shù)據(jù)
?***********************************************/
?void read(uchar addr, uchar num, uchar * buf)
?{
???? i2c_start();
???? i2c_writ2(CHIP_ADDR);??????? // Write
???? i2c_writ2(addr);
???? i2c_start();??????????????? // Start
???? i2c_writ2(CHIP_ADDR+1);??????? // Read
???? for( ; num>0 ; num--)??????? // 個(gè)數(shù)
???? {
???????? *buf++ = i2c_rea2(1);??? // 讀
???? }
???? i2c_stop();
???? delay(10);
?}
?/***********************************************
???? 寫數(shù)據(jù)
?***********************************************/
?void write(uchar addr, uchar num,uchar * buf)
?{
???? i2c_start();
???? i2c_writ2(CHIP_ADDR);??????? // Write
???? i2c_writ2(addr);
???? for( ; num>0 ; num--)??????? // 個(gè)數(shù)
???? {
???????? i2c_writ2(*buf++);??????? // 寫
???? }
???? i2c_stop();
???? delay(30);
?}
?/***********************************************
???? 讀1字節(jié)函數(shù)
?***********************************************/
?uchar i2c_rea2(bit ack)
?{
???? uchar i,j;
???? j=0x00;
???? SDA=1;
???? for(i=0;i<8;i++)
???? {
???????? SCL=1;
???????? j=j<<1;
???????? if(SDA) j|=0x01;
???????? SCL=0;delay_5us();
???? }
???? if(ack) SDA=0;
???? SCL=1;delay_5us();
???? SCL=0;delay_5us();
???? return j;
?}
?/***********************************************
???? 延時(shí)程序
?***********************************************/
?void delay_5us()
?{
???? uchar i;
???? //for(i=0;i<2;i++){}
???? for(i=0;i<4;i++)
???? {
???? }
?}
評(píng)論
查看更多