seminar_6

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: [email protected]

Upload: buiphuochuy55

Post on 06-Nov-2015

212 views

Category:

Documents


0 download

DESCRIPTION

Bài giảng VDK 8051

TRANSCRIPT

  • Microcontroller & Embedded system

    Seminar 6

    Multi-state systems

    Dr. Tran Thanh Hung Department of Automation Technology,College of Engineering Technology, Can Tho UniversityEmail: [email protected]

  • ReviewIn 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.

  • OutlineHow to implement for multi-state systems?multi-state systemsimplement for multi-state systems: type 1implement for multi-state systems: type 2

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

  • What are multi-state systems?Example of multi-state system: Traffic light system3 state system 4 state system

  • What are multi-state systems?Example of multi-state system: Washing machine

  • Types of multi-state systemsType 1: Multi-state (timed) system:The transition between states will depend only on the passage of time.- Example: Traffic light systemType 2: Multi-state (input/timed) system:The transition between states will depend both on the passage of time and system inputs.- Example: Washing machineType 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 2State 2 (RED-YELLOW): Turn both Red and Yellow lights on for 2 seconds and go to State 3State 3 (GREEN): Turn Green light on for 15 seconds and go to State 4State 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 50mswhile(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 systemsRepresent the statestypedef enum {RED,RED_YELLOW,GREEN,YELLOW} Light_State;static Light_State current_state = RED; //initial state = REDDefine duration for the states#define RED_DURATION 10#define RED_YELLOW_DURATION 2#define GREEN_DURATION 15#define YELLOW _DURATION 3Define global variables to measure timestatic unsigned char time_in_state = 0; // time (s) in each statestatic unsigned char call_count = 0; //use in EOS_ISR

  • Implement multi-state systemsUpdate states//This function must be called every 1s by EOS_ISRvoid 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 systemscase 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 1Write 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 systemsOperating description of the system:The user selects a wash program on the selector dial.The user presses the Start switch.The door lock is engaged.The water valve is opened to allow water into the wash drum.If the wash program involves detergent, the detergent hatch is opened. When the detergent has been released, the detergent hatch is closed.When the full water level is sensed, the water valve is closed.

  • Type 2 multi-state systemsIf 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.The washer motor is turned on to rotate the drum. At the end of the wash cycle, the motor is stopped.The pump is switched on to drain the drum. When the drum is empty, the pump is switched off.The water valve is opened to allow water into the wash drum.The washer motor is turned on to rotate the drum (to rinse detergent).The pump is switched on to drain the drum.

  • Type 2 multi-state systems

  • Type 2 multi-state systemstypedef 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 statestatic unsigned char call_count = 0; //use in EOS ISR

  • Type 2 multi-state systemsvoid 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 systemscase 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 systemscase 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 2Vit chng trnh cho my git, theo m t trnQuy c: Dngphm 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 ncLED 6 lm hp ng bt gitLED 5 lm b un ncLED 4 lm motor lng gitLED 3 lm bm ncTrng thi ERROR: Chp tt tt c cc LEDTrng thi WASH, RINSE: ko di 20s