Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Multiplexing problem in my 8051 clock project.

Status
Not open for further replies.

tapu

Full Member level 4
Joined
Sep 15, 2014
Messages
234
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,298
Location
india
Activity points
3,041
Dear All,
I am making a clock of dual display one 4 digit 7 segment display and Lcd(16x2) by using 89s52.both should show time(as my application).I am using multiplexing method in displaying data on led display.Lcd display running successfully but led display shows on-off continously.Here is my code.
Code:
#include <REGX51.H>





sbit set=P1^0;
sbit mov=P1^1;
sbit inc=P1^2;
sbit dec=P1^3;
sbit rly=P1^4;
sbit digi1=P1^5;//3rd e5
sbit digi4=P2^3;//4th	e6
sbit blink=P2^1;
sbit bus=P3^7;
sbit ind=P3^6;
sbit digi3=P1^7;//1st e4
sbit digi2=P1^6;//2nd e3
sbit SDA=P2^6;
sbit SCL=P2^7;

bit d;
bit e;
bit f;
bit nxt;
bit nxta;

unsigned char dec_hex(unsigned char tt)
{
if(tt>59)
tt+=36;

else if(tt>49)
tt+=30;
else if(tt>39)
tt+=24;
else if(tt>29)
tt+=18;
else if(tt>19)
tt+=12;
else if(tt>9)
tt+=6;
return(tt);
} 

display_digit();

void all_disp();
void time_set();

void alarm_set();

void set_date();
void blink_on();
	void buzzer();
void no_op();
void setdisp_ampm();
	 void alarm_setsat();

unsigned int zt;

unsigned char led_data;


void alarm_bell_HI();
void alarm_bell_LOW();
void display_day();
void puls();
void no_puls();




#define First_Line 0x80
#define Second_Line 0xc0
#define Curser_On 0x0f
#define Curser_Off 0x0c
#define Clear_Display 0x01
#define idle f==1
#define no_idle f==0

#define Data_Port P0

sbit Lcd_rs = P2^0;
//sbit Lcd_rw = P2^1;
sbit Lcd_en = P2^2;

sbit sda_rtc =P2^5;
sbit scl_rtc =P2^4;

/*sbit Lcd_rs = P3^5;
sbit Lcd_rw = P3^6;
sbit Lcd_en = P3^7; */

void Lcd8_Init();
void Lcd8_Command(unsigned char);
void Lcd8_Write(unsigned char,unsigned char);
void Lcd8_Display(unsigned char,const unsigned char*,unsigned int);
void Lcd8_decimal2(unsigned char,unsigned char val);
void Delay(unsigned int);
void del();

void hour_mode();
void set_mod1();
void set_mod2();

unsigned char LB,SB;
void Seg8_decimal2(min);

void Led8decimal2(hour);
unsigned char num;
void EEPROM_WriteByte(unsigned char eeprom_Address, unsigned char eeprom_Data);
unsigned char EEPROM_ReadByte(unsigned char eeprom_Address);
void I2C_Start();
void I2C_Stop(void);
void I2C_Ack();
void I2C_Write(unsigned char dat);
unsigned char I2C_Read();
 #define EEPROM_ID 0xa0
void delay_ms(unsigned int ms_count);
void I2C_NoAck();
void I2C_Clock();
void delay_us(unsigned int us_count);
void msdelay(unsigned int value);
void emerg_bell();
void display_ampm1();
void display_ampm2();

//unsigned char number[]="1,2,3,4";



#define DS1307_ID 0xD0
#define SEC 0x00
#define MIN 0x01
#define HOUR 0x02
#define DAY 0x03
#define DATE 0x04
#define MONTH 0x05
#define YEAR 0x06
#define ctrl 0x07
#define FRIDAY 0x06
#define SATURDAY 0x00
#define SUNDAY 0x01



DS1307_get(unsigned char);
void DS1307_settime(unsigned char, unsigned char, unsigned char);
void DS1307_setdate(unsigned char, unsigned char, unsigned char,unsigned char);
Send2lcd(unsigned char);
void Rtc_Write(unsigned char,unsigned char);
Rtc_Read(unsigned char);
void Rtc_rd_wr_sub();
void Rtc_Init();
void Rtc_Start();
void Rtc_Tx();
void Rtc_Rx();
void Rtc_Stop();
void Rtc_Ack();
void disp();
void disp_time();
void display_ampm();
void dispampm();
void set_ampm();

void set_mod();

void alarm_chk1();
void alarm_chk2();
void alarm_chk3();
void alarm_chk4();

void all_alrmset();
 void all_alrmsetsat();

void friday_chk();
void regular_chk();
	void saturday_chk();

void idle_chk();
void no_idle_chk();

void key_lock2();


unsigned int Rtc_add_wr,Rtc_add_rd;
unsigned char d_rtc,datain_rtc,in_rtc,temp_rtc,dat_rtc,flag_rtc;

unsigned char i,j,a[10],cur=0,cur1=0,cur2=0,cur3=0,cur4=0;
unsigned char dati,sec,hour,min,date,day,month,year,mode=0,clk_a  mpm,sec1,hour1,min1,sec2,hour2,min2,hh,mm,ss,dd,mn  ,yy,ahour,amin,ampm,asec,amin1,ahour1,ampm1;
 

unsigned char count[8]={0x82,0x85,0x8a,0x8d,0xc2,0xc5,0xcb,0xcd};   ///0x8a changed
code unsigned char memo[66]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x1  0,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x2  0,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x3  0,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x2a,0x2  b,0x2c,0x2d,0x2e,0x2f,0x3a,0x3b,0x3c,0x3d,0x3e,0x3  f,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0xa,0xb,0xc,0xd,0x  e,0xf,0x39,0x40,0x4a,0x4b};
code unsigned char memo1[66]={0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x50,0x5  1,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x60,0x6  1,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x70,0x7  1,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x5a,0x5  b,0x5c,0x5d,0x5e,0x5f,0x6a,0x6b,0x6c,0x6d,0x6e,0x6  f,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f,0x8a,0x8b,0x8c,0x8  d,0x8e,0x8f,0x9a,0x9b,0x9c,0x9d};

unsigned char count1[6]={0x86,0x89,0x8b,0xc6,0xc9,0xcb};  
unsigned char count2[2]={0x8b,0xcb};   

unsigned char rly_hex(unsigned char tt)
{
if(tt>59)
tt+=36;
else if(tt>49)
tt+=30;
else if(tt>39)
tt+=24;
else if(tt>29)
tt+=18;
else if(tt>19)
tt+=12;
else if(tt>9)
tt+=6;
return(tt);
} 

void all_disp()
	
