FSM Lecture 76| Exercise-007 Updating real time

 

Exercise-007 Updating real time clock

 

 

In the previous article, we completed testing this. 

Figure 1. ClockAlarm Model
Figure 1. ClockAlarm Model

 

Whenever you are in the cs_format selection, if everything is fine, OK is unhandled. I mean, if there is no problem with the user input, then this guard fails, so OK becomes unhandled. When it is unhandled, it moves to the superstate.

 

So, its superstate is Settings. And ‘OK’ is handled here, as shown in Figure 2. Here we have to update the current time variable. 

Figure 2. ClockAlarm Model
Figure 2. ClockAlarm Model

 

Here is a code for that.

Real-Time Clock Update Logic in FSM: Handling Time Format and User Input
Figure 3. Code

 

First, we will check whether the user-selected format is a 24-hour format or not. Because the temp_format variable contains the user selection regarding the format. If it is not equal to 24-hour format, we have to convert the temp_time into the 24-hour format. Because ultimately, we will save the value in the current time variable in 24-hour format. That’s why you have to convert that. convert_12hformat_to_24h is just a helper function; we already have this function.

And after that, set the mode to 12-hour format, indicating that the display should be shown in 12-hour format. Otherwise, time_mode is the 24-hour format. 

After that, we have to update this temp_time into the current time global variable. This is how you should do it. First of all, you have to convert me->temp_time into a number of 100 milliseconds. That’s why I am just multiplying it by 10 here.  

And then, I’m disabling the global interrupt – cli(). Because we don’t want any more interrupts from the timer, that’s why I’m disabling it. 

And stopping the timer. TCCR1B &= ~(0x70); is stop the TIMER1. So, this is how you do it.

Figure 4. TIMER1 register description
Figure 4. TIMER1 register description

 

You should go to the TIMER1 register description and TCCR1B, and here you should keep these 3 bits CS12, CS11, and CS10 as 0. If these 3 bits become 0, then there is No clock source for the timer, and the timer stops. That’s why I’m just clearing those bits. 

And then make the count register 0 → TCNT1 =0U;

Update Clock_Alarm_curr_time variable.

 

Provide the clock source.  So, I’m just selecting these values Clock source with the Prescaler 256, shown in Figure 5.

Updating real time
Figure 5. TCCR1B clock Select Bit description

 

TCCR1B |= 0x4U;

Now, the clock will be applied to the timer, and the timer will start ticking again and restore the value of SREG, which will also restore the interrupt enable bit if it is enabled.

I’m going to keep the selected code(Figure 3) in Settings. Click on OK transition, let’s paste that code.

And then, we already have one function called set_curr_time with the new current time. Let’s use this code, as shown in Figure 6.  Here you define the variable uint8_t save_sreg;  So, this is the correct code. This is the new current time.

Real-Time Clock Update Logic in FSM: Handling Time Format and User Input
Figure 6. Set_curr_time function

 

And let’s go to Settings, OK. Here, you should keep this code as well. temp_time multiplied by 10; then you call set_curr_time function. Clock_Alarm_set_curr_time and you provide temp_time value– code as shown in Figure 7. 

Real-Time Clock Update Logic in FSM: Handling Time Format and User Input
Figure 7. OK transition Code

 

And one more thing we need to take care of here. When leaving this Settings state, you should also make the cursor off and blink off. So, the exit function of the settings state code is shown in Figure 8. 

Real-Time Clock Update Logic in FSM: Handling Time Format and User Input
Figure 8. Exit action of Setting state

 

Now, let’s save this, generate code, and let’s compile. And now, let’s test this. Let me download this code; it is working.

Let me just set some time; I will set it for 23:59:54 24H—Press OK button (a). 

Now you see, the new time is updated(b).

Real-Time Clock Update Logic in FSM: Handling Time Format and User Input
Figure 9. Demo

 

Let’s modify it once again. Let me make this as 10:00:04 PM, Press OK. The new time is updated (Figure 10).  When displaying in 24H format, it doesn’t show that format.

Real-Time Clock Update Logic in FSM: Handling Time Format and User Input
Figure 10. Demo

 

Let’s see some invalid entries. I will set the time to 24:00:34 24H. Now, it is an Error.

Real-Time Clock Update Logic in FSM: Handling Time Format and User Input
Figure 11. Check Invalid Entry

 

When it is in Error, the OK has no effect. You have to press SET. That’s it for this article. And in the following article, we will see how to handle the alarm setting state.

 

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.