You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
1.9 KiB

#include "myiic1.h"
#include "delay.h"
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>MYMYIIC11<EFBFBD><EFBFBD>ʼ<EFBFBD>ź<EFBFBD>
void MYIIC1_Start(void)
{
SDA1_OUT(); //sda<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
MYIIC1_SDA=1;
MYIIC1_SCL=1;
delay_us(4);
MYIIC1_SDA=0; //SDA <EFBFBD>ɸ߱<EFBFBD><EFBFBD><EFBFBD>
delay_us(4);
MYIIC1_SCL=0; //׼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>MYIIC1ֹͣ<EFBFBD>ź<EFBFBD>
void MYIIC1_Stop(void)
{
SDA1_OUT(); //sda<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
MYIIC1_SCL=0;
MYIIC1_SDA=0;
delay_us(4);
MYIIC1_SCL=1; //SDA <EFBFBD>ɵͱ<EFBFBD><EFBFBD><EFBFBD>
delay_us(4);
MYIIC1_SDA=1; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>I2C<EFBFBD><EFBFBD><EFBFBD>߽<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ź<EFBFBD>
delay_us(4);
}
//<EFBFBD>ȴ<EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD>źŵ<EFBFBD><EFBFBD><EFBFBD>
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>1<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>
// 0<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>
u8 MYIIC1_Wait_Ack(void)
{
u8 ucErrTime=0;
SDA1_IN(); //SDA<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
MYIIC1_SDA=1;delay_us(1);
MYIIC1_SCL=1;delay_us(1);
while(READ1_SDA)
{
ucErrTime++;
if(ucErrTime>250)
{
MYIIC1_Stop();
return 1;
}
}
MYIIC1_SCL=0;//ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0
return 0;
}
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ACKӦ<EFBFBD><EFBFBD>
void MYIIC1_Ack(void)
{
MYIIC1_SCL=0;
SDA1_OUT();
MYIIC1_SDA=0;
delay_us(2);
MYIIC1_SCL=1;
delay_us(2);
MYIIC1_SCL=0;
}
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ACKӦ<EFBFBD><EFBFBD>
void MYIIC1_NAck(void)
{
MYIIC1_SCL=0;
SDA1_OUT();
MYIIC1_SDA=1;
delay_us(2);
MYIIC1_SCL=1;
delay_us(2);
MYIIC1_SCL=0;
}
//MYIIC1<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>
void MYIIC1_Send_Byte(u8 txd)
{
u8 t;
SDA1_OUT();
MYIIC1_SCL=0;//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ӿ<EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><EFBFBD><EFBFBD>
for(t=0;t<8;t++)
{
if((txd&0x80)>>7)
MYIIC1_SDA=1;
else
MYIIC1_SDA=0;
txd<<=1;
delay_us(2);
MYIIC1_SCL=1;
delay_us(2);
MYIIC1_SCL=0;
delay_us(2);
}
}
//<EFBFBD><EFBFBD>1<EFBFBD><EFBFBD><EFBFBD>ֽڣ<EFBFBD>ack=1ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ACKӦ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ack=0<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>nACK<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
u8 MYIIC1_Read_Byte(unsigned char ack)
{
unsigned char i,receive=0;
SDA1_IN();//SDA<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for(i=0;i<8;i++ )
{
MYIIC1_SCL=0;
delay_us(2);
MYIIC1_SCL=1;
receive<<=1;
if(READ1_SDA)receive++;
delay_us(1);
}
if (!ack)
MYIIC1_NAck();//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>nACK
else
MYIIC1_Ack(); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ACK
return receive;
}