IIC学习

来源:互联网 发布:windows共享文件夹 编辑:程序博客网 时间:2024/06/06 10:55
#include "iic.h"


void delay(u16 i)
{
while (i--);
}


void IIC_Init()
{
SCL = 1;   //正确的顺序?
delay(6);
SDA = 1;
delay(6);
}


void IIC_Start() 
{
SDA = 1;      // 以下都是按照时序图来写的
delay(6);
SCL = 1;      
delay(6);
SDA = 0;
delay(6);
SCL = 0;     //没事的时候把SCL拉低,有其中的道理
delay(6);
}


void IIC_Stop()   // 以下都是按照时序图来写的
{
SDA = 0;
delay(6);
SCL = 1;
delay(6);
SDA = 1;
delay(6);
}


u8 IIC_Rbyte(void)    
{
u8 i,byte;

SDA = 1;           //只有SDA = 1(软件控制) ;从器件才能完全控制SDA高或低
for (i = 0; i <= 7; i++)
{
SCL = 1;      //因为前面都保证了SCL = 0;所以前面就已经运行从器件传输数据
delay(6);
byte <<= 1;    //注意, 一定要位移
byte |= SDA;
SCL = 0;      //让从器件继续传送数据
delay(6);
}
return byte;
}


void IIC_Wbyte(u8 byte)
{
u8 i;

for (i = 0; i <= 7; i++)
{
SDA = 0x80 & byte;  //值得一提的是,当SDA(只有一位)= x,当x不为0时,SDA = 1;当X = 0时,SDA = 0;
delay(6);
SCL = 1;   //传送数据
delay(6);
SCL = 0;   //运行SDA改变
delay(6);
byte <<= 1;
}

}


//bit IIC_ACK()   //主机等待应答就是把SDA = 1,SCL = 1; 如果应答从器件把SDA = 0;如果不应答SDA 依然为1
//{
// u8 i;
//
// SDA = 1;
// delay(6);
// SCL = 1;
// delay(6);
// while (SDA)
// {
// i++;
// if (i > 200)
// {
// SCL = 0;
// delay(6);
// return 0;
// }
// }
// SCL = 0;
// delay(6);
// return 1;
//}








unsigned char At24c02Read(unsigned char addr)
{
u8 byte;

IIC_Start();
IIC_Wbyte(0xa0);
// IIC_ACK();
IIC_Wbyte(addr);
// IIC_ACK();
IIC_Stop();
IIC_Start();
IIC_Wbyte(0xa1);
byte = IIC_Rbyte(); //此处暂时最好不要用同一个应当,似乎答不了
IIC_Stop();
return byte;
}





void At24c02Write(unsigned char addr,unsigned char dat)
{
IIC_Start();
IIC_Wbyte(0xa0);
// IIC_ACK();
IIC_Wbyte(addr);
// IIC_ACK();
IIC_Wbyte(dat);
// IIC_ACK();
IIC_Stop();
}














//#include"iic.h"


///*******************************************************************************
//* ???         : Delay10us()
//* ????   : ??10us
//* ??           : ?
//* ??         : ?
//*******************************************************************************/


//void Delay10us()
//{
// unsigned char a,b;
// for(b=1;b>0;b--)
// for(a=2;a>0;a--);


//}
///*******************************************************************************
//* ???         : I2cStart()
//* ???? : ????:?SCL??????????SDA?????????
//* ??           : ?
//* ??         : ?
//* ??           : ????SDA?SCL??0
//*******************************************************************************/


//void I2cStart()
//{
// SDA=1;
// Delay10us();
// SCL=1;
// Delay10us();//?????SDA????>4.7us
// SDA=0;
// Delay10us();//?????>4us
// SCL=0;
// Delay10us();
//}
///*******************************************************************************
//* ???         : I2cStop()
//* ???? : ????:?SCL?????????SDA?????????
//* ??           : ?
//* ??         : ?
//* ??           : ??????SDA?SCL??1;??????
//*******************************************************************************/


//void I2cStop()
//{
// SDA=0;
// Delay10us();
// SCL=1;
// Delay10us();//??????4.7us
// SDA=1;
// Delay10us();
//}
///*******************************************************************************
//* ???         : I2cSendByte(unsigned char dat)
//* ???? : ??I2C????????SCL?????????,??????SDA????
//* ??           : num
//* ??         : 0?1???????1,??????0
//* ??           : ???????SCL=0,SDA=1
//*******************************************************************************/


//unsigned char I2cSendByte(unsigned char dat)
//{
// unsigned char a=0,b=0;//??255,???????1us,????255us?
// for(a=0;a<8;a++)//???8?,??????
// {
// SDA=dat>>7;//??????SCL=0,????????SDA??
// dat=dat<<1;
// Delay10us();
// SCL=1;
// Delay10us();//????>4.7us
// SCL=0;
// Delay10us();//????4us
// }
// SDA=1;
// Delay10us();
// SCL=1;
// while(SDA)//????,?????????SDA??
// {
// b++;
// if(b>200) //????2000us????????,??????,??????
// {
// SCL=0;
// Delay10us();
// return 0;
// }
// }
// SCL=0;
// Delay10us();
// return 1;
//}
///*******************************************************************************
//* ???         : I2cReadByte()
//* ????   : ??I2c??????
//* ??           : ?
//* ??         : dat
//* ??           : ???????SCL=0,SDA=1.
//*******************************************************************************/


//unsigned char I2cReadByte()
//{
// unsigned char a=0,dat=0;
// SDA=1; //???????????SCL??0
// Delay10us();
// for(a=0;a<8;a++)//??8???
// {
// SCL=1;
// Delay10us();
// dat<<=1;
// dat|=SDA;
// Delay10us();
// SCL=0;
// Delay10us();
// }
// return dat;
//}




///*******************************************************************************
//* ???         : void At24c02Write(unsigned char addr,unsigned char dat)
//* ????   : ?24c02???????????
//* ??           : ?
//* ??         : ?
//*******************************************************************************/


//void At24c02Write(unsigned char addr,unsigned char dat)
//{
// I2cStart();
// I2cSendByte(0xa0);//???????
// I2cSendByte(addr);//?????????
// I2cSendByte(dat);//????
// I2cStop();
//}
///*******************************************************************************
//* ???         : unsigned char At24c02Read(unsigned char addr)
//* ????   : ??24c02??????????
//* ??           : ?
//* ??         : ?
//*******************************************************************************/


//unsigned char At24c02Read(unsigned char addr)
//{
// unsigned char num;
// I2cStart();
// I2cSendByte(0xa0); //???????
// I2cSendByte(addr); //????????
// I2cStart();
// I2cSendByte(0xa1); //???????
// num=I2cReadByte(); //????
// I2cStop();
// return num;
//}





0 0