{
	
sec = DS1307_get(SEC);
min = DS1307_get(MIN);
hour = DS1307_get(HOUR);
	
dispampm();
setdisp_ampm();
hour=hour& 0x1f;
display_ampm();

date = DS1307_get(DATE);
day = DS1307_get(DAY);
month = DS1307_get(MONTH);
year = DS1307_get(YEAR);
	
sec=Send2lcd(sec);
min=Send2lcd(min);
hour=Send2lcd(hour);
day=Send2lcd(day);
	
date=Send2lcd(date);
year=Send2lcd(year);
month=Send2lcd(month);


//P3=day;


Lcd8_decimal2(0xc6,hour);
Lcd8_Write(0xc8,'.');
Lcd8_decimal2(0xc9,min);
Lcd8_Write(0xcb,'.');
Lcd8_decimal2(0xcc,sec);
	Lcd8_decimal2(0x82,date);
Lcd8_Write(0x84,'-');
Lcd8_decimal2(0x85,month);
Lcd8_Write(0x87,'-');
Lcd8_Display(0x88,"20 ",2);

Lcd8_decimal2(0x8a,year);

//Lcd8_decimal2(0x8d,day);


Seg8_decimal2(min);//..................................................  ..................................................  ...............
Led8decimal2(hour);//..................................................  .................................................



}

void main()
	
{
nxt=0;
rly=0;	
dati=0x00;
Lcd8_Init();
Lcd8_Display(0x82,"ZAKARIYA ",8);
	Lcd8_Display(0xc3,"Technologies ",12);

Delay(65000);
Delay(65000);


Lcd8_Command(0x01);
Rtc_Init();
	//puls();


//DS1307_setdate(0x04,0x11,0x11);
//DS1307_settime(0x16,0x00,0x00);

while(1)
	
{
	//P1=EEPROM_ReadByte(memo[1]);

	dati++;
all_disp();
	Rtc_Start();

Rtc_Write(0x07,0x10);
 
Rtc_Stop(); /* Stop i2c bus */

//disp();
	display_day();
	//no_idle_chk();if(no_idle) goto straight;  //No alarm betwen 8pm to 6am

//idle_chk();if(idle) goto jump;  //No alarm betwen 8pm to 6am

//if(day==SATURDAY) goto jump;
if(day==0x01) goto jump;

//if(day==FRIDAY)
//friday_chk();
//else
	if(day==0x07)
	saturday_chk();
	else
straight:
regular_chk();
jump:
	if(!set)
		key_lock2();  ///show as keypad locked(when press set/inc/dec)

 if(!inc)
	 key_lock2();  ///show as keypad locked(when press set/inc/dec)

 if(!dec)
	 
   key_lock2();  ///show as keypad locked(when press set/inc/dec)

 
 if(!dec)
 alarm_bell_HI();

  if(!set)
 alarm_bell_LOW();

if(mov==0)
{
Delay(65000);
Delay(65000);
Delay(65000);
}
if(mov==0)
{
		Lcd8_Command(0x01);

	Lcd8_Display(0xc4,"Unlocked ",8);
	Delay(65000);
time_set();
	cur2=0;
	cur4=0;
			Lcd8_Command(0x01);
		Lcd8_Command(0x0c);

	Lcd8_Display(0x82,"REGULAR ",7);
	Lcd8_Display(0xC4,"SCHEDULE ",8);

		Delay(65000);
		Delay(65000);

all_alrmset();
	Lcd8_Command(0x01);
Lcd8_Display(0x82,"SATURDAY",8);
	Lcd8_Display(0xC4,"SCHEDULE ",8);

	Delay(65000);
	Delay(65000);

all_alrmsetsat();

set_date();

}
if(dati==0x03)
{
blink=~blink;
dati=0x00;
}



}
}


void dispampm()
	
{
	if(hour==0x00)
	{
		 d=0;
clk_ampm=0;
Rtc_Start();

Rtc_Write(0x02,0x40);
 
Rtc_Stop(); /* Stop i2c bus */
	}

		if(hour==0x01)
	{
				 d=0;
clk_ampm=0;

Rtc_Start();

Rtc_Write(0x02,0x41);
 
Rtc_Stop(); /* Stop i2c bus */
	}

			if(hour==0x02)
	{
				 d=0;
clk_ampm=0;

Rtc_Start();

Rtc_Write(0x02,0x42);
 
Rtc_Stop(); /* Stop i2c bus */
	}

		if(hour==0x03)
	{
				 d=0;
clk_ampm=0;

Rtc_Start();

Rtc_Write(0x02,0x43);
 
Rtc_Stop(); /* Stop i2c bus */
	}

		if(hour==0x04)
	{
				 d=0;
clk_ampm=0;

Rtc_Start();

Rtc_Write(0x02,0x44);
 
Rtc_Stop(); /* Stop i2c bus */
	}

		if(hour==0x05)
	{
				 d=0;
clk_ampm=0;

Rtc_Start();

Rtc_Write(0x02,0x45);
 
Rtc_Stop(); /* Stop i2c bus */
	}

		if(hour==0x06)
	{
				 d=0;
clk_ampm=0;

Rtc_Start();

Rtc_Write(0x02,0x46);
 
Rtc_Stop(); /* Stop i2c bus */
	}

		if(hour==0x07)
	{
				 d=0;
clk_ampm=0;

Rtc_Start();

Rtc_Write(0x02,0x47);
 
Rtc_Stop(); /* Stop i2c bus */
	}

		if(hour==0x08)
	{
				 d=0;
clk_ampm=0;

Rtc_Start();

Rtc_Write(0x02,0x48);
 
Rtc_Stop(); /* Stop i2c bus */
	}

		if(hour==0x09)
	{
				 d=0;
clk_ampm=0;

Rtc_Start();

Rtc_Write(0x02,0x49);
 
Rtc_Stop(); /* Stop i2c bus */
	}

		if(hour==0x10)
	{
				 d=0;
clk_ampm=0;

Rtc_Start();

Rtc_Write(0x02,0x4a);
 
Rtc_Stop(); /* Stop i2c bus */
	}

		if(hour==0x11)
	{
				 d=0;
clk_ampm=0;

Rtc_Start();

Rtc_Write(0x02,0x4b);
 
Rtc_Stop(); /* Stop i2c bus */
	}

		if(hour==0x12)
	{
				 d=1;
clk_ampm=1;

Rtc_Start();

Rtc_Write(0x02,0x72);
 
Rtc_Stop(); /* Stop i2c bus */
	}

		if(hour==0x13)
	{
						 d=1;
clk_ampm=1;

Rtc_Start();

Rtc_Write(0x02,0x61);
 
Rtc_Stop(); /* Stop i2c bus */
	}

		if(hour==0x14)
	{
						 d=1;
clk_ampm=1;

Rtc_Start();

Rtc_Write(0x02,0x62);
 
Rtc_Stop(); /* Stop i2c bus */
	}

		if(hour==0x15)
	{
						 d=1;
clk_ampm=1;

Rtc_Start();

Rtc_Write(0x02,0x63);
 
Rtc_Stop(); /* Stop i2c bus */
	}

		if(hour==0x16)
	{
						 d=1;
clk_ampm=1;

Rtc_Start();

Rtc_Write(0x02,0x64);
 
Rtc_Stop(); /* Stop i2c bus */
	}

		if(hour==0x17)
	{
						 d=1;
clk_ampm=1;

Rtc_Start();

Rtc_Write(0x02,0x65);
 
Rtc_Stop(); /* Stop i2c bus */
	}

		if(hour==0x18)
	{
						 d=1;
clk_ampm=1;

Rtc_Start();

Rtc_Write(0x02,0x66);
 
Rtc_Stop(); /* Stop i2c bus */
	}

		if(hour==0x19)
	{
						 d=1;
clk_ampm=1;

Rtc_Start();

Rtc_Write(0x02,0x67);
 
Rtc_Stop(); /* Stop i2c bus */
	}

		if(hour==0x20)
	{
						 d=1;
clk_ampm=1;

Rtc_Start();

Rtc_Write(0x02,0x68);
 
Rtc_Stop(); /* Stop i2c bus */
	}

		if(hour==0x21)
	{
						 d=1;
clk_ampm=1;

Rtc_Start();

Rtc_Write(0x02,0x69);
 
Rtc_Stop(); /* Stop i2c bus */
	}

		if(hour==0x22)
	{
						 d=1;
clk_ampm=1;

Rtc_Start();

Rtc_Write(0x02,0x6a);
 
Rtc_Stop(); /* Stop i2c bus */
	}

		if(hour==0x23)
	{
						 d=1;
clk_ampm=1;

Rtc_Start();

Rtc_Write(0x02,0x6b);
 
Rtc_Stop(); /* Stop i2c bus */
	}
	
}





