rtc problem pls help

Thread Starter

Gopi Vh

Joined Jun 13, 2015
1
in the program below every thing is working fine including settings .the problem i am facing is when i am entering into setting of time rtc is stopped there and once i come back from setting its getting on. for eg if time is 12:15:42 and i am going to setting menu and i ll be there for say 2 mins and once i exit from that without changing any time den rtc ll start again from 12:15:43. if there any problm in code help me.sorry i could not attach file so i am putting my code below


Rich (BB code):
// Lcd module connections
sbit LCD_RS at LATB5_bit;
sbit LCD_EN at LATB4_bit;
sbit LCD_D4 at LATB3_bit;
sbit LCD_D5 at LATB2_bit;
sbit LCD_D6 at LATB1_bit;
sbit LCD_D7 at LATB0_bit;

sbit LCD_RS_Direction at TRISB5_bit;
sbit LCD_EN_Direction at TRISB4_bit;
sbit LCD_D4_Direction at TRISB3_bit;
sbit LCD_D5_Direction at TRISB2_bit;
sbit LCD_D6_Direction at TRISB1_bit;
sbit LCD_D7_Direction at TRISB0_bit;
//End LCD connection*/
sbit Soft_I2C_Scl at RC3_bit;
sbit Soft_I2C_Sda at RC4_bit;
sbit Soft_I2C_Scl_Direction at TRISC3_bit;
sbit Soft_I2C_Sda_Direction at TRISC4_bit;
sbit BUZZER at RB6_bit;
char timeset(); //time
char dateset();//date
char dayset();//day
char hourset();//hour
char minset();//min
char secset();//sec
char dates1();//date
char months1();//month
char years();//year
unsigned char BCD2LowerCh(unsigned char bcd);
unsigned char BCD2UpperCh(unsigned char bcd);
unsigned short read_ds1307(unsigned short address );
void write_ds1307(unsigned short address,unsigned short w_data);
unsigned char temp1, temp ,rr, sec, minute,hour,day, month,year,date,dates11,dater11,mon11,yr11,hour_11,min_11,sec_11,dater_11,mon_11,yr_11,y, sec_1, minute_1,hour_1,day_1, month_1,year_1,date_1;
unsigned char dates, months, hours, minutes,days,b,up,timer11,hour11,time11,min11,sec11,aa1,aa2,aa3,bb,cc,x,sec1,min1,hour1,day1,date1,month1,year1;

char * text  = "0";
char * text0 = "00";
char * text1 = "00";
char * text2 = "00";
char * text3 = "00";
char * text4 = "0";
char * text5 = "00";
char days1[] = "SUN";
char days2[] = "MON";
char days3[] = "TUE";
char days4[] = "WED";
char days5[] = "THU";
char days6[] = "FRI";
char days7[] = "SAT";

char timer[9];
char ddater[11];
char pus[3];
sbit set  at RA0_bit;   //buttons
sbit inc at RA1_bit;
sbit dec at RA2_bit;
sbit set1 at RA3_bit;



