Sei sulla pagina 1di 95

UP EEEI CoE 115 Lecture 1 1

COE 115
Lecture 1
UP EEEI CoE 115 Lecture 1 2

COE 115: Intro to Embedded Microcontrollers


• Instructors
• Marc D. Rosales, Ph,D.

• Class Website / Announcements / Lecture & Lab Materials


• UVLE Course:
UP EEEI CoE 115 Lecture 1 3

COE 115: Intro to Embedded Microcontrollers


• Description
• Introduction to embedded microcontrollers in electronic and
electromechanical systems. Hardware and software design techniques.
System interfaces, data acquisition and control. High speed design
techniques
• Prerequisites
• EEE 105
• Credits
• 5h (2 lec, 3 lab) 3u
UP EEEI CoE 115 Lecture 1 4

Coverage
• Programming structures and techniques.
• Conditional branching. Subroutines. Generating time delays and intervals.
Look-up tables. Indirect addressing.
• Digital interfacing.
• Analog sensing and A/D conversion. D/A conversion and PWM. Frequency
measurement.
• Communication.
• Bit banging, synchronous, asynchronous, SPI, I2C, USART, USB, CAN
• High-speed techniques and considerations
UP EEEI CoE 115 Lecture 1 5

Grading
• Lab Grade
• 65%
• Lecture Grade
• 35%
• Short Quizzes / Attendance / Etc 10%
• Long Quizzes (3) 90%
• LONG QUIZ
• September 21, 2019 (Saturday) 10-11:30 Lectures 1 -6
• October 10, 2019 (Monday) 10 -11 :30 Lectures 7-10
• November 15, 2019 (Saturday) 10 – 11:30 Lectures 10-14
• December 7, 2019 (Saturday) 10 -11:30 Lectures 14- 18
UP EEEI CoE 115 Lecture 1 6

Agenda
• Embedded Systems
• Microcontrollers
• Embedded applications using MCUs
• Basics of architecture
• PIC Microcontrollers
• PIC microcontrollers (Architecture)
• Memory organization and addressing modes
UP EEEI CoE 115 Lecture 1 7

Embedded Systems Overview


• Embedded computing systems
• Computing systems embedded within
electronic devices
• Hard to define. Nearly any computing
system other than a desktop
computer
• Billions of units produced yearly,
versus millions of desktop units
• Perhaps 50 per household and per
automobile
UP EEEI CoE 115 Lecture 1 8

Embedded Systems
• An embedded system is a special
purpose system that is used to
perform a dedicated function Home
Appliance
• Embedded systems encompass a
broad range of computational
power Printers
• Embedded systems seems
synonymous to “invisible” A/V Devices
UP EEEI CoE 115 Lecture 1 9

Domestic Refrigerator
UP EEEI CoE 115 Lecture 1 10

Autonomous Robot Platform


Ultrasound
Ranging
Module
Pulse Echo

Bump Sensor Bump Sensor


Left Right

Light Sensor Analog to Digital Light Sensor


Left Converter Right

Light Sensor Ultrasound


Rear Servo

Serial Data
PIC Compass
Power 5V
Microcontroller Serial
Regulation & Clock
Management
Serial Extension Bus
9V
Alkaline Pulse Width Modulation
Stream x2
9V Motor Drive
Interface

Motor Motor
Left Right
UP EEEI CoE 115 Lecture 1 11

Example 2: Car Door Control, within a Larger Network


Car Door Control, within a Larger Network
window control buttons
window stall sensor Right
Door

Dash Climate
window motor Board Control
lock control
lock actuator open door sensor
Driver's
Steering Seat

Central
Control Air Radio
Bag & CD

CAN Bus

Left
Door
UP EEEI CoE 115 Lecture 1 12

Embedded systems
• Cars

8
UP EEEI CoE 115 Lecture 1 13

Embedded systems
• Examples
• Digital and analog televisions
• Set-top boxes (DVDs, VCRs, Cable boxes)
• Personal digital assistants PDAs
• MP3 Players and iPods
• Kitchen appliances ( refrigerators, microwave ovens)
• Telephones, cell phones
• Cameras
• GPS receivers
• … and many others
UP EEEI CoE 115 Lecture 1 14

