Chapter1 Introduction

Download as pdf or txt
Download as pdf or txt
You are on page 1of 73

EE3490E - Fundamentals of

Embedded Programmings
Lecture 1: Introduction
Lecturer: Dr. Nguyen Tri Cuong (Nguyễn Trí Cường)
Department of Industrial Automation
School of Electrical Engineering
Email: cuong.nguyentri@hust.edu.vn
© DIA 2020.1
Content
1.1. Course Description
1.2. Introduction of programming technique
1.3. Methodologies
1.4. Software development process
1.5. Fundamentals of C/C++ languages
1.6. Getting started

Lecture 1: Introduction © DIA 2020.1 2


1.1. Course Description
❑ This course provides fundamentals of programming techniques,
implemented with C and C++ programming languages
▪ Structure programming
▪ Object oriented programming (OOP)
▪ Real-time programming and typical data structure in embedded systems
▪ Generic programming (optional)
❑ You will also learn the essential knowledge, including
▪ memory management, pointers, preprocessor macros,
▪ and how to find bugs when you inevitably use any of those incorrectly.
❑ Why C/C++:
▪ Strong and popular languages, which enables programmer to implement
most important techniques
▪ Essential in electrical and electronic systems

Lecture 1: Introduction © DIA 2020.1 3


Targeted platforms

Lecture 1: Introduction © DIA 2020.1 4


What to expect?

Lecture 1: Introduction © DIA 2020.1 5


What to expect?

Lecture 1: Introduction © DIA 2020.1 6


What to expect?
❑ Solid background is important, especially
▪ Thinking and Methodology
▪ Adaptation to different applications
▪ Adaptation to various programming languages: Java, Python, C#,
MATLAB, etc.
▪ Focus on professionality in programming: effectiveness and quality
❑ The following topics may be mentioned but not included in the
main scope of this course:
▪ Low-level system programming
▪ Graphics programing
▪ I/O interface
▪ Database
▪ Communication and Networking
❑ There will be weekly assignments and a individual mini-project
Lecture 1: Introduction © DIA 2020.1 7
Evaluations
❑ Progress assessment: 25%
▪ Weekly exercises/Mini projects
▪ Quizzes
▪ Mid-term exams
❑ Final exams: 75%

Lecture 1: Introduction © DIA 2020.1 8


Evaluations
❑ Mid-term and final exams are in written format with the
duration of an hour, they includes
▪ Theoretical questions
▪ Evaluate program results
▪ Write a program to solve a specific problem
❑ Grade distribution is in the range of 0 to 10, the average
would be 7 or 8.
❑ Student will be given 0 if submitting mini-project reports
late, and will receive -1 in progress assessment without
submission of each report
❑ 1-5% of students can be get bonus points (2-4 points) if
performing well during the semesters, i.e. complete all the
exercises and mini-projects, quizzes, mid-term exams
perfectly and/or creatively
Lecture 1: Introduction © DIA 2020.1 9
Learning methodologies
❑ Method 1: Attend lectures → self-attempt → read
material → discussion → practice
❑ Method 2: Read material → self-attempt → attend
lectures → discussion → practice
❑ Basic principle: Active learning frequently!
❑ Do not:
▪ Write too much in the class
▪ Learn by heart
▪ Rely on revision only
▪ Learn the examples in the books solely

Lecture 1: Introduction © DIA 2020.1 10


The tools
❑ A computer: PC or Laptop or Single Board Computer
❑ Editors: Visual Studio Code (highly recommended),
Notepads++, Emacs, Sublime, etc.
❑ Compilers: gcc, g++
❑ Debugger: gdb
❑ Operating systems and Packages:
▪ Windows: Mingw-w64 (recommended), Cygwin
▪ Linux (Ubuntu) and MacOS: gcc, g++ and gdb are usually pre-
installed as default packages

Lecture 1: Introduction © DIA 2020.1 11


References
❑ Kernighan B.W., Ritchie D.M., The C
Programming Language, 2nd edition, Prentice
Hall, 1998
❑ Bjarne Stroustrup,
The C++ Programming Language, 4th Edition
❑ Stanley B. Lippman et. al, C++ Primer, 5th
Edition
❑ Scott Meyers, Effective C++, 3rd Edition
❑ Many other books and resources
❑ Phạm Văn Ất, Giáo trình kỹ thuật lập trình C
căn bản và nâng cao.
❑ Phạm Văn Ất, Giáo trình C++ và lập trình
hướng đối tượng.
❑ Hoàng Minh Sơn, Bài giảng Kỹ thuật lập trình,
ĐH Bách Khoa Hà Nội.

