Anda di halaman 1dari 21

Introduction to Microcontrollers

Parts of computer: CPU, memory, I/O CPU: Control and data path Memory: Stores instruction and data Input/output: Interact with the outside of computers

Microcontroller Components Memory


Each memory location has a specific address We must supply an address to access the corresponding location R/W allows us to select reading or writing Various types of memory for different functions and speeds
Memory location 0 Memory location 1

address

data

Memory location n-2


Memory location n-1 r/w

Microcontroller Components Memory


Read Only Memory - Memory that can only be read
Holds the program code for a microprocessor used in an embedded system where the code is always the same and is executed every time the system is switched on Computer BIOS, boot-up information

Other types of Read Only Memory


Erasable Programmable Read Only Memory (EPROM) Similar to ROM but can be erased (exposure to ultraviolet light) and reprogrammed Electrically Erasable Programmable Read Only Memory (EEPROM) more common that EPROM because it can be erased by the microprocessor Flash Memory, Ferroelectric RAM (FRAM), Magnetic Random Access Memory (MRAM)
3

Microcontroller Components Memory


Random Access Memory used to store dynamic data when processor is running
Holds program code and data during execution Can be accesses in any random order unlike takes or disks

Some types of RAM


Static RAM (SRAM) Uses transistors to store bits, fast SRAM is used for cache Dynamic RAM (DRAM) Uses capacitors to store bits, must be refreshed, smaller and cheaper than SRAM
Fast Page Mode (FPM), Extended Data Out (EDO) Synchrounous DRAM (SDRAM) introduced in 1997 and replaced most DRAM in computers by 2000 Double Data Rate (DDR SDRAM) uses both clock edges found today in most computers Direct Rambus DRAM (RDRAM) somewhat of a flop

Microcontroller Components CPU


Smart part
Processes instructions and data All the parts of a microprocessor
Register 0 Register 1 Register n-1 inst

Registers fast memory used to store operands and other information


Condition register positive/negative result Exception register overflow condition Loop count register

address r/w

data

ALU

CPU

Load-store architecture
5

Microcontroller Components I/O


Connection to the outside world Examples
Analog to Digital Converter Temperature Sensor Display Communications Circuit

Microcontroller Components BUS


Group of wires used to transport information CPU to Memory
Address bus Data bus

CPU to I/O
Port mapped I/O used when address space is limited, special instructions are needed for I/O Memory mapped I/O I/O looks like memory locations, easier to use and common in Reduced Instruction Set Computing (RISC)
7

Machine-level Execution
Machine instruction:
A bundle of binary bits with certain formats Only asks for simple operations Assembly: textual notations of machine program

Example: c = a + b; Machine execution: r1 mem(a) r2 mem(b) r3 ADD r1, r2 mem(c) r3


8

Machine-Level Execution
memory
I/O address

Load CPU Store


Stack Data
Instruction
9

Fetch inst

Major Instruction Types


Arithmetic and logic: Add, subtract, multiply, divide; and, or, not, xor Data movement: transfer data between registers and/or memories Control: Branches and jumps

10

Processor Performance
CPU Time = # Cycles Cycle Time = # Instructions CPI Cycle Time

CPI: Cycles per instruction


11

RISC vs. CISC


RISC: Reduced Instruction Set computers RISC features:
Every instruction does a simple task Fixed instruction length (usually 32 bits) Data operations only applies to registers not memory Use memory load and store instruction to move data Use general purpose registers (usually 32) Use simple ways of referencing memory words

CISC: Reduced Instruction Set computers


12

Embedded System Programming


Key factors in embedded programming
Speed timing constraints, limited processing power Code size limited memory, power

Programming methods
Machine Code bits Low level language assembly High level language C, C++, Java Application level language Visual Basic, Access

Levels of abstraction factor out details to focus on few concepts at a time


13

Embedded System Programming


Why use C in embedded programming?
Ease of management of large embedded projects Provides an additional level above assembly programming Fairly efficient Supports access to I/O

Why use assembly?


High speed, low code size However, difficult to do a large project in assembly
14

Embedded System Programming


Bit is the smallest unit of information
Base 2 notation two values 1 On, True 0 Off, False

Nibble
4 bits 16 possible values 1 Hex character = 1 nibble

Byte, Word, Double Word


8 bits, 16 bits, 32 bits Some ambiguity
15

Embedded System Programming


Three most common forms of notation
Binary (base 2) 0,1 Decimal (base 10) 0,1,2,...,9 Hexadecimal (base 16) 0,1,2,...,9, A,B,C,D,E,F

Can also use octal (base 8) Converting between forms


Binary to Hexadecimal Easy, every 4 bits is a hexadecimal character 11000100 1100 0100 C 4
16

Binary to Decimal Conversion


Each bit represents an increasing power of 2 When viewing a byte least significant bit is at the right Words can be a little more confusing
Big-endian MSB is stored first Little-endian LSB is stored first

17

Decimal to Binary Conversion


Subtract the highest power of 2 Record a 1 in the corresponding bit location Pad the remaining bit locations with a 0 We will talk about signed binary later
18

C Programming for Embedded Systems


Recommended books on C as a brush-up Web resources and tutorial links on web C session we can schedule a C session if needed ANSI C (1989) Standard for C compilers across the world

19

Variables in C
Can have long variable names
X in FORTRAN vs. Area, Graph2, InFile, etc.

No punctuation marks besides underscore Must start with a letter Case sensitive MyVariable myvariable Programming practice can you remember what a variable was used for 1 year from now? Use long variable names
H vs. height Fi vs. inputFile
20

Variables in C
For looping variables, use common looping names like i, j, k Spending a little more time now = savings later when debugging Use a naming convention to help quickly identify variables Some conventions
Variable names begin with a lower case letter, multiple words are separated by _ or capital letter
my_variable, myVariable, peripheralBaseAddress

Function names begin with an uppercase letter


My_function, MyFuncation

21

Anda mungkin juga menyukai