r />_NOP();
}
// 啟動(dòng)I2C模塊
void start_IIC(void) // 啟動(dòng)I2
{
P2OUT&=0xf9; //設(shè)置P2輸出
P2DIR&=0XFD; //SDA=1, 上拉電阻使得P2.1為H,F(xiàn)D=1111,1101
P2DIR&=0XFB; //SCL=1 FB="1111",1011
P2DIR|=0X02; // SDA="0"
P2DIR|=0X04; // SCL="0"
}
// 停止I2C模塊
void stop_IIC(void) //
{
P2DIR|=0X02;//SDA=0
IIC_Delay();
P2DIR&=0XFB;//SCL=1 FB="1111",1011
P2DIR&=0XFD;//SDA=1, 上拉電阻使得P2.1為H,F(xiàn)D=1111,1101
IIC_Delay();
P2DIR|=0X04;// SCL="0"
}
// 發(fā)送 “ 0”模塊
void send_zero(void) //
{
P2DIR|=0X02;// SDA="0"
IIC_Delay();
P2DIR&=0XFB;//SCL=1 FB="1111",1011
IIC_Delay();
P2DIR|=0X04;// SCL="0"
}
// 發(fā)送 1模塊
void send_one(void) //
{
P2DIR&=0XFD;//SDA=1, 上拉電阻使得P2.1為H,F(xiàn)D=1111,1101
IIC_Delay();
P2DIR&=0XFB;//SCL=1 FB="1111",1011
IIC_Delay();
P2DIR|=0X04;// SCL="0"
}
// 發(fā)送單字符數(shù)據(jù)
void send _char(unsigned char data_out) //
{
unsigned char i,tmp=0x80;
for(i=0;i<8;i++)
{
if((data_out & tmp)>0)
send_one();
else
send_zero();
tmp/=2;
}
}
// 讀單字符數(shù)據(jù)
unsigned char read_char(void)
{
unsigned char i,tmp=0x80;
unsigned char data1=0;
for (i=0;i<8;i++)
{
P2DIR&=0XFD;//SDA=1, 11111101
IIC_Delay();//
P2DIR&=0XFB;//SCL=1 FB="1111",1011
IIC_Delay();
if((P2IN&0x02)>0x00)
{
data1|=tmp;
}
P2DIR|=0X04;// SCL="0"
IIC_Delay();
tmp/=2;
}
return data1;
}
// & 檢查應(yīng)答信號(hào)模塊
void iic_ACK(void)
{
ack_flag=0x00;
P2DIR&=0XFD;//SDA=1, FD="1111",1101
IIC_Delay();
P2DIR&=0XFB;//SCL=1 FB="1111",1011
IIC_Delay();
while((P2IN&BIT1)==BIT1);
P2DIR|=0X04;// SCL="0"
IIC_Delay();
}
// 拒絕應(yīng)答模塊
void iic_NACK(void) & nbsp; {
P2DIR&=0XFD;//SDA=1,
IIC_Delay();
P2DIR&=0XFB;//SCL=1 FB="1111",1011
IIC_Delay();
P2DIR|=0X04;// SCL="0"
IIC_Delay();
P2DIR|=0X02;// SDA="0"
IIC_Delay();//
}
// 寫(xiě)連續(xù)數(shù)據(jù)模塊
void WriteNbyte(unsigned char *p,unsigned int addr,unsigned char number)
{
start_IIC();
send_char(0xa2);
iic_ACK();
send_char(addr/256); //high address byte
iic_ACK();
send_char(addr%256);
iic_ACK();
do
{
send_char(*p);
p++;
iic_ACK();
}
while(--number);
stop_IIC();
delay(10);
}
// 發(fā)送應(yīng)答模塊:ACK (LOW)
void S_AC





