having problem with digital clock ds1307 and pic16f877a

Status
Not open for further replies.

zzssww007

Newbie level 5
Joined
Oct 9, 2012
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,324
View attachment new 2.txt

this two is my circuit and my code for mikroc, and it can not be displayed on my lcd, and i dont know the problem, i really hope who can point out my mistakes.
thanks for ur timme
 

yes, i'm simulating in proteus, but the display is like this pic attached.
 

ok, here it is. i saw that, but i think it is a little different from mine.

- - - Updated - - -

i attached the zip file ,can u see it
 

Attachments

  • test clk new circuit isis.zip
    39.1 KB · Views: 129

did u see my code? do u know the problem? i still can not figure it out
 

What is you problem? Are you testing on real hardware? I used your code, bur I just put the I2C debugger in proteus. You have to connect VBAT pin of ds1307 to + 3V.
 
Last edited:

bro, how to insert the I2C debugger?
 

Check this video.
 

Attachments

  • i2c.rar
    238.8 KB · Views: 85

thanks for helping me, but i still can not change the time, is that means my code is wrong at somewhere??
 

Check this code and attachment.

Code:
// LCD module connections-------------------------------------------------------------------------------------
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections---------------------------------------------------------------------------------

//------------------------------Declare Variables section--------------------------------------------------------
unsigned short read_ds1307(unsigned short address);
void write_ds1307(unsigned short address,unsigned short w_data);
unsigned char BCD2UpperCh(unsigned char bcd);
unsigned char BCD2LowerCh(unsigned char bcd);

unsigned short sec;
unsigned short minute;
unsigned short hour;
unsigned short day;
unsigned short date;
unsigned short month;
unsigned short year;
unsigned short dataa;
unsigned short a=0;
unsigned short b=0;
unsigned short yr=0;
unsigned short hour1;
char ddate[11];
char time[15];
char time1[15];
char ampm;
unsigned char dday;
unsigned char dhour;


//------------------------------End of Declare Variables section--------------------------------------------------------

unsigned short read_ds1307(unsigned short address)
{
I2C1_Start();
I2C1_Wr(0xD0); //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
I2C1_Wr(address);
I2C1_Repeated_Start();
I2C1_Wr(0xD1); //0x68 followed by 1 --> 0xD1
dataa=I2C1_Rd(0);
I2C1_Stop();
return(dataa);
}

unsigned char BCD2UpperCh(unsigned char bcd)
{
return ((bcd >> 4) + '0');
}

unsigned char BCD2LowerCh(unsigned char bcd)
{
return ((bcd & 0x0F) + '0');
}

void write_ds1307(unsigned short address,unsigned short w_data)
{
I2C1_Start(); // issue I2C start signal
//address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
I2C1_Wr(0xD0); // send byte via I2C (device address + W)
I2C1_Wr(address); // send byte (address of DS1307 location)
I2C1_Wr(w_data); // send data (data to be written)
I2C1_Stop(); // issue I2C stop signal
}