void key_lock2() 
{

 
{
	Lcd8_Command(0x01);

	Lcd8_Display(0xc1,"Keypad locked ",13);

Delay(65000);
Lcd8_Command(0x01);
}
}






void idle_chk()
	
{
if(hour==7)
if(d==1)
	f=1;
if(hour==8)
	if(d==1)
		f=1;
if(hour==9)
	if(d==1)
		f=1;
if(hour==10)
	if(d==1)
		f=1;
if(hour==11)
	if(d==1)
	f=1;
if(hour==12)
	if(d==0)
	f=1;
if(hour==1)
	if(d==0)
	f=1;
if(hour==2)
	if(d==0)
	f=1;
if(hour==3)
	if(d==0)
	f=1;
if(hour==4)
	if(d==0)
	f=1;
if(hour==5)
	if(d==0)
	f=1;
if(hour==6)
	if(d==0)
	f=1;
	
}	



void no_idle_chk()
	
{
if(hour==7)
if(d==0)
	f=0;
if(hour==8)
	if(d==0)
		f=0;
if(hour==9)
	if(d==0)
		f=0;
if(hour==10)
	if(d==0)
		f=0;
if(hour==11)
	if(d==0)
	f=0;
if(hour==12)
	if(d==1)
	f=0;
if(hour==1)
	if(d==1)
	f=0;
if(hour==2)
	if(d==1)
	f=0;
if(hour==3)
	if(d==1)
	f=0;
if(hour==4)
	if(d==1)
	f=0;
if(hour==5)
	if(d==1)
	f=0;
if(hour==6)
	if(d==1)
	f=0;
	
}	






 void all_alrmset(void)
 {
	 nxt=0;
	 up:alarm_set();
	 //alarm_setsat();
	 if(nxt==0) goto up;

 }
 
  void all_alrmsetsat(void)
 {
	 nxta=0;
	 up:alarm_setsat();
	 if(nxta==0) goto up;

 }
 

 
 
 
 
 
 void friday_chk()
 {
 }

 void regular_chk()
 {
 alarm_chk1();
 alarm_chk2();
 }

 
  void saturday_chk()
 {
 alarm_chk3();
 alarm_chk4();
 }

 
 
 void setdisp_ampm()
 {
	 	if(hour==0x52)
		{
	  d=0;
		clk_ampm=00;
		}
		if(hour==0x41)
		{
	  d=0;
		clk_ampm=00;
		}
    if(hour==0x42)
		{
	  d=0;
		clk_ampm=00;
		}
    if(hour==0x43)
		{
	  d=0;
		clk_ampm=00;
		}
		if(hour==0x44)
		{
	  d=0;
		clk_ampm=00;
		}
    if(hour==0x45)
		{
	  d=0;
		clk_ampm=00;
		}
    if(hour==0x46)
		{
	  d=0;
		clk_ampm=00;
		}
    if(hour==0x47)
		{
	  d=0;
		clk_ampm=00;
		}
    if(hour==0x48)
		{
	  d=0;
		clk_ampm=00;
		}
    if(hour==0x49)
		{
	  d=0;
		clk_ampm=00;
		}
    if(hour==0x4a)
		{
	  d=0;
		clk_ampm=00;
		}
    if(hour==0x4b)
		{
	  d=0;
		clk_ampm=00;
		}

	if(hour==0x72)
	{
    d=1;
		clk_ampm=01;
	}
  if(hour==0x61)
	{
    d=1;
	  clk_ampm=01;
	}
  if(hour==0x62)
	{
    d=1;
		clk_ampm=01;
	}
  if(hour==0x63)
	{
    d=1;
		clk_ampm=01;
	}
  if(hour==0x64)
	{
    d=1;
		clk_ampm=01;
	}
  if(hour==0x65)
	{
    d=1;
		clk_ampm=01;
	}
  if(hour==0x66)
	{
    d=1;
		clk_ampm=01;
	}
  if(hour==0x67)
	{
    d=1;
		clk_ampm=01;
	}
  if(hour==0x68)
	{
    d=1;
		clk_ampm=01;
	}
  if(hour==0x69)
	{
    d=1;
		clk_ampm=01;
	}
  if(hour==0x6a)
	{
    d=1;
		clk_ampm=01;
	}
  if(hour==0x6b)
	{
    d=1;
		clk_ampm=01;
	}

 }






void disp_time()
	
{
sec = DS1307_get(SEC);
min = DS1307_get(MIN);
hour = DS1307_get(HOUR);
	hour=hour& 0x1f;
sec=Send2lcd(sec);
min=Send2lcd(min);
hour=Send2lcd(hour);

	Lcd8_decimal2(0xc2,hour);
Lcd8_Write(0xc4,'.');
Lcd8_decimal2(0xc5,min);
Lcd8_Write(0xc7,'.');
Lcd8_decimal2(0xc8,sec);
set_ampm();


Seg8_decimal2(min);
Led8decimal2(hour);
	
	
}
void display_ampm()
	
