Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1308940
  • 博文数量: 92
  • 博客积分: 10389
  • 博客等级: 上将
  • 技术积分: 1918
  • 用 户 组: 普通用户
  • 注册时间: 2006-08-10 16:13
文章存档

2014年(1)

2012年(15)

2009年(6)

2008年(37)

2007年(72)

2006年(54)

我的朋友

分类: LINUX

2007-08-23 09:09:46

#include
#include
#define LOW 0
#define HIGH 1
#define FALSE 0
#define TRUE 1
typedef unsigned char INT8U;
typedef unsigned int INT16U;
sbit SCL=P1^6;
sbit SDA=P1^7;
INT8U E2PRomPageBuf[256];
/*
*******************************************************************
******************************
*/
void Delay10ms(void) small
{
//put ur delay func here,for debug use
}
/*
*******************************************************************
******************************
*/
void I2CInit(void) small
{
    SDA=HIGH;
    SCL=HIGH;
}
/*
*******************************************************************
******************************
*/
static void I2CStart(void) small
{
    SDA=HIGH;
    SCL=HIGH;
    SDA=LOW;
    _nop_ ();
    SCL=LOW;
}
/*
*******************************************************************
******************************
*/
static void I2CStop(void) small
{
    SCL=LOW;
    SDA=LOW;
    SCL=HIGH;
    SDA=HIGH;
}
/*
*******************************************************************
******************************
*/
static bit I2CSend(INT8U ch) small
{
    INT8U i;
    for(i=0;i<8;i++)
    {
        SDA=(bit)(ch&0x80);
        SCL=HIGH;
        ch<<=1;
        SCL=LOW;
    }
    SDA=HIGH;
    SCL=HIGH;
    _nop_(); /* wait slave-ACK */
    //_nop_();_nop_();_nop_();
    if(SDA) {SCL=LOW;return(FALSE);}
    else {SCL=LOW;return(TRUE);}
}
/*
*******************************************************************
******************************
*/
static INT8U I2CReceive(void) small
{
    INT8U i,ch=0;
    for(i=0;i<8;i++)
    {
        ch<<=1;
        SCL=HIGH;
        ch|=SDA;
        SCL=LOW;
    }
    SDA=HIGH; /* to reduce IO power consunption */
    return(ch);
}
/*
*******************************************************************
******************************
*/
static void I2CAck(void) small
{
    SDA=LOW;
    SCL=HIGH;
    _nop_();
    _nop_();
    SCL=LOW;
    SDA=HIGH; /* to reduce IO power consunption */
}
/*
*******************************************************************
******************************
*/
static void I2CNoAck(void) small
{
    SDA=HIGH;
    SCL=HIGH;
    _nop_();
    _nop_();
    SCL=LOW;
}
/*
*******************************************************************
******************************
*/
bit E2PRomByteWrite(INT8U romnum,INT16U address,INT8U ch) small
{
    romnum<<1;
    romnum&=0x06;
    romnum|=0xA0;
    I2CStart();
    if(!I2CSend(romnum))
        {I2CStop();return(FALSE);};
    if(!I2CSend((INT8U)(address>>8)))
        {I2CStop();return(FALSE);};
    if(!I2CSend((INT8U)(address&0x00FF)))
        {I2CStop();return(FALSE);};
    if(!I2CSend(ch))
        {I2CStop();return(FALSE);};
    I2CStop();
    return(TRUE);
}
/*
*******************************************************************
******************************
*/
bit E2PRomPageWrite(INT16U page) small
{
    INT8U i;
    page<<=7;
    I2CStart();
    if(!I2CSend(0xA0))
        {I2CStop();return(FALSE);};
    if(!I2CSend((INT8U)(page>>8)))
        {I2CStop();return(FALSE);};
    if(!I2CSend((INT8U)(page&0x00FF)))
        {I2CStop();return(FALSE);};
    for(i=0;i<128;i++)
        if(!I2CSend(E2PRomPageBuf[i]))
             {I2CStop();return(FALSE);};
    I2CStart();
    if(!I2CSend(0xA2))
        {I2CStop();return(FALSE);};
    if(!I2CSend((INT8U)(page>>8)))
        {I2CStop();return(FALSE);};
    if(!I2CSend((INT8U)(page&0x00FF)))
        {I2CStop();return(FALSE);};
    for(i=128;i<256;i++)
        if(!I2CSend(E2PRomPageBuf[i]))
             {I2CStop();return(FALSE);};
    I2CStop();
    return(TRUE);
}
/*
*******************************************************************
******************************
*/
bit E2PRomPageRead(INT16U page) small
{
    INT8U i;
    page<<=7;
    I2CStart();
    if(!I2CSend(0xA0))
        {I2CStop();return(FALSE);};
    if(!I2CSend((INT8U)(page>>8)))
        {I2CStop();return(FALSE);};
    if(!I2CSend((INT8U)(page&0x00FF)))
        {I2CStop();return(FALSE);};
    I2CStart();
    if(!I2CSend(0xA1))
        {I2CStop();return(FALSE);};
    for(i=0;i<127;i++)
    {
        E2PRomPageBuf[i]=I2CReceive();
        I2CAck();
    }
    E2PRomPageBuf[i]=I2CReceive();
    I2CNoAck();
    I2CStop();
    I2CStart();
    if(!I2CSend(0xA2))
        {I2CStop();return(FALSE);};
    if(!I2CSend((INT8U)(page>>8)))
        {I2CStop();return(FALSE);};
    if(!I2CSend((INT8U)(page&0x00FF)))
        {I2CStop();return(FALSE);};
    I2CStart();
    if(!I2CSend(0xA3))
        {I2CStop();return(FALSE);};
    for(i=127;i<256;i++)
    {
        E2PRomPageBuf[i]=I2CReceive();
        I2CAck();
    }
    E2PRomPageBuf[i]=I2CReceive();
    I2CNoAck();
    I2CStop();
    return(TRUE);
}
阅读(1412) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~