ee 42/100 lecture 19: microcontrollers...

44
EE 42/100 Lecture 19: Microcontrollers ELECTRONICS Rev A 3/26/2010 (9:44 AM) Prof. Ali M. Niknejad University of California, Berkeley Copyright c 2010 by Ali M. Niknejad A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 1/40

Upload: others

Post on 31-May-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

EE 42/100Lecture 19: Microcontrollers

ELECTRONICSRev A 3/26/2010 (9:44 AM)

Prof. Ali M. Niknejad

University of California, Berkeley

Copyright c© 2010 by Ali M. Niknejad

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 1/40 – p.

Page 2: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Digital versus Analog• Real signals are continuous (at least we perceive them that way) whereas binary

signals can only take on two values.

• For instance the current temperature in this room is a certain number of degreesand the resolution of this number is in principle infinite (noise limited).

• But for many applications, the precise value is not important. A finiterepresentation (certain number of significant figures) is all that we need. We maytherefore sample the signal periodically (not continuously) and also round off theactual amplitude of the signal. Note that we are making two approximations: thediscrete time sampling of the signal and the discretization process.

• Intuitively, as long as we sample the signal faster than the rate at which it changes(can be made precise using Nyquist’s Sampling Theorem), then no information islost.

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 2/40 – p.

Page 3: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Quantization Noise• On the other hand, the quantization of the signal introduces round-off errors in our

signal. If we subtract out the ideal signal s(t) from the quantized signal s(t), thedifference is the error signal, which varies randomly from sample to sample.

n(t) = s(t) − s(t)

• We can therefore think of the process as introducing “quantization noise” in thesignal.

s(t) = s(t) − n(t)

• In other words, if we are insensitive to changes of the signal smaller than thequantization noise, then the approximation is okay.

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 3/40 – p.

Page 4: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Binary Numbers• Binary numbers can only take on two values {0, 1}. To specify a bigger number,

you have to use more digits. For instance:

1010b = (1)23 + (0)22 + (1)21 + 0(20) = 8 + 2 = 10

• Some aliens with only two fingers may naturally use binary numbers but we usethem because our computers can only store two states (note: digit means finger).

• To convert a decimal number into binary, you can repeatedly divide by 2 and takethe remainder as the next digit.

• Example: 50 in binary is:50/2 = 25r0, 25/2 = 12r1, 12/2 = 6r0, 6/2 = 3r0, 3/2 = 1, r1, 1/2 = 0r1. Thisimplies the number is given by 110010b (read it backwards). Verify that24 + 21 = 8 + 4 = 12. Or

32 + 16 + 2 = 25 + 24 + 21 = 110010b

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 4/40 – p.

Page 5: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Binary Signals• Binary signals are designed to only take on two values, say {0V, 5V } to represent

{0, 1} (positive or negative logic possible).

• As signals propagate in wires, inevitably noise, distortion, and cross-talk occurs,which corrupts the signal. An analog signal is corrupted directly whereas a digitalsignal is only corrupted if the noise exceeds the noise margin of the circuit.

• Digital signals are therefore more robust and can be easily regenerated whereasanalog signals get more noisy as we process them.

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 5/40 – p.

Page 6: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Hexadecimal• A long string of binary numbers if hard to read. We therefore group 4 bits into a

nibble and group eight bits into a byte (early CS people had a good sense ofhumor!).

• A nibble can take on 16 different distinct values, so a base 16 number system isused to describe it: {0, 1, 2, · · · , 10, A, B, C, D, E, F}. Note that we use letters torepresent numbers. This takes some getting used to!

• A byte is described by two hexadecimal digitals. For instance, we converted 50 inbinary as 110010b. Let’s extend it to 8 bits by zero padding, 00110010b, or twonibbles of 0011 and 0010, which in hex are 3hex and 2hex, which we write as32hex. Verify

32hex = 3 · 161 + 2 · 160 = 48 + 2 = 50

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 6/40 – p.

Page 7: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Binary Arithmetic• Binary arithmetic is exactly the same as what you learned in grade school, except

there are only two digits! For adding single bits

0 + 0 = 0

1 + 0 = 0 + 1 = 1

1 + 1 = 0 + carry

• Let’s add two nibbles

0101 (5)+0011 (3)——-1000 (8)

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 7/40 – p.

Page 8: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Signed Binary and Two’s Complement• To represent negative numbers, we could add a “sign bit” in front of the number but