{
 if(d==0)
  Lcd8_Display(0xce,"AM ",2);/////////////////////////////////////////////////////////changed

 if(d==1)
	Lcd8_Display(0xce,"PM ",2);/////////////////////////////////////////////////////////changed
}


void display_ampm1()
	
{
 if(d==0)
  Lcd8_Display(0x8b,"AM ",2);/////////////////////////////////////////////////////////changed

 if(d==1)
	Lcd8_Display(0x8b,"PM ",2);/////////////////////////////////////////////////////////changed
}


void display_ampm2()
	
{
 if(d==0)
  Lcd8_Display(0xcb,"AM ",2);/////////////////////////////////////////////////////////changed

 if(d==1)
	Lcd8_Display(0xcb,"PM ",2);/////////////////////////////////////////////////////////changed
}









void set_ampm()
	
{
	 if(d==0)
  Lcd8_Display(0xcb,"AM ",2);/////////////////////////////////////////////////////////changed
	 clk_ampm=00;

 if(d==1)
	Lcd8_Display(0xcb,"PM ",2);/////////////////////////////////////////////////////////changed
clk_ampm=01;
}

void emerg_bell()
{
}

void no_op()

{
	
}
	
void hour_mode()
	
{
	
	if(hour>12)
		hour=hour-12;
	
}

void puls()
	
{
	Rtc_Start();

Rtc_Write(0x07,0x10); /* Write date on RAM address 04H */
	

Rtc_Stop(); /* Stop i2c bus */
}

void no_puls()
{
Rtc_Start();

Rtc_Write(0x07,0x00); /* Write date on RAM address 04H */
	

Rtc_Stop(); /* Stop i2c bus */
}


void display_day()
{

 if(day==0x01)
 
Lcd8_Display(0xc1,"SUN    ",3);

 if(day==0x02)

Lcd8_Display(0xc1,"MON    ",3);
 if(day==0x03)

Lcd8_Display(0xc1,"TUE   ",3);
 if(day==0x04)

Lcd8_Display(0xc1,"WED ",3);
 if(day==0x05)

Lcd8_Display(0xc1,"THU  ",3);
 if(day==0x06)

Lcd8_Display(0xc1,"FRI    ",3);
 if(day==0x07)

Lcd8_Display(0xc1,"SAT  ",3);

}


void set_date()

{
	blink_on();

Lcd8_Command(0x01);

	
all_disp();
	Lcd8_decimal2(0x8d,day);
cur=0;
while(set)
{
	
	Lcd8_Command(0x0f);
Lcd8_Command(count[cur]);

if(mov==0)
{
while(!mov);
cur++;
			blink_on();

if(cur==4)
cur=0;
}


else if(count[cur]==0x82)
{
if(inc==0)
{
while(inc==0);
date++;
blink_on();
Lcd8_decimal2(count[cur],date);

if(date>31)
date=0;
}

else if(dec==0)
{
while(!dec);
date--;
blink_on();

if(date==0xff)
date=99;
}

}

else if(count[cur]==0x85)
{
if(inc==0)
{
while(inc==0);
month++;
	blink_on();

Lcd8_decimal2(count[cur],month);


if(month>12)
month=0;
}

else if(dec==0)
{
while(!dec);
month--;
	blink_on();

if(month==0xff)
month=99;
}

}


else if(count[cur]==0x8a)
{
if(inc==0)
{
while(!inc);
year++;
	blink_on();

	Lcd8_decimal2(count[cur],year);

if(year>99)
year=0;
}
else if(dec==0)
{
while(!dec);
year--;
	blink_on();

if(year==0xff)
year=99;
}
}



else if(count[cur]==0x8d)
{
if(inc==0)
{
while(!inc);
day++;
	blink_on();

 //Lcd8_Command(0x01);


	display_day();

	Lcd8_decimal2(count[cur],day);

if(day>6)
day=0;
}
else if(dec==0)
{
while(!dec);
day--;
	blink_on();
		display_day();


if(day==0xff)
day=7;
}
}


}
dd=dec_hex(date);
mn=dec_hex(month);
yy=dec_hex(year);
									
day=dec_hex(day);



DS1307_setdate(dd,mn,yy,day); 

Lcd8_Command(0x0c);
puls();


}



void blink_on()
{
blink=0;
  Delay(6500);
blink=1;
	
}

void set_mod()
	
{
	
if(mode==1)
{
d=0;
clk_ampm=00;
Lcd8_Display(0xcb,"AM ",2);/////////////////////////////////////////////////////////changed
}

if(mode==2)

{
d=1;
clk_ampm=01;

Lcd8_Display(0xcb,"PM ",2);/////////////////////////////////////////////////////////changed
	
}

}	


void set_mod1()
	
{
	
if(mode==1)
{
d=0;
		//hour=hour-12;

Lcd8_Display(0x8b,"AM ",2);/////////////////////////////////////////////////////////changed
}

if(mode==2)

{
	d=1;
	//hour=hour+12;
Lcd8_Display(0x8b,"PM ",2);/////////////////////////////////////////////////////////changed
	
}

}	




void set_mod2()
	
{
	
if(mode==1)
{
d=0;
		//hour=hour-12;

Lcd8_Display(0xcb,"AM ",2);/////////////////////////////////////////////////////////changed
}

if(mode==2)

{
	d=1;
	//hour=hour+12;
Lcd8_Display(0xcb,"PM ",2);/////////////////////////////////////////////////////////changed
	
}

}	







	void time_set()

{	

	blink_on();

 Lcd8_Command(Curser_On);

 Lcd8_Command(0x01);
 Lcd8_Display(0x84,"Set Time",8);
disp_time();


cur=4;
while(set)


{
		Lcd8_Command(0x0f);
Lcd8_Command(count[cur]);


if(mov==0)
{
while(!mov);
cur++;
blink_on();

if(cur==7)
cur=4;
}


else if(count[cur]==0xc2)
{
if(inc==0)
{
while(inc==0);
hour++;
		blink_on();
	Led8decimal2(hour);
	Lcd8_decimal2(0xc2,hour);



if(hour>12)
hour=0;
}




else if(dec==0)
{
while(!dec);
hour--;

		blink_on();

	Led8decimal2(hour);
	Lcd8_decimal2(0xc2,hour);


if(hour==0)
hour=12;
}

}

else if(count[cur]==0xc5)
{
if(inc==0)
{
while(inc==0);
min++;
		blink_on();

Seg8_decimal2(min);
		Lcd8_decimal2(0xc5,min);


if(min>=60)
min=0;
}

else if(dec==0)
{
while(!dec);
min--;
		blink_on();

	Seg8_decimal2(min);
			Lcd8_decimal2(0xc5,min);


if(min==0xff)
min=31;
}

}


else if(count[cur]==0xcb)
{
if(inc==0)
{
while(!inc);
mode++;
set_mod();
Delay(6500);


blink_on();
//display_ampm();
if(mode>1)
mode=0;

}
}


}
									

hh=rly_hex(hour);
mm=rly_hex(min);


if(d==1)
	hh= hh | 0x60;
if(d==0)
	hh= hh | 0x40;


DS1307_settime(hh,mm,ss);

}
	

	void buzzer()
		
	{

	//e4=0;
	//e3=0;
//P3=0xff;
//e4=1;	
//e3=1;
		//  Delay(6500);
//Led8decimal2(hour);

	}
	
	














