AVR 模拟I2C
时间:10-02
整理:3721RD
点击:
void delay_nus(uint tttt)
{
do
{
tttt--;
}
while(tttt>1);
}
char Receive_Ack(void) /////If have recevie ask return 8,else retutn 0.
{
char re_num = 8;
SCL_OUT;SDA_IN;
SCL_0;
delay_nus(5);
SDA_1;
delay_nus(5);
SCL_1;////the clock is high
if(SDA_READ > 0)
{re_num = 0;}////It have not ack,we will return 0.
delay_nus(5);
SCL_0;///
delay_nus(5);
return re_num;
}/////////////attention : At the end of this function "SDA = x"and "SCL = 0"
void Send_Ack(char num) /////If num > 0 send ask , else not send ask.
{
SCL_OUT;SDA_OUT;
SCL_0;
delay_nus(1);
SDA_1;
delay_nus(1);
if(num > 0)
{SDA_0;}
delay_nus(1);
SCL_1;////the clock is high
delay_nus(1);
SCL_0;///
delay_nus(1);
}/////////////attention : At the end of this function "SDA = x"and "SCL = 0"
void Start(void)
{SDA_OUT;
SCL_OUT;
SDA_1;
delay_nus(6);
SCL_1;
delay_nus(6);
SDA_0;
delay_nus(6);
}
void Stop(void)
{ SDA_OUT;
SCL_OUT;
SDA_0;
delay_nus(6);
SCL_1;
delay_nus(6);
SDA_1;
delay_nus(2);
}
/////////////////////////////////////////////////////////////
////////////////////8 位数据的 移入到单片机 位操作///////////
/////////////////////////////////////////////////////////////
unsigned char Move_In(void)
// 从AT24Cxx移入数据到MCU
{
unsigned int i = 0,read_data=0;
SCL_OUT;
SCL_0;
SDA_IN;//设置SDA为输入
SDA_1;
delay_nus(2);
for(i=0;i<8;i++)
{
delay_nus(1);
SCL_1;
read_data = (read_data << 1)|SDA_READ;
delay_nus(1);
SCL_0;SDA_1;
}
return(read_data);
}
void Move_Out(char write_data)
{
unsigned char i;
//unsigned int jj=0;
char ack_bit;
SDA_OUT;
SCL_OUT;
SCL_0;
for(i=0;i<8;i++)
{
if(write_data&0x80)
{SDA_1;}
else
{SDA_0;}
delay_nus(2);
SCL_1; //// The clock is high
delay_nus(2);
write_data<<=1;
SCL_0; //// The clock is high
delay_nus(2);
}
delay_nus(2);
}
郁闷啊程序 反反复复的看了没问题啊 根据逻辑分析仪把写字节哪里改了 if(write_data&0x80) {SDA_0;}
else
{SDA_1;}
是可以写了 不过读又出现了问题 太悲催了我觉得程序完全没问题啊 为什么会这样?
那个高手来解答一下咯
{
do
{
tttt--;
}
while(tttt>1);
}
char Receive_Ack(void) /////If have recevie ask return 8,else retutn 0.
{
char re_num = 8;
SCL_OUT;SDA_IN;
SCL_0;
delay_nus(5);
SDA_1;
delay_nus(5);
SCL_1;////the clock is high
if(SDA_READ > 0)
{re_num = 0;}////It have not ack,we will return 0.
delay_nus(5);
SCL_0;///
delay_nus(5);
return re_num;
}/////////////attention : At the end of this function "SDA = x"and "SCL = 0"
void Send_Ack(char num) /////If num > 0 send ask , else not send ask.
{
SCL_OUT;SDA_OUT;
SCL_0;
delay_nus(1);
SDA_1;
delay_nus(1);
if(num > 0)
{SDA_0;}
delay_nus(1);
SCL_1;////the clock is high
delay_nus(1);
SCL_0;///
delay_nus(1);
}/////////////attention : At the end of this function "SDA = x"and "SCL = 0"
void Start(void)
{SDA_OUT;
SCL_OUT;
SDA_1;
delay_nus(6);
SCL_1;
delay_nus(6);
SDA_0;
delay_nus(6);
}
void Stop(void)
{ SDA_OUT;
SCL_OUT;
SDA_0;
delay_nus(6);
SCL_1;
delay_nus(6);
SDA_1;
delay_nus(2);
}
/////////////////////////////////////////////////////////////
////////////////////8 位数据的 移入到单片机 位操作///////////
/////////////////////////////////////////////////////////////
unsigned char Move_In(void)
// 从AT24Cxx移入数据到MCU
{
unsigned int i = 0,read_data=0;
SCL_OUT;
SCL_0;
SDA_IN;//设置SDA为输入
SDA_1;
delay_nus(2);
for(i=0;i<8;i++)
{
delay_nus(1);
SCL_1;
read_data = (read_data << 1)|SDA_READ;
delay_nus(1);
SCL_0;SDA_1;
}
return(read_data);
}
void Move_Out(char write_data)
{
unsigned char i;
//unsigned int jj=0;
char ack_bit;
SDA_OUT;
SCL_OUT;
SCL_0;
for(i=0;i<8;i++)
{
if(write_data&0x80)
{SDA_1;}
else
{SDA_0;}
delay_nus(2);
SCL_1; //// The clock is high
delay_nus(2);
write_data<<=1;
SCL_0; //// The clock is high
delay_nus(2);
}
delay_nus(2);
}
郁闷啊程序 反反复复的看了没问题啊 根据逻辑分析仪把写字节哪里改了 if(write_data&0x80) {SDA_0;}
else
{SDA_1;}
是可以写了 不过读又出现了问题 太悲催了我觉得程序完全没问题啊 为什么会这样?
那个高手来解答一下咯
我是用TWI口写的,我也遇到这样的问题,能写能读 ,且都能通过中断验证,就是得到的数据是错的,很不解,请问小编的问题是否得到解决
换了STC的片子 果断就行了 用不起AVR啊
我用52也成了,就是想移植到AVR上