#include<reg52.h>
#include<stdio.h>
#include<absacc.h>
#include<intrins.h>
//*********************************
#define TURE 1
#define FALSE 0
#define uchar unsigned char
#define uint unsigned int
//*********************************
sbit I2c_scl = P1^0;
sbit I2c_sda = P1^1;
//********************************
uchar e24c02_date[]={0x00,0x01,0x02,0x03};
//**********************************
void delay();
bit Check_ack();
void I2c_ini();
void I2c_stop();
void I2c_start();
void I2c_writeB(uchar date);
void I2c_write_one_byte(uchar addr,uchar thedate);
void I2c_write_one_page(uchar addr,uchar *thedate,uchar m);
void main()
{
I2c_ini();
I2c_write_one_byte(0X00,0XCC);
delay();
I2c_write_one_page(0x10,e24c02_date,4);
while(1)
{
;
}
}
//**********************************
void delay()
{
uchar i="0";
while(i<255)
{
_nop_();
i++;
}
}
//**********************************
void I2c_ini()
{
I2c_scl=1;
I2c_sda=1;
}
//******************
void I2c_stop()
{
I2c_scl=0;
I2c_sda=0;
_nop_();
I2c_scl=1;
_nop_();
I2c_sda=1;
_nop_();
}
//***********************************
void I2c_start()
{
I2c_sda=1;
I2c_scl=1;
_nop_();
I2c_sda=0;
_nop_();
_nop_();
I2c_scl=0;
}
//**************************************
void I2c_writeB(uchar date)
{
uchar temp, i;
temp="date";
for(i=0;i<8;i++)
{
temp=temp<<1;
I2c_scl=0;
_nop_();
I2c_sda=CY;
_nop_();
I2c_scl=1;
_nop_();
}
I2c_scl=0;
_nop_();
I2c_sda=1;
_nop_();
I2c_scl=1;
_nop_();
}
//**********************I2C ACK CHECK***************************
bit Check_ack()
{
uchar i="0";
while(i<255)
{
if(I2c_sda==0)
{
i=255;
}
else i++;
}
return (~I2c_sda);
}
//******************write one byte*******************************
void I2c_write_one_byte(uchar addr,uchar thedate)
{
I2c_start();
I2c_writeB(0xa0);
Check_ack();
I2c_writeB(addr);
Check_ack();
I2c_writeB(thedate);
Check_ack();
I2c_stop();
}
//********************write a page***************************
void I2c_write_one_page(uchar addr,uchar *thedate,uchar m)
{
uchar i;
// bit ack_temp;
I2c_start();
I2c_writeB(0xa0);
Check_ack();
I2c_writeB(addr);
Check_ack();
for(i=0;i<m;i++)
{
I2c_writeB(thedate);
if(!Check_ack())
{
I2c_stop();
}
}
I2c_stop();
}
文章评论(0条评论)
登录后参与讨论