void alarm_setsat()
	{


	no_puls();
	cur4++;
 ahour=EEPROM_ReadByte(memo1[cur4]);
  cur4++;
 amin=EEPROM_ReadByte(memo1[cur4]);
	cur4++;
	 ampm=EEPROM_ReadByte(memo1[cur4]);
	cur4++;

ahour1=EEPROM_ReadByte(memo1[cur4]);
	  cur4++;
amin1=EEPROM_ReadByte(memo1[cur4]);
		cur4++;
ampm1=EEPROM_ReadByte(memo1[cur4]);

Delay(10000);
	blink_on();


	Lcd8_Command(Curser_Off);

	Lcd8_Command(0x0f);
 
  Lcd8_Command(0x01);
	Lcd8_Display(0x88,":",1);
  	Lcd8_Display(0xc8,":",1);
		if(cur4<25)
		{	
						
  
  Lcd8_Display(0x82,"LB)",3);

	Lcd8_decimal2(0x86,ahour);
  Lcd8_decimal2(0x89,amin);
	Lcd8_decimal2(0x8b,ampm);
if(ampm==0)
{
	d=0;
	display_ampm1();

}
if(ampm==1)
{
	d=1;
	display_ampm1();

}
	
  Lcd8_Display(0xc2,"LB)",3);
  Lcd8_decimal2(0xc6,ahour1);
  Lcd8_decimal2(0xc9,amin1);
	Lcd8_decimal2(0xcb,ampm1);

if(ampm1==0)
{
	d=0;
	display_ampm2();

}
if(ampm1==1)
{
	d=1;
	display_ampm2();

}





		}
else
		{	
  Lcd8_Display(0x82,"SB)",3);
  Lcd8_decimal2(0x86,ahour);
  Lcd8_decimal2(0x89,amin);
	Lcd8_decimal2(0x8b,ampm);
if(ampm==0)
{
	d=0;
	display_ampm1();

}
if(ampm==1)
{
	d=1;
	display_ampm1();

}

			
			
  Lcd8_Display(0xc2,"SB)",3);
  Lcd8_decimal2(0xc6,ahour1);
  Lcd8_decimal2(0xc9,amin1);
	Lcd8_decimal2(0xcb,ampm1);

if(ampm1==0)
{
	d=0;
	display_ampm2();

}
if(ampm1==1)
{
	d=1;
	display_ampm2();

}

		}


cur1=0;

while(set)
{
Lcd8_Command(0x0f);
Lcd8_Command(count1[cur1]);
if(mov==0)
{
while(!mov);
cur1++;
if(cur1==6)
cur1=0;
}
else if(count1[cur1]==0x86)			                    ///1st line of lcd
{
if(inc==0)
{
while(inc==0);
ahour++;
	blink_on();

if(ahour>=13)
ahour=0;
Lcd8_decimal2(count1[cur1],ahour);
}

else if(dec==0)
{
while(!dec);
ahour--;
	blink_on();



if(ahour==0)
ahour=12;
Lcd8_decimal2(count1[cur1],ahour);

}

}
else if(count1[cur1]==0x89)
{
if(inc==0)
{
while(inc==0);
amin++;
	blink_on();

if(amin>=60)
amin=0;
Lcd8_decimal2(count1[cur1],amin);
}

else if(dec==0)
{
while(!dec);
amin--;
		blink_on();


if(amin==0)
amin=59;
Lcd8_decimal2(count1[cur1],amin);

}

}



else if(count1[cur1]==0x8b)
{
if(inc==0)
{
while(inc==0);
ampm=0;
	d=0;
display_ampm1();

	blink_on();

if(ampm>1)
ampm=0;
//Lcd8_decimal2(count1[cur1],ampm);
}

else if(dec==0)
{
while(!dec);
ampm=1;
	d=1;
display_ampm1();


		blink_on();


//Lcd8_decimal2(count1[cur1],ampm);

}

}







else if(count1[cur1]==0xc6)
{
if(inc==0)
{
while(inc==0);
ahour1++;
	blink_on();

if(ahour1>=13)
ahour1=0;
Lcd8_decimal2(count1[cur1],ahour1);
}

else if(dec==0)
{
while(!dec);
ahour1--;
	blink_on();


if(ahour1==0)
ahour1=12;
Lcd8_decimal2(count1[cur1],ahour1);

}

}
else if(count1[cur1]==0xc9)
{
if(inc==0)
{
while(inc==0);
amin1++;
	blink_on();

if(amin1>=60)
amin1=0;
Lcd8_decimal2(count1[cur1],amin1);
}

else if(dec==0)
{
while(!dec);
amin1--;
	blink_on();

if(amin1==0)
amin1=60;
Lcd8_decimal2(count1[cur1],amin1);

}

}




else if(count1[cur1]==0xcb)
{
if(inc==0)
{
while(inc==0);
ampm1=0;
	d=0;
display_ampm2();

	blink_on();


//Lcd8_decimal2(count1[cur1],ampm1);
}

else if(dec==0)
{
while(!dec);
ampm1=1;
	d=1;
	display_ampm2();

	blink_on();

//Lcd8_decimal2(count1[cur1],ampm1);

}

}







}


Lcd8_Command(0x0c);
//Delay(65000);Delay(65000);
cur4--;
cur4--;
cur4--;
cur4--;
cur4--;



 EEPROM_WriteByte(memo1[cur4], ahour);
cur4++;
 EEPROM_WriteByte(memo1[cur4], amin);
cur4++;
 EEPROM_WriteByte(memo1[cur4], ampm);
cur4++;
 EEPROM_WriteByte(memo1[cur4], ahour1);
cur4++;
 EEPROM_WriteByte(memo1[cur4], amin1);
 cur4++;
 EEPROM_WriteByte(memo1[cur4], ampm1);

 Lcd8_Command(0x01);	                                       

if(cur4==66)
	
	nxta=1;

}







void alarm_set()
	