void main(){
//------------------------------Start of Initialization Section-------------------------------------------------------------
I2C1_Init(100000); //DS1307 I2C is running at 100KHz
TRISB = 0x00; // Configure PORTB as output
//TRISC = 0xFF;
TRISD = 0xFF;
PORTD=0x00;
Lcd_Init();
Lcd_Cmd(_Lcd_CLEAR); // Clear display
Lcd_Cmd(_Lcd_CURSOR_OFF); // Turn cursor off
Lcd_Out(1,1,"Dt:");
Lcd_Out(2,1,"Time:");

//Set Time
write_ds1307(0,0x80); //Reset second to 0 sec. and stop Oscillator
write_ds1307(1,0x59); //write min 59
write_ds1307(2,0x11); //write hour 11 PM
write_ds1307(3,0x04); //write day
write_ds1307(4,0x15); // write date 15
write_ds1307(5,0x06); // write month June
write_ds1307(6,0x12); // write year 12 --> 2012
write_ds1307(7,0x10); //SQWE output at 1 Hz
write_ds1307(0,0x00); //Reset second to 0 sec. and start Oscillator
//------------------------------End of Initialization Section---------------------------------------------------------

while(1)
{
sec=read_ds1307(0); // read second
minute=read_ds1307(1); // read minute
hour=read_ds1307(2); // read hour
day=read_ds1307(3); // read day
date=read_ds1307(4); // read date
month=read_ds1307(5); // read month
year=read_ds1307(6); // read year

a=BCD2UpperCh(year);
b=BCD2LowerCh(year);
yr=a*10+b;
hour1 =hour;

//------------------------------Start of Setting Clock Time Section-------------------------------------------------------

if (PORTD.F0==1 && PORTD.F6==0)
{
   if (PORTD.F1==1){
   minute=minute+1;
   if (minute==0x0A||minute==0x1A||minute==0x2A||minute==0x3A||minute==0x4A){
   minute=minute+6;}
   else if (minute==0x5A){
   minute=0x00;} }

   else if (PORTD.F2==1)
   {
   hour=hour+1;
   if (hour==0x0A||hour==0x1A||hour==0x2A){
   hour=hour+6;}
   else if (hour==0x24){
   hour=0x00;} }

   else if (PORTD.F3==1) {
   date=date+1;
   day=day+1;
   if (month==0x01||month==0x03||month==0x05||month==0x07||month==0x08||month==0x10||month==0x12){
   if (date==0x0A||date==0x1A||date==0x2A){
   date=date+6;}
   else if (date==0x32){
   date=0x01;}

   if(day==8)
   day=1;}

   else if (month==0x04||month==0x06||month==0x09||month==0x11){
   if (date==0x0A||date==0x1A||date==0x2A){
   date=date+6;}
   else if (date==0x31){
   date=0x01;}

   if(day==8)
   day=1;}

   else if (month==0x02){
   if (yr%4==0){
   if (date==0x0A||date==0x1A){
   date=date+6;}
   else if (date==0x2A){
   date=0x01;}

   if(day==8)
   day=1;}

   else if (yr%4==3||yr%4==0||yr%4==1){
   if (date==0x0A||date==0x1A){
   date=date+6;}
   else if (date==0x29){
   date=0x01;}

   if(day==8)
   day=1;}
   }
   }

   else if (PORTD.F4==1) {
   month=month+1;
   if (month==0x0A||month==0x1A){
   month=month+6;}
   else if (month==0x13){
   month=0x01;}
   }



   else if (PORTD.F5==1) {
   year=year+1;
   if (year==0x0A||year==0x1A||year==0x2A||year==0x3A||year==0x4A||year==0x5A||year==0x6A||year==0x7A||year==0x8A){
   year=year+6;}
   else if (year==0x9A){
   year=0x11;}
   }

   else if (PORTD.F7 = 1){
   day = day + 1 ;
   if (day==0x8){
   year=0x01;}
   }

   Delay_ms(100);



write_ds1307(0,0x80);
write_ds1307(1, minute);
write_ds1307(2, hour);
write_ds1307(3, day);
write_ds1307(4, date);
write_ds1307(5, month);
write_ds1307(6, year);
write_ds1307(0,0x00);



dday = BCD2LowerCh(day);
if (dday == '1') {
Lcd_Out(1,4,"Sun");
}
else if (dday == '2') {
Lcd_Out(1,4,"Mon");
}
else if (dday == '3') {
Lcd_Out(1,4,"Tue");
}
else if (dday == '4') {
Lcd_Out(1,4,"Wed");
}
else if (dday == '5') {
Lcd_Out(1,4,"Thu");
}
else if (dday == '6') {
Lcd_Out(1,4,"Fri");
}
else if (dday == '7') {
Lcd_Out(1,4,"Sat");
}

ddate[0] = BCD2UpperCh(date);
ddate[1] = BCD2LowerCh(date);
ddate[2] = '/';
ddate[3] = BCD2UpperCh(month);
ddate[4] = BCD2LowerCh(month);
ddate[5] = '/';
ddate[6] = '2';
ddate[7] = '0';
ddate[8] = BCD2UpperCh(year);
ddate[9] = BCD2LowerCh(year);
ddate[10] = '\0';


time[0] = BCD2UpperCh(hour);
time[1] = BCD2LowerCh(hour);
time[2] = ':';
time[3] = BCD2UpperCh(minute);
time[4] = BCD2LowerCh(minute);
time[5] = ':';
time[6] = BCD2UpperCh(sec);
time[7] = BCD2LowerCh(sec);
time[8] = ' ';
time[9] = ' ';
time[10] = ' ';
time[11] = ' ';
time[12] = '\0';

Lcd_Out(1,7,ddate);
Lcd_Out(2,7,time);
Delay_ms(50);






   }
else if (PORTD.F0==0 && PORTD.F6==0)
{

dday = BCD2LowerCh(day);
if (dday == '1') {
Lcd_Out(1,4,"Sun");
}
else if (dday == '2') {
Lcd_Out(1,4,"Mon");
}
else if (dday == '3') {
Lcd_Out(1,4,"Tue");
}
else if (dday == '4') {
Lcd_Out(1,4,"Wed");
}
else if (dday == '5') {
Lcd_Out(1,4,"Thu");
}
else if (dday == '6') {
Lcd_Out(1,4,"Fri");
}
else if (dday == '7') {
Lcd_Out(1,4,"Sat");
}

ddate[0] = BCD2UpperCh(date);
ddate[1] = BCD2LowerCh(date);
ddate[2] = '/';
ddate[3] = BCD2UpperCh(month);
ddate[4] = BCD2LowerCh(month);
ddate[5] = '/';
ddate[6] = '2';
ddate[7] = '0';
ddate[8] = BCD2UpperCh(year);
ddate[9] = BCD2LowerCh(year);
ddate[10] = '\0';

time[0] = BCD2UpperCh(hour);
time[1] = BCD2LowerCh(hour);
time[2] = ':';
time[3] = BCD2UpperCh(minute);
time[4] = BCD2LowerCh(minute);
time[5] = ':';
time[6] = BCD2UpperCh(sec);
time[7] = BCD2LowerCh(sec);
time[8] = ' ';
time[9] = ' ';
time[10] = ' ';
time[11] = ' ';
time[12] = '\0';

Lcd_Out(1,7,ddate);
Lcd_Out(2,7,time);
Delay_ms(50);
}

//------------------------------End of Setting Clock Time Section-------------------------------------------------------
//------------------------------Start of Display Time in 12 hr system Section------------------------------------------
else if (PORTD.F0==0 && PORTD.F6==1)
{

dday = BCD2LowerCh(day);
if (dday == '1') {
Lcd_Out(1,4,"Sun");
}
else if (dday == '2') {
Lcd_Out(1,4,"Mon");
}
else if (dday == '3') {
Lcd_Out(1,4,"Tue");
}
else if (dday == '4') {
Lcd_Out(1,4,"Wed");
}
else if (dday == '5') {
Lcd_Out(1,4,"Thu");
}
else if (dday == '6') {
Lcd_Out(1,4,"Fri");
}
else if (dday == '7') {
Lcd_Out(1,4,"Sat");
}

ddate[0] = BCD2UpperCh(date);
ddate[1] = BCD2LowerCh(date);
ddate[2] = '/';
ddate[3] = BCD2UpperCh(month);
ddate[4] = BCD2LowerCh(month);
ddate[5] = '/';
ddate[6] = '2';
ddate[7] = '0';
ddate[8] = BCD2UpperCh(year);
ddate[9] = BCD2LowerCh(year);
ddate[10] = '\0';

if (hour1<0x12){
if (hour1==0){
hour1=0x12;
ampm= 'A';

}
else
ampm= 'A';
}

else if (hour1 == 0x12){
ampm= 'P';
}

else if (hour1>0x12 && hour1<0x20){
hour1=hour1-0x12;
ampm= 'P';
}

else if (hour1==0x20){
hour1=0x08;
ampm= 'P';
}

else if (hour1==0x21){
hour1=0x09;
ampm= 'P';
}

else if (hour1==0x22){
ampm= 'P';
hour1=0x10;
}

else if (hour1==0x23){
ampm= 'P';
hour1=0x11;
}

time1[0] = BCD2UpperCh(hour1);
time1[1] = BCD2LowerCh(hour1);
time1[2] = ':';
time1[3] = BCD2UpperCh(minute);
time1[4] = BCD2LowerCh(minute);
time1[5] = ':';
time1[6] = BCD2UpperCh(sec);
time1[7] = BCD2LowerCh(sec);
time1[8] = ' ';
time1[9] = ampm;
time1[10] = 'M';
time1[11] = '\0';

Lcd_Out(1,5,ddate);
Lcd_Out(2,1,time1);
Delay_ms(50);


hour1=hour;

}
}
//------------------------------Start of Display Time in 12 hr system Section------------------------------------------

}

