Discussion in "8051 Discussion Forum" started by    jayachandra    Aug 19, 2014.
Tue Aug 19 2014, 05:52 pm
#1
HELLO
im facing problem with eeprom, i wrote a program in AT89S51, EEPROM is 24C16K, WITH 12MHZ crystal, and my program is with 2modes(mode1 & mode2), if im working in mode2 and then i switch of the power supply, if i switch on again it has to come to mode 2, but it is not like that, default it is going to mode1, is ther any necessary to attach battery to 24c16k eeprom, im attaching my program please check once and guide me is ther any mistake in program,
[#include<REGX51.H>
#include"pro_lcd.h"
#include"eprom.h"

sbit switch1=P1^3;
sbit mod1=P1^4;
sbit mod2=P1^5;

unsigned char i;
unsigned char c1,vl1,vl2;
unsigned char mode_add;

void main()
{
mod1=mod2=1;
I2CInit();
lcdinit();
lcdcmd(0x01);
lcdcmd(0x80);
lcdstr("EEPROM TEST");
delay1(100);

I2CStart();
I2CSend(0xA0);
I2CSend(0x00);

I2CRestart();
I2CSend(0xA1);
for(i=0;i<7;i++)
{
mode_add=I2CRead();
if(i==6)
I2CNak();
else
I2CAck();
}
I2CStop();


if(switch1==0)
{ mode_add++;

if(mode_add>=2) mode_add=0;
}

if(mode_add==0)
{
mode_add=0;
I2CStart();
I2CSend(0xA0);
I2CSend(0x00);
I2CSend(0x00);
I2CSend(0x00);
I2CSend(0x00);
I2CSend(0x00);
I2CSend(0x00);
I2CSend(0x00);
I2CSend(mode_add);
I2CStop();


mod1=1;
mod2=0;
lcdcmd(0x01);
lcdcmd(0x80);
lcdstr("MODE 1");
while(1);
}

else if(mode_add==1)
{
mode_add=1;
j=mode_add;
I2CStart();
I2CSend(0xA0);
I2CSend(0x00);
I2CSend(0x00);
I2CSend(0x00);
I2CSend(0x00);
I2CSend(0x00);
I2CSend(0x00);
I2CSend(0x00);
I2CSend(mode_add);
I2CStop();

mod1=0;
mod2=1;
lcdcmd(0x01);
lcdcmd(0x80);
lcdstr("MODE 2");
while(1);
}
}]



[sbit SDA=P3^4;
sbit SCL=P3^5;

void I2CInit();
void I2CStart();
void I2CRestart();
void I2CStop();
void I2CAck();
void I2CNak();
unsigned char I2CSend(unsigned char);
unsigned char I2CRead();

void I2CInit()
{
SDA = 1;
SCL = 1;
}

void I2CStart()
{
SCL = 1;
SDA = 0;
SCL = 0;
}

void I2CRestart()
{
SDA = 1;
SCL = 1;
SDA = 0;
}

void I2CStop()
{
SDA = 0;
SCL = 1;
SDA = 1;
}

void I2CAck()
{
SDA = 0;
SCL = 1;
SCL = 0;
}

void I2CNak()
{
SDA = 1;
SCL = 1;
SCL = 0;
}

unsigned char I2CSend(unsigned char Data)
{
unsigned char i, ack_bit;
for(i=0;i<8;i++)
{
SCL = 0;
if ((Data & 0x80) == 0)
SDA = 0;
else
SDA = 1;
SCL = 1;
Data<<=1;
}
SCL = 0;
ack_bit = SDA;
SCL = 1;
SCL = 0;
return !ack_bit;
}

unsigned char I2CRead()
{
unsigned char i, Data=0;
for(i=0;i<8;i++)
{
SCL = 0;
SCL = 1;
if(SDA)
Data |=1;
if(i<7)
Data<<=1;
}
SCL = 0;
SDA = 1;
return Data;
}]

sbit rs=P1^0;
sbit rw=P1^1;
sbit en=P1^2;

#define ldata P2

void lcdinit();
void lcddat(unsigned char );
void lcdcmd(unsigned char );
void delay1(unsigned char );
void lcdstr(unsigned char* );
unsigned char x,y,z;

void lcdstr(unsigned char *p)
{
while(*p)
{
lcddat(*p);
p++;
delay1(10);
}
}
void lcdinit()
{
lcdcmd(0x38);
lcdcmd(0x01);
lcdcmd(0x02);
lcdcmd(0x0e);
lcdcmd(0x06);
lcdcmd(0x80);
}
void lcddat(unsigned char dat)
{
ldata=dat;
rs=1;
rw=0;
en=1;
delay1(2);
en=0;
}
void lcdcmd(unsigned char cmd)
{
ldata=cmd;
rs=0;
rw=0;
en=1;
delay1(2);
en=0;
}

void delay1(unsigned char itime)
{
unsigned int i,j ;
for(i=0;i<itime;i++)
for(j=0;j<1000;j++);
}[]
Wed Aug 20 2014, 04:10 pm
#2
Hi Mr jayachandra,

I feel ur if statement for switch is making your application to switch to mode1 instead of 2 because as far as i understood your program accessable memory is getting its initial value which relates to mode1 so whenever you are trying to resume mode2 it is entering mode1 and starting its work freshly.
please check if I am right, if not please post where it went wrong so it will be helpful for other learners.

if(switch1==0)
{ mode_add++;

if(mode_add>=2) mode_add=0;
}

if(mode_add==0)
{
mode_add=0;
I2CStart();
I2CSend(0xA0);
I2CSend(0x00);
I2CSend(0x00);
I2CSend(0x00);
I2CSend(0x00);
I2CSend(0x00);
I2CSend(0x00);
I2CSend(0x00);
I2CSend(mode_add);
I2CStop();


mod1=1;
mod2=0;
lcdcmd(0x01);
lcdcmd(0x80);
lcdstr("MODE 1");
while(1);
}
Wed Aug 20 2014, 05:41 pm
#3
thanq for reply @hasmukhjain,
actually my application is while i power on the circuit, it has to come previous time witch mode is running before power off, but it is not like that
Thu Aug 21 2014, 11:20 am
#4
for that try to add a variable wch can save your previous mode when you turn it off later when u turn ON it should read the previous mode data which can hopefully make u to start your work in the mode you left it.

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

Robertrip
Fri Apr 26 2024, 11:20 am
ArnoldDiant
Fri Apr 26 2024, 03:53 am
RodneyKnorb
Thu Apr 25 2024, 07:08 pm
Williamjef
Thu Apr 25 2024, 02:08 pm
SamuelSmise
Thu Apr 25 2024, 09:56 am
DustinErele
Thu Apr 25 2024, 08:44 am
ztaletpzca
Wed Apr 24 2024, 11:19 pm
IrardlPex
Wed Apr 24 2024, 08:42 pm