Embedded systems implementations


• Microcontrollers (microprocessor based systems)

• Custom digital circuits that perform the desired function


• Discrete Digital Circuits (TTL chips)
• Programmable Logic Devices (PLDs)
• Field-Programmable gate arrays (FPGAs)
• Application specific integrated circuits (ASIC)

• Combination of ASIC, FPGA, Microcontrollers


UP EEEI CoE 115 Lecture 1 15

Embedded systems implementations Comparison


UP EEEI CoE 115 Lecture 1 16

The Embedded System –


Embedded System: Generic Block Diagram
Generic Block Diagram (User Interface)
(display, keypad etc.)
(User Interface)
(display, keypad etc.)

Embedded Computer
Conditioning

Conversion
Signal
Input

Output
Drive
EmbeddedSoftware
Computer Output
Conditioning

Data
Conversion
Variables Variables
Signal

Input

Output
Drive
Output
Data
Variables Software Variables
Hardware
Hardware

(Link to other Systems)


(Link to other Systems)

An embedded system is a system whose principal function is not


computational, but which is controlled by a computer embedded within it.
UP EEEI CoE 115 Lecture 1 17

Common Characteristics of Embedded Systems


• Single-functioned
• Executes a single program, repeatedly
• Tightly constrained
• Low cost, low power, small, fast, etc.
• Reactive and real-time
• Continually reacts to changes in the system’s environment
• Must compute certain results in real-time without delay
UP EEEI CoE 115 Lecture 1 18

An embedded system example -- a


An embedded system example
digital camera– a digital camera
Digital camera chip
CCD

CCD preprocessor Pixel coprocessor D2A


A2D

lens

JPEG codec Microcontroller Multiplier/Accum

DMA controller Display ctrl

Memory controller ISA bus interface UART LCD ctrl

• • Single-functioned
Single-functioned -- always a--digital
always a digital camera
camera
• Tightly-constrained -- Low cost, low power, small, fast
• Tightly constrained – Low cost, low power, small, fast
• Reactive and real-time -- only to a small extent
• Reactive and real-time – only to a small extent
13
UP EEEI CoE 115 Lecture 1 19

Design Challenge – optimizing design metrics


• Obvious design goal:
• Construct an implementation with desired functionality
• Key design challenge:
• Simultaneously optimize numerous design metrics
• Design metric:
• A measurable feature of a system’s implementation
• Optimizing design metrics is a key challenge
UP EEEI CoE 115 Lecture 1 20

Design Challenge – optimizing design metrics


• Common metrics
• Unit cost: the monetary cost of manufacturing each copy of the system,
excluding NRE cost
• NRE cost (Non-Recurring Engineering cost) : the one-time monetary cost
of designing the system
• Size: the physical scape required by the system
• Performance: the execution time or the throughput of the system
• Power: the amount of power consumed by the system
• Flexibility: the ability to change the functionality of the system without
incurring heavy NRE cost
UP EEEI CoE 115 Lecture 1 21

Design Challenge – optimizing design metrics


• Time-to-prototype: the time needed to build a working version of the
system
• Time-to-market: the time required to develop a system to the point that
it can be released and sold to costumers
• Maintainability: the ability to modify the system after its initial release
• Correctness, safety, many more
UP EEEI CoE 115 Lecture 1 22

The Embedded System – What Disciplines does


it Involve?

Digital Integrated Circuit


Electronics Design

Analog
Electronics Computer Architecture

Embedded Systems
Sensors and Software Engineering
Measurements

Data
Electric Motors & Communications
Actuators Control
Engineering
UP EEEI CoE 115 Lecture 1 23

What is a Microcontroller?

• Full computer system on a


chip, even if its resources are
far more limited than a
desktop personal computer

• Designed for stand alone


operations
UP EEEI CoE 115 Lecture 1 24

The Microcontroller
• A microcontroller is a microprocessor designed primarily to perform
simple control functions.
• Usually have these features
• low cost,
• physically small,
• input/output intensive, and capable of easy interfacing,
• limited memory capability for program and data,
• instruction set leading to compact code, with limited arithmetic capability, •
ability to operate in a real-time environment.
• For some applications, these features may also be needed:
• ability to operate in hostile environment, e.g. high or low temperature,
tolerant to electromagnetic interference, low power, with features adapted
to battery power.
UP EEEI CoE 115 Lecture 1 25

