Interfacing I2C EEPROM with 8051 microcontroller and lcd display.
Circuit Diagram:
C Code
Code (Text):
#include <reg51.h> #include <absacc.h> #define REG0 XBYTE[0x0000] //LCD instruction register to write the address, according to configuration changes #define REG1 XBYTE[0x0001] //D0 ~ D7 LCD read data to the address, according to configuration changes #define REG2 XBYTE[0x0002] //LCD write data register address, configuration changes can #define REG3 XBYTE[0x0003] //LCD read data register address, configuration changes can unsigned char bdata busyflag; unsigned char dat,datn; unsigned char word1[16]={" This programm "}; //Line 1 shows the cache unsigned char word2[16]={" by Samsung "}; //Line 2 shows the cache code unsigned char word3[16]={"8051projects"}; //Fixed display for the data to the ROM in curing code unsigned char word4[16]={" 8051projects !"}; //Fixed display for the data to the ROM in curing code unsigned char word5[16]={" Wellcome To "}; //Fixed display for the data to the ROM in curing code unsigned char word6[16]={" Proteus Tools!"}; //Fixed display for the data to the ROM in curing sbit busyflag_7=busyflag^7; sbit p10=P1^0; sbit p11=P1^1; sbit p12=P1^2; sbit p13=P1^3; sbit p14=P1^4; sbit p15=P1^5; sbit p16=P1^6; sbit p17=P1^7; //////////////////////////////////////start of IIC //////////////////////////// #define uchar unsigned char #define uint unsigned int #include "reg51.h" #include "intrins.h" sbit Scl=P3^4; //Serial Clock sbit Sda=P3^5; //Serial Data /*Send start condition*/ void Start(void) /*Initial conditions*/ { Sda=1; Scl=1; _nop_ (); _nop_ (); _nop_ (); _nop_ (); Sda=0; _nop_ (); _nop_ (); _nop_ (); _nop_ (); } void Stop(void) /*Stop condition*/ { Sda=0; Scl=1; _nop_ (); _nop_ (); _nop_ (); _nop_ (); Sda=1; _nop_ (); _nop_ (); _nop_ (); _nop_ (); } void Ack(void) /*Acknowledge bit*/ { Sda=0; _nop_ (); _nop_ (); _nop_ (); _nop_ (); Scl=1; _nop_ (); _nop_ (); _nop_ (); _nop_ (); Scl=0; } void NoAck(void) /*Reverse acknowledge bit*/ { Sda=1; _nop_ (); _nop_ (); _nop_ (); _nop_ (); Scl=1; _nop_ (); _nop_ (); _nop_ (); _nop_ (); Scl=0; } void Send(uchar Data) /*Subroutine to send data,Data is required to send data*/ { uchar BitCounter=8; /*Median control*/ uchar temp; /*Intermediate variable control*/ do { temp=Data; Scl=0; _nop_ (); _nop_ (); _nop_ (); _nop_ (); if((temp&0x80)==0x80)/* If the highest bit is 1*/ Sda=1; else Sda=0; Scl=1; temp=Data<<1; /*RLC*/ Data=temp; BitCounter--; }while(BitCounter); Scl=0; } uchar Read(void) /*Read one byte of data, and returns the byte value*/ { uchar temp=0; uchar temp1=0; uchar BitCounter=8; Sda=1; do{ Scl=0; _nop_ (); _nop_ (); _nop_ (); _nop_ (); Scl=1; _nop_ (); _nop_ (); _nop_ (); _nop_ (); if(Sda) /*If Sda=1;*/ temp=temp|0x01; /*The lowest temp 1*/ else temp=temp&0xfe; /*Otherwise, the lowest temp clear 0*/ if(BitCounter-1) { temp1=temp<<1; temp=temp1; } BitCounter--; }while(BitCounter); return(temp); } void WrToROM(uchar Data[],uchar Address,uchar Num)//Write a set of data to the AT24C02 in { //The first parameter is an array address, data in the AT24C02 in the beginning of the address, number of data uchar i=0; uchar *PData; PData=Data; Start(); Send(0xa0); //A0, A1, A2 earth, solid AT24C02 write address 0XA0 Ack(); Send(Address); Ack(); for(i=0;i<Num;i++) { Send(*(PData+i)); Ack(); } Stop(); } void RdFromROM(uchar Data[],uchar Address,uchar Num)//Read out in a set of data to the AT24C02 //The first parameter is an array address, data in the AT24C02 in the beginning of the address, number of data { uchar i=0; uchar *PData; PData=Data; for(i=0;i<Num;i++) { Start(); Send(0xa0); //A0, A1, A2 earth, solid AT24C02 write address 0XA0 Ack(); Send(Address+i); Ack(); Start(); Send(0xa1); //A0, A1, A2 earth, solid AT24C02 read address 0XA1 Ack(); *(PData+i)=Read(); Scl=0; NoAck(); Stop(); } } /////////////////////////////////END of IIC///////////////////////////////// void busy() //Check whether the busy routines LCD { do { busyflag=REG1; }while(busyflag_7); } void wrc(unsigned char wcon) //Subroutine to write control instructions { busy(); REG0=wcon; } void wrd(unsigned char wdat) //Subroutine write data register { busy(); REG2=wdat; } void rdd() //Subroutine read data register { busy(); dat=REG3; } void lcdint() //LCD initialization subroutine { wrc(0x38); wrc(0x01); wrc(0x06); wrc(0x0c); } void wrn(unsigned char word[]) //Continuous data write n characters to the LCD data register { unsigned char i; for(i=0;i<16;i++) { wrd(word[i]); } } /////////////////////////////////// void main() { unsigned char i; lcdint(); wrc(0x80); wrn(word1); wrc(0xc0); wrn(word2); while(1) { if(p10==0) { for(i=0;i<16;i++) word1[i]='d'; WrToROM(word1,0x00,16); wrc(0x80); wrn(word1); } if(p11==0) { RdFromROM(word2,0x00,16); WrToROM(word2,0x80,16); wrc(0x80); wrn(word1); wrc(0xc0); wrn(word2); } if(p12==0) { wrc(0x80); wrn(word3); wrc(0xc0); wrn(word4); } if(p13==0) { wrc(0x80); wrn(word5); wrc(0xc0); wrn(word6); } if(p14==0) { wrc(0x80); wrn(word3); wrc(0xc0); wrn(word4); } if(p15==0) { wrc(0x80); wrn(word5); wrc(0xc0); wrn(word6); } if(p16==0) { wrc(0x80); wrn(word3); wrc(0xc0); wrn(word4); } if(p17==0) { wrc(0x80); wrn(word5); wrc(0xc0); wrn(word6); } } }
Interfacing I2C EEPROM with 8051 microcontroller 1.0
Interfacing I2C EEPROM with 8051 microcontroller and lcd display.