{


	no_puls();
	cur2++;
 ahour=EEPROM_ReadByte(memo[cur2]);
  cur2++;
 amin=EEPROM_ReadByte(memo[cur2]);
	cur2++;
	 ampm=EEPROM_ReadByte(memo[cur2]);
	cur2++;

ahour1=EEPROM_ReadByte(memo[cur2]);
	  cur2++;
amin1=EEPROM_ReadByte(memo[cur2]);
		cur2++;
ampm1=EEPROM_ReadByte(memo[cur2]);

Delay(10000);
	blink_on();


	Lcd8_Command(Curser_Off);

	Lcd8_Command(0x0f);
 
  Lcd8_Command(0x01);
	Lcd8_Display(0x88,":",1);
  	Lcd8_Display(0xc8,":",1);
		if(cur2<25)
		{	
						
  
  Lcd8_Display(0x82,"LB)",3);

	Lcd8_decimal2(0x86,ahour);
  Lcd8_decimal2(0x89,amin);
	Lcd8_decimal2(0x8b,ampm);
if(ampm==0)
{
	d=0;
	display_ampm1();

}
if(ampm==1)
{
	d=1;
	display_ampm1();

}
	
  Lcd8_Display(0xc2,"LB)",3);
  Lcd8_decimal2(0xc6,ahour1);
  Lcd8_decimal2(0xc9,amin1);
	Lcd8_decimal2(0xcb,ampm1);

if(ampm1==0)
{
	d=0;
	display_ampm2();

}
if(ampm1==1)
{
	d=1;
	display_ampm2();

}





		}
else
		{	
  Lcd8_Display(0x82,"SB)",3);
  Lcd8_decimal2(0x86,ahour);
  Lcd8_decimal2(0x89,amin);
	Lcd8_decimal2(0x8b,ampm);
if(ampm==0)
{
	d=0;
	display_ampm1();

}
if(ampm==1)
{
	d=1;
	display_ampm1();

}

			
			
  Lcd8_Display(0xc2,"SB)",3);
  Lcd8_decimal2(0xc6,ahour1);
  Lcd8_decimal2(0xc9,amin1);
	Lcd8_decimal2(0xcb,ampm1);

if(ampm1==0)
{
	d=0;
	display_ampm2();

}
if(ampm1==1)
{
	d=1;
	display_ampm2();

}

		}


cur1=0;

while(set)
{
Lcd8_Command(0x0f);
Lcd8_Command(count1[cur1]);
if(mov==0)
{
while(!mov);
cur1++;
if(cur1==6)
cur1=0;
}
else if(count1[cur1]==0x86)			                    ///1st line of lcd
{
if(inc==0)
{
while(inc==0);
ahour++;
	blink_on();

if(ahour>=13)
ahour=0;
Lcd8_decimal2(count1[cur1],ahour);
}

else if(dec==0)
{
while(!dec);
ahour--;
	blink_on();



if(ahour==0)
ahour=12;
Lcd8_decimal2(count1[cur1],ahour);

}

}
else if(count1[cur1]==0x89)
{
if(inc==0)
{
while(inc==0);
amin++;
	blink_on();

if(amin>=60)
amin=0;
Lcd8_decimal2(count1[cur1],amin);
}

else if(dec==0)
{
while(!dec);
amin--;
		blink_on();


if(amin==0)
amin=59;
Lcd8_decimal2(count1[cur1],amin);

}

}



else if(count1[cur1]==0x8b)
{
if(inc==0)
{
while(inc==0);
ampm=0;
	d=0;
display_ampm1();

	blink_on();

if(ampm>1)
ampm=0;
//Lcd8_decimal2(count1[cur1],ampm);
}

else if(dec==0)
{
while(!dec);
ampm=1;
	d=1;
display_ampm1();


		blink_on();


//Lcd8_decimal2(count1[cur1],ampm);

}

}







else if(count1[cur1]==0xc6)
{
if(inc==0)
{
while(inc==0);
ahour1++;
	blink_on();

if(ahour1>=13)
ahour1=0;
Lcd8_decimal2(count1[cur1],ahour1);
}

else if(dec==0)
{
while(!dec);
ahour1--;
	blink_on();


if(ahour1==0)
ahour1=12;
Lcd8_decimal2(count1[cur1],ahour1);

}

}
else if(count1[cur1]==0xc9)
{
if(inc==0)
{
while(inc==0);
amin1++;
	blink_on();

if(amin1>=60)
amin1=0;
Lcd8_decimal2(count1[cur1],amin1);
}

else if(dec==0)
{
while(!dec);
amin1--;
	blink_on();

if(amin1==0)
amin1=60;
Lcd8_decimal2(count1[cur1],amin1);

}

}




else if(count1[cur1]==0xcb)
{
if(inc==0)
{
while(inc==0);
ampm1=0;
	d=0;
display_ampm2();

	blink_on();


//Lcd8_decimal2(count1[cur1],ampm1);
}

else if(dec==0)
{
while(!dec);
ampm1=1;
	d=1;
	display_ampm2();

	blink_on();

//Lcd8_decimal2(count1[cur1],ampm1);

}

}







}


Lcd8_Command(0x0c);
//Delay(65000);Delay(65000);
cur2--;
cur2--;
cur2--;
cur2--;
cur2--;



 EEPROM_WriteByte(memo[cur2], ahour);
cur2++;
 EEPROM_WriteByte(memo[cur2], amin);
cur2++;
 EEPROM_WriteByte(memo[cur2], ampm);
cur2++;
 EEPROM_WriteByte(memo[cur2], ahour1);
cur2++;
 EEPROM_WriteByte(memo[cur2], amin1);
 cur2++;
 EEPROM_WriteByte(memo[cur2], ampm1);

 Lcd8_Command(0x01);	                                       

if(cur2==66)
	
	nxt=1;

}







void alarm_chk1()
{
	for(cur2=1;cur2<25;cur2++)
	{
ahour=EEPROM_ReadByte(memo[cur2]);
		cur2++;
amin=EEPROM_ReadByte(memo[cur2]);
		cur2++;
ampm=EEPROM_ReadByte(memo[cur2]);

		asec=00;
   if(hour==ahour)
	 if(min==amin)
   if(sec==asec)
	 if(clk_ampm==ampm)
alarm_bell_HI();
else
rly=0;
}
}


void alarm_chk2()
{
		for(cur2=25;cur2<67;cur2++)
	{
ahour=EEPROM_ReadByte(memo[cur2]);
		cur2++;
		amin=EEPROM_ReadByte(memo[cur2]);
		cur2++;
		ampm=EEPROM_ReadByte(memo[cur2]);

		asec=00;
   if(hour==ahour)
	 if(min==amin)
   if(sec==asec)
	 if(clk_ampm==ampm)

alarm_bell_LOW();
else
rly=0;
}

	
}




void alarm_chk3()
{
	for(cur4=1;cur4<25;cur4++)
	{
ahour=EEPROM_ReadByte(memo1[cur4]);
		cur4++;
		amin=EEPROM_ReadByte(memo1[cur4]);
				cur4++;
		ampm=EEPROM_ReadByte(memo1[cur4]);

		asec=00;
 if(hour==ahour)
	 if(min==amin)
   if(sec==asec)
	 if(clk_ampm==ampm)

alarm_bell_HI();
else
rly=0;
}
}