What is a Microcontroller?
• Difference between microcontroller and a microprocessor
system.
UP EEEI CoE 115 Lecture 1 26

The Microcontroller
UP EEEI CoE 115 Lecture 1 27

What is a Microcontroller?
• A microcontroller has a processor and many peripherals integrated
with it on the same chip, like a flash memory, RAM, I/O ports, serial
communication ports, ADC …Etc.
UP EEEI CoE 115 Lecture 1 28

What is a Microcontroller?
• A timer module to allow the MCU to perform tasks for certain time
periods.

• A serial I/O port to allow data to flow between the MCU and other
devices such as a PC or another MCU

• An ADC to allow the MCU to accept analog inputs for processing


UP EEEI CoE 115 Lecture 1 29

A Typical Microcontroller
• Central processing unit
• Arithmetic logic unit (ALU), which performs computation.
• Registers needed for the basic operation of the CPU, such as the program
counter (PC), stack pointer (SP), and status register (SR). or Special
function registers (SFR)
• Further registers to hold temporary results.
• Instruction decoder and other logic to control the CPU, handle resets,
and interrupts, and so on.
UP EEEI CoE 115 Lecture 1 30

What is a Microcontroller?
• A microprocessor can’t do all the functions of a computer system
on its own, and needs another circuits or devices to support it.
• I/O devices, RAM, ROM, DMA controllers, Timers, ADC, LCD driver.. Etc.
UP EEEI CoE 115 Lecture 1 31

A Typical Microcontroller
• Memory for the program: Nonvolatile (read-only memory, ROM),
meaning that it retains its contents when power is removed.
• Memory for data: Known as random- access memory (RAM) and
usually volatile.
• Input and output ports: To provide digital communication with the
outside world.
• Address and data buses: To link these subsystems to transfer data
and instructions.
• Clock: To keep the whole system synchronized.
UP EEEI CoE 115 Lecture 1 32

A Typical Microcontrollers: Peripherals


• Timers: Most microcontrollers have at least one timer because of
the wide range of functions that they provide.
• Watchdog timer: This is a safety feature, which resets the processor
if the program becomes stuck in an infinite loop.
• Communication interfaces: A wide choice of interfaces is available
to exchange information with another IC or system.
• Nonvolatile memory for data: This is used to store data whose
value must be retained when power is removed.
UP EEEI CoE 115 Lecture 1 33

A Typical Microcontrollers: Peripherals


• Nonvolatile memory for data: This is used to store data whose
value must be retained when power is removed.
• Analog-to-digital converter: This is very common because so many
quantities in the real world vary continuously.
• Digital-to-analog converter: This is much less common, because
most analog outputs can be simulated using PWM.
• Real-time clock: These are needed in applications that must track
the time of day.
• Monitor, background debugger, and embedded emulator: These are
used to download the program into the MCU
UP EEEI CoE 115 Lecture 1 34

Comparison
UP EEEI CoE 115 Lecture 1 35

Microcontrollers vs. Microprocessors


• Consider a simple demo project: A heat control system
• periodically read the temperature
• (analog value, is digitized by sensor; uses 4-bit interface),
• control heating according to the temperature
• (turn heater on/off; 1 bit),
• display the current temperature on a simple 3-digit numeric display
• (8+3 bits),
• allow the user to adjust temperature thresholds
• (buttons; 4 bits), and
• be able to configure/upgrade the system over a serial interface.
UP EEEI CoE 115 Lecture 1 36
Some Microprocessors and Microcontrollers

Microprocessors and microcontrollers


Motorola PIC 16F877
PIC 16C72 68HC05B16
PIC 16F84A

Motorola 68000

PIC 12F508
UP EEEI CoE 115 Lecture 1 37

Microprocessor based solution


2 CHAPTER 1. MICROCONTROLLER BASICS

memory), and EEPROM (for constants).1 The resulting board layout is depicted in Figure 1.1; as you
can see, there are a lot of chips on the board, which take up most of the space (euro format, 10 ⇥ 16
cm).
• Using Zilog Z80