it turns out that addition no longer works (between positive and negative numbers).

• A more elegant system that preserves addition (and hence supports subtraction) isthe two’s complement system.

• The numbers 0 to 8 are represented with 0xyz where the three bits xyz take onvalues from 000 to 111. To preserve addition, we want a number and it’s negativeto add to zero. This means the negative of a number is formed as 1XY Z where0xyz + 0XY Z = 1000. If we invert all the bits in xyz (one’s complement), we getxyz, which when added gives xyz + xyz = 111. If we add 1 to this we get 1000,which is exactly the two’s complement of the number.

• 2’s complement: Invert all the bits and add 1.

• Example: 5 = 0101. −4 is formed by first forming 4, 4 = 0100, inverting, 1011, andthen adding 1, 0001 + 1011 = 1100. Now if we add5 + (−4) = 0101 + 1100 = 0001, which shows that everything works!

• Note that the most significant bit is still 1 for all negative numbers, which is also akind of sign bit.

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 8/40 – p.

Page 9: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Microcontrollers – Introduction• Microcontrollers are stripped down versions of microprocessors with additional

functionality specific to control applications.

• To save power, they run at much lower speeds than microprocessors (MHz clockrates), work with less memory, and run much simpler software. Often there is nooperating system and no more than a few kBytes of RAM.

• Speed is usually a secondary concern in microcontroller applications but latency isvery important. In other words, we want the system to be very responsive tochanges. Running a complex operating system is not a good idea!

• The most important additional functionality is the ability to interface with externalsignals, both digital and analog, using several ports.

• Most importantly, microcontrollers are very low cost, less than a dollar in certainapplications.

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 9/40 – p.

Page 10: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Microprocessors• Contrast this to today’s microprocessors, which run at multi-GHz clock speeds,

have multiple cores, and have access to Mbytes of on-chip cache and Gbytes ofoff chip memory.

• Today’s micrprocessors are designed to run complex applications, often inmulti-tasking modes, and work with relatively complex multimedia signals (audio,video, 2D/3D graphics).

• The downside is that they consume several watts of power and have complexpinouts and cost tens to hundreds of dollars. In many control applications, werequire a part that must run at much lower power levels and the required softwareis very simple.

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 10/40 – p

Page 11: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Applications• Almost any electronic device today has a microcontroller in it. From a toaster oven

to a dishwasher or battery operated toy, the microcontroller is used to “control” thedevice.

• In more complex systems, such as automobiles or aircrafts, tens to hundreds ofmicrocontrollers are used in almost every aspect of the design.

• Good Example: Compression ratio of modern internal combustion engines.

• While custom circuitry can be used in certain applications, such as a toaster oven,it is usually faster to design a system based on a microcontroller. Most of thedesign time is the software, which can be changed and fine tuned, sometimeseven after a product is shipped! (the much beloved software update)

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 11/40 – p

Page 12: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Oven Block Diagram•• In this simple system, we have heating elements, switches (homework!), a timer,

and user input. This is simple enough that an EE42/100 student could easilydesign a custom circuit to make it all work. But as the complexity of the systemincreases, the pure hardware solution gets increasingly more complex. Software iseasier to write (12-year olds make good programmers!) and the system is muchmore generic.

• More complex functionality is enabled by a microcontroller. For instance, feedbackcontrol can be used to work with less precise components by monitoring the oventemperature and adjusting the voltage accordingly.

• This also allows a richer interface (toaster oven can have settings for bread,potatoes, etc) to be introduced, giving the user a “push button” experience: Justtoast my bread the way I like it! (customized settings)

• In the future, a radio in your toaster will be used in a “smart home”. Why? Repair,upgrades, recall, remote shutdown (using a phone app), integration with theheating/cooling system, ...

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 12/40 – p

Page 13: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Generic Digital Process Control• Sensors: Sense a physical quantity to be measured. Can be digital or analog. A

digital light sensor only detects if it’s dark or light. An analog light sensor detectsthe amount of light impinging on the sensor. Other examples: temperature,pressure, humidity, ...

• Actuators: These devices take an electrical input (analog or digital) and actuate(cause into action). A good example is a solenoid switch, which responds to anelectrical signal with a mechanical force that can be used to shut on/off a valve.Examples: Motors, steppers ...

