Embedded Programming B Furman 09 MAY 2011 Learning
Embedded Programming B. Furman 09 MAY 2011
Learning Objectives n n Distinguish between procedural programming and embedded programming Explain the Events and Services embedded programming framework Explain what an Event is ¨ Explain what a Service is ¨ n n Explain the key rule and its two corollaries Describe how an event checking routine works for Discrete quantities ¨ Analog quantities ¨ n Develop code for: an event checking routine ¨ a service ¨ an event driven system ¨
Mechatronics Concept Map Power Source User Interface ME 106 ME 120 Controller ME 106 (Hardware & Software) Power Interface INTEGRATION ME 106 ME 154 ME 157 ME 195 Signal Conditioning ME 106 ME 190 ME 187 ME 106 ME 120 Actuator Sensor System to Control ME 110 ME 182 ME 136 ME 189 ME 154 ME 195 ME 157 ME 120 ME 297 A BJ Furman 22 JAN 11
Procedural vs. Embedded Programming n Procedural ME 30/Cmp. E 46 ¨ Computation and analysis programs ¨ Mostly sequential ¨ n ¨ Known inputs and outputs n ¨ Start … End Program is in control Predictable operation and timing n Embedded ME 106 ¨ Inputs and outputs can occur at any time, in any order and are not predictable ¨ Inputs can come from multiple sources ¨ n Sensors, user inputs, or internal (timer, ADC, etc. ) May handle simultaneous inputs and outputs ¨ Program never ends ¨
Event Driven Program Structure n Programming task divides into: Checking for events ¨ Servicing events when they occur ¨ n Event ¨ A detectable change or transition in something of interest n n n Button press (before: not pressed, after: pressed) ADC complete flag bit set Service ¨ An action taken in response to an event
Requirements for Events and Services The occurrence of events must be checked for continuously and often n Services must execute quickly and must be non-blocking n ¨ Ex. n Determine if a switch has closed Blocking code: ¨ while(digital. Read(pin) == HIGH);
Event Checkers for Discrete Events n Ex. Check that a switch has closed ¨ Pseudocode n IF switch is closed AND switch was open last time, THEN ¨ n ELSE ¨ ¨ Switch. Closed event has occurred Switch. Closed event has not occurred Need to keep track of the state of the switch (i. e. , maintain its history) n n Use a state variable Will need to be declared as a static local variable in the function that checks for the event ¨ Need to retain the value between successive calls to the event checking function
Code to Check for Discrete Events
Events involving an analog quantity A single-valued threshold will likely result in “chatter” when the quantify of interest is near the threshold http: //www. soe. ucsc. edu/classes/cmpe 118/Winter 08/Lecture. Notes/Event. Driven. Prog. pdf
Event Checkers for Events Involving Analog Quantities n n Filtering the signal may help Add hysteresis in the event checker ¨ Make the criteria for when the event occurs a variable instead of a fixed value n n ¨ Initially threshold is set to an upper value As soon as the signal crosses the threshold, the threshold is dropped to a lower value Pseudocode: n n Set threshold to high value IF var is greater than or equal to the threshold, THEN ¨ ¨ n Event has happened Set threshold to lower value ELSE ¨ Event has not happened
Event Detection with Hysteresis http: //www. soe. ucsc. edu/classes/cmpe 118/Winter 08/Lecture. Notes/Event. Driven. Prog. pdf
Code to Check for Events With Analog Quantities
Main body of Events & Services Code
- Slides: 13