void alarm_chk4()
{
		for(cur4=25;cur4<67;cur4++)
	{
ahour=EEPROM_ReadByte(memo1[cur4]);
		cur4++;
		amin=EEPROM_ReadByte(memo1[cur4]);
						cur4++;
		ampm=EEPROM_ReadByte(memo1[cur4]);

		asec=00;
 if(hour==ahour)
	 if(min==amin)
   if(sec==asec)
	 if(clk_ampm==ampm)

alarm_bell_LOW();
else
rly=0;
}

	
}








void alarm_bell_HI()
{
	Lcd8_Command(0x01);

	Lcd8_Display(0xc0,"Bell ringing.. ",14);

rly=1;
  delay_ms(12000);            

rly=0;
	 Delay(65000);
   Delay(65000);

		 Lcd8_Command(0x01);

}

void alarm_bell_LOW()
{
			 Lcd8_Command(0x01);

		Lcd8_Display(0xc0,"Bell ringing.. ",14);

rly=1;
delay_ms(6000);  
	
rly=0;
	
	 Delay(65000);
   Delay(65000);

		 Lcd8_Command(0x01);

}












void Lcd8_Init()
{
Lcd8_Command(0x38); //to select function set
Lcd8_Command(0x06); //entry mode set
Lcd8_Command(0x0c); //display on
Lcd8_Command(0x01); //clear display
}

void Lcd8_Command(unsigned char com)
{
Data_Port=com;
Lcd_en=1;
Lcd_rs=0;
//Lcd_rw=0;

Delay(125);
Lcd_en=0;
Delay(125);
}

void Lcd8_Write(unsigned char com,unsigned char lr)
{
Lcd8_Command(com);
Data_Port=lr; // Data 
Lcd_en=Lcd_rs=1;
//Lcd_rw=0;
Delay(125);
Lcd_en=0;
Delay(125);
}

void Lcd8_Display(unsigned char com,const unsigned char *word,unsigned int n)
{
unsigned char Lcd_i;
for(Lcd_i=0;Lcd_i<n;Lcd_i++)
{ 
Lcd8_Write(com+Lcd_i,word[Lcd_i]);
}
}

void Lcd8_decimal2(unsigned char com,unsigned char val)
{
unsigned int Lcd_hr,Lcd_t,Lcd_o;

Lcd_hr=val%100;
Lcd_t=Lcd_hr/10;
Lcd_o=Lcd_hr%10;

Lcd8_Write(com,Lcd_t+0x30);
Lcd8_Write(com+1,Lcd_o+0x30);
}



void Delay(unsigned int del)
{
while(del--);
}



void Seg8_decimal2(min)

{
unsigned int Lcd_hr,Lcd_t,Lcd_o;

Lcd_hr=min%100;
Lcd_t=Lcd_hr/10;
Lcd_o=Lcd_hr%10;
led_data=Lcd_t+0x30;
	display_digit();

	digi3=1;
		 delay_ms(5);
digi3=0;

led_data=Lcd_o+0x30;
display_digit();

	digi4=1;
		 delay_ms(5);
digi4=0;

}







void Led8decimal2(hour)
{
unsigned int Lcd_hr,Lcd_t,Lcd_o;

Lcd_hr=hour%100;
Lcd_t=Lcd_hr/10;
Lcd_o=Lcd_hr%10;

led_data=Lcd_t+0x30;
	
	display_digit();

digi1=1;
		 delay_ms(5);
digi1=0;

led_data=Lcd_o+0x30;
	display_digit();

	digi2=1;
		 delay_ms(5);
digi2=0;


}



display_digit()
{

if(led_data==0x30)
P3=0x40;
//else
if(led_data==0x31)
P3=0xf9;
//else

if(led_data==0x32)
P3=0x24;
//else

if(led_data==0x33)
P3=0x30;
//else

if(led_data==0x34)
P3=0x99;
//else

if(led_data==0x35)
P3=0x92;
//else

if(led_data==0x36)
P3=0x82;
//else

if(led_data==0x37)
P3=0xf8;
//else

if(led_data==0x38)
P3=0x00;
//else

if(led_data==0x39)
P3=0x90;
	
	
}




/*********************************LCD HEADER FILE END*************************************/



/*********************************RTC HEADER FILE START*********************************/


DS1307_get(unsigned char addr)
{
unsigned char ret;

Rtc_Start(); 
ret = Rtc_Read(addr);
Rtc_Stop();

return ret;
}

void DS1307_settime(unsigned char hh, unsigned char mm, unsigned char ss)
{
Rtc_Start(); 

Rtc_Write(0x00,ss); /* Write sec on RAM address 00H */
Rtc_Write(0x01,mm); /* Write min on RAM address 01H */
Rtc_Write(0x02,hh); /* Write hour on RAM address 02H */
	hh=hh& 0x1f;

Rtc_Stop(); /* Stop i2c bus */

}


void DS1307_setdate(unsigned char dd, unsigned char mm, unsigned char yy,unsigned char day)
{
Rtc_Start();

Rtc_Write(0x04,dd); /* Write date on RAM address 04H */
Rtc_Write(0x05,mm); /* Write month on RAM address 05H */
Rtc_Write(0x06,yy); /* Write year on RAM address 06H */
Rtc_Write(0x03,day); /* Write year on RAM address 03H */


Rtc_Stop(); /* Stop i2c bus */
}


Send2lcd(unsigned char value)
{
unsigned char buf1,buf2,buf = 0;

buf1 = value & 0xF0; /* Filter for high byte */
buf1 = (buf1>>4); /* Convert to ascii code */
buf2 = value & 0x0F; /* Filter for low byte */
buf=(buf1*10)+buf2;

return buf;
}


void Rtc_Init()//lower order 256 bytes of the chip
{
Rtc_add_wr=0xd0;
Rtc_add_rd=0xd1;
}


void Rtc_Write(unsigned char zig,unsigned char zag)// program to write to EEPROM
{
dat_rtc=zig;
temp_rtc=zag;
Rtc_rd_wr_sub();
above:
d_rtc=temp_rtc;
Rtc_Tx();
if (CY==1)goto above;
CY=0;
Rtc_Stop();
}


Rtc_Read(unsigned char zig)// program to read from EEPROM
{
dat_rtc=zig;
Rtc_rd_wr_sub();
Rtc_Start();
be:
d_rtc=Rtc_add_rd; // 0xd1 =Rtc_add_rd
Rtc_Tx();
if(CY==1)goto be;
Rtc_Rx();
Rtc_Ack();
CY=0;
Rtc_Stop();
return(datain_rtc);
}


void Rtc_Start()// must for any operation on EEPROM
{
sda_rtc=1;
scl_rtc=1;
sda_rtc=0;
scl_rtc=0;
}


void Rtc_Stop()// this is similar to the START operation whereas this should be performed after the completion of any operation
{
sda_rtc=0;
scl_rtc=1;
sda_rtc=1;
}


