Вы находитесь на странице: 1из 22

Microcontroller &

Embedded system
Seminar 6

Multi-state systems
Dr. Tran Thanh Hung
Department of Automation Technology,
College of Engineering Technology, Can Tho University
Email: tthung@ctu.edu.vn

Review
In the seminar 5, we have used EOS to
implement periodic functions for one state
system.
In this seminar, we will use EOS to implement
multi-state systems.

Outline
How to implement for multi-state systems?
multi-state systems
implement for multi-state systems: type 1
implement for multi-state systems: type 2

Seminar objectives
At the end of this seminar, by referring the
lecture notes, students will be able to:
understand issue of multi-state systems
implement multi-state systems, using EOS

What are multi-state systems?


Example of multi-state system:
Traffic light system

3 state system

4 state system

What are multi-state systems?


Example of multi-state system:
Washing machine

Types of multi-state systems


Type 1: Multi-state (timed) system:
The transition between states will depend only on the
passage of time.
- Example: Traffic light system
Type 2: Multi-state (input/timed) system:
The transition between states will depend both on the
passage of time and system inputs.
- Example: Washing machine
Type 3: Multi-state (input) system:
The transition between states will depend only on system
inputs.

How to implement multi-state systems?


You are going to write a program for a traffic light
system, operating on the following states:
State 1 (RED): Turn Red light on for 10 seconds and
go to State 2
State 2 (RED-YELLOW): Turn both Red and Yellow
lights on for 2 seconds and go to State 3
State 3 (GREEN): Turn Green light on for 15 seconds
and go to State 4
State 4 (YELLOW): Turn Yellow light on for 3 seconds
and go to State 1

EOS: Review
#include "Main.h"
#include "EOS.h"
void main (void)
{

EOS_init(50000); //Initialize EOS to interrupt every 50ms


while(1) // Super Loop
{

go_to_sleep(); // Enter idle mode to save power

}
}
EOS_ISR() interrupt INTERRUPT_Timer_2_Overflow //in file EOS.c
{
TF2 = 0;
// Put your code here

Implement multi-state systems


Represent the states
typedef enum {RED,RED_YELLOW,GREEN,YELLOW} Light_State;
static Light_State current_state = RED; //initial state = RED

Define duration for the states


#define
#define
#define
#define

RED_DURATION 10
RED_YELLOW_DURATION 2
GREEN_DURATION 15
YELLOW _DURATION 3

Define global variables to measure time


static unsigned char time_in_state = 0; // time (s) in each state
static unsigned char call_count = 0; //use in EOS_ISR

Implement multi-state systems


Update states
//This function must be called every 1s by EOS_ISR
void TRAFFIC_LIGHTS_Update(void)
{ switch (current_state)
{ case RED:
{ turn on Red light; turn off other lights;
if (++time_in_state == RED_DURATION)
{ current_state = RED_YELLOW; //change state
time_in_state = 0;
}
break;
}

Implement multi-state systems


case RED_YELLOW :
{ turn on Red light; turn on Yellow light;
if (++time_in_state == RED_YELLOW _DURATION)
{ current_state = GREEN; //change state
time_in_state = 0;
}
break;
}
case GREEN :
{ }
case YELLOW :
{ }
}
}

Exercise 1
Write a program to control a complete traffic light
system with states and hardware configuration as
following:

Type 2 multi-state systems

Type 2 multi-state systems


Operating description of the system:
1. The user selects a wash program on the selector dial.
2. The user presses the Start switch.
3. The door lock is engaged.
4. The water valve is opened to allow water into the wash
drum.
5. If the wash program involves detergent, the detergent
hatch is opened. When the detergent has been released,
the detergent hatch is closed.
6. When the full water level is sensed, the water valve is
closed.

Type 2 multi-state systems


7. If the wash program involves warm water, the water heater
is switched on. When the water reaches the correct
temperature, the water heater is switched off.
8. The washer motor is turned on to rotate the drum. At the
end of the wash cycle, the motor is stopped.
9. The pump is switched on to drain the drum. When the drum
is empty, the pump is switched off.
10. The water valve is opened to allow water into the wash
drum.
11. The washer motor is turned on to rotate the drum (to rinse
detergent).
12. The pump is switched on to drain the drum.

Type 2 multi-state systems

Type 2 multi-state systems


typedef enum {INIT, START, FILL1, HEAT,WASH,DRAIN1,FILL2, RINSE,
DRAIN2, ERROR} System_state;
#define MAX_FILL_DURATION 1000
#define MAX_HEAT_DURATION 1000
#define MAX_DRAIN_DURATION 1000
static System_state current_state = INIT; //initial state
static unsigned char Detergent[8] = {1,1,1,0,0,1,0,1};
static unsigned char Heat[8] = {1,1,1,0,0,1,0,1};
static unsigned char wash_program = 0;
static unsigned char time_in_state = 0; // time (s) in each state
static unsigned char call_count = 0; //use in EOS ISR

Type 2 multi-state systems


void WASHER_Update(void) // Call once per second by EOS_ISR
{
switch (current_state)
{
case INIT:
{ Display the state on LEDs;
turn all motor, valve off
wash_program = read Selector Dial (SWs); //0-7
Display the wash_program user chosen on LEDs;
if (Start SW pressed) current_state = START;
break;
}

Type 2 multi-state systems


case START:
{ Display the state on LEDs;
lock the door;
if (Detergent[wash_program] ==1)
open Detergent Hatch;
current_state = FILL1;
Time_in_state = 0;
break;
}

Type 2 multi-state systems


case FILL1 :
{ Display the state on LEDs;
open the valve to fill water;
if (++Time_in_state >= MAX_FILL_DURATION)
current_state = ERROR;
if (Drum is full water)
{ close the valve;
if (Heat[Program] == 1) // Does program require hot water?
{
current_state = HEAT;Time_in_state = 0;}
else
{ current_state = WASH;Time_in_state = 0;}
}
break;
}

Exercise 2
Vit chng trnh cho my git, theo m t trn
Quy c: Dng
- phm SW7 lm nt Start
- phm SW6 lm cm bin mc nc (bm = nc y)
- phm SW5 lm cm bin nhit (bm = nhit t yu cu)
- phm SW4 lm cm bin ht nc (bm = ht nc)
- LED 7 lm van nc
- LED 6 lm hp ng bt git
- LED 5 lm b un nc
- LED 4 lm motor lng git
- LED 3 lm bm nc
Trng thi ERROR: Chp tt tt c cc LED
Trng thi WASH, RINSE: ko di 20s

Вам также может понравиться