• Transducers: Convert a non-electrical signal to electrical form. Piezoelectricdevice is a good example (electricity <–> mechanical force), speakers (audio),resistor (electrical <–> heat).

• Display: As simple as an LED or as complicated as a HD LCD screen. Printers.

• Operator Inputs or Settings: Some way to input a setting into a system. The inputcan come remotely from a radio signal, a dial, a keyboard, a touch screen, etc.

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 13/40 – p

Page 14: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Microcontroller Block Diagram• The microcontroller has several key blocks including a central processing unit

(CPU), several registers (local high speed memory), on-board RAM/ROM memory,an addressing and data bus (sometimes shared), and several ports (digital,analog) for input/output. There is also a timing system used to synchronize events.

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 14/40 – p

Page 15: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Central Processing Unit: CPU• The CPU is the “brain” of the microcontroller, and it consist of an Arithmetic Logic

Unit (ALU) and control circuitry.

• The CPU is fed with instructions and data from memory. Each instruction tells theCPU to perform some simple function (add two numbers, for instance).

• CPU instructions are in the form of op-code and then operand(s). The op-code is anumeric code that tell the CPU to perform a specific function, such as “addition”.The operands (the two numbers to add) may be supplied directly or they must befetched from memory. The actual operation occurs in the ALU.

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 15/40 – p

Page 16: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

System Clock• The CPU runs at a certain clock speed. Each clock cycle the CPU performs a

given operation. The clock speed is directly related to the power dissipation of theCPU.

• Simple instructions may complete in one cycle. More complex instructions maytake several clock cycles. The key point is that the operations of the CPU aredesigned to occur in discrete units of time.

• For low power applications, an internal clock can be generated. This clock tends todrift with time due to temperature variations and noise in the circuit.

• For precision applications where timing accuracy is very important, a crystalreference is used to establish the clock frequency.

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 16/40 – p

Page 17: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

ALU• The ALU can perform simple operations (integer addition/subtraction, maybe

integer multiplication). Often only integer operations are supported (no floatingpoint!). For example, if division is not supported, the calculation must be donethrough software.

• In this class we’ll design a simple ALU that can perform addition and subtraction.

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 17/40 – p

Page 18: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Registers• The CPU has several important registers that are used as “scratch space” and to

store the current location in memory for retrieving instructions.

• Fundamental to the operation of the CPU, the following registers are used• The instruction pointer (IP) or program counter(PC), points to the next

instruction to be executed.• The stack pointer (SP), which points to the location of the “stack”• Accumulators or general purpose registers that are used for arithmetic

operations.• Index registers used for addressing modes.• A condition registor records any significant events that may have occurred in

the previous calculation (overflow, carry, zero, etc).

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 18/40 – p

Page 19: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Instruction Pointer (IP)• During each execution cycle, the data stored at location IP is read into the CPU

and executed. Each instruction contains an op-code (instruction) and operands(ADD A B) which specify where to retrieve the data for the calculation (memory orother general purpose registers).

• The instruction pointer is increment each cycle to point to the next instruction (thewidth of instructions varies based on the number and width of operands).

• The flow of instruction can jump to a new place (JSR – such as a jump tosub-routine) or jump back (to implement a loop) or forward (goto new location).Thus “jump" instructions just perform arithmetic on the value stored in the IP.

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 19/40 – p

Page 20: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Stack Pointer (SP)• The “stack” is a memory location used for temporary storage. The structure is a

First-In Last-Out (FILO) structure, much like a stack of plates.

• Data is pushed onto and off of the stack with a “PUSH” and “PULL” (or “POP”)instruction. The stack pointer is set to a high value of memory and the stack growsdownward.

• The FILO structure is very convenient for implementing sub-routines, especiallynested sub-routines or recursion.

• Suppose a program is executing and it is interrupted, which means it needs torespond to an external (or internal) event – new data is available from a sensor.The CPU needs to do a “context switch”, which means it has to save what it’scurrently doing, save the current location, jump to a new memory location (theinterrupt service routing (ISR)), and then come back and continue where it left off.

• The way this is done is that the current location (next instruction) is pushed ontothe stack, and then all the registers are “pushed” onto the stack. The IP is updatedto the ISR address, the ISR is executed, and then to return, the registers are alsopulled off the stack and calculations resumes by pulling off the return address fromthe stack.

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 20/40 – p

Page 21: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Sub-Routines

