Chapter 7: Design and Development
Jonathan Valvano and Ramesh Yerraballi
As part of the edX online class, we made some interactive web pages to illustrate fundamental concepts
2. Fundamental Concepts | Number conversions |
5. Introduction to C | Flowcharts, C vs assembly |
6. Microcontroller Ports | Input/output, direction register |
7. Design and Development | Successive refinement, if-then, loops |
10. Finite State Machines | Vending machine and stepper motor |
11. UART Serial Interface | Blind, busy-wait, interrupt, serial port |
12. Interrupts | Mail box, context switch |
13. DAC and Sound | Sampling rate, precision, how a DAC works |
14. ADC and Data Acquisition | How an ADC works, Nyquist Theorem |
The system has seven binary inputs from the switches and one binary output to the door lock. The state of this system is defined as “door locked” and “door unlocked”. Initially, we want the door to be locked, which we can make happen by turning a solenoid off (make binary output low). If the 7-bit binary pattern on the switches matches a pre-defined keycode, then we want to unlock the door (make binary output high). Because the switches might bounce (flicker on and off) when changed, we will make sure the switches match the pre-defined keycode for at least 1 ms before unlocking the door. We can change states by writing to the output port for the solenoid. Like most embedded systems, there is no ending state. Once the switches no longer match the keycode the door will lock again. The first step in successive refinement is to divide the tasks into those performed once (Initialization), and those tasks repeated over and over (Execute lock), as shown as the left flowchart in the Interactive tool 7.0 As shown in the middle flow chart, we implement if the switches match the key, then unlock. If the switches do not match we will lock the door. To verify the user entered the proper keycode the switches must match, then match again after 1ms. There are two considerations when designing a system: security and safety. Notice that the system will lock the door if power is removed, because power applied to the solenoid will unlock the door. For safety reasons, there should be a mechanical way to unlock the door from the inside in case of emergency.
The
animation below
shows how successive refinement is done in designing a solution to this
problem. Click on the expand button to generate new flowcharts.
If-Then
Statement -
The statements inside an if statement will execute once if the
condition is true. If the condition is false, the program will skip
those instructions. Choose two unsigned integers as variables a and b,
press run and follow the flow of the program and examine the output
screen. You can repeat the process with different variables.
variable a:
variable b:
C Code | |
---|---|
volatile long a; volatile long b; int main () { printf("Starting the Construct ...\n"); if (a<b){ printf("a is less than b\n"); } printf("Ending the Construct ...\n"); return 0; } |
|
Output Screen | |
If-then-else - If
statements can be expanded by an "else" statement. If the condition is
false, the program will execute the statements under the "else"
statement. Choose two unsigned integers as variable a and b, press run
and follow the flow of the program and examine the output screen. You
can repeat the process with different variables.
variable a:
variable b:
C Code | |
---|---|
volatile unsigned long a; volatile unsigned long b; int main () { printf("Starting the Construct ...\n"); if (a<b){ printf("a is less than b\n"); } else { printf("a is not less than b\n"); } printf("Ending the Construct ...\n"); return 0; } |
|
Output Screen | |
The
while loop - The
statements inside a while statement, will continuously be executed if
the while condition is true. If the condition is/becomes false, the
program will skip the loop and continue with the execution of the
remaining statements. Choose an unsigned integer less than 100000 as
variable a, press run and follow the flow of the program and examine
the output screen. The loop continuously divides the variable a by 10
and outputs the result. You can repeat the process with different
variables.
variable a:
C Code | |
---|---|
volatile unsigned long a; // a is always less than 100000 int main () { printf("Starting the loop ..., a = %d\n",a); while (a>0){ a = (a/10); printf("current a = %d\n",a); } printf("Ending the loop ...\n"); return 0; } |
|
Output Screen | |
The
do-while loop -
The statements inside a do-while statement will always be executed
once, and will continuously be executed if the while condition is true.
If the condition becomes false, the program will skip the loop and
continue with the execution of the remaining statements. Choose an
unsigned integers less than 100000 as variable a, press run and follow
the flow of the program and examine the output screen. The loop
continuously divides the variable a by 10 and outputs the result. You
can repeat the process with different variables.
variable a:
C Code | |
---|---|
volatile unsigned long a; int main () { printf("Starting the loop ..., a = %d\n",a); do { a = (a/10); printf("current a = %d\n",a); } while (a>0); printf("Ending the loop ...\n"); return 0; } |
|
Output Screen | |
The
for loop - A for
loop functionality is similar to a while loop, with automatic
initialization and update. It is usually used when the number of
iterations is known. One variable is used as a counter of iterations.
The first field of a "for loop" is the initialization, which is always
executed once. The second field is the condition to be checked. The
loop will continue being executed until the condition becomes false.
The third field (update) is executed at the end of each iteration and
is usually used for incrementing/decrementing the counter. Choose a
number less than 6 for variable a, and observe the flow of the
following program. You can repeat the process with different variables.
variable a:
C Code | |
---|---|
volatile long a; // a must be less than 6 in this example. int main () { int i; printf("Starting the loop ...\n"); for (i=0;i<a;i++){ printf("current i = %d\n",i); } printf("Ending the loop ...\n"); return 0; } |
|
Output Screen | |
Reprinted with approval from Embedded Systems: Introduction to ARM Cortex-M Microcontrollers, 2016, ISBN: 978-1477508992, http://users.ece.utexas.edu/~valvano/arm/outline1.htm