Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
INTRODUCTION
Lecturer: Dr. Bui Ha Duc
Dept. of Mechatronics
Email: ducbh@hcmute.edu.vn
1
INSTRUCTOR INTRODUCTION
• Full Name: Bui Ha Duc
• From: Vung Tau City
• Bachelor: HCMC University of Technology
• Ph.D: National University of Singapore
(NUS)
• Research fellow / TA in NUS
• Join HCMUTE in 2015
• Lecturer in Department of Mechatronics –
FME
• Teaching: Microcontroller, Embedded
system, Digital signal processing.
2
COURSE OBJECTIVES
• Fundamental knowledge of microcontroller
• How to apply microcontroller to solve technical problems
• MCU: PIC 18F4550
4
ORDER OF INTRUCTION
Overview about microcontroller and its architecture
o Computer architectures
o Memory organization and program space
o Compilers and simulation software
Assembly language – brief introduction
C Language ?
Input and output of microcontroller – basic I/O, IO
expansion, LED, LCD
5
ORDER OF INTRUCTION (cnt.)
Interrupt and its applications – very important, concern
with essential concepts such as
o Concurrency
o Polling
o Scheduling
Timer
Analog digital conversion – super important, required for
advance course
o Aliasing
o Data acquisition
6
ORDER OF INTRUCTION (cnt.)
Capture/Compare/PWM module – need PWM for motor
control
Interface microcontroller with other devices – important
for real applications
o Asynchronous UART
o Synchronous communication protocol
Apply microcontroller in practical situations - students
present their project
7
COURSE LEARNING OUTCOMES
Upon completion of the course students will be able to
1. Given a simple program, students will be able to explain the
instruction execution and data flow of the microcontroller.
2. Design an interface that allows a microcontroller port pin to
control peripheral devices such as LED, LCD, motor.
3. Create an interrupt service routine that performs a prescribed
task
4. Compile C programs to continuous monitor the status of an
ongoing event when microcontroller is processing other
activities.
8
COURSE OBJECTIVES (cnt.)
Upon completion of the course students will be able to
5. Analyze the execution of ADC under different configurations.
6. Select the ADC sampling rate required based on input signal
characteristics.
7. Apply PWM to control DC motor and step motor.
8. Construct a connection between microcontroller and
peripheral devices under various situations.
9
ASSESSMENTS
• Assignments or Quizzes – 30%
10
References
• Programming and Customizing the PIC Microcontroller,
3rd Ed, Mc Graw Hill, 2008.
• Microchip Webinar
http://www.microchip.com/wwwcategory/Webinarsearch.aspx/WebSemCHome.
aspx
11
Software
• MPLAB X
http://www.microchip.com/pagehandler/en-us/family/mplabx/
12
Introduction to Microcontroller
• What can a microcontroller do?
Pretty much everything as long as you can program it
E.g. control devices (LED, LCD, Motor…), read sensor (light,
temperature, encoder…), control processes, signal processing,…
Some cool projects using microcontrollers:
13
AUTOMOBILE
http://libelium.com/es/smart_cars_m2m_accident_prevention
14
Introduction to Microcontroller
• Is microcontroller widely used in Industry?
Yes, especially in robotics industry
PLC versus Microcontroller
PLC Microcontroller
• Simple and easy to use • Require knowledge in
• Required less electronics and programming
knowledge • Fast
• Relatively slow • Can implement complex
• Can’t implement algorithm
complex algorithm • Cheap
• Expensive
15
Introduction to Microcontroller
http://www.edn.com/design/systems-design/4398891/Processor-
architectures--the-sweet-spot-spectrum
16
Introduction to Microcontroller
• What microcontrollers available in the market?
Countless
E.g. Microchip (PIC, dsPIC), AVR (ATMEGA, XMEGA), TI (MSP,
TM), Freescale, ARM (M0, M3, M4), Holtek…
There is no the best microcontroller.
Microcontroller changes rapidly, you don't know what could be the
requirement of the product in your future company
http://www.edn.com/design/systems-design/4398891/Processor-
architectures--the-sweet-spot-spectrum
17
Introduction to Microcontroller
• How a microcontroller is made?
https://www.youtube.com/watch?v=p4vZwycU4Cw
Microcontroller vs Human
5 1 6 2 4 3
• Discussion
How to teach a microcontroller do this job?
19
What is Microcontroller?
Wiki definition:
• a small digital computer on a single integrated circuit
• Contain processor core(s), memory, I/O peripherals
• Programmable
20
How does microcontroller work?
Algorithm
PROBLEMS
Inputs Outputs
MCU
21
How does microcontroller work?
Algorithm
Code
(C / Assembly)
Compiler
List of Instructions
Inputs Outputs
MCU
• Instruction Fetch: get the instruction from memory into the processor
• Decode: decode the instruction to get the operation (what it has to do)
• Execute: Perform the operation
• Store: store the result back into another register
22
Example code in
Assembly
23
Inside a microcontroller
24
Central Processor Unit (CPU)
CPU monitors and controls all
processes within the microcontroller
• Instruction decoder
• Accumulator
25
Pipelining
• Laundry analogy
http://www.ece.arizona.edu/~ece462/Lec03-pipe/
26
Pipelining
http://www.ece.arizona.edu/~ece462/Lec03-pipe/
27
Pipelining
28
INSTRUCTION SET
RISC (Reduced instruction set
computers)
• 1 instruction = 1 operation
• all instructions execute in the same time
• more RAM is needed to store more line of
code
• Require less transistor on processor
• EX:
LOAD A, 2:3
LOAD B, 5:2
PRO A, B
STORE 2:3, A
29
INSTRUCTION SET
CISC (Complex instruction set
computers)
• complex instructions are built directly
into the hardware
• resembles a higher level language
• 1 instruction = many operations
• does not require the programmer to
explicitly call any loading or storing
functions
• More transistor on processor, less
RAM needed
• Ex:
MULT 2:3, 5:2
30
MEMORY
• Nonvolatile program memory
(ROM)
‒ store application software
‒ retain the information stored in
it even when power is removed
• Volatile variable memory
(RAM)
‒ Temporary storage of data
‒ Value will be lost when power is
removed
• Optional nonvolatile data
memory (EEPROM)
31
PROCESSOR ARCHITECTURES
• Von Neumann (Princeton) Computer Architecture
34
Memory Organization
Program memory
35
Instruction in program memory
36
Special Function Register
37
Oscillator
39
Microcontroller selection criteria
• Selecting the right microcontroller for a product is an
important task
• Criteria:
• Required hardware interfaces
• Architecture
• Memory needs
• Power constraints
• Availability
• Cost
• Packaging
40
Microcontroller selection criteria
• Required hardware interfaces
• Number of I/O requirements
• Features, communication interfaces
e.g. autonomous robot
Drivers Motors
2 PWM + 2 I/O
Minimum I/O
Encoders Requirements
4 I/O
• 12 I/O
2 I/O Ultrasonic
• 2 PWM
sensor
MCU • 1 UART
UART
Bluetooth
2 I/O 2 I/O
LEDs Buttons
41
Microcontroller selection criteria
• Architecture
• Processing power requirement – MCU speed
• 8 bit or 32 bit MCU?
• Memory needs
• Estimate how much program and data memory will be required
• Power constraints
• Operating voltage
• Power consumption
42
Microcontroller selection criteria
• Availability and cost
• Keep the cost as low as possible
• Select a popular MCU
• Packaging
• DIP or SMD
• Number of pins DIP package SMD package
How to know if a microcontroller meet the
requirements?
– Check the datasheet
– Ask local shops
Check the specification of microcontroller PIC
18F4550? 43
PIC 18F4550 Highlights
• Package: 40-pin DIP or 44-pin QFN (SMD)
• Oscillator: max 48 MHz
• Program memory: 32 KB
• Data memory: 2 KB + 256B EEPROM
• Number of I/O: 35
• ADC: 13 channels, 10-bit resolution
• PWM: 1 output
• Communication interfaces: SPI, I2C, UART, USB
• Timer: 1 x 8-bit timer, 3 x 16-bit timers
• Operating voltage: 2.0V to 5.5V
44
PIC 18F4550 Pin Diagram
45
PIC 18F4550 basic circuit
Reset
Circuit
I/O
Devices
Vdd
Power Voltage MCU
Vss
In Regulator 18F4550
OSC1
Oscillator OSC2
46
Reset Circuit
A Reset is generated by holding the MLCR
pin low.
47
Voltage regulator
• A voltage regulator is designed to automatically regulate a
constant voltage level.
• How Voltage regulator work:
http://www.analog.com/en/technical-articles/how-voltage-regulator-works.html
Regulator
L4931 AMS1117
7805 MAX603
48
Oscillator
• Oscillator Source: Ceramic
Crystal
capacitor
• Crystal Oscillator
• External Clock Input
• Internal Oscillator
49
Oscillator
• External Clock Input (EC)
• This Oscillator require an external clock source to be connected to
the OSC1 pin.
50
Oscillator
• Internal Oscillator
The PIC18F4550 device include an internal oscillator block
which generates two different clock signals
• The main output (INTOSC) is an 8 MHz clock source which can be
used to directly drive the device clock.
• the internal RC oscillator (INTRC) which provides a nominal 31 kHz
output.
51
Oscillator