• Subroutines make heavy use of the stack:• Caller pushes arguments onto the stack (caller stack frame)• Caller pushes a location for the return value on the stack (caller stack frame)• Callee accesses arguments in caller’s stack frame• Callee pushes space for local variables (callee stack frame)• Callee may itself call other subroutines• When the callee computes the return value, it places it in the caller part of the

stack. Remember that the caller reserved some section of the stack for thereturn value.

• Callee restores stack pointer back to the way it found it just as it was beingcalled. Thus, stack pointer now points to the return value.

• Caller gets the return value, and eventually pops that off and the argumentsoff.

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 21/40 – p

Page 22: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Memory• Memory is a critical component of a microcontroller. Both instructions (programs)

and data are stored in memory.

• There are several kinds of memory in a microcontroller. Let’s focus on theon-board (internal) memory.

• The main differences in memory types are the speed (how many clock sycles doesit take to access the memory) and the volatility (does the information persist afterthe power is shut down?).

• The fastest memory is in the form of “static RAM”, which is a set of register fileswhich reside close the CPU and the cache. These consume the most power andtake up the most area, and so only a few (3-4 or dozens of registers, or kbytes orMbytes of cache) are available the the CPU.

• Many CPU instructions operate on these registers. Example: ABA –> Add thecontents of registers A and B and store the result in A.

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 22/40 – p

Page 23: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

The Address/Data Bus• When the CPU needs to access external memory, it does so through the “bus”.

While communication can occur over a “serial” or “parallel” link. A parallel link isfaster but less immune to interference and cross-talk (short range).

• The bus is a series of parallel wires that connect the CPU to external memory. Thewidth of the bus depends strongly on the application. In the lowest costapplications, a small bus of 8-bits is used.

• More complex buses use 16-bits, 32-bits, or even 64-bits. The width of thememory has two important implications: the speed at which data is read off perclock cycle and the number of unique points in memory that can be addressed. A16-bit address bus can only access a maximum of 216 = 65536 points in memory,or 64K. To access more memory, early computers used various addressing modes(“paging”). For most microcontrollers, this is enough memory!

• The bus is bidirectional since information flow occurs in both directions. Forinstance, if the CPU wants to read from memory, it can write a certain address tothe bus and then “assert the address”. The memory responds by writing thecontents of the address back onto the bus and then it “asserts the data”.

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 23/40 – p

Page 24: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To
Page 25: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To
Page 26: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Random Access Memory – RAM• RAM stands for random-access memory, meaning that any memory location can

be accessed randomly. This terminology is outdated because most memory is likethis, but the name stuck. A more accurate name would be RW memory, orread-write memory, since each memory cell can be easily read or written.

• RAM is volatile, meaning that the contents of the memory cells will be lost if poweris not applied to the chip.

• RAM also comes in two flavors: static and dynamic. We’ll learn how to designstatic RAM later in the course but the basic ingredient is a bistable circuit. Youhave already met a circuit that has two output states only – the Schmitt Trigger. Ina bistable circuit, the input only needs to be applied momentarily to “flip” the stateof the cell, and then the cell will store the state as long as power is supplied.

• In a dynamic RAM cell, tiny capacitors are used to store states. The presence orabsence of charge stores a “1” or “0”. As we shall show, this state needs to berefreshed periodically otherwise the charge leaks away.

• Access time for static RAM is much faster, but this kind of RAM is much moreexpensive since it occupies larger area.

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 24/40 – p

Page 27: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Dynamic RAM• The capacitors are so small that the charge quickly leaks away due to leakage

currents (tunneling, leakage in diodes, very small conductance of insulators, etc).

• Suppose the capacitor is 10 fF and there is 100pA of leakage current (parasiticdiodes associated with the switches that access the transistor). That means theswitch will discharge from 1V to 0V in a time

∆t =Q

I0=

C∆V

I0=

10 fF · 1V

100 pA=

10 × 10−15

100 × 10−12= 0.1 ms

• This means that the capacitor has to be refreshed at a rate of 10 kHz or more tokeep the data persistent. If power is turned off, the information leaks away veryquickly.

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 25/40 – p

Page 28: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

ROM• ROM – Read Only Memory. This kind of memory can only be read (cannot write to

it). It’s used to store small programs (boot up sequence for a CPU).

• Unlike RAM, ROM is non-volatile memory, meaning that it keeps its stateregardless of the wether power is supplied to a cell.

