what is state
play

What is state? You see a DPS officer approaching you. Are you happy? - PowerPoint PPT Presentation

5.1 5.2 What is state? You see a DPS officer approaching you. Are you happy? It's late at night and ______________________. It's late at night and you've been ___________________________. Unit 5 Your interpretation is based on


  1. 5.1 5.2 What is state? • You see a DPS officer approaching you. Are you happy? – It's late at night and ______________________. – It's late at night and you've been ___________________________. Unit 5 • Your interpretation is based on more than just what your senses are telling you RIGHT NOW, but by what ___________ _____________________ State Machines – The sum of all your previous experiences is what is known as _______ – Your ________ determines your ______________ of your senses and thoughts • In a circuit, 'state' refers to all the _______ being remembered (___________ or memory) • In software, 'state' refers to all the _____________ values that are being used 5.3 5.4 State Machine Block Diagram State Diagrams • A system that utilizes state is often referred to as a state machine • Abstractly a state machine can be visualized and represented – A.k.a. ____________________________________________ as a flow chart (or state diagram) • Most state machines can be embodied in the following form – Circles or boxes represent _________ – Logic examines what's happening NOW (inputs) & in the PAST (state) to… – Arrows show what input causes a _______________ • Produce outputs (actions you do now) – Outputs can be generated whenever you reach a particular state or • Update the state (which will be used in the future to change the decision) based on the combination of state + input • Inputs will go away or change, so state needs to summarize/capture State Machine to check for two consecutive 1's on a digital input anything that might need to be ___________ and used in the future Input=__ Input=__ Input=__ Input=__ Inputs Logic Outputs (A-to-D, Timer, S0 S1 S2 Buttons) Out=False out=True Out=False Input=__ State On startup Input=__ (memory)

  2. 5.5 5.6 Washing Machine State Diagram Washing Machine State Diagram /RESET /RESET Stay in the initial state Move to the Fill state until there is enough when there is enough COINS + DOOR COINS + DOOR Idle Idle money (coins) and money (coins) and N=2 N=2 the door is closed the door is closed COINS • DOOR COINS • DOOR Fill Fill FULL FULL WV = 1 WV = 1 FULL FULL Agitate Agitate We move through the 5 MIN 5 MIN Motor = 1 Motor = 1 states based on the conditions. Outputs 5 MIN / Reset_Timer=1 5 MIN / Reset_Timer=1 get asserted when the Drain Drain machine is in that EMPTY EMPTY DV = 1 DV = 1 state and the transition is true. EMPTY EMPTY N > 0 N > 0 N = N - 1 N = N - 1 N = 0 N = 0 5.7 5.8 Washing Machine State Diagram Washing Machine State Diagram /RESET /RESET COINS + DOOR COINS + DOOR Idle Idle Stay in the Fill state N=2 N=2 until it is full…also COINS • DOOR COINS • DOOR Move to the Agitate set the Water Valve state after it is full Open output to be Fill Fill FULL FULL true WV = 1 WV = 1 FULL FULL Agitate Agitate 5 MIN 5 MIN Motor = 1 Motor = 1 5 MIN / Reset_Timer=1 5 MIN / Reset_Timer=1 Drain Drain EMPTY EMPTY DV = 1 DV = 1 EMPTY EMPTY N > 0 N > 0 N = N - 1 N = N - 1 N = 0 N = 0

  3. 5.9 5.10 Thermostat Counter Example • Sample state machine to control a thermostat • Consider a system that has two button inputs: UP and DOWN and a 1-decimal digit display. It should count up or down at a rate of 500 temp < THESH_LO milliseconds and change directions only when the appropriate direction OFF HEAT button is pressed PROG_BTN heater = off heater = on • Every time interval we need to _______________ to check for a direction ac = off OFF_BTN RUN_BTN DONE_BTN change, _______ the state and then based on the current state, PROGRAM temp < THESH_LO ______________________ the count (Update THRESH_HI & temp >= THRESH_LO State Machine to count up or down (and continue counting) based on 2 RUN_BTN THRESH_LO) pushbutton inputs: UP and DOWN INRANGE ____________ heater = off ____________ ______ PROG_BTN ac = off temp <= THRESH_HI UP DIGIT UP DOWN Counter temp > THESH_HI DISPLAY cnt++ (wrap cnt-- (wrap to DOWN COOL to 0 after 9) 9 after 0) ______ ac = on On startup temp > THESH_HI 5.11 5.12 Software vs. Hardware Software Implementation • Store 'state' in some variable and assign ________ to • Software • Hardware represent state (0=Idle, 1=Fill, etc.) – State = just a variable(s) – State = Register int main() • Use a timer or just ______ (D-Flip-Flops) – Logic = if statements to { certain inputs and then make bool coins, door; update the next state – Logic = AND/OR Gates to produce int currst = 0, nextst = 0, n = 2; appropriate transitions the next state & outputs • if(state == 'A' && input == 1) while(1) { { state = 'B'; } /RESET – Transitions triggered by clock _delay_ms(10); – Transitions triggered by input signal coins = PIND & (1 << PD0); COINS + DOOR Idle door = PIND & (1 << PD1); or timers N=2 – More on this later in the if(currst == 0){ COINS • DOOR – We'll start by implementing if( coins && door ){ semester Fill FULL WV = 1 nextst = 1; state machines in SW FULL } Agitate Use nested 'if' 5 MIN } Motor = 1 statements: Inputs Outputs else if(currst == 1){ 5 MIN / Reset _Timer=1 Logic outer 'if' selects Drain ... EMPTY (ADC, Timer, DV = 1 state then inner } EMPTY Buttons) 'if' statements ... N > 0 N = N - 1 examine inputs State currst = nextst; // update state N = 0 } (memory) State Diagram for a return 0; Washing Machine }

  4. 5.13 5.14 State Machine Implementation Template More Implementation Tips int main() { ... // input = PD0, output = PD7 unsigned char cstate=0, nstate=0; // be sure to init. state int main() • Continuously loop unsigned char input, output; { // be sure to init. state while(1) unsigned char state=0, nstate=0; { • Each iteration: _delay_ms(10); // choose appropriate delay unsigned char input, output; while(1) – Poll inputs // Capture inputs { input = PIND & (1 << PD0); – Use _____________ to decide _delay_ms(10); // Use if..else if statement to select current state input = PIND & (1 << PD0); current state // Don't use if..if..if since many can trigger if(state == 0){ if(cstate == 0){ – In each state, update state PORTD &= ~(1 << PD7); PORTD &= ~(1 << PD7); // Perform outputs based on state Select current state // In each state use if..else if statement if( input ){ nstate = 1; } appropriately based on desired // to determine input and go to next state } if( input ){ Select input val. transitions from that state else if(state == 1){ nstate = 1; /* transition */ PORTD &= ~(1 << PD7); // Perform outputs based on state + inputs – Produce appropriate output from } if( input ){ nstate = 2; } else { that state else { state = 0; } nstate = 2; /* transition */ } // Perform outputs based on state + inputs } else { // state == 2 Select input val. } PORTD |= (1 << PD7); else if(cstate == 1){ if( !input ) { nstate = 0; } if( input ){ nstate = 2; } } else { nstate = 0; } } state = nstate; // update state else if(cstate == 2) { } if( !input ) { nstate = 0; } return 0; } } } return 0; } 5.15 5.16 Formal Definition State Machines as a Problem Solving Technique • Modeling a problem as a state machine is a powerful • Mathematically, a state machine is defined by a 6-tuple (a tuple is just several pieces of information that go together): problem-solving tool – A set of possible _____________ • When you need to write a program, design HW, or – A set of possible _____________ solve a more abstract problem at least consider if it – A set of possible _____________ can be modeled with a state machine – An _______________ – A transition function: {______ x ______} -> ______________ – Ask questions like: – An output function: {______ x _______} -> ________________ • What do I need _______________ to interpret my inputs or produce my outputs? [e.g. Checking for two consecutive 1's] • Is there a distinct sequence of __________________ that are used (each step/mode is a ________) [e.g. Thermostat, washing machine, etc.]

  5. 5.17 5.18 Formal Definition • Mathematically, a state machine consists of: Inputs – A set of possible input values: {________} State 0 1 – A set of possible states: {____________} S0 – A set of possible outputs: {___________} S1 – An initial state = ____ S2 – A transition function: • {States x Inputs} -> the Next state State Transition Function – An output function: HW (Instruction Cycle) & Software (String Matching) • {States x Inputs} -> Output value(s) State Outputs MORE EXAMPLES IF TIME S0 S1 S2 Output Function 5.19 5.20 More State Machines Another Example • On the Internet, packets of data are transferred between “router” devices • State machines are all over the place in digital • Each router receives thousands of packet per second each of systems 100’s-1000’s of bytes of data • Instruction Cycle of a computer processor • These packets may contain viruses, spam, etc. • Given patterns (common spam words or virus definitions), can ! Error && ! Interrupt we find these in the data and filter them out? 1110 0010 0101 1001 0110 1011 0000 1100 0100 1101 0111 1111 1010 1100 0010 1011 Fetch Decode Execute 0001 0110 0011 1000 On Startup Process Error || Interrupt Exception

Recommend


More recommend