Lecture 1: Introduction © DIA 2020.1 12


1.2. Introduction of programming
technique
❑ Programming technique: The technique is used to
implement a software solution (data structure and
algorithm) based on a methodology and one or a few
programming languages suitable with the requirements
of a specific application.
❑ Programs = Design idea + Coding method
= Data Structure + Algorithm
+ Programming Language
❑ Programming technique ≠ Analysis & Design

Lecture 1: Introduction © DIA 2020.1 13


Some definitions
❑ A programming paradigm is a style, or “way,” of
programming. Some languages make it easy to write in some
paradigms but not others.
❑ Programming technique relates to algorithm ideas to solve a
class of respected problems, e.g.: “Divide and conquer” or
“program development by stepwise refinement”
❑ Programming style is a set of guidelines used to format
programming instructions. It is useful to follow a style as it
makes it easier for programmers to understand the code, maintain
it, and assists in reducing the likelihood of introducing errors.
❑ Programming culture
▪ Programming activities, usually relates to programming languages
▪ A combination of programming model, style and techniques
▪ Programming ethics as well as the way software is used

Lecture 1: Introduction © DIA 2020.1 14


History
❑ 1940s – Machine code
❑ 1950s – Early programming languages: Assembler
code, Autocodes, first version of Fortran
❑ 1960s – Stronger computational languages: Cobol,
Lisp, Algol 60, Basic, PL/1. However the coding style
is still based on that of Assembly Language
❑ 1970s – “software crisis” started:
▪ Less dependent on the machine – Interchangeability
▪ Increment of program accuracy – Structured Programming,
Modular Programming and Information Hiding
▪ Example: Pascal, Algol 68 and C

Lecture 1: Introduction © DIA 2020.1 15


History
❑ 1980s – Simplicity – Object Orientation, Functional
programming.
❑ 1990s – Parallel and distributed computing: the
program ran faster, a number of new languages support
parallel computing
❑ 2000s – Programming languages facilitating Internet
applications

Lecture 1: Introduction © DIA 2020.1 16


Software crisis
❑ Software crisis included various issues when developing
software in 1960s, especially with large software system based
on conventional techniques at that time.
❑ Consequences:
1. Time and price increased tremendously
2. Low productivity
3. Low quality
❑ It resulted in appearance of Software Engineering

Lecture 1: Introduction © DIA 2020.1 17


1.2. Introduction of programming
technique
Write a program to Write a function to
calculate factorial of calculate the
100! factorial!

Write a program to Write a program to


print the first 100 print the first N
prime numbers! prime numbers!

This is not proper This is programming!


programming!

Lecture 1: Introduction © DIA 2020.1 18


A good program
❑ Provide the right solution accepted by customers
❑ Reliability
▪ Proper execution
▪ Less errors in quantity and frequency of occurrence
▪ Non-severe errors
❑ Efficiency
▪ Compact programs, low memory utilization
▪ Fast run, low CPU usage
❑ Effectiveness
▪ Short development time
▪ Easy to maintain
▪ Highly usable
▪ User friendly
▪ Rich features

Lecture 1: Introduction © DIA 2020.1 19


Examples
❑ Write a program to calculate the factorial of a positive integer n
▪ The function
int factorial (int N);
❑ Algorithm:
▪ Recursive method:
if (N > 1)
return N*factorial(N-1);
return 1;

▪ Iterative method
int kq = 1;
while (N > 1)
kq *= N--;
return kq;
Lecture 1: Introduction © DIA 2020.1 20
How to program well?
❑ Learn the way of thinking and programming methodologies
▪ Mathematical thinking, logical thinking, structure thinking, OOP
thinking and generic thinking
▪ Exploring data structures and algorithms
❑ Understand the computer
▪ Interaction amongst CPU, Programs and Memory
▪ Memory management
❑ Mastering the programming language
▪ Understand the strengths and limits of the language
▪ Programming skill
❑ Self-practicing
▪ Understand the above aspects
▪ Keep practicing
▪ Creativity
Lecture 1: Introduction © DIA 2020.1 21
Basic principles
❑ Abstraction
❑ Encapsulation
❑ Modularization
❑ Decentralization

Lecture 1: Introduction © DIA 2020.1 22


Supreme principle

Keep it simple :
as simple as possible,
but no simpler
(Alber Einstein)

Lecture 1: Introduction © DIA 2020.1 23


