GET 212 - Fundamentals of Computer Aided Engineering - Lecture 1 - Introduction To Arduino C Programming-Released
GET 212 - Fundamentals of Computer Aided Engineering - Lecture 1 - Introduction To Arduino C Programming-Released
Aided Engineering
Lecture 1: Introduction to CAE and Arduino C
Theory
11:00 Course Learning Outcome and Course Plan [15 mins]
11:15 What is Computer Aided Engineering? [10 mins]
11:25 What we need: Linux OS, Arduino Starter Kit? [20 mins]
11:50 Break [10 mins]
Exercises/Practical
12:00 Lecture Ia: Gentle introduction to Linux – Ubuntu [30mins]
12:30 Lecture Ib: Gentle introduction to Arduino C [20mins]
12:50 Prepare for Laboratory Practical
Agenda - Topic No 1
Introduction to CAE and Arduino C - Practical
3
Practical
2:00 What is in the Starter Kit? [20 mins]
2:20 Installing CAELinux 2020 [15 mins]
2:40 Lesson 0 - Installing Arduino IDE [15 mins]
2:50 Break [10 mins/Catch Up]
3:00 Lesson 1 Add Libraries and Open Serial Monitor [20 mins]
3:30 Lesson 2 Blink [20 mins]
3:50 Break [20 mins/Catch Up]
Embedded Systems
Operating System
• Operating system is a software program that manages the computer
hardware, system resources, memory, and interaction between the
system and its users.
• Some popular operating systems are:
• Linux
• Unix-Solaris, SCO and Irix, AIX, HP-UX
• Microsoft DOS
• Microsoft Windows Vista
• Microsoft Windows Server 2012
• Android, BlackBerry, BSD, Chrome OS, iOS, Linux, share roots
in UNIX
❑ About UNIX http://www.unix.org/what_is_unix.html
❑ About Linux http://www.linux.org
Introduction to CAE and Arduino C - Lecture
What we need
Evolution of Linux
• In 1991, Linus Torvalds, a student, while working on Minix
wrote the source code for the kernel and named it Linux.
• Torvalds made the Linux kernel available on the Internet.
• The Linux kernel was combined with the GNU system resulting
in a complete operating system called GNU/Linux because it
was a combination of the GNU system and Linux as the kernel.
• Linux follows the open development model which means that
the current development version of Linux is always open to
everybody.
• Tux, the Linux penguin, is the official mascot.
GNU is a recursive acronym for "GNU's Not Unix!", chosen because GNU's design is Unix-like, but differs
from Unix by being free software and containing no Unix code. Stallman chose the name by using various
plays on words, including the song The Gnu
Introduction to CAE and Arduino C - Lecture
What we need
❑ About Linux
• http://www.linux.org
❑ Distributions
• Red Hat based
• Debian based
• Slackware based
• Mandriva based
Linus Torvalds
• Fedora based
• SUSE based
Introduction to CAE and Arduino C - Lecture
What do we need – ARDUINO IDE?
Introduction to CAE and Arduino C - Lecture
What do we need – ARDUINO IDE?
Installing Libraries
Introduction to CAE and Arduino C - Lecture
What do we need?
Linux Architecture
Components of Linux operating system
Exploring Linux Command Line Tools
Features and Advantages of Linux
• The kernel:
• Is the core of Linux operating system program
• Controls the resources of the computer
• Interacts directly with the hardware
• The shell:
• Is a user interface providing services that a user wants
• Protects the user from knowing the intricate hardware
details
• Accepts and interpret text-mode commands
• Linux utilities and application programs:
• Are a collection of programs that service day-to-day
processing requirements
• Are invoked through the shell
Exploring Linux Command Line Tools
Features and Advantages of Linux
Distributions of Linux
• All the distributors use the Linux kernel.
• The distributors add their own utilities and
applications and sell these as a customized package.
• The following are the popular distributors of Linux:
• Red Hat and its variations, such as Linux ES, Linux
AS, Linux WS, Desktop, and Fedora Core
• Mandriva
• Debian (eg Ubuntu)
• SuSE
• Slackware
Exploring Linux Command Line Tools
Features and Advantages of Linux
Features of a Shell
• The shell has the following features:
• Interactive processing
• Background processing
• Input/output redirection
• Pipes
• Shell scripts
• Shell variables
• Programming language constructs
• Command history
• Command alias
• Job control
• The cd spell checker
• File name completion
• Command completion
Exploring Linux Command Line Tools
Features and Advantages of Linux
Unix Shells
• Some of the popular Unix Shells are:
• Bourne Shell: sh is the executable filename for
this shell
• C Shell: csh is the executable filename for this
shell
• Korn Shell: The executable filename is ksh
• Restricted Shell: Is typically used for guest
logins
Exploring Linux Command Line Tools
Features and Advantages of Linux
Linux Shells
• Some of the popular shells available in Linux are:
• Bash:
• Is an acronym for ‘Bourne Again Shell’ and is the default shell for most
Linux systems
• Uses the symbolic link sh, which is stored under the /bin directory in Fedora
Core 2
• Tcsh:
• Is an acronym for ‘Tom’s C shell’ also known as the TC shell
• It is an enhancement of the C shell
• Uses the symbolic link csh
• Can be executed by using either csh or tcsh at the shell prompt
• ASH:
• Is usually suitable on machines that have very limited memory
• Uses the symbolic link, bsh in Fedora Core 2
Exploring Linux Command Line Tools
Features and Advantages of Linux
Linux Shells
• bsh – Bourne Shell upon which bas is
built. Not often used in Linux
• csh – the original C Shell. Not often used
• ksh – the Korn Shell (ksh) takes the best
of Bourne Shell and C Shell.
• zsh – Z Shell form Korn Shell
Exploring Linux Command Line Tools
Features and Advantages of Linux
Example
pwd
Example
echo Hello
Example
exec myprogram
Exploring Linux Command Line Tools
Features and Advantages of Linux
Example
time pwd
Exploring Linux Command Line Tools
Features and Advantages of Linux
Example
set variable
Exploring Linux Command Line Tools
Features and Advantages of Linux
Environment Variables
• All exported variables are environment variables, some of which are
meaningful to the shell.
• By changing the values of these variables, a user can customize the
environment.
• Some of the environment variables are:
• HOME: Stores the location of the HOME directory of a user
• PATH: Contains a list of colon-delimited path names of directories that
are to be searched for an executable program
• PS1: Contains the shell prompt, the $ symbol. You can change the shell
prompt by setting the value of this variable to the desired prompt
• PS2: Sets a value for the secondary prompt, which is by default >
• LOGNAME: Contains the user’s login name
• SHLVL: Contains the shell level that you are currently working in
• SHELL: Stores the user’s default shell
Exploring Linux Command Line Tools
Features and Advantages of Linux
Getting Help
man – Manual
• Text-based help document
• Uses the less pager, Exit with Q
• Organised in sections
1 – Executable programs
2 – System calls
3 – Library calls
4 – Device files
5 – File formats
6 - Games
to
9 – Kernel routine
Streams
• Inputs and outputs from programs are
treated as streams
• Standard input
• Standard output
• Standard error
Exploring Linux Command Line Tools
Features and Advantages of Linux
Standard Files
• A computer system consists of mainly three parts:
•The input devices, such as keyboard and mouse,
are used to accept data from the user.
•The output devices, such as monitor and printer,
are used to display/print information or any error
messages that might occur.
•The processing device, such as CPU, processes
the user input according to the specified
instructions, to generate the desired output.
Exploring Linux Command Line Tools
Features and Advantages of Linux
Redirection
• Redirection:
• Changes the assignments for the standard input, standard
output, and the standard error
• Enables you to take input to a command from a file other
than the keyboard
• Enables you to write the output of a command or the error
to a disk file or printed, instead of the monitor
• Is of the following three types:
• Input redirection
• Output redirection
• Error redirection
Exploring Linux Command Line Tools
Features and Advantages of Linux
Input Redirection
• The following example illustrates the usage of input
redirection:
$ cat < test1
•The less than symbol, <, implies input redirection
from the file, test1.
• The above command can also be written using the
file descriptor as:
$ cat 0< test1
•In the preceding code, 0 indicates input
redirection.
Exploring Linux Command Line Tools
Features and Advantages of Linux
Output Redirection
• The following example illustrates the usage of output redirection:
$ cat test1 > out_test
• The greater than symbol, >, implies redirection of output to the file, test2.
• In output redirection, the file to which the output is redirected is first created
on the disk as an empty file and then the output is sent to this file.
• If the file already exists, its content is deleted before the output is written to it.
• If you want to append the output to the file, test2, the command is:
$ cat test1 >> out_test
• The previous commands can also be written using the file descriptor as:
$ cat test1 1> out_test
$ cat test1 1>>out_test
Exploring Linux Command Line Tools
Features and Advantages of Linux
Error Redirection
• Error redirection first creates the file to which the error
messages are redirected and then writes the error output to the
file.
• The following example illustrates the usage of error
redirection:
$ cat test_2 2> error-mesg
• In the preceding command, the file, test_2, does not exist in
the current directory.
• When a user tries to execute the preceding command, Linux
will generate an error message because the execution is
unsuccessful. This message, which would otherwise be
displayed on the monitor (the standard error file), is written
to the file, error-mesg.
Exploring Linux Command Line Tools
Features and Advantages of Linux
Filters
• A filter is a program that takes its input from the
standard input file, processes (or filters) it, and sends
its output to the standard output file.
• Some examples of filters are:
•grep
•cat
•wc
•tr
•cut
Exploring Linux Command Line Tools
Features and Advantages of Linux
• You can also specify complex regular expressions, such as, [], [] with
hyphen, ^, ^ within [], $, . (dot), and \ (backslash).
Exploring Linux Command Line Tools
Features and Advantages of Linux
The wc Filter
• The wc filter:
• Is used to count the number of lines, words, and characters in
a disk file or in the standard input
• Has the following syntax:
wc [option] [filename]
• The following table lists the options of the wc filter:
Option Function
Option Function
The tr Filter
• The tr filter:
• Can be used to translate one set of characters to another
• Can also be used to squeeze repeated occurrences of a
character into one
• Uses the -s option to squeeze several occurrences of a
character into one character. For example,
who > temporary
$ tr -s " " < temporary
root tty1 Sep 28 17:02
steve pts/4 Sep 28 19:36 (172.17.55.167)
• Enables you to perform case-conversion
Exploring Linux Command Line Tools
Features and Advantages of Linux
Option Function
-r Sorts the input in reverse order
-f Arranges the input to the sort filter consisting of digits,
alphabets, and other characters in the ASCII sequence
-n Arranges the numbers also in ASCII sequence
<filename> Sorts the content of the specified file
+pos1 –pos2 Sorts a file in the order of a specific column
-t Creates a file with a different column separator
-o Saves the output of the sort filter to a disk file
Exploring Linux Command Line Tools
Features and Advantages of Linux
Pipes
• Pipes enable you to combine multiple commands and execute
them as a single command. For example,
$ ls | more
• Pipes are represented using the vertical bar (|), which
indicates to the shell that the output of the command before
the ‘|’ is to be sent as input to the command after the ‘|’.
The following figure shows the working of a pipe:
Exploring Linux Command Line Tools
Features and Advantages of Linux
Review
1. Summarize features that Linux shells offer to speed up
command entry.
The command history often enables you to retrieve an earlier command that's similar or identical to the
one you want to enter. Tab completion reduces typing effort by letting the shell finish long command
names or filenames. Command-line editing lets you edit a retrieved command or change a typo before
committing the command.
Review
4. Describe the difference between standard output and
standard error.
Standard output carries normal program output, whereas standard error carries high-priority output,
such as error messages. The two can be redirected independently of one another.
Microprocessors
Microcontrollers
❑ Microcontroller is single chip device containing the processor along with
memory and interface devices on the same integrated circuit chip.
❑ Developed for use in control appliances, automotive, entertainment and
telecommunications industries
❑ Compact and low cost
❑ RAM from 1k to 100k of bytes
❑ Clock speed is in tens of MHz
❑ Uses internal bus to communicate with memory and other devices on the
chip
❑ Processor - used to execute program instructions. Called the Central
Processing Unit (CPU)
❑ Processor contains
❑ Control Unit – Determines timing and sequence operation
❑ Arithmetic Unit – performs logical evaluations and data manipulation
❑ Registers – memory locations inside the CPU that holds internal data
while instructions are being executed
Gentle introduction to Arduino C
Introduction to Microcontrollers
CPU RAM
EEPROM Digital I/O
section .text
global _start
_start:
mov eax,4 ; The system call for write (sys_write)
mov ebx,1 ; File descriptor 1 - standard output
mov ecx,hello ; Put the offset of hello in ecx
mov edx,helloLen ; helloLen is a constant, so we don't need to say
; mov edx,[helloLen] to get it's actual value
int 80h ; Call the kernel
ARDUINO
Overview
❑ Expressions
❑ Statements
❑ Statement Blocks
❑ Function Blocks
Introduction to Arduino C
Building Blocks of Arduino C
Expressions
❑ An expression is created by combining operands and operators and always
resolves to one value.
❑ An operand is typically a piece of data that is acted upon by and operator
❑ An operator is often a mathematical or logical action that is performed on
one or more operands
Example
a +b addition
m – 3000 subtraction
g<d relational
A+B+C
Introduction to Arduino C
Building Blocks of Arduino C
Statements
❑ A statement is a complete C instruction for the computer.
❑ Statements end with a semicolon (;)
Example
a = b + c; assignment statement
j = 5 + k * 2; assignment statement
❑ A variable is a location in memory
❑ Operator Precedence
Precedence Level Operator
1 * , /, %
2 +, -
Introduction to Arduino C
Building Blocks of Arduino C
Statement Blocks
❑ A statement block consists of one or more statements grouped together so
they are viewed by the compiler as though they are single statement.
Example
If (time >= 5am) {
// next 3 statements form a statement block body
PutOnYourSports Dress(); //function block
GoToTheSportsField(); //function block
StartJogging() //function block
} else {
GoBackToSleep();
}
Introduction to Arduino C
Building Blocks of Arduino C
Function Blocks
❑A Function block is a block of code that is designed
to accomplish a single task.
Example
Void PutOnYourSportsDress(void) {
if (NotDressed) {
PutOnSportsDress();
PutOnRunningShoes();
}
ComeOutOfHostel();
WalkToSportsField();
}
Introduction to Arduino C
The Five Program Steps
Overview
❑ Initialisation Step
❑ Input Step
❑ Process Step
❑ Output Step
❑ Termination Step
Introduction to Arduino C
The Five Program Steps
Initialisation Step
❑ The purpose of the initialization step is to establish
the environment in which the program will run.
❑Background preparations before program execution.
❑ Ports needed are initialized, sensors activated,
thermocouples stabilized, etc
Introduction to Arduino C
The Five Program Steps
Input Step
❑ This is the sequence of program statements that are
necessary to acquire the information needed to solve
the task in hand.
❑ Data can come a sensor, potentiometer, file handle,
database or Wi-Fi, etc
Introduction to Arduino C
The Five Program Steps
Process Step
❑ This is responsible for taking a set of inputs and
processing it to get a new set of data.
❑ The sensor signals are interpreted.
Introduction to Arduino C
The Five Program Steps
Output Step
❑ After the process step has finished its work, new value
is output on some device or sent for further
processing.
❑ Eg The output may cause an LED to display a green
colour under normal condition or red under an
abnormal condition.
Introduction to Arduino C
The Five Program Steps
Termination Step
❑ The termination step has the responsibility of cleaning
up.
❑ In the Arduino C we do not normally use the
termination step.
Introduction to Arduino C
The First Example
Overview
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
*/
// Pin 13 has an LED connected on most Arduino boards. give it a name:
int led = 13; // Step 1 Initialisation Step
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
// Step2-4 Input, Process and Output Steps
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
//Step 5, Termination Step not Used. Program runs forever
Introduction to Arduino C
The First Example
Comments
❑ Comments do not form part of C statements and
therefore does not end with semicolon
❑ Single line Comment begin with a pair of slash (//)
characters
// Pin 13 has an LED connected on most Arduino boards.
❑ Multi-line Comments begin with a slash-asterisk pair (/*)
and end with an asterisk-slash pair (*/)
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
*/
Introduction to Arduino C
Arduino C Library
Overview
❑ Visit http://arduino.cc/en/|Reference/HomePage
Introduction to Arduino C
Arduino C Library
pinMode()
Introduction to Arduino C
Arduino C Library
Variables
❑Constants
❑Data Types
❑Conversion
❑Variable Scope and Qualifiers
❑Utilities
Introduction to Arduino C
Arduino C Library
Functions
❑Digital I/O
❑Analog I/O
❑Due & Zero Only
❑Advanced I/O
❑Time
❑Math
❑Trigonometry
❑Characters
❑Random Numbers
❑Bits and Bytes
❑External Interrupts
❑Interrupts
❑Communication
❑USB (32u4 based boards and Due/Zero only)
Introduction to Arduino C
Arduino C Compilers & IDE
Compilers
Review
1. What distinguishes a microcontroller from a microcomputer?
2. Where are program instructions stored in a microcontroller?
3. Which bus handles the transfer of data between CPU and
memory in a PIC microcontroller?
4. How is a PIC MCU ‘programmed’?
5. What is the advantage of storing data in an program memory?
6. What is an interrupt?
7. What are the advantages of programming in assembly
language?
Gentle introduction to Arduino C
Review Questions