• There are many ROM technologies. The simplest are made of resistor fuses. If alarge current is run through a thin wire, it melts and we get an “open circuit”between two points. This corresponds to say a “1”; if the fuse is not blown, thatrepresents a “0” state.

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 26/40 – p

Page 29: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Flash• Flash: Store data on “floating gate” transistors – something we’ll learn about later.

• Invented in 1980, Flash memory has become very popular in the past 10 years –think of the iPod and the introduction of solid-state disk drives.

• Flash memory is like ROM in that it’s non-volatile, but it’s electricallyprogrammable. This is very convenient since the “hardware code” in a device canbe updated on the fly.

• The read times are slower than RAM, but still fast enough for many applications.The write times are much slower.

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 27/40 – p

Page 30: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Ports• Input/Output ports on a microprocessor allow external signals to directly interface

with the microprocessor.

• Typically a few or tens of signals are available. The microprocessor can writedigital signals to each port (say an LED display) or it can read data from externalinputs. The same pins are shared to save real estate.

• Some microcontrollers have built-in conversion capability from digital to analogform.

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 28/40 – p

Page 31: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Digital I/O• Some sensors output digital signals naturally. These need to be conditioned to

have the right voltage levels before they are applied to the microprocessor. Even ifthe signal is analog, we can restrict it to take digital levels (light detector example)using some simple analog signal processing (Schmitt trigger).

• The CPU can only do one thing at a time. In every given clock cycle, it is executingan instruction. If we want it to read/write a digital port, we usually read/write aparticular memory address reserved for the port.

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 29/40 – p

Page 32: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Interrupts versus Polling• We may read the digital ports either periodically (synchronous) by polling or in an

event driven fashion (asynchronous) using interrupts.

• Say we have a microprocessor running at 1 MHz, or a clock period of 1 µs. Wecan periodically examine the port signals (say 1 out of every 100 cycles), whichmeans we can read signals changing as fast as 10 kHz, or 0.1 ms. This meansthat we need a mechanism to do this periodically.

• Most microcontrollers have timers that can be used to raise interrupts periodically.When an interrupt occurs, the microprocessor saves its state, stops the currentinstruction, and instead jumps to a particular memory address and executes thecode say corresponding to reading the port.

• On the other hand, we can use the digital input to interrupt the microprocessorwhen a new value is ready. This is done by raising the Interrupt Request (IRQ) pinof the microprocessor.

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 30/40 – p

Page 33: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Analog I/O• Some microcontrollers can read and write analog signal directly.

• Inside the microcontroller, all signals are digital. To read/write analog signals,therefore, requires signal conversion.

• This circuit building block is an Analog-to-Digital Converter (ADC) andDigital-to-Analog Convertors (DAC).

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 31/40 – p

Page 34: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Analog-to-Digital Converter• The important specifications for an ADC are the sampling rate (clock rate) of the

ADC and its resolution (number of bits).

• Analog signals need to be sampled at a rate of twice the signal bandwidth (audiobandwidth is roughly 5 kHz).

• The resolution determines the smallest discernible signal since the full-scale inputvoltage (say 5V) is divided by 2N where N is the number of bits. Any signalsmaller than this level is lost in the “quantization noise” (round-off error).

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 32/40 – p

Page 35: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Digital-to-Analog Converter• Likewise, some microcontrollers can write analog outputs directly using a

Digital-to-Analog Converter (DAC).

• In class and homework we learned how to build some simple DACs using currentsumming op-amp circuits.

• A “poor person’s DAC” can always be realized by using a variable duty cycledsignal to represent an analog output. The signal must be filtered and oversampled(clock frequency much higher than the signal bandwidth).

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 33/40 – p

Page 36: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Instruction Set• The typical microcontroller includes several simple instructions. We’ll explore

some sample instructions to get a flavor for the machine language of the CPU(examples from the 68HC11 ubiquitous controller):

ABA (opr) ; Add accum: A + B --> AADDA (opr) ; Add memory to A: A + M --> AADDB (opr) ; B + M --> BADDD (opr)BCS (rel) ; Branch if carry setBEQ (rel) ;Branch if zeroBLO (rel) ;Branch if LowerBNE (rel) ; Branch if not equalBRA (rel) ; Always branchCLRA ; Clear acc ACLRB ; Clear acc BCOMA ; Complement A: $FF - A --> AINCA ; Increment acc AINCB ; Increment acc BJMP (opr) ; Jump Address --> PCJSR (opr) ; Jump to subroutine (see RTS)LDAA (opr) ; Load Accumulator A: M-->ALDABLDXLDDLDY

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 34/40 – p