Programming in Electrical and
Electronic Engineering Applications
❑ Control application (microcontroller, computer,
programmable logic controller, distributed controller
system)
❑ Data acquisition and management
❑ Human Machine Interface
❑ System integration
❑ Design and calculation software
❑ Simulation software
❑ Optimization
❑ Communication protocols
❑ …
Lecture 1: Introduction © DIA 2020.1 24
1.3. Methodologies
❑ Sequential programming
❑ Structured programming
❑ Modular programming
❑ Object-oriented programming
❑ Generic programming
❑ Component-based programming
❑ Real-time programming

Lecture 1: Introduction © DIA 2020.1 25


Sequential programming
❑ The most classical method, listing sequential
commands, low abstraction
❑ The operation flow is controlled by performing jumping
instruction, or including subroutines
❑ Examples:
▪ Machine code
▪ ASSEMBLY
▪ BASIC
▪ IL (Instruction List), STL (Statement List)
▪ LD, LAD (Ladder Diagram

Lecture 1: Introduction © DIA 2020.1 26


Sequential programming
❑ Example: Machine Code
▪ Only logic operations with 1 and 0 bits
▪ Used at early stage of computers
▪ Too complicated and impossible to deal with big problems!!!

23fc 0000 0001 0000 0040


0cb9 0000 000a 0000 0040
6e0c
06b9 0000 0001 0000 0040
60e8

Lecture 1: Introduction © DIA 2020.1 27


Sequential programming
❑ Example: Calculating the factorial of a positive integer
n in Assemply
1: MOV AX, n
2: DEC n
3: CMP n,1
4: JMPI
5: MUL AX, n
6: JMP 2
7: MOV n, AX
8: RET
Lecture 1: Introduction © DIA 2020.1 28
Sequential programming
❑ Strengths:
▪ Simple thinking
▪ Low-level abstraction, easy to manage resources
▪ May be high efficient
▪ Appropriate to small problems or in embedded system
❑ Weakness:
▪ Difficult to read, thus easy to make mistakes
▪ Difficult to reuse
▪ Low effectiveness
▪ Not suitable to large-scale applications

Lecture 1: Introduction © DIA 2020.1 29


Structured programming
❑ Data is structured as data types, and the programs are
also structured to avoid jumping instructions
❑ Analysis and design with top-down approach
❑ Implementation with bottom-up approach
❑ Structured programming use sequential operations,
selection instruction (if then else), loop (while), and
escape (exit)
❑ Examples:
▪ PASCAL, ALGO, FORTRAN, C, …
▪ SFC (Sequential Function Charts)
▪ ST (Structured Text)
Lecture 1: Introduction © DIA 2020.1 30
Structured programming
❑ Example: Calculating the factorial of a positive integer
n in PASCAL

Lecture 1: Introduction © DIA 2020.1 31


Structured programming
❑ Example: Student management application

Lecture 1: Introduction © DIA 2020.1 32


Modular programming
❑ It is an improvement of Structured Programming. The
program is built with more strict structure, and the unit
of program structure is a module
❑ Module:
▪ An independent unit which has been standardized to establish a
system
▪ Each module includes an interface (exposed) and an
implementation (hidden)
▪ The modules communicate with each other via their intefaces
❑ Examples:
▪ Modula-2, based on PASCAL and designed by Niclaus Wirth
in 1977
Lecture 1: Introduction © DIA 2020.1 33
Object-oriented programming
❑ It programming paradigm based on the concept of "objects",
which can contain data and code: data in the form of fields (often
known as attributes or properties), and code, in the form of
procedures (often known as methods).
❑ 3 principles:
▪ Data encapsulation
▪ Subtyping/inheritance
▪ Polymorphism
❑ Examples:
▪ C++, C#, Ojective-C
▪ Java
▪ Python
▪ MATLAB
▪ …

Lecture 1: Introduction © DIA 2020.1 34


Object-oriented programming
❑ Example: Student management application

Lecture 1: Introduction © DIA 2020.1 35


Object-oriented programming
❑ Example: Matrix calculation in MATLAB

// read the input u

// return output y

Lecture 1: Introduction © DIA 2020.1 36


Generic programming
❑ Generalizing whatever possible to introduce a solution
template for a class of specific programs
❑ Strength:
▪ Reduce source codes
▪ Increase the reusability of software
▪ Can be combined with other programming paradigms
▪ Interchangeability
❑ Generalization approaches:
▪ Data types
▪ Basic operations
▪ Data structures
▪ Memory management, etc.
Lecture 1: Introduction © DIA 2020.1 37
Generic programming
❑ Example: Mathematical structures

Lecture 1: Introduction © DIA 2020.1 38


Component-based programming
❑ Software development is based on available “IC”
components or created ones
❑ Evolved from object oriented programming
❑ Most of Windows and Internet applications are built
with this paradigm
❑ Examples:
▪ C/C++, C#
▪ Delphi, Visual Basic
▪ Script, HTML, XML, …

Lecture 1: Introduction © DIA 2020.1 39


Real-time programming
❑ Software development with real-time responsive to the
system, especially control systems in manufacturing,
robotics, unmanned vehicles, etc.
❑ Properties:
▪ Competitive programming (multi-tasking, multi-threading)
▪ Event driven
▪ Scheduling mechanism
▪ Synchronization
▪ High efficiency
❑ Languages: ASM, C/C++, ADA, etc
❑ Needs the support of
▪ Operating systems
▪ Hardware platforms
▪ Communication networks
Lecture 1: Introduction © DIA 2020.1 40
1.4. Software development process

PROBLEM ? SOLUTION REAL LIFE

Analysis Coding
Testing

ANALYSED DESIGN DESIGNED


MODEL MODEL WORLD

Lecture 1: Introduction © DIA 2020.1 41


1.4. Software development process

Lecture 1: Introduction © DIA 2020.1 42


1.4. Software development process

Lecture 1: Introduction © DIA 2020.1 43


Waterfall Model
❑ Requirements – defines
needed information,
function, behavior,
performance and
interfaces.
❑ Design – data structures,
software architecture,
interface representations,
algorithmic details.
❑ Implementation – source
code, database, user
documentation, testing.

Lecture 1: Introduction © DIA 2020.1 44


Waterfall Model – Strengths
❑ Easy to understand, easy to use
❑ Provides structure to inexperienced staff
❑ Milestones are well understood
❑ Sets requirements stability
❑ Good for management control (plan, staff, track)
❑ Works well when quality is more important than cost or
schedule

Lecture 1: Introduction © DIA 2020.1 45


Waterfall Model – Deficiencies
❑ All requirements must be known upfront
❑ Deliverables created for each phase are considered
frozen – inhibits flexibility
❑ Can give a false impression of progress
❑ Does not reflect problem-solving nature of software
development – iterations of phases
❑ Integration is one big bang at the end
❑ Little opportunity for customer to preview the system
(until it may be too late)

Lecture 1: Introduction © DIA 2020.1 46


Waterfall Model – When to use
❑ Requirements are very well known
❑ Product definition is stable
❑ Technology is understood
❑ New version of an existing product
❑ Porting an existing product to a new platform.
▪ High risk for new systems because of specification and
design problems.
▪ Low risk for well-understood developments using familiar
technology.

Lecture 1: Introduction © DIA 2020.1 47


Agile model

Lecture 1: Introduction © DIA 2020.1 48


Step 1 – Analyse Requirements
❑ Tasks:
▪ List requirements
▪ Discuss with system analysis staff and users
▪ Clarify input, output, processing, and data components

Input Process Output (IPO) chart –


Identify inputs, outputs and
processing steps

Lecture 1: Introduction © DIA 2020.1 49


Step 2 – Design Solution
❑ Approaches

Object-oriented
design
Two approaches

Structure design, or
top-down design
Break the
system into
procedures
to solve Starting with
problems general then
detailed design

Lecture 1: Introduction © DIA 2020.1 50


Step 2 – Design Solution
❑ Hierarchy chart
▪ Observant modules
▪ Architecture

Lecture 1: Introduction © DIA 2020.1 51


Step 2 – Design Solution
❑ Object-oriented (OO)
design
▪ Developer encapsulates data
and related procedures into an
object
▪ Similar objects are grouped
into classes
▪ A class diagram represents the
relationship amongst the
classes

Lecture 1: Introduction © DIA 2020.1 52


Step 3 – Validate Design
❑ Tasks:

Developer verify the logic of


Evaluate the program
the program and identify the
accuracy
logic errors

Logic error
would cause unexpected
Desk check
Developer uses testing data results
to verify the program

Structured walkthrough
Test data Developer describes algorithm
is similar to actual data while the programming team
which should be validate program logic
processed by the program

Lecture 1: Introduction © DIA 2020.1 53


Step 4 – Implementation
❑Writing code: transform the design into executable
program
▪ Syntax – Principles of writing instructions
▪ Comments – Program documentation
❑ Extreme programming (XP) – coding and testing right
after requirements have been identified

Lecture 1: Introduction © DIA 2020.1 54


Step 5 – Test solutions
❑ Tasks

Debugging – Search and correct


Ensure that the program is
syntax and logic errors
runnable and provides
expected results

Test beta version, deliver


to Users for trial use and
collect feedbacks

Lecture 1: Introduction © DIA 2020.1 55


Step 6 – Documentation
❑ It’s an essential task!!!
▪ 2 sub-tasks

Revise program code – remove


Revise and finalise
dead code, i.e. the codes are
documentation
never called

Lecture 1: Introduction © DIA 2020.1 56


What is programming?
❑ Programming > Coding
❑ Programming ≈ Design idea + Coding + Testing +
Debugging

Lecture 1: Introduction © DIA 2020.1 57


Programming steps
Source code

Compiler

Compiled
code

Linker Library

Executable
program

Deploy &
Execute
Lecture 1: Introduction © DIA 2020.1 58
Developing tools / environment
❑ IDE (Integrated Development Environment)
▪ Support the whole process of programming
▪ E.g.: MS Visual Studio, Borland C++ (Builder), Keil-C
❑ Tools
▪ Editor
▪ Compiler
▪ Linker
▪ Loader
▪ Debugger
▪ Project manager

Lecture 1: Introduction © DIA 2020.1 59


Compiler
❑ A compiler is a computer
program that translates
computer code written in
one programming language
(the source language) into
another language (the
target language).
❑ The name "compiler" is
primarily used for
programs that translate
source code from a high-
level programming
language to a lower level
language (e.g., assembly
language, object code, or
machine code) to create an
executable program

Lecture 1: Introduction © DIA 2020.1 60


Interpreter
❑ An interpreter is a computer
program that directly executes
instructions written in a
programming or scripting
language, without requiring
them previously to have been
compiled into a machine
language program
❑ It does not generate an object
program

Lecture 1: Introduction © DIA 2020.1 61


1.5. Fundamentals of C/C++ languages
❑ C:
▪ At Bell Labs by Dennis Ritchie between 1972 and 1973 for
Unix.
▪ K&R C (Brian Kernighan and Dennis Ritchie) in 1978
▪ During the 1980s, C gradually gained popularity, many C
compilers
▪ Standardized by the ANSI since 1989 (ANSI C - American
National Standards Institute) and by the International
Organization for Standardization.
▪ Newest release C18 (ISO/IEC 9899:2018)
▪ Next version C2x
▪ Embedded C
Lecture 1: Introduction © DIA 2020.1 62
1.5. Fundamentals of C/C++ languages
❑ C++:
▪ Bjarne Stroustrup’s “C with Classes” in 1979
▪ C++ in 1982

▪ First commercial implementation in 1985


▪ In 1998, C++ started to be standardized as C++98 (ISO/IEC
14882:1998)

▪ Newest realease C++17 (ISO/IEC 14882:2017)


▪ Next version C++20

Lecture 1: Introduction © DIA 2020.1 63


Why C/C++
❑ Features:
▪ Close to hardware
▪ High efficiency
▪ Relatively user-friendly to developers
▪ Interchangeable
▪ International Standardization
❑ Strength of ANSI-C
▪ Popular in most of microprocessor, microcontroller, DSP
▪ Popular to world-wide developers
❑ Strength of ANSI/ISO C++:
▪ Object-oriented programming
▪ Generic programming (template)
▪ Mathematical programming (Data Abstraction and Operator
Overloading)
Lecture 1: Introduction © DIA 2020.1 64
Why C/C++

https://spectrum.ieee.org/computing/software/the-top-programming-languages-2019
Lecture 1: Introduction © DIA 2020.1 65
Getting started
❑ Windows:
▪ Install Visual Studio Code (VS Code)
▪ Install C++ extension for VS Code
▪ Install Mingw-w64 in a folder with
simple path, i.e.: C\Mingw64
▪ Set Mingw64\bin in Windows
Environment Variables

Lecture 1: Introduction © DIA 2020.1 66


Getting started
❑ Configuration files in VSCode project
▪ c_cpp_properties.json (compiler path and IntelliSense settings)
▪ tasks.json (build instructions)
▪ launch.json (debugger settings) (.json JavaScript Object
Notation (JSON) format)

Lecture 1: Introduction © DIA 2020.1 67


Compiler path

Lecture 1: Introduction © DIA 2020.1 68


Build task (tasks.json)

Lecture 1: Introduction © DIA 2020.1 69


Debugger configuration
(launch.json)

Lecture 1: Introduction © DIA 2020.1 70


Start coding

Lecture 1: Introduction © DIA 2020.1 71


Homework 1
❑ Install the following software packages in Windows
7/10:
▪ Visual Studio Code
▪ C++ Extension in Visual Studio Code
▪ Mingw64 and respected configuration
❑ Create a new project, write a simple program (Hello
World as an example) and execute

Lecture 1: Introduction © DIA 2020.1 72


TO BE CONTINUED

Lecture 1: Introduction © DIA 2020.1 73

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy