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.

[SOLVED] Countdown Timer using DS3231 RTC module

Status
Not open for further replies.

electronicnoob

Junior Member level 1
Junior Member level 1
Joined
Jul 4, 2019
Messages
19
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
142
Hi, I am doing a project where I am using a PIC16f887 microcontroller and a DS3231 RTC module. I am designing an etching tank that allows the user to enter their desired etching duration. I have got the code to program the RTC, but I have some problems with the countdown part.

I uploaded my code in a separate .txt file

The Clock() function is used to display the time properly. For Testing, I made it a comment.
I am using a CheckCurrentTime() function to get the current time. Assuming that the RunTime[] (minutes) is entered into an array, how do I add the Time from the StartTime[] to the EndTime[] since it is a static char array. Do I have to convert it to an integer or is there any suggestions.

Thank you very much in advance.
 

Attachments

  • code.txt
    10.7 KB · Views: 271

An RTC module isn't the best solution to this at all, you can do it more easily using only the 16F887 and some software.
However, if you want to use clock times the method I would suggest is to turn the times into an integers. Something like:
int StartTime = (Hours * 60) + Minutes;
int EndTime = StartTime + EtchingTimeInMinutes;

then loop reading the clock until:
(Hours * 60) + Minutes == EndTime

Brian.
 

Does this mean I have to convert the char array to int?
 

hello,

Yes, convert each element of the array inot a int decimal !
don't forget RTC data is in BCD format

Code:
char time[]="00H00M00S";
char date[]="00/00/00";
unsigned char second,minute,heure,jS,jour,mois,Annee; // en BCD
unsigned char DateTime[9];  // en Ascii
volatile unsigned char Sec,Mn,Hour,Month;   //en decimal
int Start,Stop;


void Refresh_Date_Time(void)
{   // GIE_bit=0;
     second=read_DS3231(0);
     minute=read_DS3231(1);
     heure =read_DS3231(2);
     jS=read_DS3231(3) & 0x07;
     if (jS>7) jS=0;
     jour=read_DS3231(4);
     mois=read_DS3231(5);
     Annee=read_DS3231(6);
     Month=Bcd2Dec(mois);
  Sec=Bcd2Dec(seconds);
Mn= Bcd2Dec(minute);
Hour=  Bcd2Dec(heure);
       if (Month>12) Month=0;
    // GIE_bit=1;
  }


void Display_Date_Time(char Visu)
{   unsigned char cx,mx,hx;

  I2C1_Start();
  I2C1_Wr(DS3231_ADDR);
  I2C1_Wr(0); // // start from word at address (REG0)
  I2C1_Repeated_Start();
  I2C1_Wr(DS3231_ADDR+1);
  second=I2C1_Rd(1);
  Sec=Bcd2Dec(second);
   minute=I2C1_Rd(1);
    Mn=Bcd2Dec(minute);
    heure=I2C1_Rd(1);
     Hour= Bcd2Dec(heure);
     jS=I2C1_Rd(1) & 0x07;     // 3em= Day 1 à 7
          if (jS>7) jS=0;
      jour=I2C1_Rd(1);  // Date  01 à 31
       mois=I2C1_Rd(1) & 0x1F;   // century + mois= 01 à 12
         Annee=I2C1_Rd(0);   // 0 a 99
           I2C1_Stop();
       Month=Bcd2Dec(mois);
       if (Month>12) Month=0;
     BCD_To_Ascii(heure,time) ;   time[2]=':';
     BCD_To_Ascii(minute,time+3 );  time[5]=':';
     BCD_To_Ascii(second,time+6);   time[8]=0;
     BCD_To_Ascii(jour,date); date[2]='-';
     BCD_To_Ascii(mois,date+3); date[5]='-';
     BCD_To_Ascii(Annee,date+6); date[8]=0;
   if( Visu==1)
   {  Print(date);
     CPrint(Blancs);
     Print(time);
     CPrint(Blancs);
     CRLF1();
     }
  }

then you can collapse data into secondes, or mn or ???

Start=(int)Hour*3600+ Mn*60+ Sec
Stop= your value..


or if it is only for time elpased
you can handle all, without reading RTC
you can connect SQWE 1Hz RTC to RB0
and use RB0 interrupt to count up or down your counter
 
Assuming that my Keypad works, is this right?
Code:
uint8_t  i, second, minute, hour, m_day, month, year;
uint8_t  duration = 0;
uint32_t  StartTime, TimeNow, EndTime;

void RTC()
{
  // convert data from BCD format to decimal format
  second = bcd_to_decimal(second);
  minute = bcd_to_decimal(minute);
  hour   = bcd_to_decimal(hour);
  m_day  = bcd_to_decimal(m_day);
  month  = bcd_to_decimal(month);
  year   = bcd_to_decimal(year);
  // end conversion
  TimeNow = (hour*3600)+(minute*60)+second;
}

void CheckCurrentTime(void)
{
    I2C_Init(100000);   // initialize I2C bus with clock frequency of 100kHz

    // read current time and date from the RTC chip
    I2C_Start();           // start I2C
    I2C_Write(0xD0);       // RTC chip address
    I2C_Write(0);          // send register address
    I2C_Repeated_Start();  // restart I2C
    I2C_Write(0xD1);       // initialize data read
    second = I2C_Read(1);  // read seconds from register 0
    minute = I2C_Read(1);  // read minutes from register 1
    hour   = I2C_Read(1);  // read hour from register 2
    I2C_Read(1);           // read day from register 3 (not used)
    m_day  = I2C_Read(1);  // read date from register 4
    month  = I2C_Read(1);  // read month from register 5
    year   = I2C_Read(0);  // read year from register 6
    I2C_Stop();            // stop I2C

    RTC();    // print time & date
}

void main(void)
{
    static char Duration[3];
    char Key = 'n';
    int a = 0;
  
    LCD_Begin();
    InitKeypad();
    LCD_Cmd(LCD_CLEAR);
    __delay_ms(1000);
  
    LCD_Goto(1, 1);
    LCD_Print("Enter Duration");
    while(a<2)
    {
        (keypadcode)
    } 
    duration = atoi(Duration);
    duration = duration*60;
  
    __delay_ms(1000);
    LCD_Cmd(LCD_CLEAR);
  
    CheckCurrentTime();
    StartTime = TimeNow;
    EndTime = StartTime + duration;
  
    while (TimeNow <= EndTime)
    {
        CheckCurrentTime();
    }
    TRISAbits.TRISA7 = 0;
    PORTAbits.RA7 = 1;
}
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top