Figure 1.1: Z80 board layout for 32 I/O pins and Flash, EEPROM, SRAM.

Incidentally, we could also solve the problem with the ATmega16 board we use in the Microcon-
troller lab. In Figure 1.2, you can see the corresponding part of this board superposed on the Z80
PCB. The reduction in size is about a factor 5-6, and the ATmega16 board has even more features
than the Z80 board (for example an analog converter)! The reason why we do not need much space
UP EEEI CoE 115 Lecture 1 38

Microcontroller Solution
Using
•1.1. An Atmega 16 board
INTRODUCTION 3

Figure 1.2: ATmega16 board superposed on the Z80 board.


UP EEEI CoE 115 Lecture 1 39

Microcontroller and Microprocessor


Figure 1.2: ATmega16 board superposed on the Z80 board.

Figure 1.3: Pinouts of the Z80 processor (left) and the ATmega16 controller (right).

PC0-PC7, PD0-PD7, which can be used for different functions. For example, PD0 and PD1 can be
used as the receive and transmit lines of the built-in serial interface. Apart from the power supply,
the only dedicated pins on the ATmega16 are RESET, external crystal/oscillator XTAL1 and XTAL2,
and analog voltage reference AREF.
UP EEEI CoE 115 Lecture 1 40

Popular microcontrollers
• 8051 (Intel and others)
• 80286 EX (Intel)
• PIC (Microchip)
• 68HC05 (Motorola)
• Z8 (Zilog)
• MSP430 (TI)
UP EEEI CoE 115 Lecture 1 41

Agenda
• What are embedded systems? How do we implement them?
• What is a microcontroller
• Developing embedded applications using MCUs
• Basics of architecture
• PIC Microcontrollers
• Basics of PIC micro devices architecture
• Memory organization and addressing modes
• C Programming language
UP EEEI CoE 115 Lecture 1 42

Developing Embedded Systems


UP EEEI CoE 115 Lecture 1 43

Hardware Development
• This includes choosing the right MCU for your application so that it
can satisfy the required applications.

• The criteria for choosing a microcontroller is:


• Number of I/O ports,
• Serial communication modules
• Peripherals like (Timer, ADC, PWM, .. Etc.)
• Memory requirements
• Processing speed requied
• Power requirements
UP EEEI CoE 115 Lecture 1 44

Which Microcontroller ?
• Which one for a given application
• Cost is important.
• Cheapest device that matches the application needs
• Microcontrollers are generally tailored for specific applications.
• Wide variety to choose from
UP EEEI CoE 115 Lecture 1 45

Which Microcontroller ?
• Controller family
• Defines the controller architecture
• Microcontrollers of same family will contain same processor core
• Code compatible
• Differ in additional components:
• amount of memory, number of timers, a different peripherals
• INTEL 8051, HC, PIC, ATMEL, TI
• Even in a single family there will be different controllers
UP EEEI CoE 115 Lecture 1 46
A Manufacturer’s Microcontroller Portfolio

Family 1 Family 2 Family 3

Memory Memory
Core P Core Q Core R
Peripherals Peripherals Memory

Peripherals
Core P Memory Core Q Memory
Peripherals Peripherals
Core R Memory

Memory Core Q Memory


Core P Peripherals

Peripherals Peripherals

Memory
Core R
Memory Memory
Core Q
Core P

Peripherals Peripherals
Peripherals
UP EEEI CoE 115 Lecture 1 47

Software Development
UP EEEI CoE 115 Lecture 1 48

Software Development
UP EEEI CoE 115 Lecture 1 49

Software Development
• Using assembly language involves learning the microcontroller’s
specific instruction set and may result in more compact and faster
codes

• Using C programming language makes codes portable.


• Could be used for another target microcontroller
• Eases process of software development (shorter time to market) with
acceptable quality
UP EEEI CoE 115 Lecture 1 50

C vs Assembly language

• Assembly programs are optimized more than C programs,


but to develop more complicated programs, using C is
more practical and also efficient
UP EEEI CoE 115 Lecture 1 51

Linker
• The linker’s function is to link code modules saved in different files
together into a single final program .

• At the same time it takes care of the chip's memory allocation by


