Anda di halaman 1dari 71

Microcontroller &

Applications
By Mr. AsimSayed
microprocessor and microcontroller
Microprocessors vs Microcontrollers
The contrast between a microcontroller and a microprocessor is best exemplified by
the fact that most microprocessors have many operational codes (opcodes) for
moving data from external memory to the CPU; microcontrollers may have one, or
two.
Microprocessors may have one or two types of bit-handling instructions;
microcontrollers will have many.
The microprocessor is concerned with rapid movement of code and data from
external addresses to the chip; the microcontroller is concerned with rapid
movement of bits within the chip.
The microcontroller can function as a computer with the addition of no external
digital parts; the microprocessor must have many additional parts to be operational.
Eight-Bit Microcontrollers
Eight bits has proven to be a very useful word size for small
computing tasks.
Capable of 256 decimal values or quarter-percent resolution. the 1-
byte word is adequate for many control and monitoring applications.
Serial ASCII data is also stored in byte sizes, making 8 bits the natural
choice for data communications.
Most integrated circuit memories and many logic functions are
arranged in an 8-bit configuration that interfaces easily to data buses
of 8 bits.
Harvard architecture
The Harvard architecture is a computer architecture with physically
separate storage and signal pathways for instructions and data.
The term originated from the Harvard Mark I relay-based computer, which
stored instructions on punched tape (24 bits wide) and data in electro-
mechanical counters.
These early machines had limited data storage, entirely contained within
the central processing unit, and provided no access to the instruction
storage as data.
Programs needed to be loaded by an operator, the processor could not
boot itself.
Modified Harvard architecture
Today, most processors implement such separate signal pathways for
performance reasons but actually implement a Modified Harvard
architecture, so they can support tasks like loading a program from
disk storage as data and then executing it.
Von Neumann architecture
This is also known as Princeton architecture.
The data and code memories are combined into one architecture.
In this architecture instruction fetching and data operations ant occur
at the same time.
The design is simpler than Harvard architecture.
Microcontrollers for embedded systems
Microprocessors and microcontrollers are widely used in embedded
system products.
An embedded product uses a microprocessor (or microcontroller) to
do one task and one task only.
Ex. of embedded applications-
Use of microcontroller based systems and PCs.
One of the most critical needs of an embedded system is to decrease
power consumption and space.
This can be achieved by integrating more functions into the CPU chip.
All the embedded processors based on the x86 and 680x0 have low
Power consumption in addition to some forms of 110, COM p011,
and ROM all on a single chip.
In high-performance embedded processors, the trend is to integrate
more and more functions on the CPU chip and let the designer decide
which features he/she wants to use.
Criteria for choosing a microcontroller
Three criteria in choosing microcontrollers are as follows:
(I) meeting the computing needs or the task at hand efficiently and cost
effectively,
(2) availability of software development tools such as compilers,
assemblers, and debuggers
(3) wide availability and reliable sources of the microcontroller.
Bitwise Data Transmission
Data transmission requires:
Encoding bits as energy
Transmitting energy through medium
Decoding energy back into bits
Energy can be electric current, radio, infrared, light, smell,
etc.
Transmitter and receiver must agree on encoding scheme
and transmission timing
Asynchronous Transmission
One definition of asynchronous: transmitter and receiver do not
explicitly coordinate each data transmission
Transmitter can wait arbitrarily long between transmissions
Used, for example, when transmitter such as a keyboard may not always
have data ready to send
Asynchronous may also mean no explicit information about where
data bits begin and end
E.g. when we send individual ASCII characters
Using Electric Current to Send Bits
Simple idea - use varying voltages to represent 1s and 0s
One common encoding use negative voltage for 1 and
positive voltage for 0
In following figure, transmitter puts positive voltage on line
for 0 and negative voltage on line for 1
Transmission Timing Problems
Encoding scheme leaves several questions
unanswered:
How long will voltage last for each bit?
How soon will next bit start?
How will the transmitter and receiver agree on timing?
Later : Self-clocking codes (e.g. Manchester Encoding)
Standards specify operation of communication
systems
Devices from different vendors that adhere to the
standard can interoperate
Example organizations:
International Telecommunications Union (ITU)
Electronic Industries Association (EIA)
Institute for Electrical and Electronics Engineers (IEEE)
RS-232
Standard for transfer of characters across copper wire
Produced by EIA
Full name is RS-232-C
RS-232 defines serial, asynchronous communication
Serial - bits are encoded and transmitted one at a time (as opposed to parallel
transmission)
Asynchronous - characters can be sent at any time and bits are not
individually synchronized
Details of RS-232
Components of standard:
Connection must be less than 50 feet
Data represented by voltages between +15v and -15v
25-pin connector, with specific signals such as data, ground and control
assigned to designated pins
Specifies transmission of characters between, e.g., a terminal and a modem
Transmitter never leaves wire at 0v; when idle, transmitter puts negative
voltage (a 1) on the wire
Identifying asynchronous
characters
Transmitter indicates start of next character by
transmitting a one
Receiver can detect transition as start of character
Extra one called the start bit
Transmitter must leave wire idle so receiver can detect
transition marking beginning of next character
Transmitter sends a zero after each character
Extra zero call the stop bit
Thus, character represented by 7 data bits requires
transmission of 9 bits across the wire
Start, Stop Bits
Typically one of the data bits might be a parity bit
(7N1, 8E1)
Timing
Transmitter and receiver must agree on timing of
each bit
Agreement accomplished by choosing transmission
rate
Measured in bits per second
Detection of start bit indicates to receiver when
subsequent bits will arrive
Hardware can usually be configured to select
matching bit rates
Switch settings
Software
Autodetection
Transmission Rates
Baud rate measures number of signal changes per second
Bits per second measures number of bits transmitted per second
In RS-232, each signal change represents one bit, so baud rate and
bits per second are equal
If each signal change represents more than one bit, bits per second
may be greater than baud rate
This is the case with modems nowadays!
More on this when we look at modulation
Framing
Start and stop bits represent framing of each character
If transmitter and reciver are using different speeds, stop bit
will not be received at the expected time
Problem is called a framing error
RS-232 devices may send an intentional framing error
called a BREAK
Duplex
Two endpoints may send data simultaneously -
full-duplex communication
Requires an electrical path in each direction
If only one endpoint may send data half-duplex
communications or simplex
Pin 2 - Receive (RxD)
Pin 3 - Transmit (TxD)
Pin 4 - Ready to send (RTS)
Pin 5 - Clear to send (CTS)
Pin 7 - Ground
Limitations on Transmission
Limitations on wires makes waveforms look like:
Longer wire, external interference may make signal look even worse
RS-232 standard specifies how precise a waveform the transmitter
must generate, and how tolerant the receiver must be of imprecise
waveform
Channel Capacity
Data rate
In bits per second
Rate at which data can be communicated
Bandwidth
In cycles per second, or Hertz
Amount of bandwidth constrained by transmitter and medium (and the feds!)
For digital data: Want as high a data rate as possible given some slice
of bandwidth! Limited by the error rate
Nyquist Bandwidth(1)
If the rate of signal transmission is 2B then a signal with frequencies
no greater than B is sufficient to carry the signal rate
Converse: Given a bandwidth of B, the highest signal rate that can
be carried is 2B
Ex: Given 3000Hz (typical on phone lines), the capacity C of the
channel is : C=2B = 6000bps
Nyquist Bandwidth(2)
Wait! But given about 3000Hz our modems go much faster than
6000bps. How?
The previous capacity assumes a binary signal element. If a signal
element can represent more than one bit, the formulation becomes:
C=2B(log
2
M) ; M = # of signal elements
If M=32, we get C=30,000bps
Shannons Capacity
Shannons capacity includes the concept of error rates.
At a given noise level, the higher the data rate, the
higher the error rate. This is a theoretical maximum!
Signal to Noise Ratio:
SNR = SignalPower/NoisePower
Ratio measured at the receiver
SNR
db
= 10log
10
(SNR)
SNR of 100 = 20 dB
SNR of 1000 = 30 dB
Capacity:
C = B*log
2
(1+SNR)
Shannon Capacity Examples
If voice telephone has a SNR of 30 dB and bandwidth of 3000 Hz:
C = 3000 log
2
(1 + 1000) = 30,000 bps
If our LAN technology has a SNR=251, B = 1Mhz
C=10
6
*log
2
(252) = 8Mbps
Using Nyquists formula, the number of symbols we would need to
transmit this data per signaling element:
8*10
6
= 2*10
6
*log
2
M M = 2
4
= 16
RS-232 Signals
Architecturally RS-232 is a
bi-directional point to point
link.
(serial port - PC side)
Two independent channels are
established for two-way (full-
duplex) communications.
RS-232 can also carry
additional signals used for flow
control (RTS, CTS) and modem
control (DCD, DTR, DSR, RI).
RS-232 Signals
Common 25 pin D-shell connector pinout used for
asynchronous data communications.
Pin Signal
1 PGND Protective Ground
2 TXD Transmit Data
3 RXD Receive Data
4 RTS Ready To Send
5 CTS Clear To Send
6 DSR Data Set Ready
7 SG Signal Ground
8 CD Carrier Detect
20 DTR Data Terminal Ready
22 RI Ring Indicator
(serial port - PC side)
RS-232 Line Driver
Unbalanced Line Drivers
Each signal appears on the interface connector as a voltage with
reference to a signal ground.
The idle state (MARK) has the signal level negative with respect
to common whereas the active state (SPACE) has the signal level
positive respest to the same reference.
RS-232 Speed
How fast can RS-232 be?
The maximum speed, according to the standard, is 20kbit/s.
However, modern equipment can operate much faster than this.
(i.e. Lynx can reach 115200 baud.)
The length of the cable also plays a part in
maximum speed.
The longer the cable and the slower the speed at which you can
obtain accurate results.
A large wire capacitance and inductance limits the maximum
length of the cable and/or the maximum speed; Moreover higher
is the capacitance of the cable higer is the interference between
two adjacent signal wire.
50 feet (15m) @ max baudrate is commonly quoted as the
maximum distance.
It is not specified in EIA standard but its recommended respect
these values.
RS-232 sw settings
One byte of async data has:
Start Bit = 1 (always)
Data Bits = 8 (or 7)
Stop bits = 1 (or 2)
Parity = NONE (or EVEN or ODD)
+ 25
- 25
What is RS-485
What is RS-485?
RS-485 is a EIA standard interface which is very common in the
data acquisition world
RS-485 provides balanced transmission line which also can be
shared in Multidrop mode.
It allows high data rates communications over long distances in
real world environments.
How fast can RS-485 be?
RS-485 was designed for greater distance and higher baudrates
than RS-232.
According to the standard, 100kbit/s is the maximum speed and
distance up to 4000 feet (1200 meters) can be achieved.
RS-485 Line Driver
Balanced Line Drivers
Voltage produced by the driver appears across a pair of signal
wires that transmit only one signal. Both wires are driven opposite.
RS-485 driver has always the Enable direction control signal.
Differential system provides noise immunity, because much of the
common mode signal can be rejected by the receiver. So ground
shifts and induced noise signals can be nullified.
RS-485 Network
RS-485 provides Half-Duplex, Multidrop communications over a
single twisted pair cable.
MASTER
SLAVE-1
SLAVE-2
SLAVE-3
The standard specifies
up to 32 drivers and
32 receivers can share
a multidrop network
Terminator resistors
avoid reflected signal
RS-485 Half-duplex
Datalogic uses Half-Duplex configurations for Data Collecting
and Master/Slave layouts.
Usually we talk about Multidrop
network (i.e. MX4000,DPS9000)
Slave 1
TX
ENABLE
RX
Slave 2
TX
ENABLE
RX
Slave N
TX
ENABLE
RX
RTX485+
RTX485-
485GND
Master
TX
ENABLE
RX
RS-485 Full-duplex
Potentially RS-485 interface can also use 4-wires to comunicate in
multidrop mode but...
Scanner
TX
ENABLE
RX
TX485+
TX485-
RX485+
RX485-
485GND
TX
ENABLE
RX
Other device
RS-232 vs RS-485
The architectural difference between RS-232 and RS-485 is
that 232 is a bi-directional point to point link, whereas 485 is
a single channel bus.
Electrically, each 232 signal uses a single wire with symmetric
voltages about a common ground wire. 485 uses two wires to
carry the single signal differentially.
The big difference to the software is that only one device on a
485 bus can transmit at a time, whilst there is not similar
limitation on RS232 because is a peer-to-peer link .
RS-232 vs RS-485
RS-232 RS-485
Mode of Operation SINGLE-ENDED DIFFERENTIAL
Total Number of Drivers 1 DRIVER 32 DRIVER
and Receivers on One Line 1 RECEIVER 32 RECEIVER
Maximum Cable Length 50 FEET 4000 FEET
Maximum Data Rate @Max length 20kb/s 100kb/s
Driver Output Signal Level
(Loaded Min.) Loaded +/-5V to +/-15V +/-1.5V
Driver Output Signal Level
(Unloaded Max) Unloaded +/-25V +/-6V
Driver Load Impedance 3k to 7k 54
Max. Driver Current in
High Z State Power On N/A N/A
Max. Driver Current in
High Z State Power Off +/-6mA @ +/-2v +/-100uA
Slew Rate (Max.) 30V/S N/A
Receiver Input Voltage Range +/-15V -7V to +12V
Receiver Input Sensitivity +/-3V +/-200mV
Receiver Input Resistance 3k to 7k 12k
Data Format and Protocols
Information content passing through peer-to-peer connection is packed in a
very simple structure:
<Header-string> <Code identifier ><INFO-FIELD><Terminator-string>
<Header-string> and <Terminator-string> are both configurable via software
(device configuration parameters)
Most common generic Handshake are available/selectable with RS232
interface:
Hardware (RTS-CTS)
Software XON/XOFF
Single ended signalling
Single-ended signaling is the simplest and most commonly used method of
transmitting electrical signals over wires. One wire carries a varying voltage
that represents the signal, while the other wire is connected to a reference
voltage, usually ground.
Single ended signaling is less expensive to implement than differential, but it
lacks the ability to reject noise caused by:
differences in ground voltage level between transmitting and receiving circuits
induction picked up on the signal wire
Ex of single ended signalling
Single-ended signaling is widely used, and can be seen in numerous common
transmission standards, including:
RS-232 serial communications
PS/2 mouse and keyboard connectors
IC serial bus
TTL circuits
CMOS logic circuits
ECL circuits
Most parallel computer buses, such as:
VMEbus
PCI
continued
Some more examples:
VGA video connectors
SCSI interfaces for hard drives and other peripherals
Parallel ATA interfaces for hard drives and other peripherals
Some kinds of connectors, though more often used for balanced pairs, are
sometimes used for single-ended operation:
RCA jacks for audio signals
TRS phone connectors for audio signals
The main advantage of single-ended over differential signaling is that fewer
wires are needed to transmit multiple signals.
If there are n signals, then there are n+1 wires - one for each signal and one
for ground. (Differential signaling uses at least 2n wires.)
A disadvantage of single-ended signaling is that the return currents for all the
signals share the same conductor (even if separate ground wires are used, the
grounds are inevitably connected together at each end), and this can
sometimes cause interference ("crosstalk") between the signals.
Differential Signalling
Differential signaling is a method of transmitting information electrically with
two complementary signals sent on two paired wires, called a differential pair.
Since external interference tends to affect both wires together, and
information is sent only by the difference between the wires, the technique
improves resistance to electromagnetic noise compared with use of only one
wire and an un-paired reference (ground).
The technique can be used for both analog signaling, as in balanced audio, and
digital signaling, as in RS-422, RS-485, Ethernet over twisted pair, PCI
Express, Display Port, HDMI, and USB. The opposite technique is called single-
ended signaling. Differential pairs are usually found on a printed circuit board,
in cables (twisted-pair cables, ribbon cables), and in connectors.
SPI
The Serial Peripheral Interface or SPI bus is a synchronous serial
data link, a de facto standard, named by Motorola, that operates in
full duplex mode.
Devices communicate in master/slave mode where the master device
initiates the data frame. Multiple slave devices are allowed with
individual slave select lines. Sometimes SPI is called a four-wire
serial bus, contrasting with three-, two-, and one-wire serial buses.
SPI is often referred to as SSI (Synchronous Serial Interface).
V 0.3 50
The MOSI/MISO convention requires that, on devices using the alternate names, SDI on
the master be connected to SDO on the slave, and vice versa.
Chip select polarity is rarely active high, although some notations (such as SS or CS
instead of nSS or nCS) suggest otherwise.
Slave select is used instead of an addressing concept.
The SPI bus specifies four logic signals:
SCLK : Serial Clock (output from master).
MOSI : Master Output, Slave Input (output from
master).
MISO : Master Input, Slave Output (output from
slave).
SS : Slave Select (active low, output from master).
Alternative naming conventions are also widely used,
and SPI port pin names for particular IC products may
differ from those depicted in these illustrations:
SCLK : SCK, CLK.
MOSI : SIMO, SDO, DO, DOUT, SI, MTSR.
MISO : SOMI, SDI, DI, DIN, SO, MRST.
SS : nCS, CS, CSB, CSN, nSS, STE, SYNC.
V 0.3 51
V 0.3 52
Serial Peripheral Interface (SPI)
SDI: data
in
SDO:
data out
SCK:
clock
V 0.3 53
CKE configuration bit allows either falling or rising edge
of clock to be used, while CKP selects clock polarity.
V 0.3 54
SPM bit selects whether data is sampled in middle of clock
period or at end of clock period.
Between the CKP, CKE, SPM bits there is a lot of flexibility in
how data is clocked in. Can make the SPI protocol work with
just about any serial device.
V 0.3 55
Multiple SPI peripherals each require a separate chip select line
via parallel port line. We will concentrate on the I2C serial bus
as it does not require use of chip selects.
I2C
IC (Inter-Integrated Circuit, alternately spelled I2C or IIC, most commonly
pronounced I-squared-C) is a multimaster serial single-ended computer bus
invented by the Philips semiconductor division, today NXP Semiconductors,
and used for attaching low-speed peripherals to a motherboard,
embedded system, cellphone, or other digital electronic devices.
IC uses only two bidirectional open-drain lines, Serial Data Line (SDA) and
Serial Clock Line (SCL), pulled up with resistors.
Typical voltages used are +5 V or +3.3 V although systems with other
voltages are permitted.
V 0.3 56
V 0.3 57
I
2
C (Inter-Integrated-Circuit) Bus
I2C is a two wire serial interface.
18F242
SDA
Microchip 24LC515
SDA
A2
SCL
10K
10K
SCL
Vdd
Vdd
A1
A0
SDA
A2 SCL
A1
A0
SCL clock line
SDA data
(bidirectional)
V 0.3 58
What is a bus??
V 0.3 59
I
2
C Features
Multiple receivers do not require separate select
lines as in SPI
At start of each I
2
C transaction a 7-bit device address is
sent
Each device listens if device address matches internal
address, then device responds
SDA (data line) is bidirectional, communication is
half duplex
SDA, SCLK are open-drain, require external
pullups
Allows multiple bus masters (will discuss this more
later).
V 0.3 60
I2C Bus Addressing
No chip selects needed!!!!!
pullups are needed
V 0.3 61
I2C Bus Transfer
Multiple bytes sent in a transaction; every 8 bits has a
9
th
bit that is an acknowledge.
V 0.3 62
Write (master to slave)
Read (master from slave)
Master
initiates all
transactions,
read or write.
IC is appropriate for peripherals where simplicity and low manufacturing cost are
more important than speed. Common applications of the IC bus are:
Reading configuration data from SPD EEPROMs on SDRAM, DDR SDRAM, DDR2 SDRAM
memory sticks (DIMM) and other stacked PC boards
Supporting systems management for PCI cards, through an SMBus 2.0 connection.
Accessing NVRAM chips that keep user settings.
Accessing low speed DACs and ADCs.
Changing contrast, hue, and color balance settings in monitors (Display Data Channel).
Changing sound volume in intelligent speakers.
Controlling OLED/LCD displays, like in a cellphone.
Reading hardware monitors and diagnostic sensors, like a CPU thermostat and fan speed.
Reading real-time clocks.
Turning on and turning off the power supply of system components.
V 0.3 63
Assembler
An assembler is a program which creates object code by translating combinations
of mnemonics and syntax for operations and addressing modes into their
numerical equivalents. This representation typically includes an operation code
("opcode") as well as other control bits.
The assembler also calculates constant expressions and resolves symbolic names
for memory locations and other entities.
The use of symbolic references is a key feature of assemblers, saving tedious
calculations and manual address updates after program modifications.

