Input Signal Edge Detection using Software

In the digital design, sometimes we need to detect the transition from '0' to '1' or '1' to '0' of a signal.

For example, suppose you design a counter with a input pin connected to a push button. When you push the button every time, the counter will count by one. The circuit diagram is shown as Figure 1.

SoftwareEdgeDectector s
Figure 1
: Digital Counter

In other words, the counter counts the number of times the switch transitions from open to close state. The pseudoscope is shown as follows:

if (SW1 from open to close) counter ++;

Some microcontrollers provide edge detect circuitry, and you can configure edge detection on the desired pins. In this example, we configure it on PA3. If the edge is detected, a flag will be set in the status register, or trigger an interrupt to the CPU. In your C code, you just need to check the status register, or implement an ISR function to increase the counter value. But here, we only discuss how to implement edge detection using software.

 

Implementation of Edge Detection Algorithms

SIgnalEdges
Figure 2
: Rising Edge and Falling Edge

If a digital signal rises from low (0) to high (1), this transition is called "rising edge". On the contrary, if it drop from high to low, it is called "falling edge". Each transition has two states — previous state and current state. Current state of input pins can be read from the port DATA register, but the previous state must be stored in a variable.

Every time you read the current input state, compare it to the previous state value. If current state is not equivalent to previous state, a transition had occurred. Before the next cycle starts to read the current input state, the previous state needs to be updated by the current state.

 

Implementation of Edge Detection in C

There are two methods to implement software edge detection:

  1. Simple if statement
  2. State machine

 

Polling Method in Embedded Programming

Polling operation in embedded programming refers to actively sampling the status of an external device. Polling is most often used in terms of input/output (I/O), and it is the process where the embedded system waits for an external device to check for its readiness or status. For example, we have a simple microcontroller device with one push button and one LED. When we press the button, LED will be lit. If we press the button again, then the LED will be off. The pseudocode code would be like:

loop:
    if button pressed, toggle LED
repeat

In this program you can see that the microcontroller always checks the status of the button and then decides the LED condition.

A real-life example about polling method is you are working on homework in your room, and waiting for UPS to deliver an important package. The polling method is exactly like you often (may be every 5 minutes) go to the front door and open the door to check whether your package has come or not.

Polling Cycle

Polling basically means that the embedded system will check the all the status for each device at set intervals, which is called polling cycle. For example, check the switch state every 5 ms, detect the obstacle every 20 ms, and read the room temperature every 5 sec. The optimal polling cycle will vary according to several factors, including the desired speed of response and the overhead (e.g. processor time and bandwidth) of the polling.

 

Implementation of Polling Method in a Single Task

For a simple embedded system, it is usually designed to execute multiple jobs in a single task, such as in the main() function. There are two types of polling methods can be used in the code, as shown below. Assume that the latency time on each job can be ignored.

 

State Machines in C

If a system functionality is where outputs depend on the order in which input events occur, or input events that occur at different times (states) will cause to different output, then these models are called "Time-Ordered Behavior". For example:

  • An electronic lock may require a user to press and release button 5, then 3, then 2 to unlock a door
  • A fan has only one button. When a user first presses the button to turn on fan at 25% speed. If the user presses the button continuously, the fan speed will be changed to 50%, 75%, then 100%. When the fan speed reaches to 100% and user presses the button again, the system will turn off the fan.

State machines are perhaps the most effective method for capturing Time-Ordered Behavior for embedded system. A state machine has several components:

  • Set of inputs and outputs
  • Set of states with actions
  • Set of transitions with conditions
  • and an initial state

The following tools are used to describe the time-ordered behaviors:

  • State Diagram:
    A drawing of a state machine, also called State Transition Graph, (STG). It is a popular way for representing state machines.
    A state diagram includes the following components:
    • State: stores information about the past and reflect changes from the start of system to the present state.
    • Current State: determined by past states of the system.
    • Transition: indicates a change from one state to another. Transitions are invoked by Events.
    • Event: the inputs to a state machine.
    • Action: also called an output. It is a description of an activity that is to be performed as a result of an input and change of state. An event can be depicted either on the transition or within the state.

    MooreSM 01MealySM 01
    Figure 1
    : State Diagrams
  • State Transition Table:
    A state transition table (or state table) describes a state machine in a tabular format.

    Table 1: State Transition Table
    SM DataTable

    This simple model exemplifies a LED controller that embodies two states: LED off (S_IDLE) and LED on (S_1) and two inputs: SW1 and SW2. If the LED is in the S_IDLE state, and an input SW1 is presented, the state machine progresses to the S_1 state. If an input of SW1 is presented to the machine in the S_1 state the machine stays in the S_1 state.

 

There are two models for incorporating actions into state machines:

  • Moore
    A Moore machine performs actions when entering a state. Each state may have it's own entry action.
  • Mealy
    A Mealy machine performs actions on transitions. Each transition in a state machine may invoke a unique action.

Moore and Mealy state machines have their own advantages and disadvantages. But one of great advantage of both is that they are not mutually exclusive, and these two models can be used together in the system.

 

 

What are the difference between Moore and Mealy state machines? The following table shows the point the differences from a Moore machine to  a Mealy machine:

Moore State Machine Mealy Sate Machine
Outputs depend on current state only
outputs = F(currentState);
nextState = F(inputs, currentState);
Outputs depend on current state and the inputs
outputs = F(inputs, currentState);
nextState = F(inputs, currentState);
Generally, it has more states then Mealy Generally, it has fewer states than Moore
Output does not react immediately to input change (one clock cycle later) Outputs have immediate reaction to inputs
Output is placed on states Output is placed on transitions
Easy to design It it difficult to design

 


Implement State Machine in C

State machine for a stoplight controller is shown as Figure 5.

StoplightController SM
Figure 5: Stoplight Controller

A state diagram can be implement in three main ways: Nested Switch, the State Pattern, and State Tables.

State machines are a very useful programming model, especially when the system needs to act according to its history (state) and the environment (event). Implementing code using a state machine is very difficult to maintain, because the system reacts differently depending on the state and event. Therefore, a good documentation is very important.

 

State Machines v.s. Flowcharts

Maybe you will confuse state machines with flowcharts. The most important difference between state machines and flowcharts is that the state machines perform to next actions (state) only in response to explicit events, they are event driven. In contrast, flowcharts do not need to be triggered by events; they are used to describe the progression of the task from beginning to end, step by step.

StateMachineVsFlowchart
Figure 6
: State Machine vs Flowchart

The distinction between state machines and flowcharts is especially important when analyzing system behavior. Because these two concepts represent two diametrically opposed programming paradigms: state machines are event-driven programming; and flowcharts are transformational programming.

When you design a state machine, you have to constantly thinking about available events (inputs). In contrast, events are not the first concern for flowcharts, but procedures.

 

Set, Clear, Toggle and Check Bit Value in C

In the embedded system design, the system needs to check the input state and then send signal to other devices, or change the state of the outputs. Since each GPIO pin is connected to a different device (some pins are input and other pins may be output or other functions), it is not possible to check the pin state by comparing with a constant value. Setting, clearing and toggling pins also have the same situation: constant values can not be assigned directly to the port DATA register. Because each GPIO pin is associated with the corresponding bit in the port DATA register, a bitwise operation can be used to solve these issues.

EmbeddedSystem01
Figure 1
: Example of Embedded System

 

 


Bit Access Functions

The following functions get or set a particular bit of a variable can make programs easier to read.

 

Go to top