void Rtc_Tx()// program to send the device address, read/write address,data to be written
{
signed char i_rtc;
for(i_rtc=7;i_rtc>=0;i_rtc--)// should necessarily be initialised as signed char.
{
CY=(d_rtc>>i_rtc)&0x01;
sda_rtc=CY;
scl_rtc=1;// clock is essential inorder to write or read
scl_rtc=0;// clk should be alternated
}
sda_rtc=1;
scl_rtc=1;
CY=sda_rtc;
scl_rtc=0;
}


void Rtc_Rx()// program read the data from the EEPROM
{
unsigned char l_rtc;
sda_rtc=1;
for (l_rtc=0;l_rtc<=7;l_rtc++)
{
scl_rtc=1;
in_rtc=in_rtc<<1;
in_rtc|=sda_rtc;
scl_rtc=0;
}
datain_rtc=in_rtc;
in_rtc=0;
}


void Rtc_Ack()// this is to intimate the EEPROM that the read operation is over
{
sda_rtc=1;
scl_rtc=1;
scl_rtc=0;
}


void Rtc_rd_wr_sub()// this routine will be used by both the read & write operations to send the device address & the address at which the corresponding action is to be taken
{
Rtc_Start();
here1:
d_rtc=Rtc_add_wr;// 0xd0 device address is passed
Rtc_Tx();
if(CY==1)goto here1;
again1:
d_rtc=dat_rtc;// the address from which data is to be read/written is to be passed
Rtc_Tx();
if(CY==1)goto again1;
}

void EEPROM_WriteByte(unsigned char eeprom_Address, unsigned char eeprom_Data)
{
 
    I2C_Start();               // Start i2c communication
    I2C_Write(EEPROM_ID);      // connect to AT2404 by sending its ID on I2c Bus
    I2C_Ack();
    I2C_Write(eeprom_Address); // Select the Specified EEPROM address of AT2404
    I2C_Ack();
    I2C_Write(eeprom_Data);    // Write the data at specified address
    I2C_Ack();
    I2C_Stop();                // Stop i2c communication after Writing the data
    delay_ms(1);            // Write operation takes max 5ms, refer At2404 datasheet        
}
 
unsigned char EEPROM_ReadByte(unsigned char eeprom_Address)
{
  unsigned char eeprom_Data;
 
    I2C_Start();               // Start i2c communication
    I2C_Write(EEPROM_ID);      // connect to AT2404(write) by sending its ID on I2c Bus
    I2C_Ack();
    I2C_Write(eeprom_Address); // Select the Specified EEPROM address of AT2404
    I2C_Ack();
 
    I2C_Start();               // Start i2c communication
    I2C_Write(0xA1);           // connect to AT2404(read) by sending its ID on I2c Bus
    I2C_Ack();
   eeprom_Data = I2C_Read();  // Read the data from specified address
    I2C_NoAck();
    I2C_Stop();                // Stop i2c communication after Reading the data
    delay_us(10);
  return eeprom_Data;          // Return the Read data
 
}
void I2C_Start()
{
    SCL = 0;        // Pull SCL low
 
    SDA = 1;        // Pull SDA High
    delay_us(1);
 
    SCL = 1;        //Pull SCL high
    delay_us(1);
 
    SDA = 0;        //Now Pull SDA LOW, to generate the Start Condition
    delay_us(1);
 
    SCL = 0;        //Finally Clear the SCL to complete the cycle
}

void I2C_Stop(void)
{
    SCL = 0;            // Pull SCL low
    delay_us(1);
 
    SDA = 0;            // Pull SDA  low
    delay_us(1);
 
    SCL = 1;            // Pull SCL High
    delay_us(1);
 
    SDA = 1;            // Now Pull SDA High, to generate the Stop Condition
}

void I2C_Ack()
{
    SDA = 0;        //Pull SDA low to indicate Positive ACK
    I2C_Clock();    //Generate the Clock
    SDA = 1;        // Pull SDA back to High(IDLE state)
}

void I2C_NoAck()
{
    SDA = 1;        //Pull SDA high to indicate Negative/NO ACK
   I2C_Clock();     // Generate the Clock  
    SCL = 1;        // Set SCL */
}

void I2C_Write(unsigned char dat)
{
    unsigned char i;
 
    for(i=0;i<8;i++)         // loop 8 times to send 1-byte of data
     {
        SDA = dat & 0x80;    // Send Bit by Bit on SDA line
        I2C_Clock();         // Generate Clock at SCL
        dat = dat<<1;
      }
        SDA = 1;                 // Set SDA at last
}
 

unsigned char I2C_Read(void)
{
    unsigned char i, dat=0x00;
 
       SDA=1;               //Make SDA as I/P
    for(i=0;i<8;i++)        // loop 8times to read 1-byte of data
     {
       delay_us(1);
        SCL = 1;            // Pull SCL High
        delay_us(1);
 
        dat = dat<<1;       //dat is Shifted each time and
        dat = dat | SDA;    //ORed with the received bit to pack into byte
 
        SCL = 0;            // Clear SCL to complete the Clock
       }
   return dat;               // Finally return the received Byte*
}
 
 void delay_ms(unsigned int ms_count)
 {
        while(ms_count!=0)
         {
            delay_us(112);   //delay_us is called to generate 1ms delay
             ms_count--;
         }
   }
 

  void delay_us(unsigned int us_count)
 {  
    while(us_count!=0)
      {
         us_count--;
       }												   
   }
 
 void I2C_Clock(void)
{
    delay_us(1);
    SCL = 1;        // Wait for Some time and Pull the SCL line High
 
    delay_us(1);        // Wait for Some time
    SCL = 0;        // Pull back the SCL line low to Generate a clock pulse
}

video shoot of proteus simulator
but same problen seen on actual hardware.
 

It is because of using delays in the main loop where the 7 Segment display codes are also called. It is time for you to learn how to multiplex 7 Segment display using Timer interrupt in the interrupt service routine (ISR).
 

I’m not about to analyze 5000 lines of code, but it looks like your problem is that it multiplexes the 4 digits properly and then blanks them for a long period. It might be that you’ve got a routine that sends the data to the display which gets called occasionally, then the software goes off and does something else for a long time before it does another display routine.
 

I think that should win a prize for the most convoluted and un-commented code ever written to do a simple task.
When writing a program like that you should first look at the individual tasks it has to perform:
1. update an LED display
2. update an LCD display
3. read an RTC IC
4. read a keypad

then prioritize them - only the LED needs constant updating, everything else has latched inputs so use a timer interrupt to keep the multiplexing running at a constant rate.
Write a routine to do each of those tasks, bearing in mind that the timer ISR will do much of the LED updating.

I would estimate you could fix the problem and reduce the code down to 25% of its present size if you re-write it that way.

Brian.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top