Most assemblers also include macro facilities for performing textual


substitutione.g., to generate common short sequences of instructions as inline,
instead of called subroutines.
There are two types of assemblers based on how many passes
through the source are needed to produce the executable program.
One-pass assemblers go through the source code once. Any symbol used
before it is defined will require "errata" at the end of the object code (or, at
least, no earlier than the point where the symbol is defined) telling the linker
or the loader to "go back" and overwrite a placeholder which had been left
where the as yet undefined symbol was used.
Multi-pass assemblers create a table with all symbols and their values in the
first passes, then use the table in later passes to generate code.
Compiler
A compiler is a computer program (or set of programs) that
transforms source code written in a programming language (the
source language) into another computer language (the target
language, often having a binary form known as object code).
The most common reason for wanting to transform source code is to
create an executable program.
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 or machine code).
If the compiled program can run on a computer whose CPU or operating system is
different from the one on which the compiler runs, the compiler is known as a
cross-compiler.
A cross compiler is necessary to compile for multiple platforms from one
machine. A platform could be infeasible for a compiler to run on, such as for the
microcontroller of an embedded system because those systems contain no
operating system.
In paravirtualization one machine runs many operating systems, and a cross
compiler could generate an executable for each of them from one main source.
Debugger
A debugger or debugging tool is a computer program that is used to test
and debug other programs (the "target" program).
The code to be examined might alternatively be running on an instruction
set simulator (ISS), a technique that allows great power in its ability to halt
when specific conditions are encountered but which will typically be
somewhat slower than executing the code directly on the appropriate (or
the same) processor.
Some debuggers offer two modes of operationfull or partial simulation
to limit this impact.
A "trap" occurs when the program cannot normally continue because of a
programming bug or invalid data. For example, the program might have tried to
use an instruction not available on the current version of the CPU or attempted to
access unavailable or protected memory.
When the program "traps" or reaches a preset condition, the debugger typically
shows the location in the original code if it is a source-level debugger or symbolic
debugger, commonly now seen in integrated development environments.
If it is a low-level debugger or a machine-language debugger it shows the line in
the disassembly (unless it also has online access to the original source code and
can display the appropriate section of code from the assembly or compilation).
IDE
An integrated development environment (IDE) or interactive
development environment is a software application that provides
comprehensive facilities to computer programmers for software
development.
An IDE normally consists of a source code editor, build automation
tools and a debugger.
Most modern IDEs offer Intelligent code completion features.
Some IDEs contain a compiler, interpreter, or both, such as Net Beans and Eclipse;
others do not, such as SharpDevelop and Lazarus.
The boundary between an integrated development environment and other parts
of the broader software development environment is not well-defined.
Sometimes a version control system and various tools are integrated to simplify
the construction of a GUI. Many modern IDEs also have a class browser, an object
browser, and a class hierarchy diagram, for use in object-oriented software
development.

Anda mungkin juga menyukai