1. You can now buy finished microcontroller project from us, Check out the Store for the complete list of projects.
  2. Need a custom project, Send us some details about your project. So that we can quote the price for it.

Interfacing I2C EEPROM with 8051 microcontroller 1.0

Interfacing I2C EEPROM with 8051 microcontroller and lcd display.

  1. sajiv jess
    Interfacing I2C EEPROM with 8051 microcontroller and lcd display.

    Circuit Diagram:

    [​IMG]
    C Code

    Code (Text):
    1. #include <reg51.h>
    2. #include <absacc.h>
    3.  
    4. #define REG0  XBYTE[0x0000]    //LCD instruction register to write the address, according to configuration changes
    5. #define REG1  XBYTE[0x0001]    //D0 ~ D7 LCD read data to the address, according to configuration changes
    6. #define REG2  XBYTE[0x0002]    //LCD write data register address, configuration changes can
    7. #define REG3  XBYTE[0x0003]    //LCD read data register address, configuration changes can
    8.  
    9. unsigned char bdata busyflag;
    10. unsigned char dat,datn;
    11.  
    12. unsigned char word1[16]={" This programm "};    //Line 1 shows the cache
    13. unsigned char word2[16]={"  by Samsung "};        //Line 2 shows the cache
    14. code unsigned char word3[16]={"8051projects"};    //Fixed display for the data to the ROM in curing
    15. code unsigned char word4[16]={" 8051projects !"};    //Fixed display for the data to the ROM in curing
    16. code unsigned char word5[16]={"  Wellcome To "};    //Fixed display for the data to the ROM in curing
    17. code unsigned char word6[16]={" Proteus Tools!"};    //Fixed display for the data to the ROM in curing
    18.  
    19.  
    20. sbit busyflag_7=busyflag^7;
    21. sbit p10=P1^0;
    22. sbit p11=P1^1;
    23. sbit p12=P1^2;
    24. sbit p13=P1^3;
    25. sbit p14=P1^4;
    26. sbit p15=P1^5;
    27. sbit p16=P1^6;
    28. sbit p17=P1^7;
    29. //////////////////////////////////////start of IIC ////////////////////////////
    30. #define uchar unsigned char
    31. #define uint  unsigned int
    32. #include "reg51.h"
    33. #include "intrins.h"
    34.  
    35. sbit    Scl=P3^4;    //Serial Clock
    36. sbit    Sda=P3^5;    //Serial Data
    37.  
    38.  
    39.  
    40. /*Send start condition*/
    41. void Start(void)        /*Initial conditions*/
    42. {
    43.         Sda=1;
    44.         Scl=1;
    45.         _nop_ ();
    46.         _nop_ ();
    47.         _nop_ ();
    48.         _nop_ ();
    49.         Sda=0;
    50.         _nop_ ();
    51.         _nop_ ();
    52.         _nop_ ();
    53.         _nop_ ();
    54. }
    55. void Stop(void) /*Stop condition*/
    56. {
    57.         Sda=0;
    58.         Scl=1;
    59.         _nop_ ();
    60.         _nop_ ();
    61.         _nop_ ();
    62.         _nop_ ();
    63.         Sda=1;
    64.         _nop_ ();
    65.         _nop_ ();
    66.         _nop_ ();
    67.         _nop_ ();
    68. }
    69. void Ack(void)  /*Acknowledge bit*/
    70. {
    71.         Sda=0;
    72.         _nop_ ();
    73.         _nop_ ();
    74.         _nop_ ();
    75.         _nop_ ();
    76.         Scl=1;
    77.         _nop_ ();
    78.         _nop_ ();
    79.         _nop_ ();
    80.         _nop_ ();
    81.         Scl=0;
    82. }
    83. void  NoAck(void)      /*Reverse acknowledge bit*/
    84. {
    85.         Sda=1;
    86.         _nop_ ();
    87.         _nop_ ();
    88.         _nop_ ();
    89.         _nop_ ();
    90.         Scl=1;
    91.         _nop_ ();
    92.         _nop_ ();
    93.         _nop_ ();
    94.         _nop_ ();
    95.         Scl=0;
    96. }
    97. void Send(uchar Data)  /*Subroutine to send data,Data is required to send data*/
    98. {
    99.         uchar BitCounter=8;      /*Median control*/
    100.         uchar temp;    /*Intermediate variable control*/
    101.         do
    102.         {
    103.           temp=Data;
    104.           Scl=0;
    105.           _nop_ ();
    106.           _nop_ ();
    107.           _nop_ ();
    108.           _nop_ ();
    109.           if((temp&0x80)==0x80)/* If the highest bit is 1*/
    110.                 Sda=1;
    111.           else
    112.                 Sda=0;
    113.           Scl=1;
    114.           temp=Data<<1;        /*RLC*/
    115.           Data=temp;
    116.           BitCounter--;
    117.         }while(BitCounter);
    118.         Scl=0;
    119. }
    120. uchar Read(void) /*Read one byte of data, and returns the byte value*/
    121. {
    122.         uchar temp=0;
    123.         uchar temp1=0;
    124.         uchar BitCounter=8;
    125.         Sda=1;
    126.         do{
    127.           Scl=0;
    128.           _nop_ ();
    129.           _nop_ ();
    130.           _nop_ ();
    131.           _nop_ ();
    132.           Scl=1;
    133.           _nop_ ();
    134.           _nop_ ();
    135.           _nop_ ();
    136.           _nop_ ();
    137.           if(Sda)      /*If Sda=1;*/
    138.                 temp=temp|0x01;  /*The lowest temp 1*/
    139.           else
    140.                 temp=temp&0xfe;  /*Otherwise, the lowest temp clear 0*/
    141.           if(BitCounter-1)
    142.           {  temp1=temp<<1;
    143.               temp=temp1;
    144.           }
    145.           BitCounter--;
    146.         }while(BitCounter);
    147.         return(temp);
    148. }
    149. void WrToROM(uchar Data[],uchar Address,uchar Num)//Write a set of data to the AT24C02 in
    150. {    //The first parameter is an array address, data in the AT24C02 in the beginning of the address, number of data
    151.     uchar i=0;
    152.     uchar *PData;
    153.     PData=Data;
    154.     Start();      
    155.     Send(0xa0);    //A0, A1, A2 earth, solid AT24C02 write address 0XA0
    156.     Ack();
    157.     Send(Address);
    158.     Ack();
    159.     for(i=0;i<Num;i++)
    160.     {
    161.         Send(*(PData+i));
    162.         Ack();
    163.     }
    164.     Stop();
    165. }
    166. void  RdFromROM(uchar Data[],uchar Address,uchar Num)//Read out in a set of data to the AT24C02
    167.     //The first parameter is an array address, data in the AT24C02 in the beginning of the address, number of data
    168. {
    169.     uchar i=0;
    170.     uchar *PData;
    171.     PData=Data;
    172.     for(i=0;i<Num;i++)
    173.     {
    174.         Start();
    175.         Send(0xa0); //A0, A1, A2 earth, solid AT24C02 write address 0XA0
    176.         Ack();
    177.         Send(Address+i);
    178.         Ack();
    179.         Start();
    180.         Send(0xa1); //A0, A1, A2 earth, solid AT24C02 read address 0XA1
    181.         Ack();
    182.         *(PData+i)=Read();
    183.         Scl=0;
    184.         NoAck();
    185.         Stop();
    186.   }
    187. }
    188.  
    189. /////////////////////////////////END of IIC/////////////////////////////////
    190. void busy()        //Check whether the busy routines LCD
    191. {
    192.     do
    193.     {
    194.         busyflag=REG1;
    195.     }while(busyflag_7);
    196. }
    197.  
    198. void wrc(unsigned char wcon)    //Subroutine to write control instructions
    199. {
    200.     busy();
    201.     REG0=wcon;
    202. }
    203.  
    204. void wrd(unsigned char wdat)    //Subroutine write data register
    205. {
    206.     busy();
    207.     REG2=wdat;
    208. }
    209.  
    210. void rdd()    //Subroutine read data register
    211. {
    212.     busy();
    213.     dat=REG3;
    214. }
    215.  
    216. void lcdint()    //LCD initialization subroutine
    217. {
    218.     wrc(0x38);  
    219.     wrc(0x01);
    220.     wrc(0x06);
    221.     wrc(0x0c);
    222. }
    223. void wrn(unsigned char word[])    //Continuous data write n characters to the LCD data register
    224. {
    225.     unsigned char i;
    226.     for(i=0;i<16;i++)
    227.     {
    228.         wrd(word[i]);
    229.     }
    230. }
    231.  
    232.  
    233. ///////////////////////////////////
    234. void main()
    235. {
    236.         unsigned char i;
    237.         lcdint();  
    238.         wrc(0x80);  
    239.         wrn(word1);
    240.         wrc(0xc0);  
    241.         wrn(word2);
    242.  
    243.         while(1)
    244.         {
    245.             if(p10==0)
    246.             {
    247.                 for(i=0;i<16;i++)
    248.                     word1[i]='d';
    249.                 WrToROM(word1,0x00,16);
    250.                 wrc(0x80);
    251.                 wrn(word1);
    252.  
    253.             }
    254.             if(p11==0)
    255.             {
    256.                 RdFromROM(word2,0x00,16);
    257.                 WrToROM(word2,0x80,16);
    258.                 wrc(0x80);
    259.                 wrn(word1);
    260.                 wrc(0xc0);
    261.                 wrn(word2);
    262.             }
    263.             if(p12==0)
    264.             {
    265.                 wrc(0x80);
    266.                 wrn(word3);
    267.                 wrc(0xc0);
    268.                 wrn(word4);
    269.             }
    270.             if(p13==0)
    271.             {
    272.                 wrc(0x80);
    273.                 wrn(word5);
    274.                 wrc(0xc0);
    275.                 wrn(word6);
    276.             }
    277.             if(p14==0)
    278.             {
    279.                 wrc(0x80);
    280.                 wrn(word3);
    281.                 wrc(0xc0);
    282.                 wrn(word4);
    283.             }
    284.             if(p15==0)
    285.             {
    286.                 wrc(0x80);
    287.                 wrn(word5);
    288.                 wrc(0xc0);
    289.                 wrn(word6);
    290.             }
    291.             if(p16==0)
    292.             {
    293.                 wrc(0x80);
    294.                 wrn(word3);
    295.                 wrc(0xc0);
    296.                 wrn(word4);
    297.             }
    298.             if(p17==0)
    299.             {
    300.                 wrc(0x80);
    301.                 wrn(word5);
    302.                 wrc(0xc0);
    303.                 wrn(word6);
    304.             }
    305.         }
    306. }
Loading...