FSM Lecture 66: Exercise-007 Defining initial transition actions

  • Post author:
  • Post category:Blog

 

Exercise-007 Defining initial transition actions

 

 

Define initial transition actions

We completed the Timer code in our previous article. And in this article, let’s display something on the LCD. 

Let’s go to our model. And for the model, you already know that there should be one starting point. The initial pseudo state is a starting point—the initial transition, as shown in Figure 1.

Figure 1. ClockAlarm:QHsm model
Figure 1. ClockAlarm:QHsm model

 

Let’s define some action for the initial transition. That action you can mention here.

Figure 2. Initial transition action
Figure 2. Initial transition action

 

Whatever you write in the real code section will be copied to the file during the code generation. And whatever you write in the pseudocode section will be considered a pseudo code, and whatever you write here will not be considered during the code generation. This is a pseudo code section (Figure 2). 

 

Initially, we will set the current time variable to some value. To set the value of the current time variable, let me create one more class operation.

Defining initial transition actions
Figure 3. Set the current time class operation

 

I will call this a set_curr_time. And the return type is void, and visibility you keep it public, and I will make it a static class operation. Generate the code.

 

And for the set_curr_time function will add one parameter. Right-click on set_curr_time and select Add parameter. You can set the parameter’s name, I’ll give the name new_curr_time, and its type is uint32_t, as shown in Figure 4.

Defining initial transition actions
Figure 4. Adding parameter to set_curr_time function

 

After that, generate the code. You can see the set_curr_time function (in Figure 5). In this function, we have to modify or set a new value to the current time variable. But you cannot do just like that because that variable is being shared with an ISR. That’s why, before you modify the current time variable, you have first to disable the interrupt, modify it, and then re-enable the interrupt.

Defining initial transition actions
Figure 5. Clock_Alarm_set_curr_time function

 

For that, we can make use of the SREG register. Just look at SREG in Atmel’s datasheet.

Defining initial transition actions
Figure 6. SREG – AVR Status Register

 

And you see, SREG is an AVR Status register, and it has a Bit 7, a global interrupt enable. This bit can be manipulated using SEI and CLI instructions in the program.

 

First, we will disable the interrupt. Before that, we will save the status of the SREG status register before doing any modification to this register. 

/*.${HSMs::Clock_Alarm::set_curr_time} .....................................*/
static void Clock_Alarm_set_curr_time(uint32_t new_curr_time) {
uint8_t save_sreg = SREG;
cli();
Clock_Alarm_curr_time = new_curr_time;
SREG = save_sreg;
}

Clock_Alarm_set_curr_time modification

I‘ll create a variable  uint8_t save_sreg = SREG; 

And then disable the interrupt, use this function cli(). It’s an inline function, or it’s a macro actually, it is there in interrupt.h, which executes the inline assembly code, which executes the instruction “cli.”

And after that, Clock_Alarm_curr_time, so you have to make modifications to this variable, set the new value ‘new_curr_time’. 

After that, you restore the status of  SREG, as shown above. 

 

Copy this code and put that code in the model under the set_curr_time function(Figure 7).

Defining initial transition actions
Figure 7. set_curr_ time code

 

Defining initial transition actions
Figure 8. Initial transition code

 

Let’s go to the model and call the function Clock_Alarm_set_curr_time. We will assign some new values; I call this INITIAL_CURR_TIME macro, which we will define later. 

After that, for the alarm_time also, we will assign some initial value; I will call this INITIAL_ALARM_TIME. 

And after that time_mode. For this mode, we will use some initial values. me->time_mode = MODE_12H. 

And the alarm_status. We will keep the alarm_status off initially. me->alarm_status = ALARM_OFF. We can create all these macros in the ClockAlarm_SM.h.

 

Enum creation
Figure 9. Enum creation

 

Let’s create enum time_mode. And let’s create another enum, enum alarm_status, like that.  

And after that, we’ll keep some more macros here. 

#define INITIAL_CURR_TIME. INITIAL_CURR_TIME, let’s keep 10 hours. 10 hours that means 10 multiplied by 3600, + 10 minutes. That is, 10 multiplied by 60 + 10 seconds. And this whole value should be multiplied by 10. All are unsigned long values. Remember that all these values represent the 24-hour format. 

And for the INITIAL_ALARM_TIME, you assign some values; I’ll keep it as 8 hours. The INITIAL_ALARM_TIME is 8 o’clock.

So, this is an initial value you can give anything you like. Let’s save this, and let’s generate the code.

static QState Clock_Alarm_initial(Clock_Alarm * const me) {
/*.${HSMs::Clock_Alarm::SM::initial} */
Clock_Alarm_set_curr_time(INITIAL_CURR_TIME);
me->alarm_time = INITIAL_ALARM_TIME;
me->time_mode = MODE_12H;
me->alarm_status = ALARM_OFF;
/* state history attributes */
/* state history attributes */
me->hist_Clock = Q_STATE_CAST(&Clock_Alarm_Ticking);
return Q_TRAN(&Clock_Alarm_Ticking);
}

Alarm_initial function

And now you can check in the initial transition function; you can see that these codes are shown above. 

In the upcoming article, we will see how to display this time information on the LCD.

 

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.

Leave a Reply