assigning each instruction to a microcontroller memory addresses
in such a way that different modules do not overlap.
UP EEEI CoE 115 Lecture 1 52

Debugger
• Common debugging features include:

• The capability to examine and modify the microcontroller’s on-chip


registers, data – and program- memory

• Pausing or stopping program execution at defined program locations by


setting breakpoints

• Single-stepping ( execute one instruction at a time ) through the code ;


and looking at a history executed code (trace)
UP EEEI CoE 115 Lecture 1 53

Software Development
• An Integrated Development Environment (IDE) puts all of the
previously discussed software components under one common
unified user interface
• MPLAB / MPLAB X ( Proprietary to Microchip)
• TI Code composer Studio ( Proprietary to TI)
• IAR Systems (3rd Party)
• KEIL (3rd Party)

• 3rd Party IDE supports multiple microcontrollers


UP EEEI CoE 115 Lecture 1 54

Agenda
• What are embedded systems? How do we implement them?
• What is a microcontroller
• Developing embedded applications using MCUs
• Basics of architecture
• PIC Microcontrollers
• Basics of PIC micro devices architecture
• Memory organization and addressing modes
• C Programming language
UP EEEI CoE 115 Lecture 1 55

Architecture (Memory Access)


UP EEEI CoE 115 Lecture 1 56

Architecture (Memory Access)


Harvard and von Neumann
Architectures

21
UP EEEI CoE 115 Lecture 1 57

Architecture (Instructions)
UP EEEI CoE 115 Lecture 1 58

CISC vs. RISC


• CISC
• complex microcoded instruction which may take many clock cycles to execute
• large and variable instruction size and offers many powerful instructions and
added addressing modes
• relatively longer to execute instructions, but the instruction set is richer
• RISC
• Simple, hard-wired instructions which often take only one or a few clock
cycles to execute
• Small and fixed instructions size with comparatively few instructions and few
addressing modes
• Execution of instruction is very fast, but the instruction set is rather simple
UP EEEI CoE 115 Lecture 1 59

CISC vs. RISC


• CISC
• complex microcoded instruction which may take many clock cycles to execute
• large and variable instruction size and offers many powerful instructions and
added addressing modes
• relatively longer to execute instructions, but the instruction set is richer
• RISC
• Simple, hard-wired instructions which often take only one or a few clock
cycles to execute
• Small and fixed instructions size with comparatively few instructions and few
addressing modes
• Execution of instruction is very fast, but the instruction set is rather simple
UP EEEI CoE 115 Lecture 1 60
UP EEEI CoE 115 Lecture 1 61

Pipelining
Pipelining
Classic 5-stage RISC pipeline

Pipelining in the PIC microcontroller


UP EEEI CoE 115 Lecture 1 62

Agenda
• What are embedded systems? How do we implement them?
• What is a microcontroller
• Developing embedded applications using MCUs
• Basics of architecture
• PIC Microcontrollers
• Basics of PIC micro devices architecture
• Memory organization and addressing modes
• C Programming language
UP EEEI CoE 115 Lecture 1 63

PIC microcontrollers
• One of the leading architectures
for low end applications (
applications requiring 4,8,16-bit
processors)

• RISC, Harvard architecture


processors

• Easier implementation to
pipelining without having a
complex hardware, less silicon
area and less power consumption
UP EEEI CoE 115 Lecture 1 64

PIC Microcontrollers
• PICmicro devices architecture
• Harvard, RISC architecture

• Single Word Instructions


• Each instruction take one word of memory (14-bits)

• Single Cycle Instructions


• Each instruction is fetched in one instruction cycle, decoded and executed in the
next instruction

• Instruction Pipelining
• An instruction is fetched and another instruction is executed at the same time
every single TCY
UP EEEI CoE 115 Lecture 1 65

PIC Microcontrollers example


UP EEEI CoE 115 Lecture 1 66

Agenda
• What are embedded systems? How do we implement them?
• What is a microcontroller
• Developing embedded applications using MCUs
• Basics of architecture
• PIC Microcontrollers
• Basics of PIC micro devices architecture
• Memory organization and addressing modes
• C Programming language
UP EEEI CoE 115 Lecture 1 67

Oscillator
• Choice of suitable oscillator mode in the applications according to:

• The required processing speed


