Serial EEPROM Board
/*
* Project name:
Serial EEPROM Board
* Copyright
(c) Researchdesignlab.com
* Description:
* Test configuration:
MCU: AT89S52
Dev.Board: 8051
Oscillator: 11.0592 MHz
Software: Keil uVision3
*/
#include"reg51.h"
#include"string.h"
#include "intrins.h"
#define DEV_WR 0xa0
#define DEV_RD 0xa1
#define LCD_PORT P2
sbit rs=P3^5;
sbit en=P3^7;
sbit D7=P2^7;
sbit rw=P3^6;
sbit scl=P3^2;
sbit sda=P3^3;
sbit LED=P1^5;
void startbit();
unsigned char data_read();
void slaveaddr(unsigned char );
void stopbit();
void busy();
void CMD_WRT(unsigned char);
void LCD_WRT(unsigned char *);
void DATA_WRT(unsigned char);
void DELAY();
void DELAY1();
void main()
{
unsigned char CMD[]={0x38,0x01,0x0f,0x06,0x80},TEMP1,i;
unsigned char ch,temp;
for(i=0;i<5;i++)
{
TEMP1=CMD[i];
CMD_WRT(TEMP1);
}
start:
LED=0;
ch='A';
CMD_WRT(0X01);
CMD_WRT(0X80);
LCD_WRT("STORING 'A'");
startbit();
slaveaddr(DEV_WR);
slaveaddr(0x10);
slaveaddr(ch);
stopbit();
DELAY();
DELAY();
DELAY();
DELAY();
DELAY();
CMD_WRT(0X01);
CMD_WRT(0X80);
LCD_WRT("READING...");
startbit();
slaveaddr(DEV_WR);
slaveaddr(0x10);
startbit();
slaveaddr(DEV_RD);
temp= data_read();
if(temp!='A')
{
goto start;
}
LED=1;
DATA_WRT(temp);
while(1);
}
void DELAY()
{
unsigned int X=800000;
while(X--);
}
void busy()
{
D7=1;
rs=0;
rw=1;
while(D7!=0)
{
en=0;
en=1;
}
}
void CMD_WRT(unsigned char val)
{
busy();
LCD_PORT=val;
rs=0;
rw=0;
en=1;
en=0;
}
void LCD_WRT(unsigned char *string)
{
while(*string)
DATA_WRT(*string++);
}
void DATA_WRT(unsigned char ch)
{
busy();
LCD_PORT = ch;
rs=1;
rw=0;
en=1;
en=0;
}
void startbit()
{
sda=1;
scl=1;
//delay();
_nop_();
_nop_();
sda=0;
//delay();
//delay();
_nop_();
_nop_();
_nop_();
_nop_();
scl=0;
_nop_();
_nop_();
}
//**********************************************************
void stopbit()
{
sda=0;
scl=1;
//delay();
_nop_();
_nop_();
sda=1;
//delay();
//delay();
_nop_();
_nop_();
_nop_();
_nop_();
scl=0;
//delay();
_nop_();
_nop_();
_nop_();
_nop_();
scl=1;
}
//*********************************************************
void slaveaddr(unsigned char m)
{
unsigned char i;
unsigned char b;
b=m;
//scl=1;
for(i=0;i<8;i++)
{
b= (b & 0x80);
if(b!=0)
sda=1;
else
sda=0;
scl=1;
//delay();
_nop_();
_nop_();
_nop_();
scl=0;
b=m<<1;
m=b;
}
sda=1;
scl=1;
//lay();
_nop_();
_nop_();
_nop_();
scl=0;
// delay();
}
//************************************************************ */
unsigned char data_read()
{
unsigned char i,a1,b1=0;
sda=1;
for(i=0;i<8;i++)
{
scl=0;
//delay();
_nop_();
_nop_();
_nop_();
scl=1;
//delay(); _nop_();
if(sda==1)
b1=(b1 | 0x01);
a1=b1;
b1=b1<<1;
scl=0;
}
sda=1;
scl=1;
//delay();
_nop_();
_nop_();
_nop_();
scl=0;
return(a1);
}