I just changed the line
Code:
 write_ds1307(6,0x11); // write year 11 --> 2011

to

Code:
 write_ds1307(6,0x12); // write year 12 --> 2012

This is a part of your code in the while(1) loop

Code:
if (PORTD.F0==1 && PORTD.F6==0)
{
   if (PORTD.F1==1){
   minute=minute+1;
   if (minute==0x0A||minute==0x1A||minute==0x2A||minute==0x3A||minute==0x4A){
   minute=minute+6;}
   else if (minute==0x5A){
   minute=0x00;} }

   else if (PORTD.F2==1)
   {
   hour=hour+1;
   if (hour==0x0A||hour==0x1A||hour==0x2A){
   hour=hour+6;}
   else if (hour==0x24){
   hour=0x00;} }

   else if (PORTD.F3==1)

It checks PORTD.F0 == 1 and PORTD.F6 == 0 If that becomes true then it checks if PORTD.F1 == 1 or else PORTD.F2 == 1 or else PORTD.F3 == 1

Let us assume that you press and make PORTD.F0 == 1 and PORTD.F6 will be 0.

Then when you press PORTD.F1 or PORTD.F2 or PORTD.F3 you will have released PORTD.F0. So, PORTD.F0 will be 0 , then how does the conditions PORTD.F1 == 1, PORTD.F2 == 1, PORTD.F3 == 1 work. If that has to work PORTD.F0 should be 1.

Assuming you have used momentary switches and not push to on switch. Can you tell me the function of each button?
 

Attachments

  • zzssww007 v2.rar
    88.4 KB · Views: 88
Last edited:

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…