• The required timing precision
UP EEEI CoE 115 Lecture 1 68

Oscillator
UP EEEI CoE 115 Lecture 1 69

Oscillator
UP EEEI CoE 115 Lecture 1 70

Instruction Cycle
• Time of fetching, decoding and executing an instruction is
the instruction cycle.
• For PICmicro devices:
UP EEEI CoE 115 Lecture 1 71

Instruction Cycle
• The instruction fetch begins with the program counter incrementing
Q1
• In the execution cycle, the fetched instruction is latched in the
“Instruction Register (IR) in cycle Q1. This instruction is then
decoded and executed during the Q2, Q3 and Q4 cycles. Data
memory is read during Q2 (operand read) and written during Q4
(destination write)
UP EEEI CoE 115 Lecture 1 72

Instruction Cycle
• Each instruction is fetched in one instruction cycle, and
then decoded and executed in another instruction cycle

• With pipelining, while the current instruction is fethed,


the previous instruction is executed. This means that
each instruction is fetched, decoded and executed in one
instruction cycle (4 clock cycles)
UP EEEI CoE 115 Lecture 1 73

Instruction cycle
• Ex. If you use a 4 MHz oscillator, the MCU will execute 1 Million
instruction per second (1 MIPS)

• Clock frequency (4MHz) = Instruction execution (1MHz)


UP EEEI CoE 115 Lecture 1 74

Agenda
• What are embedded systems? How do we implement them?
• What is a microcontroller
• Developing embedded applications using MCUs
• Basics of architecture
• PIC Microcontrollers
• Basics of PIC micro devices architecture
• Memory organization and addressing modes
• C Programming language
UP EEEI CoE 115 Lecture 1 75

Memory Organization
UP EEEI CoE 115 Lecture 1 76
UP EEEI CoE 115 Lecture 1 77

Memory organization
UP EEEI CoE 115 Lecture 1 78

Memory Organization
UP EEEI CoE 115 Lecture 1 79

Memory Organization
UP EEEI CoE 115 Lecture 1 80

Memory Organization
UP EEEI CoE 115 Lecture 1 81

Agenda
• What are embedded systems? How do we implement them?
• What is a microcontroller
• Developing embedded applications using MCUs
• Basics of architecture
• PIC Microcontrollers
• Basics of PIC micro devices architecture
• Memory organization and addressing modes
• C Programming language
UP EEEI CoE 115 Lecture 1 82

C Programming Language
• Features of C
• Extensive use of function calls
• Low level (Bitwise) programming readily available
• Pointer implementation- extensive user of pointers for memory and
arrays
• Structures and functions
• Can handle low-level activities
• Produces efficient programs
• Fast
• It can be compiled on a variety of computers
UP EEEI CoE 115 Lecture 1 83

C Programming Language
UP EEEI CoE 115 Lecture 1 84

Data Types
UP EEEI CoE 115 Lecture 1 85

Operators
UP EEEI CoE 115 Lecture 1 86

Operators
UP EEEI CoE 115 Lecture 1 87

Conditional Statements
UP EEEI CoE 115 Lecture 1 88

Switch Statements
UP EEEI CoE 115 Lecture 1 89

For loop
UP EEEI CoE 115 Lecture 1 90

While & Do-while loop


UP EEEI CoE 115 Lecture 1 91

Arrays
UP EEEI CoE 115 Lecture 1 92

Functions
UP EEEI CoE 115 Lecture 1 93

MIX C and Assembly


UP EEEI CoE 115 Lecture 1 94

Agenda
• Embedded Systems
• Microcontrollers
• Embedded applications using MCUs
• Basics of architecture
• PIC Microcontrollers
• PIC microcontrollers (Architecture)
• Memory organization and addressing modes
UP EEEI CoE 115 Lecture 1 95

Summary

• An embedded system is a product that has one or more


computers embedded within it, which exercise primarily a
control function.
• The embedded computer is usually a microcontroller: a
microprocessor adapted for embedded control applications.
• Microcontrollers are designed according to accepted
electronic and computer principles, and are fundamentally
made up of microprocessor core, memory and peripherals; it is
important to be able to recognise their principal features.

End of Lecture Note

Potrebbero piacerti anche