void main()
{
  x=1;
  TRISB.F6=0;
  TRISA.F0=1;
  TRISA.F1=1;
  TRISA.F2=1;
  TRISA.F3=1;
  ADRESL  = 0;
  ADRESH = 0;  // Configure AN pins as digital I/O
  ADCON0=0;
  ADCON1 = 0x0F;
  ADCON2 =0;
  CMCON =7;
  PORTA =0;
  PORTB =0;
  PORTC =0;
  BUZZER =0;

  /*OSCCON  = 0x70;*/
  //TRISA= 0;
  TRISC=0xFF;
  Lcd_Init();  // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);  // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);  // Cursor off
  Soft_I2C_Start();

  do{
  for(rr=0;rr<=2;rr++)
  {
  Lcd_Cmd(_LCD_CLEAR);  // Clear LCD
  Lcd_Cmd(_LCD_CURSOR_OFF);
  Lcd_Out(1, 1, "TIME:");
  Lcd_Out(2, 1, "DATE:");
  sec=read_ds1307(0X00); // read second
  minute=read_ds1307(0X01); // read minute
  hour=read_ds1307(0X02); // read hour
  day=read_ds1307(0X03); // read day
  date=read_ds1307(0X04); // read date
  month=read_ds1307(0X05); // read month
  year=read_ds1307(0X06); // read year

  sec1=sec;
  min1=minute;
  hour1=hour;
  day1=day;
  date1=date;
  month1=month;
  year1=year;
  write_ds1307(0x26,sec);
  write_ds1307(0x27,minute);
  write_ds1307(0x28,hour);
  write_ds1307(0x29,day);
  write_ds1307(0x30,date);
  write_ds1307(0x2A,month);
  write_ds1307(0x2B,year);

  timer[0] = BCD2UpperCh(hour);
  timer[1] = BCD2LowerCh(hour);
  timer[2] = ':';
  timer[3] = BCD2UpperCh(minute);
  timer[4] = BCD2LowerCh(minute);
  timer[5] = ':';
  timer[6] = BCD2UpperCh(sec);
  timer[7] = BCD2LowerCh(sec);
  timer[8] ='\0';

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

  Lcd_Out(1,6,timer);
  Lcd_Out(2,6,ddater);

  if(day ==1)
  Lcd_Out(1,14,days1);
  if(day ==2)
  Lcd_Out(1,14,days2);
  if(day ==3)
  Lcd_Out(1,14,days3);
  if(day ==4)
  Lcd_Out(1,14,days4);
  if(day ==5)
  Lcd_Out(1,14,days5);
  if(day ==6)
  Lcd_Out(1,14,days6);
  if(day ==7)
  Lcd_Out(1,14,days7);
  delay_ms(1000);
  }
//setting part


  if(set==0)
  {
  b=1;
  do
  {
  up=0;
  Lcd_Cmd(_LCD_CLEAR);
  lcd_out(1,1, "1:time 2:date");  delay_ms(500);
  lcd_out(2,1, "3:day use sw2");
  delay_ms(500);
  if(inc ==0)
  {  up=1;
  for(up=1;up<5;)
  {
  Lcd_Cmd(_LCD_CLEAR);
  switch(up)
  {
  case 1:  lcd_out(1,1, "1:time ");
  delay_ms(500);
  if(set == 0)
  {
  x = timeset();
  up=4;
  }
  break;
  case 2:  lcd_out(1,1, "2:date ");
  delay_ms(500);
  if(set == 0)
  {
  x=dateset();  up=4;
  }
  break;
  case 3:  lcd_out(1,1, "3:day ");
  delay_ms(500);
  if(set == 0)
  {
  x=dayset();  up=4;
  }
  break;
  case 4:
  if(x==0)
  {
  sec_1=read_ds1307(0X00); // read second
  minute_1=read_ds1307(0X23); // read minute
  hour_1=read_ds1307(0X22); // read hour
  day_1=read_ds1307(0X25); // read day
  date_1=read_ds1307(0X21); // read date
  month_1=read_ds1307(0X20); // read month
  year_1=read_ds1307(0X24); // read year
  }
  else
  {
  sec_1=read_ds1307(0X26); // read second
  minute_1=read_ds1307(0X27); // read minute
  hour_1=read_ds1307(0X28); // read hour
  day_1=read_ds1307(0X29); // read day
  date_1=read_ds1307(0X30); // read date
  month_1=read_ds1307(0X2A); // read month
  year_1=read_ds1307(0X2B); // read year
  }
  write_ds1307(0x00,sec_1);
  write_ds1307(0x01,minute_1);
  write_ds1307(0x02,hour_1);
  write_ds1307(0x03,day_1);
  write_ds1307(0x04,date_1);
  write_ds1307(0x05,month_1);
  write_ds1307(0x06,year_1);
  up=5;
  break;
  }
  if(inc == 0)
  up++;

  b=0;
  }
  }

  }while(b == 1);
  }

}while(1);  //

}
unsigned short read_ds1307(unsigned short address )
{
int dat;
soft_I2C_Start();
soft_I2C_Write(0xd0); //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
soft_I2C_Write(address);
soft_I2C_Start();
soft_I2C_Write(0xd1); //0x68 followed by 1 --> 0xD1
dat=soft_I2C_Read(0);
soft_I2C_Stop();
return(dat);
}

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)
{
soft_I2C_Start(); // issue I2C start signal
//address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
soft_I2C_Write(0xD0); // send byte via I2C (device address + W)
soft_I2C_Write(address); // send byte (address of DS1307 location)
soft_I2C_Write(w_data); // send data (data to be written)
soft_I2C_Stop(); // issue I2C stop signal
}
char timeset()
{
  Lcd_Cmd(_LCD_CLEAR);

  lcd_out(1,1,"1:hour2:min3:sec");
  delay_ms(500);
  lcd_out(2,1,"use sw4");
  delay_ms(500);
  timer11=0;
  for(timer11=0;timer11<4;)
  {
  if(set1 ==0)
  timer11++;

  switch(timer11)
  {
  case 1:  Lcd_Cmd(_LCD_CLEAR);
  lcd_out(1,1, "1:hour ");
  delay_ms(500);
  if(set ==0)
  {
  hour_11=hourset();
  write_ds1307(0x22,Dec2Bcd(hour_11));  return 0;
  }
  break;
  case 2:  Lcd_Cmd(_LCD_CLEAR);
  lcd_out(1,1, "2:min ");
  delay_ms(500);
  if(set ==0)
  {
  min_11=minset();
  write_ds1307(0x23,Dec2Bcd(min_11));  return 0;
  }
  break;

  case 3:  Lcd_Cmd(_LCD_CLEAR);
  lcd_out(1,1, "3:sec");
  delay_ms(500);
  if(set ==0)
  {
  sec_11= secset();  write_ds1307(0x21,Dec2Bcd(sec_11));
  return 0;
  }
  break;
  }

}
return 1;
}
char dateset()
{
  Lcd_Cmd(_LCD_CLEAR);

  lcd_out(1,1,"1:date2:mon3:yr");
  delay_ms(500);
  lcd_out(2,1,"use sw4");
  delay_ms(500);
  timer11=0;
  for(timer11=0;timer11<4;)
  {
  if(set1 ==0)
  timer11++;

  switch(timer11)
  {
  case 1:  Lcd_Cmd(_LCD_CLEAR);
  lcd_out(1,1, "1:date ");
  delay_ms(500);
  if(set ==0)
  {
  dater_11= dates1();
  write_ds1307(0x21,Dec2Bcd(dater_11));  return 0;
  }
  break;
  case 2:  Lcd_Cmd(_LCD_CLEAR);
  lcd_out(1,1, "2:mon ");
  delay_ms(500);
  if(set ==0)
  {
  mon_11 = months1();
  write_ds1307(0x20,Dec2Bcd(mon_11));return 0;  }
  break;

  case 3:  Lcd_Cmd(_LCD_CLEAR);
  lcd_out(1,1, "3:yr ");
  delay_ms(500);
  if(set ==0)
  {
  yr_11= years();  write_ds1307(0x24,Dec2Bcd(yr_11));
  return 0;
  }
  break;
  }

}
return 1;
}
char dayset()
{
unsigned char day_11;
day_11=0;
day_11=day1;
  Lcd_Cmd(_LCD_CLEAR);
  lcd_out(1,1," inc sw2  dec sw3"); delay_ms(500);
  for(;day_11<=7;day_11)
  {

  if(inc  == 0)
  day_11++;
  if(dec ==0)
  {
  day_11--;
  }
  switch(day_11)
  {
  case 1: lcd_out(1,1,"sun");  delay_ms(500);
  break;
  case 2: lcd_out(1,1,"mon");delay_ms(500);
  break;
  case 3: lcd_out(1,1,"tue"); delay_ms(500);
  break;
  case 4: lcd_out(1,1,"wed");  delay_ms(500);
  break;
  case 5: lcd_out(1,1,"thu"); delay_ms(500);
  break;
  case 6: lcd_out(1,1,"fri"); delay_ms(500);
  break;
  case 7: lcd_out(1,1,"sat"); delay_ms(500);
  break;
  }
  if(set == 0)
  {
  write_ds1307(0x25,Dec2Bcd(day_11));
  return 0;
  }
  }
  return 1;
}
char hourset()
{  Lcd_Cmd(_LCD_CLEAR);  lcd_out(1,1," inc sw2  dec sw3");
  delay_ms(500);
  hour11=0;
  hour11=hour1;
for(;hour11<25;)
  {

  if(inc  == 0)
  hour11++;
  if(dec ==0)
  hour11--;
  pus[0]= hour11/10+48;
  pus[1]= hour11%10+48;
  pus[2]='\0';
  Lcd_Cmd(_LCD_CLEAR);
  lcd_out(1,1,"hour:");
  lcd_out(1,6,pus);
  delay_ms(500);
  if(set == 0)
  return hour11;
}  }

