rtc problem pls help

Discussion in 'Embedded Systems and Microcontrollers' started by Gopi Vh, Jun 13, 2015.

  1. Gopi Vh

    Thread Starter New Member

    Jun 13, 2015
    1
    0
    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


    Code ( (Unknown Language)):
    1.  
    2.  
    3. // Lcd module connections
    4. sbit LCD_RS at LATB5_bit;
    5. sbit LCD_EN at LATB4_bit;
    6. sbit LCD_D4 at LATB3_bit;
    7. sbit LCD_D5 at LATB2_bit;
    8. sbit LCD_D6 at LATB1_bit;
    9. sbit LCD_D7 at LATB0_bit;
    10.  
    11. sbit LCD_RS_Direction at TRISB5_bit;
    12. sbit LCD_EN_Direction at TRISB4_bit;
    13. sbit LCD_D4_Direction at TRISB3_bit;
    14. sbit LCD_D5_Direction at TRISB2_bit;
    15. sbit LCD_D6_Direction at TRISB1_bit;
    16. sbit LCD_D7_Direction at TRISB0_bit;
    17. //End LCD connection*/
    18. sbit Soft_I2C_Scl at RC3_bit;
    19. sbit Soft_I2C_Sda at RC4_bit;
    20. sbit Soft_I2C_Scl_Direction at TRISC3_bit;
    21. sbit Soft_I2C_Sda_Direction at TRISC4_bit;
    22. sbit BUZZER at RB6_bit;
    23. char timeset(); //time
    24. char dateset();//date
    25. char dayset();//day
    26. char hourset();//hour
    27. char minset();//min
    28. char secset();//sec
    29. char dates1();//date
    30. char months1();//month
    31. char years();//year
    32. unsigned char BCD2LowerCh(unsigned char bcd);
    33. unsigned char BCD2UpperCh(unsigned char bcd);
    34. unsigned short read_ds1307(unsigned short address );
    35. void write_ds1307(unsigned short address,unsigned short w_data);
    36. 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;
    37. 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;
    38.  
    39. char * text  = "0";
    40. char * text0 = "00";
    41. char * text1 = "00";
    42. char * text2 = "00";
    43. char * text3 = "00";
    44. char * text4 = "0";
    45. char * text5 = "00";
    46. char days1[] = "SUN";
    47. char days2[] = "MON";
    48. char days3[] = "TUE";
    49. char days4[] = "WED";
    50. char days5[] = "THU";
    51. char days6[] = "FRI";
    52. char days7[] = "SAT";
    53.  
    54. char timer[9];
    55. char ddater[11];
    56. char pus[3];
    57. sbit set  at RA0_bit;   //buttons
    58. sbit inc at RA1_bit;
    59. sbit dec at RA2_bit;
    60. sbit set1 at RA3_bit;
    61.  
    62.  
    63.  
    64. void main()
    65. {
    66.   x=1;
    67.   TRISB.F6=0;
    68.   TRISA.F0=1;
    69.   TRISA.F1=1;
    70.   TRISA.F2=1;
    71.   TRISA.F3=1;
    72.   ADRESL  = 0;
    73.   ADRESH = 0;  // Configure AN pins as digital I/O
    74.   ADCON0=0;
    75.   ADCON1 = 0x0F;
    76.   ADCON2 =0;
    77.   CMCON =7;
    78.   PORTA =0;
    79.   PORTB =0;
    80.   PORTC =0;
    81.   BUZZER =0;
    82.  
    83.   /*OSCCON  = 0x70;*/
    84.   //TRISA= 0;
    85.   TRISC=0xFF;
    86.   Lcd_Init();  // Initialize LCD
    87.   Lcd_Cmd(_LCD_CLEAR);  // Clear display
    88.   Lcd_Cmd(_LCD_CURSOR_OFF);  // Cursor off
    89.   Soft_I2C_Start();
    90.  
    91.   do{
    92.   for(rr=0;rr<=2;rr++)
    93.   {
    94.   Lcd_Cmd(_LCD_CLEAR);  // Clear LCD
    95.   Lcd_Cmd(_LCD_CURSOR_OFF);
    96.   Lcd_Out(1, 1, "TIME:");
    97.   Lcd_Out(2, 1, "DATE:");
    98.   sec=read_ds1307(0X00); // read second
    99.   minute=read_ds1307(0X01); // read minute
    100.   hour=read_ds1307(0X02); // read hour
    101.   day=read_ds1307(0X03); // read day
    102.   date=read_ds1307(0X04); // read date
    103.   month=read_ds1307(0X05); // read month
    104.   year=read_ds1307(0X06); // read year
    105.  
    106.   sec1=sec;
    107.   min1=minute;
    108.   hour1=hour;
    109.   day1=day;
    110.   date1=date;
    111.   month1=month;
    112.   year1=year;
    113.   write_ds1307(0x26,sec);
    114.   write_ds1307(0x27,minute);
    115.   write_ds1307(0x28,hour);
    116.   write_ds1307(0x29,day);
    117.   write_ds1307(0x30,date);
    118.   write_ds1307(0x2A,month);
    119.   write_ds1307(0x2B,year);
    120.  
    121.   timer[0] = BCD2UpperCh(hour);
    122.   timer[1] = BCD2LowerCh(hour);
    123.   timer[2] = ':';
    124.   timer[3] = BCD2UpperCh(minute);
    125.   timer[4] = BCD2LowerCh(minute);
    126.   timer[5] = ':';
    127.   timer[6] = BCD2UpperCh(sec);
    128.   timer[7] = BCD2LowerCh(sec);
    129.   timer[8] ='\0';
    130.  
    131.   ddater[0] = BCD2UpperCh(date);
    132.   ddater[1] = BCD2LowerCh(date);
    133.   ddater[2] ='/';
    134.   ddater[3] = BCD2UpperCh(month);
    135.   ddater[4] = BCD2LowerCh(month);
    136.   ddater[5] ='/';
    137.   ddater[6] = '2';
    138.   ddater[7] = '0';
    139.   ddater[8] = BCD2UpperCh(year);
    140.   ddater[9] = BCD2LowerCh(year);
    141.   ddater[10] = '\0';
    142.  
    143.   Lcd_Out(1,6,timer);
    144.   Lcd_Out(2,6,ddater);
    145.  
    146.   if(day ==1)
    147.   Lcd_Out(1,14,days1);
    148.   if(day ==2)
    149.   Lcd_Out(1,14,days2);
    150.   if(day ==3)
    151.   Lcd_Out(1,14,days3);
    152.   if(day ==4)
    153.   Lcd_Out(1,14,days4);
    154.   if(day ==5)
    155.   Lcd_Out(1,14,days5);
    156.   if(day ==6)
    157.   Lcd_Out(1,14,days6);
    158.   if(day ==7)
    159.   Lcd_Out(1,14,days7);
    160.   delay_ms(1000);
    161.   }
    162. //setting part
    163.  
    164.  
    165.   if(set==0)
    166.   {
    167.   b=1;
    168.   do
    169.   {
    170.   up=0;
    171.   Lcd_Cmd(_LCD_CLEAR);
    172.   lcd_out(1,1, "1:time 2:date");  delay_ms(500);
    173.   lcd_out(2,1, "3:day use sw2");
    174.   delay_ms(500);
    175.   if(inc ==0)
    176.   {  up=1;
    177.   for(up=1;up<5;)
    178.   {
    179.   Lcd_Cmd(_LCD_CLEAR);
    180.   switch(up)
    181.   {
    182.   case 1:  lcd_out(1,1, "1:time ");
    183.   delay_ms(500);
    184.   if(set == 0)
    185.   {
    186.   x = timeset();
    187.   up=4;
    188.   }
    189.   break;
    190.   case 2:  lcd_out(1,1, "2:date ");
    191.   delay_ms(500);
    192.   if(set == 0)
    193.   {
    194.   x=dateset();  up=4;
    195.   }
    196.   break;
    197.   case 3:  lcd_out(1,1, "3:day ");
    198.   delay_ms(500);
    199.   if(set == 0)
    200.   {
    201.   x=dayset();  up=4;
    202.   }
    203.   break;
    204.   case 4:
    205.   if(x==0)
    206.   {
    207.   sec_1=read_ds1307(0X00); // read second
    208.   minute_1=read_ds1307(0X23); // read minute
    209.   hour_1=read_ds1307(0X22); // read hour
    210.   day_1=read_ds1307(0X25); // read day
    211.   date_1=read_ds1307(0X21); // read date
    212.   month_1=read_ds1307(0X20); // read month
    213.   year_1=read_ds1307(0X24); // read year
    214.   }
    215.   else
    216.   {
    217.   sec_1=read_ds1307(0X26); // read second
    218.   minute_1=read_ds1307(0X27); // read minute
    219.   hour_1=read_ds1307(0X28); // read hour
    220.   day_1=read_ds1307(0X29); // read day
    221.   date_1=read_ds1307(0X30); // read date
    222.   month_1=read_ds1307(0X2A); // read month
    223.   year_1=read_ds1307(0X2B); // read year
    224.   }
    225.   write_ds1307(0x00,sec_1);
    226.   write_ds1307(0x01,minute_1);
    227.   write_ds1307(0x02,hour_1);
    228.   write_ds1307(0x03,day_1);
    229.   write_ds1307(0x04,date_1);
    230.   write_ds1307(0x05,month_1);
    231.   write_ds1307(0x06,year_1);
    232.   up=5;
    233.   break;
    234.   }
    235.   if(inc == 0)
    236.   up++;
    237.  
    238.   b=0;
    239.   }
    240.   }
    241.  
    242.   }while(b == 1);
    243.   }
    244.  
    245. }while(1);  //
    246.  
    247. }
    248. unsigned short read_ds1307(unsigned short address )
    249. {
    250. int dat;
    251. soft_I2C_Start();
    252. soft_I2C_Write(0xd0); //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
    253. soft_I2C_Write(address);
    254. soft_I2C_Start();
    255. soft_I2C_Write(0xd1); //0x68 followed by 1 --> 0xD1
    256. dat=soft_I2C_Read(0);
    257. soft_I2C_Stop();
    258. return(dat);
    259. }
    260.  
    261. unsigned char BCD2UpperCh(unsigned char bcd)
    262. {
    263. return ((bcd >> 4) + '0');
    264. }
    265.  
    266. unsigned char BCD2LowerCh(unsigned char bcd)
    267. {
    268. return ((bcd & 0x0F) + '0');
    269. }
    270. void write_ds1307(unsigned short address,unsigned short w_data)
    271. {
    272. soft_I2C_Start(); // issue I2C start signal
    273. //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
    274. soft_I2C_Write(0xD0); // send byte via I2C (device address + W)
    275. soft_I2C_Write(address); // send byte (address of DS1307 location)
    276. soft_I2C_Write(w_data); // send data (data to be written)
    277. soft_I2C_Stop(); // issue I2C stop signal
    278. }
    279. char timeset()
    280. {
    281.   Lcd_Cmd(_LCD_CLEAR);
    282.  
    283.   lcd_out(1,1,"1:hour2:min3:sec");
    284.   delay_ms(500);
    285.   lcd_out(2,1,"use sw4");
    286.   delay_ms(500);
    287.   timer11=0;
    288.   for(timer11=0;timer11<4;)
    289.   {
    290.   if(set1 ==0)
    291.   timer11++;
    292.  
    293.   switch(timer11)
    294.   {
    295.   case 1:  Lcd_Cmd(_LCD_CLEAR);
    296.   lcd_out(1,1, "1:hour ");
    297.   delay_ms(500);
    298.   if(set ==0)
    299.   {
    300.   hour_11=hourset();
    301.   write_ds1307(0x22,Dec2Bcd(hour_11));  return 0;
    302.   }
    303.   break;
    304.   case 2:  Lcd_Cmd(_LCD_CLEAR);
    305.   lcd_out(1,1, "2:min ");
    306.   delay_ms(500);
    307.   if(set ==0)
    308.   {
    309.   min_11=minset();
    310.   write_ds1307(0x23,Dec2Bcd(min_11));  return 0;
    311.   }
    312.   break;
    313.  
    314.   case 3:  Lcd_Cmd(_LCD_CLEAR);
    315.   lcd_out(1,1, "3:sec");
    316.   delay_ms(500);
    317.   if(set ==0)
    318.   {
    319.   sec_11= secset();  write_ds1307(0x21,Dec2Bcd(sec_11));
    320.   return 0;
    321.   }
    322.   break;
    323.   }
    324.  
    325. }
    326. return 1;
    327. }
    328. char dateset()
    329. {
    330.   Lcd_Cmd(_LCD_CLEAR);
    331.  
    332.   lcd_out(1,1,"1:date2:mon3:yr");
    333.   delay_ms(500);
    334.   lcd_out(2,1,"use sw4");
    335.   delay_ms(500);
    336.   timer11=0;
    337.   for(timer11=0;timer11<4;)
    338.   {
    339.   if(set1 ==0)
    340.   timer11++;
    341.  
    342.   switch(timer11)
    343.   {
    344.   case 1:  Lcd_Cmd(_LCD_CLEAR);
    345.   lcd_out(1,1, "1:date ");
    346.   delay_ms(500);
    347.   if(set ==0)
    348.   {
    349.   dater_11= dates1();
    350.   write_ds1307(0x21,Dec2Bcd(dater_11));  return 0;
    351.   }
    352.   break;
    353.   case 2:  Lcd_Cmd(_LCD_CLEAR);
    354.   lcd_out(1,1, "2:mon ");
    355.   delay_ms(500);
    356.   if(set ==0)
    357.   {
    358.   mon_11 = months1();
    359.   write_ds1307(0x20,Dec2Bcd(mon_11));return 0;  }
    360.   break;
    361.  
    362.   case 3:  Lcd_Cmd(_LCD_CLEAR);
    363.   lcd_out(1,1, "3:yr ");
    364.   delay_ms(500);
    365.   if(set ==0)
    366.   {
    367.   yr_11= years();  write_ds1307(0x24,Dec2Bcd(yr_11));
    368.   return 0;
    369.   }
    370.   break;
    371.   }
    372.  
    373. }
    374. return 1;
    375. }
    376. char dayset()
    377. {
    378. unsigned char day_11;
    379. day_11=0;
    380. day_11=day1;
    381.   Lcd_Cmd(_LCD_CLEAR);
    382.   lcd_out(1,1," inc sw2  dec sw3"); delay_ms(500);
    383.   for(;day_11<=7;day_11)
    384.   {
    385.  
    386.   if(inc  == 0)
    387.   day_11++;
    388.   if(dec ==0)
    389.   {
    390.   day_11--;
    391.   }
    392.   switch(day_11)
    393.   {
    394.   case 1: lcd_out(1,1,"sun");  delay_ms(500);
    395.   break;
    396.   case 2: lcd_out(1,1,"mon");delay_ms(500);
    397.   break;
    398.   case 3: lcd_out(1,1,"tue"); delay_ms(500);
    399.   break;
    400.   case 4: lcd_out(1,1,"wed");  delay_ms(500);
    401.   break;
    402.   case 5: lcd_out(1,1,"thu"); delay_ms(500);
    403.   break;
    404.   case 6: lcd_out(1,1,"fri"); delay_ms(500);
    405.   break;
    406.   case 7: lcd_out(1,1,"sat"); delay_ms(500);
    407.   break;
    408.   }
    409.   if(set == 0)
    410.   {
    411.   write_ds1307(0x25,Dec2Bcd(day_11));
    412.   return 0;
    413.   }
    414.   }
    415.   return 1;
    416. }
    417. char hourset()
    418. {  Lcd_Cmd(_LCD_CLEAR);  lcd_out(1,1," inc sw2  dec sw3");
    419.   delay_ms(500);
    420.   hour11=0;
    421.   hour11=hour1;
    422. for(;hour11<25;)
    423.   {
    424.  
    425.   if(inc  == 0)
    426.   hour11++;
    427.   if(dec ==0)
    428.   hour11--;
    429.   pus[0]= hour11/10+48;
    430.   pus[1]= hour11%10+48;
    431.   pus[2]='\0';
    432.   Lcd_Cmd(_LCD_CLEAR);
    433.   lcd_out(1,1,"hour:");
    434.   lcd_out(1,6,pus);
    435.   delay_ms(500);
    436.   if(set == 0)
    437.   return hour11;
    438. }  }
    439.  
    440. char minset()
    441. {  Lcd_Cmd(_LCD_CLEAR);
    442.   lcd_out(1,1," inc sw2  dec sw3");delay_ms(500);
    443.   min11=0;
    444.   min11=min1;
    445.   for(;min11<60;)
    446.   {
    447.   if(inc  == 0)
    448.   min11++;
    449.   if(dec ==0)
    450.   min11--;
    451.   pus[0]= min11/10+48;
    452.   pus[1]= min11%10+48;  pus[2]='\0';
    453.   Lcd_Cmd(_LCD_CLEAR);
    454.   lcd_out(1,1,"min:");
    455.   lcd_out(1,6,pus);  delay_ms(500);
    456.   if(set  == 0)
    457.   return min11;
    458. }
    459. }
    460. char secset()
    461. {  Lcd_Cmd(_LCD_CLEAR);
    462.   lcd_out(1,1,"inc sw2 dec sw3");  delay_ms(500);
    463.   for(sec11=0;sec11<60;sec11)
    464.   {
    465.   if(inc  == 0)
    466.   sec11++;
    467.   if(dec ==0)
    468.   sec11--;
    469.   pus[0]= sec11/10+48;
    470.   pus[1]= sec11%10+48;  pus[2]='\0';
    471.   Lcd_Cmd(_LCD_CLEAR);
    472.   lcd_out(1,1,"sec:");  delay_ms(500);
    473.   lcd_out(1,6,pus);
    474.   if(set  == 0)
    475.   return sec11;
    476.   }
    477. }
    478. char dates1()
    479. {  Lcd_Cmd(_LCD_CLEAR);
    480.   lcd_out(1,1," inc sw2  dec sw3");delay_ms(500);
    481.   dater11=0;
    482.   dater11=date1;
    483.   for(;dater11<32;)
    484.   {
    485.   if(inc  == 0)
    486.   dater11++;
    487.   if(dec ==0)
    488.   dater11--;
    489.   pus[0]= dater11/10+48;
    490.   pus[1]= dater11%10+48;  pus[2]='\0';
    491.   Lcd_Cmd(_LCD_CLEAR);
    492.   lcd_out(1,1,"date:");  delay_ms(500);
    493.   lcd_out(1,6,pus);  delay_ms(500);
    494.   if(set  == 0)
    495.   return dater11;
    496. }
    497. }
    498. char months1()
    499. {  Lcd_Cmd(_LCD_CLEAR);
    500.   lcd_out(1,1," inc sw2  dec sw3");delay_ms(500);
    501.   mon11=0;
    502.   mon11=month1;
    503.   for(;mon11<12;)
    504.   {
    505.   if(inc  == 0)
    506.   mon11++;
    507.   if(dec ==0)
    508.   mon11--;
    509.   pus[0]= mon11/10+48;
    510.   pus[1]= mon11%10+48;  pus[2]='\0';
    511.   Lcd_Cmd(_LCD_CLEAR);
    512.   lcd_out(1,1,"mon:");  delay_ms(500);
    513.   lcd_out(1,6,pus);  delay_ms(500);
    514.   if(set  == 0)
    515.   return mon11;
    516. }
    517. }
    518. char years()
    519. {  Lcd_Cmd(_LCD_CLEAR);
    520.   lcd_out(1,1," inc sw2  dec sw3");  delay_ms(500);
    521.   yr11=0;
    522.   yr11=year1;
    523. for(;yr11<99;)
    524.   {
    525.  
    526.   if(inc  == 0)
    527.   yr11++;
    528.   if(dec ==0)
    529.   yr11--;
    530.   pus[0]= yr11/10+48;
    531.   pus[1]= yr11%10+48;  pus[2]='\0'; Lcd_Cmd(_LCD_CLEAR);
    532.   lcd_out(1,1,"year:");  delay_ms(500);
    533.   lcd_out(1,6,pus);  delay_ms(500);
    534.   if(set  == 0)
    535.   return yr11;
    536. }
    537. }
    538.  
    539.  
     
    Last edited by a moderator: Jun 13, 2015
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    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.
     
Loading...