1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

RTCC Set

Discussion in 'Microcontrollers' started by TucsonDon, Jul 6, 2017.

  1. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    45
    Likes:
    0
    I am a hobbyist and have a basic knowledge of C. I am trying to set up a call function to set the time on an rtcc. I have it set so that if a button is pushed for a set time it will call the function. I want to toggle between the HH:MM:SS with the "Set" button and then use a "up" or “down" to change the value. I was thinking of using a switch statement to toggle between but unsure how to setup.


    I have also defined the following

    volatile uint8_t Time[3]

    volatile uint8_t stime[Time]

    HH=Time[2]

    MM=Time[1]

    SS=Time[0]
     
  2. ci139

    ci139 Active Member

    Joined:
    Apr 12, 2012
    Messages:
    470
    Likes:
    37
    i only know computers not mcu-s
    use switch(index){case 0: ... break;default:index=0}
    or use index=++index%3
    _________________
    ? if you want to know how to access RTC built in or external - not how to organize the program structure - then post your equipment specs ?
     

    Attached Files:

    Last edited: Jul 7, 2017
  3. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,161
    Likes:
    340
    Location:
    Brisbane Australia
    I would cycle through the digits. On pressing set, read the time and set a flag to stop the clock from updating, display the time, set the 10s of hours flashing and set a counter to 0. You will need an ok button to move the digits along and up/down to change it. Once the counter reaches 4 you can write the time back to the RTC. You could use the set button to move to the next digit or, it you have an ok button, set could act as cancel.

    Mike.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    45
    Likes:
    0

    Thanks for you input:)
     
  6. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    45
    Likes:
    0
    So far this is what I have come up with:

    Please let me know what you think

    Code (text):

    void TimeSet(void)
    {
        while (sett!=30)
        {
            TMR3_StartTimer();
            if(Set_GetValue()==Hi)stime=1;
            switch (stime)
            {
                case 0:
                    stime = 1;
                    while(Set_GetValue()!=Hi); //wait for button or timer
                    {
                        if(Up_GetValue()==Hi & Pushed!=On) //up button pushed
                        {
                            Pushed=On;
                            HH=HH+1; //hour inc.
                            if (HH==13)HH=1;
                            sett=0;//reset time counter
                        }
                        if(Down_GetValue()==Hi & Pushed!On) //down button pushed
                        {
                            Pushed=On;
                            HH=HH-1; //hour deinc.+
                            if(HH==0)HH=12;
                            sett=0;
                        }
                        if(Up_GetValue()!=Hi|Down_GetValue()!=Hi)Pushed=Off;
                        //only inc or deinc one number @ a time
                    }
                    if (Set_GetValue()==Hi)stime++;
                    break;
                case 1:
                    stime = 2;
                     while(Set_GetValue()!=Hi); //wait for button or timer
                    {
                        if(Up_GetValue()==Hi & Pushed!=On) //up button pushed
                        {
                            Pushed=On;
                            MM=MM+1;
                            if (MM==60)MM=0;
                            sett=0;
                        }
                        if(Down_GetValue()==Hi & Pushed!=On) //down button pushed
                        {
                            Pushed=On;
                            MM=MM-1;
                            if(MM==-1)MM=59;
                            sett=0;
                        }
                        if(Up_GetValue()!=Hi|Down_GetValue()!=Hi)Pushed=Off;
                    }
                     if (Set_GetValue()==Hi)stime++;
                    break;
                case 2:
                    stime = 3;
                     while(Set_GetValue()!=Hi); //wait for button or timer
                    {
                        if(Up_GetValue()==Hi & Pushed!=On) //up button pushed
                        {
                            Pushed=On;
                            SS=SS+1;
                            if (SS==60)SS=1;
                            sett=0;
                        }
                        if(Down_GetValue()==Hi) //down button pushed
                        {
                            Pushed=On;
                            SS=SS-1;
                            if(SS==-1)SS=59;
                            sett=0;
                        }
                        if(Up_GetValue()!=Hi|Down_GetValue()!=Hi)Pushed=Off;
                    }
                     if (Set_GetValue()==Hi)stime=1;
                    break;
                default:
                   if(sett == 30) return;
                    //sett is advanced by timer to = 30 sec.
                    //if no action is taken return to main
            }
            if (sett == 30)return;
            //if no action is taken in 30 sec. return to main
        }
    }
    [code/]
     
  7. ci139

    ci139 Active Member

    Joined:
    Apr 12, 2012
    Messages:
    470
    Likes:
    37
    PIC18C Reference Manual ♦ 14.5 Timer1 Operation in Asynchronous Counter Mode ♦ If T1SYNC (T1CON register) is set, the external clock input is not synchronized. The timer continues to increment asynchronously to the internal phase clocks. The timer will continue to run during SLEEP and can generate an interrupt on overflow that will wake-up the processor. However, special precautions in software are needed to read/write the timer (Subsection 14.6.4 “Reading and Writing Timer1 in Asynchronous Counter Mode with RD16 = 0” ). Since the counter can operate in sleep, Timer1 can be used to implement a true real-time clock. The timer increments at the Q4:Q1 and Q2:Q3 edges. In asynchronous counter mode, Timer1 cannot be used as a time-base for capture or compare operations.
    ~about -- identifying the likely product , cant eval. your code coz "too lazy" to dig into MC-"X" specifics
    the construction:
    NB! may be a cause for confusing situations e.g. assuming system state
     
  8. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    45
    Likes:
    0
    The MCU that I am using is the PIC18F45K22

    My goal with "Pushed" was to only allow the variable to change once per button press.
     
  9. ci139

    ci139 Active Member

    Joined:
    Apr 12, 2012
    Messages:
    470
    Likes:
    37
    http://www.flowgorithm.org/ may be for help as well as to slow things down -- at least it allows you to chart all possible system states
    your code can be optimized as : (write one) subroutine for all time-record (array in your case) fields changes/updates -- as:
    • function usrIFx(step,modulo){var rr;if(step<0){rr=modulo}else{rr=0};return rr}
      function dec_inc_Date(TmFld,step,modulo){TmFld+=usrIFx(step,modulo)+step;TmFld%=modulo}
    • also setting it up as a software interrupt rather than the code branch
    • PS! i only hope i didn't pass ↑↑there↑↑ any mistakes !! -- as not too busy with progg'ing these days
      -- Now! with some example
      . . . and yet another (witch more than explains why i'm not too busy with progg'ing)
    _________
    i wonder the century when this "Coming soon" was written http://microchipdeveloper.com/8bit:he
    attempting to get into the subject https://www.google.com/search?q=microchip+pic18+TMR0_StartTimer()+does+what+exactly+-confusion
    ←this search -- required to verify that 30s inactivity causes anything
    i think i retire here
    ____________
    BTW your current "code form" is easier to debug (you asked about other opinions . . . you can/should/may start compacting when the more space in micro is required)
     

    Attached Files:

    Last edited: Jul 8, 2017
  10. ci139

    ci139 Active Member

    Joined:
    Apr 12, 2012
    Messages:
    470
    Likes:
    37

Share This Page