Page 37: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Instruction Set (cont)• To push and pull the contents of the stack, special instructions are used. Also,

instructions to move the contents of the registers to memory are given.

MUL ; Multiply registers A and B and store in D: A * B --> DPSHA ; Push onto stackPSHBPSHXPULA ; Pull off of stackPULBPULXPULYRTS ; Return from sub-routineSTAA ; Store the contents of register into memory: A--> MSTABSTD

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 35/40 – p

Page 38: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Assembly Language/Machine Language• Ultimately each instruction is represented by a number and the operands are also

represented by numbers. So an entire program is a long string of binary numbers.

• Humans have difficulty reading/writing this kind of program, so we use mnemonicrepresentations for the instructions (ADD versus $F5 for instance).

• In addition, the registers are named and constants can be represented withsymbols (not variables!).

• Certain points in memory can also be labeled to make it easy to write instructionssuch as (JMP END_PROGRAM).

• The process of converting assembly code into machine code is done by theassembler. The program is loaded into memory by a loader. Some programs arewritten to only run in certain parts of memory but most good code should be ableto run in any memory location. So the JMP instruction should be a relative offset!

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 36/40 – p

Page 39: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Example Programs• Contrast writing a program in C (a high level language) with Assembly Language,

which is closely related to Machine Language, the actual instructions fed into thecomputer.

int acc = 0;for(i = 0; i < 100; i++){acc += func(i);}

• Versus:

LDA #99CLRA ; clear contents of acc ACLRB ; clear contents of acc BSTAB $RESULT ; clear memory address (our final result stored here)BRANCH_POINT:PSHA ; put argument on stackPSHA ; this is the return value (just making room on the stack)JSR CALC_SUB ; call the function; return value is now on the stackPULB ; put return value in register BADDB $RESULTDECA ; increment XBNE BRANCH_POINT ; if A is not zero, branch to start of loop; result is now stored in address $RESULT

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 37/40 – p

Page 40: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Higher Level Languages• Higher level languages such as C or C++ are translated into assembly language

by a compiler. More sophisticated programs can be written this way becausehigher level programs abstract away all the small steps involved in a complicatedcalculation. For instance, in C++ you may write

object->Display()

• This involves thousands of lines of code but to understand the program, you onlyneed to understand that the above code displays an object on the screen. Youdon’t need to see all the small steps involved (break object into sub-objects, findx-y coordinates, translate coordinates to screen coordinates, draw lines, drawpoints, write to memory locations ...)

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 38/40 – p

Page 41: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Addressing Modes• The operands of the instructions require the CPU to fetch data from memory. The

memory location can be specified in different ways, which allows one to optimizethe code for a specific application.

• Extended Addressing: The entire address is specified directly. The followinginstruction fetches the contents stored at $FA0B

ADDA $FA0B ; fetch the value at $FA0B and add it to the value in register

• Direct Addressing: Only the least significant digits (hex) are specified and the mostsignificant bits are assumed zero:

ADDA $F5 ; fetch the value at $00F5 and add it to the value in register

• Inherent Addressing: Only access the registers

ABA ; add register B and A

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 39/40 – p

Page 42: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To

Addressing Modes (cont)• Immediate Addressing: The operand is in the location immediately following the

instruction (signified by # preceding operand)

ADD #$93 ; load 83 to the contents of A

• It’s called immediate because of the way it’s loaded in memory:

8B (op code for ADDA)93 (the operand)

• Indexed Addressing: Effective address is the sum of an offset byte contained in thecontents of a register (say X)

ADDA $03,X

• This loads the memory contents stored at X + offset

• Relative Addressing: Used for branching. The program counter is used as theindex and a relative offset is supplied.

BEQ -$05 ; branch if result equals zero; if zero, increment PC by -5 bytes, which means we’re branching back in

A. M. Niknejad University of California, Berkeley EE 100 / 42 Lecture 19 p. 40/40 – p

Page 43: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To
Page 44: EE 42/100 Lecture 19: Microcontrollers ELECTRONICSinst.eecs.berkeley.edu/~ee100/su10/pdf/lect19_ann2.pdf · Binary Numbers • Binary numbers can only take on two values {0,1}. To