Welsh Centre for Printing and Coatings
Tuesday October 15, 2024
When developing an application to run on a microcontroller or other embedded system, there are five key steps which should be followed:
/*
* This line basically imports the "stdio" header file, part of
* the standard library. It provides input and output functionallity
* to the program.
*/
# include <stdio.h>
/*
* Function (method) declaration. This outputs "Hello, world\n" to
* standard output when invoked.
*/
void sayHello(void) {
// printf() in C outputs the specified text (with optional
// formatting options) when invoked.
printf("Hello, world\n");
}
/*
* This is a "main function"/ The compiled program will run the code
* defined here.
*/
int main(void) {
// invoke the sayHello() function.
sayHello();
return 0;
}
A program or algorithm is a recorded sequence of instructions that performs a task or set of tasks when executed to produce a specific result.
Software is the term used to describe collections of programs, libraries, and related data.
A programming language is a formal notation comprising a set of instructions and rules for writing programs.
Figure 1: IBM type 704 electronic data processing machine used for making computations for aeronautical research at NASA Langley Research Center – 21st March 1957. (Image source: www.flickr.com/photos/nasacommons/9467782802)
Figure 2: Early program source code! Input into a computer with punched paper tape. Image Source: How it was: Paper tapes and punched cards | EETimes
Figure 3: Writing programs on a modern high resolution display screen. (Image source: unsplash.com/photos/ieic5Tq8YMk by Chris Reid)
Figure 4: Programming language level versus complexity
Figure 5: Word cloud of common programming languages
C is a general-purpose computer programming language. It was created in the 1970s by Dennis Ritchie, and remains very widely used and influential. Wikipedia - C (programming language)
A flowchart is a pictorial representation of the logic flow through a program.
Flowcharts are a useful tool for planning, visualizing and documenting a program without having to write any code.
Basic flowcharts are convenient because they
Bash (a command-line shell used in Unix and MacOS) |
BASIC |
MATLAB |
Python |
C (for Arduino) |
C++ |
HTML |
JavaScript |
In short flow charts should look visually tidy and the program flow should be obvious to the viewer.
Web-based drawing tool
From a software designer’s point of view, any program function can be described using only three program constructs:
Figure 12: Flowchart for a decision
Figure 13: Flowchart for a sequence of tasks
Task: Find the sum of the numbers 1 to 10
Program design method 1:
Figure 14 shows the flow chart of this program.
Figure 14: Flowchart of a program to sum the first 10 numbers written as a sequence.
START
sum = 0
sum = sum + 1
sum = sum + 2
sum = sum + 3
sum = sum + 4
sum = sum + 5
sum = sum + 6
sum = sum + 7
sum = sum + 8
sum = sum + 9
sum = sum + 10
END
Task: Find the sum of numbers 1 to 10
Program design method 2:
Figure 15 shows the solution of task 1 using a decision and repetition.
Figure 15: Flowchart of the solution to task 1 using a decision and repetition.
START
sum = 0
counter = 1
WHILE counter < 11
DO
sum = sum + counter
counter = counter + 1
END_DO
END
Consider a program that asks a user for a ‘limit’ value and sums the numbers up to that limit before outputting the sum?
I.e. \(\mathrm{sum} = 1 + 2 + 3 + \dots n\)
Figure 16: A program that asks for a limit value and sums numbers up to that limit before outputting the sum.
Let us tabulate the results of this program assuming that \(n=4\).
We will complete the table in class:
sum |
counter |
counter > limit |
---|---|---|
\(0\) | \(1\) | No |
\(2\) | ||
\(3\) | ||
\(4\) | ||
\(5\) |
For our program summing the numbers up to a limit, there are three key parts.
sum = sum + counter
counter = counter + 1
Variation | Part 1 | Part 2 | Part 3 |
---|---|---|---|
1 | C | S | I |
2 | C | I | S |
3 | I | C | S |
4 | I | S | C |
5 | S | C | I |
6 | S | I | C |
Figure 17: Variation 6: Sum - Increment - Check
Figure 18: Variation 4: Increment - Sum - Check
Figure 19: Variation 2: Check - Increment - Sum
What if we counted backawards from \(n\)?
\[\mathrm{sum} = 1 + 2 + 3 + \cdots + \left(n - 1\right) + n\]
\[\equiv\]
\[\mathrm{sum} = n + \left(n-1\right) + \cdots 3 + 2 + 1\]
Let us tabulate the results of this program assuming that \(n=4\).
sum |
counter |
counter > limit |
---|---|---|
\(0\) | \(4\) | |
\(3\) | ||
\(2\) | ||
\(1\) | ||
\(0\) |
A flow chart that implements this variation is given in Figure 20.
Figure 20: Another variation: Check - Decrement - Sum
The basic approach to modularization is to consider a top-down design where the top level defines the main function, and a separate module represents each smaller function in the program (see Figure 21).
Write a program to perform n different tasks with a ten second delay between each task.
The program we have been discussing that sums the numbers 1-10 can be used to simulate a delay.
DO
Task 1
END
/* Delay between task 1 and task 2 */
DO
number = 0
WHILE number < 11
DO
number = number + 1
END_DO
END_WHILE
END
DO
Task 2
END
/* Delay between task 2 and task 3 */
DO
number = 0
WHILE number < 11
DO
number = number + 1
END_DO
END_WHILE
END
DO
Task 3
END
:
Now suppose that we want to change the delay between tasks to 20 seconds.
Using a non-modular approach the developer/programmer now needs to change the line
WHILE number < 11
to
WHILE number < 21
For each of the n-1 times it is used.
The code that implements the delay is made into a module which is called when needed
MODULE delay
DO
number = 0
WHILE number < 21
DO
number = number + 1
END_DO
END_WHILE
END
END_MODULE
The modular program becomes:
DO
Task 1
END
/* Delay between task 1 and task 2 */
DO
delay
END
DO
Task 2
END
/* Delay between task 2 and task 3 */
DO
delay
END
DO
Task 3
END
:
With the time remaining can you represent this program as a flow chart?
Feel free to work individually or in small groups.
See Figure 22 in the notes online.
In this chapter we have:
This week on the canvas course page, there is:
Please use the Course Question Board on Canvas or take advantage of the lecturers’ office hours.
In most programming languages, text is stored in memory as a sequence of binary codes which each represent the individual characters as in 'H'
, 'e'
, 'l'
, 'l'
, 'o'
, ','
, ' '
, 'W'
, 'o'
, 'r'
, 'l'
, 'd'
, '!'
. Such a sequence is usually called a string.
Repetition is used a lot in microcontroller programming.
Pseudocode is a formal text which is used to represent the operation of a section of code in a way that can be understood without needing to resort to formal rules of a programming language.
In science, computing, and engineering, a black box is a system which can be viewed in terms of its inputs and outputs (or transfer characteristics), without any knowledge of its internal workings. Its implementation is “opaque” (black). The term can be used to refer to many inner workings, such as those of a transistor, an engine, an algorithm, the human brain, or an institution or government.—Blackbox (Wikipedia)