char minset()
{  Lcd_Cmd(_LCD_CLEAR);
  lcd_out(1,1," inc sw2  dec sw3");delay_ms(500);
  min11=0;
  min11=min1;
  for(;min11<60;)
  {
  if(inc  == 0)
  min11++;
  if(dec ==0)
  min11--;
  pus[0]= min11/10+48;
  pus[1]= min11%10+48;  pus[2]='\0';
  Lcd_Cmd(_LCD_CLEAR);
  lcd_out(1,1,"min:");
  lcd_out(1,6,pus);  delay_ms(500);
  if(set  == 0)
  return min11;
}
}
char secset()
{  Lcd_Cmd(_LCD_CLEAR);
  lcd_out(1,1,"inc sw2 dec sw3");  delay_ms(500);
  for(sec11=0;sec11<60;sec11)
  {
  if(inc  == 0)
  sec11++;
  if(dec ==0)
  sec11--;
  pus[0]= sec11/10+48;
  pus[1]= sec11%10+48;  pus[2]='\0';
  Lcd_Cmd(_LCD_CLEAR);
  lcd_out(1,1,"sec:");  delay_ms(500);
  lcd_out(1,6,pus);
  if(set  == 0)
  return sec11;
  }
}
char dates1()
{  Lcd_Cmd(_LCD_CLEAR);
  lcd_out(1,1," inc sw2  dec sw3");delay_ms(500);
  dater11=0;
  dater11=date1;
  for(;dater11<32;)
  {
  if(inc  == 0)
  dater11++;
  if(dec ==0)
  dater11--;
  pus[0]= dater11/10+48;
  pus[1]= dater11%10+48;  pus[2]='\0';
  Lcd_Cmd(_LCD_CLEAR);
  lcd_out(1,1,"date:");  delay_ms(500);
  lcd_out(1,6,pus);  delay_ms(500);
  if(set  == 0)
  return dater11;
}
}
char months1()
{  Lcd_Cmd(_LCD_CLEAR);
  lcd_out(1,1," inc sw2  dec sw3");delay_ms(500);
  mon11=0;
  mon11=month1;
  for(;mon11<12;)
  {
  if(inc  == 0)
  mon11++;
  if(dec ==0)
  mon11--;
  pus[0]= mon11/10+48;
  pus[1]= mon11%10+48;  pus[2]='\0';
  Lcd_Cmd(_LCD_CLEAR);
  lcd_out(1,1,"mon:");  delay_ms(500);
  lcd_out(1,6,pus);  delay_ms(500);
  if(set  == 0)
  return mon11;
}
}
char years()
{  Lcd_Cmd(_LCD_CLEAR);
  lcd_out(1,1," inc sw2  dec sw3");  delay_ms(500);
  yr11=0;
  yr11=year1;
for(;yr11<99;)
  {

  if(inc  == 0)
  yr11++;
  if(dec ==0)
  yr11--;
  pus[0]= yr11/10+48;
  pus[1]= yr11%10+48;  pus[2]='\0'; Lcd_Cmd(_LCD_CLEAR);
  lcd_out(1,1,"year:");  delay_ms(500);
  lcd_out(1,6,pus);  delay_ms(500);
  if(set  == 0)
  return yr11;
}
}
 
Last edited by a moderator:

ErnieM

Joined Apr 24, 2011
8,377
I can't follow the structure of your code so here's a general answer:

Make a dirty_flag that is set false when you enter the setting function and only set when and if anything is changed.

Then test the flag before you do an update.
 
Top