I2C in 89C51 SOS!!! - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

I2C in 89C51 SOS!!!

I am trying to establish I2C communication interface for 89C51 (a FLASH version of 8051). Can anybody help me out with assembly programming?
What pins do I use for implementing I2c?
Sridhar.

Comments

  • My_AusweisMy_Ausweis Posts: 6Member
    As I Know there is no I2C in 89C51,you have to create by your ownself based on the datasheet for I2C Communication, Maybe you can open www.microchip.com, and downloading for the i2c Communication for PIC16F877. Hope this information will help u. Thx
  • AsAdiAsAdi Posts: 117Member
    : As I Know there is no I2C in 89C51,you have to create by your ownself based on the datasheet for I2C Communication, Maybe you can open www.microchip.com, and downloading for the i2c Communication for PIC16F877. Hope this information will help u. Thx
    :
    I have implemented a board using AT89C51 which includes PCF8583 as my RTC,and its protocol is I2C,so I had to implement this protocol too,maybe some of these code,with alittle bit changes is useful for you:

    sbit RTCSDA = P2^4;
    sbit RTCSCL = P2^5;
    //RTC PCF8583 Driver
    void RTCClock()
    {
    RTCSCL = 0;
    Delay(1*CPUCLOCK/4);
    RTCSCL = 1;
    Delay(2*CPUCLOCK/4);
    RTCSCL = 0;
    Delay(1*CPUCLOCK/4);
    }
    /******************************************************/
    void RTCSendOneByte(unsigned char byte)
    {
    RTCSCL = 0;
    if((byte & 0x80) == 0x80) //Sending Byte
    RTCSDA = 1;
    else
    RTCSDA = 0;
    RTCClock();

    if((byte & 0x40) == 0x40)
    RTCSDA = 1;
    else
    RTCSDA = 0;
    RTCClock();

    if((byte & 0x20) == 0x20)
    RTCSDA = 1;
    else
    RTCSDA = 0;
    RTCClock();

    if((byte & 0x10) == 0x10)
    RTCSDA = 1;
    else
    RTCSDA = 0;
    RTCClock();

    if((byte & 0x08) == 0x08)
    RTCSDA = 1;
    else
    RTCSDA = 0;
    RTCClock();

    if((byte & 0x04) == 0x04)
    RTCSDA = 1;
    else
    RTCSDA = 0;
    RTCClock();

    if((byte & 0x02) == 0x02)
    RTCSDA = 1;
    else
    RTCSDA = 0;
    RTCClock();

    if((byte & 0x01) == 0x01)
    RTCSDA = 1;
    else
    RTCSDA = 0;
    RTCClock();

    RTCSDA = 1; //Master Makes SDA HIGH
    Delay(1*CPUCLOCK/4);
    RTCSCL = 1;
    while(RTCSDA == 1); //Wait for RTC Acknowledges means it makes RTCSDA low
    Delay(1*CPUCLOCK/4);
    RTCSCL = 0;
    }
    /*****************************************************/
    void RTCWriteData(unsigned char ads,unsigned char dt)
    {
    RTCSDA = 1;
    Delay(5*CPUCLOCK/4);
    RTCSCL = 1;
    Delay(5*CPUCLOCK/4);
    RTCSDA = 0; /*Start Bit*/
    Delay(5*CPUCLOCK/4);
    RTCSCL = 0;

    RTCSendOneByte(0xA0); //Sending Slave Address 10100000
    Delay(1*CPUCLOCK/4);

    RTCSendOneByte(ads); //Sending Word Address
    Delay(1*CPUCLOCK/4);

    RTCSendOneByte(dt); //Sending data byte
    Delay(1*CPUCLOCK/4);

    RTCSDA = 0;
    Delay(5*CPUCLOCK/4);
    RTCSCL = 1;
    Delay(5*CPUCLOCK/4);
    RTCSDA = 1; //Stop bit
    Delay(5*CPUCLOCK/4);
    }
    /******************************************************/
    unsigned char RTCReadData(unsigned char ads)
    {
    unsigned char dd=0,k;

    RTCSDA = 1;
    Delay(5*CPUCLOCK/4);
    RTCSCL = 1;
    Delay(5*CPUCLOCK/4);
    RTCSDA = 0; /*Start Bit*/
    Delay(5*CPUCLOCK/4);
    RTCSCL = 0;

    RTCSendOneByte(0xA0); //Sending Slave Address 10100000
    Delay(1*CPUCLOCK/4);

    RTCSendOneByte(ads); //Sending Word Address
    Delay(1*CPUCLOCK/4);

    RTCSDA = 1;
    RTCSCL = 1;
    RTCSDA = 0; /*Start Bit*/
    Delay(1*CPUCLOCK/4);
    RTCSCL = 0;

    RTCSendOneByte(0xA1); //Sending Slave Address 10100001
    Delay(1*CPUCLOCK/4);

    for (k=0x80;k>0;k/=2)
    {
    RTCSCL = 1;
    Delay(1*CPUCLOCK/4);
    dd |= (RTCSDA ==1?k:0);
    Delay(1*CPUCLOCK/4);
    RTCSCL = 0;
    Delay(2*CPUCLOCK/4);
    }

    RTCSDA = 1; //Master Acknowledges
    RTCClock();

    RTCSDA = 0;
    Delay(5*CPUCLOCK/4);
    RTCSCL = 1;
    Delay(5*CPUCLOCK/4);
    RTCSDA = 1; //Stop bit
    Delay(5*CPUCLOCK/4);

    return dd;
    }
    /******************************************************/
    unsigned char RTCGetSecond()
    {
    unsigned char a = RTCReadData(2);
    return BCD2DEC(a);
    }
    /******************************************************/
    unsigned char RTCGetMinute()
    {
    unsigned char a = RTCReadData(3);
    return BCD2DEC(a);
    }
    /******************************************************/
    unsigned char RTCGetHour()
    {
    unsigned char a = RTCReadData(4);
    return BCD2DEC(a);
    }
    /******************************************************/
    unsigned char RTCGetDay()
    {
    unsigned char a = RTCReadData(5);
    a &= 0x3f;
    return BCD2DEC(a);
    }
    /******************************************************/
    unsigned char RTCGetMonth()
    {
    unsigned char a = RTCReadData(6);
    a &= 0x1f;
    return BCD2DEC(a);
    }
    /******************************************************/
    unsigned char RTCGetYear()
    {
    unsigned char a = RTCReadData(5);
    a /= 0x40;
    return a;
    }
    /******************************************************/
    void RTCSetTime(unsigned char hh,unsigned char mm,unsigned char ss)
    {
    RTCWriteData(4,DEC2BCD(hh));
    RTCWriteData(3,DEC2BCD(mm));
    RTCWriteData(2,DEC2BCD(ss));
    }
    /******************************************************/
    void RTCSetDate(unsigned char yy,unsigned char mn,unsigned char dd)
    {
    unsigned char a = yy/4;
    a = yy-a*4;
    a *= 64;
    a |= DEC2BCD(dd);
    RTCWriteData(5,a);
    a = DEC2BCD(mn);
    RTCWriteData(6,a);
    }
    /******************************************************/
    /******************************************************/
    /******************************************************/

Sign In or Register to comment.