You are on page 1of 105

Embedded Systems Design: A Unified

Hardware/Software Introduction

Chapter 4 Standard Single Purpose


Processors: Peripherals

1
Introduction

• Single-purpose processors
– Performs specific computation task
– Custom single-purpose processors
• Designed by us for a unique task
– Standard single-purpose processors
• “Off-the-shelf” -- pre-designed for a common task
• a.k.a., peripherals
• serial transmission
• analog/digital conversions

Embedded Systems Design: A Unified 2


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Timers, counters, watchdog timers

• Timer: measures time intervals


– To generate timed output events
• e.g., hold traffic light green for 10 s
– To measure input events Basic timer

• e.g., measure a car’s speed Clk


16-bit up 16 Cnt
counter
• Based on counting clock pulses
Top
• E.g., let Clk period be 10 ns Reset
• And we count 20,000 Clk pulses
• Then 200 microseconds have passed
• 16-bit counter would count up to 65,535*10 ns =
655.35 microsec., resolution = 10 ns
• Top: indicates top count reached, wrap-around

Embedded Systems Design: A Unified 3


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Counters

• Counter: like a timer, but counts Timer/counter


pulses on a general input signal Clk
2x1 16-bit up 16 Cnt
rather than clock mux counter

– e.g., count cars passing over a sensor Cnt_in Top

– Can often configure device as either a Reset


Mode
timer or counter

Embedded Systems Design: A Unified 4


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Other timer structures
• Interval timer 16/32-bit timer

– Indicates when desired time Clk 16-bit up


counter 16 Cnt1
Timer with a terminal
interval has passed count
– We set terminal count to 16-bit up
Top1
Clk
desired interval counter 16 Cnt
16-bit up Cnt2
• Number of clock cycles counter 16

= Desired time interval / Reset Top2


Clock period
=
• Cascaded counters Top Time with prescaler

• Prescaler Clk Prescaler 16-bit up


counter
Terminal count
– Divides clock
– Increases range, decreases
resolution Mode

Embedded Systems Design: A Unified 5


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Example: Reaction Timer
indicator reaction /* main.c */
light button
#define MS_INIT 63535
void main(void){
LCD time: 100 ms int count_milliseconds = 0;

configure timer mode


• Measure time between turning light on and set Cnt to MS_INIT

user pushing button wait a random amount of time


turn on indicator light
start timer
– 16-bit timer, clk period is 83.33 ns, counter
increments every 6 cycles while (user has not pushed reaction button){
if(Top) {
stop timer
– Resolution = 6*83.33=0.5 microsec. set Cnt to MS_INIT
start timer
– Range = 65535*0.5 microseconds = 32.77 reset Top
count_milliseconds++;
milliseconds }
}
– Want program to count millisec., so initialize turn light off
printf(“time: %i ms“, count_milliseconds);
counter to 65535 – 1000/0.5 = 63535 }

Embedded Systems Design: A Unified 6


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Watchdog timer

• A timing device such that it is set for a preset time


interval and an event must occur during that interval
else the device will generate the timeout signal on
failure to get that event in the watched time interval.
• On that event, the watchdog timer is disabled to
disable generation of timeout or reset.
• Timeout may result in processor start a service routine
or start from beginning

Embedded Systems Design: A Unified 7


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Example

• Assume that we anticipate that a set of tasks must


finish in 100 ms interval.
• The watchdog timer is disabled and stopped by the
program instruction in case the tasks finish within 100
ms interval.
• In case task does not finish (not disabled by the
program instruction), watchdog timer generates
interrupts after 100 ms and executes a routine, which is
programmed to run because there is failure of finishing
the task in anticipated interval.
Embedded Systems Design: A Unified 8
Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Watchdog timer
• Must reset timer every
osc clk overflow overflow
X time unit, else timer prescaler scalereg timereg
to system reset
or
generates a signal interrupt

• Common use: detect checkreg

failure, self-reset
• Another use: timeouts /* main.c */ watchdog_reset_routine(){
/* checkreg is set so we can load value into
main(){ timereg. Zero is loaded into scalereg and
– e.g., ATM machine wait until card inserted 11070 is loaded into timereg */
call watchdog_reset_routine
– 16-bit timer, 2 checkreg = 1
microsec. resolution while(transaction in progress){ scalereg = 0
if(button pressed){ timereg = 11070
– timereg value = 2*(216- perform corresponding action }
call watchdog_reset_routine
1)–X = 131070–X } void interrupt_service_routine(){
– For 2 min., X = eject card
/* if watchdog_reset_routine not called every reset screen
120,000 microsec. < 2 minutes, interrupt_service_routine is }
called */
}

Embedded Systems Design: A Unified 9


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Explanation
t = 12 x 211 x 1 /12 x 106
= 0.002 sec = 2 milli second Timer range = 216 x 2 ms =
2.18 minutes
Resolution = 2 milli second

When
Osc freq: Divides the 11-bit up overflow
12 MHz = frequency by counter, occurs, it
12 means roll over to
12 X 10 6 adds the 16-bit up
0 again and
after every counter give signal checkreg counter, adds the
12 clock value up to timereg,
enables or counter value up
cycles, clk is after every overflow
generated 1 clk signal occurs
disable the timer after every 2 ms
when scale by controlling overflow trigger
reg counts timereg register, signal, when
cycle = (all to reset the timereg counts
timer, checkreg (all 1s)2
16
1s) 211
must be enabled

Embedded Systems Design: A Unified 10


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Explanation, ATM

• To configure timereg for 2 minutes, the time reg must


count upto 120,000 msec but the timer range is 2 ms x
216 which is 131070 msec or 2.18 minutes.
• Time reg initialize value for 2 minute time out is
timereg value= 131070 – X;
• Timereg value is 120,000 ms, So X= 131,070-120,000
= 11070msec
• Timer need to initiate with an offset of 11070 msec, so
that when overflow occurs, it count exactly 2 minutes

Embedded Systems Design: A Unified 11


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Watchdog timer application

• An application in mobile phone is that display is off in


case no GUI interaction takes place within a watched
time interval.
• The interval is usually set at 15 s, 20 s, 25 s, 30 s in
mobile phone.
• This saves power.

Embedded Systems Design: A Unified 12


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
More Applications

• An application in temperature controller is that if


controller takes no action to switch off the current
within preset watched time interval, the current is
switched off and warning signal is raised as indication
of controller failure. Failure to switch off current may
burst a boiler in which water is heated.

Embedded Systems Design: A Unified 13


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Provisioning of watchdog timer

• A software task can also be programmed as a


watchdog timer. Microcontroller may also provide for
a watchdog timer.

Embedded Systems Design: A Unified 14


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Serial Transmission Using UARTs
• UART: Universal
embedded
Asynchronous Receiver 1 0
device
1 1
Transmitter 0
0
1 1

– Takes parallel data and


transmits serially
1 0 0 1 1 0 1 1 1 0 0 1 1 0 1 1
– Receives serial data and
converts to parallel Sending UART Receiving UART
start bit end bit
• Parity: extra bit for simple data

error checking
• Start bit, stop bit
• Baud rate
– signal changes per second
– bit rate usually higher 1 0 0 1 1 0 1 1

Embedded Systems Design: A Unified 15


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
UART

• Remember when printers, mice, and modems had thick


cables with those huge clunky connectors? The ones
that literally had to be screwed into your computer?
Those devices were probably using UARTs to
communicate with your computer. While USB has
almost completely replaced those old cables and
connectors, UARTs are definitely not a thing of the
past.

Embedded Systems Design: A Unified 16


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Embedded Systems Design: A Unified 17
Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Where you will find UART?

• You’ll find UARTs being used in many DIY


electronics projects to connect GPS
modules, Bluetooth modules, and RFID card reader
modules to your Raspberry Pi, Arduino, or other
microcontrollers.
• It is a physical circuit in a microcontroller, or a stand-
alone IC. A UART’s main purpose is to transmit and
receive serial data.

Embedded Systems Design: A Unified 18


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
UART Communication

• One of the best things about UART is that it only uses


two wires to transmit data between devices. The
principles behind UART are easy to understand.
• In UART communication, two UARTs communicate
directly with each other. The transmitting UART
converts parallel data from a controlling device like a
CPU into serial form, transmits it in serial to the
receiving UART, which then converts the serial data
back into parallel data for the receiving device.

Embedded Systems Design: A Unified 19


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
UART Communication

• UARTs transmit data asynchronously, which means


there is no clock signal to synchronize the output of
bits from the transmitting UART to the sampling of
bits by the receiving UART. Instead of a clock signal,
the transmitting UART adds start and stop bits to the
data packet being transferred. These bits define the
beginning and end of the data packet so the receiving
UART knows when to start reading the bits.

Embedded Systems Design: A Unified 20


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
UART Communication

• Both UARTs must also must be configured to transmit


and receive the same data packet structure.

Embedded Systems Design: A Unified 21


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
UART Communication and Baud Rate

• When the receiving UART detects a start bit, it starts


to read the incoming bits at a specific frequency
known as the baud rate. Baud rate is a measure of the
speed of data transfer, expressed in bits per second
(bps). Both UARTs must operate at about the same
baud rate. The baud rate between the transmitting and
receiving UARTs can only differ by about 10% before
the timing of bits gets too far off.

Embedded Systems Design: A Unified 22


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
How it Works?

• UART transmitted data is organized into packets.


Each packet contains 1 start bit, 5 to 9 data bits
(depending on the UART), an optional parity bit,
and 1 or 2 stop bits:

Embedded Systems Design: A Unified 23


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Start bit

• The UART data transmission line is normally held at a


high voltage level when it’s not transmitting data. To
start the transfer of data, the transmitting UART pulls
the transmission line from high to low for one clock
cycle. When the receiving UART detects the high to
low voltage transition, it begins reading the bits in the
data frame at the frequency of the baud rate.

Embedded Systems Design: A Unified 24


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
The data frame

• The data frame contains the actual data being


transferred. It can be 5 bits up to 8 bits long if a parity
bit is used. If no parity bit is used, the data frame can
be 9 bits long. In most cases, the data is sent with the
least significant bit first.

Embedded Systems Design: A Unified 25


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Parity bit

• Parity describes the evenness or oddness of a number.


The parity bit is a way for the receiving UART to tell
if any data has changed during transmission. Bits can
be changed by electromagnetic radiation, mismatched
baud rates, or long distance data transfers. After the
receiving UART reads the data frame, it counts the
number of bits with a value of 1 and checks if the
total is an even or odd number. 

Embedded Systems Design: A Unified 26


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Parity bit Continue

• If the parity bit is a 0 (even parity), the 1 bits in the


data frame should total to an even number. If the parity
bit is a 1 (odd parity), the 1 bits in the data frame
should total to an odd number. When the parity bit
matches the data, the UART knows that the
transmission was free of errors. But if the parity bit is
a 0, and the total is odd; or the parity bit is a 1, and the
total is even, the UART knows that bits in the data
frame have changed.

Embedded Systems Design: A Unified 27


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Stop bit

• To signal the end of the data packet, the sending


UART drives the data transmission line from a low
voltage to a high voltage for at least two bit durations.

Embedded Systems Design: A Unified 28


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Embedded Systems Design: A Unified 29
Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Advantages of UART

• Only uses two wires


• No clock signal is necessary
• Has a parity bit to allow for error checking
• The structure of the data packet can be changed as
long as both sides are set up for it
• Well documented and widely used method

Embedded Systems Design: A Unified 30


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Disadvantage of UART

• The size of the data frame is limited to a maximum of


9 bits
• Doesn’t support multiple slave or multiple master
systems
• The baud rates of each UART must be within 10% of
each other

Embedded Systems Design: A Unified 31


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Analog Signal

• An analog signal has a continuously varying value, with


infinite resolution in both time and magnitude. A nine-volt
battery is an example of an analog device, in that its
output voltage is not precisely 9V, changes over time, and
can take any real-numbered value. Similarly, the amount
of current drawn from a battery is not limited to a finite set
of possible values.
• Analog signals are distinguishable from digital signals
because the latter always take values only from a finite set
of predetermined possibilities, such as the set {0V, 5V}.

Embedded Systems Design: A Unified 32


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Digital Signal and Pulse Width Modulation

• By controlling analog circuits digitally, system costs


and power consumption can be drastically reduced.
What's more, many microcontrollers and DSPs already
include on-chip PWM controllers, making
implementation easy.

Embedded Systems Design: A Unified 33


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Pulse Width modulation

• PWM is a way of digitally encoding analog signal


levels. Through the use of high-resolution counters,
the duty cycle of a square wave is modulated to encode
a specific analog signal level. The PWM signal is still
digital because, at any given instant of time, the full
DC supply is either fully on or fully off.
• Given a sufficient bandwidth, any analog value can be
encoded with PWM.

Embedded Systems Design: A Unified 34


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
PWM example

• Figure shows a PWM output at a 10% duty cycle. That


is, the signal is on for 10% of the period and off the
other 90%.

Embedded Systems Design: A Unified 35


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
PWM example

• Figures show PWM outputs at 50% and 90% duty


cycles, respectively.

Embedded Systems Design: A Unified 36


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Interpretation of three PWM outputs

• These three PWM outputs encode three different


analog signal values, at 10%, 50%, and 90% of the full
strength. If, for example, the supply is 9V and the duty
cycle is 10%, a 0.9V analog signal results.

Embedded Systems Design: A Unified 37


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Another Example PWM

• Figure 2 shows a simple circuit that could be driven


using PWM. In the figure, a 9V battery powers an
incandescent lightbulb. If we closed the switch
connecting the battery and lamp for 50ms, the bulb
would receive 9V during that interval. If we then
opened the switch for the next 50ms, the bulb would
receive 0V.

Embedded Systems Design: A Unified 38


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Example

If we repeat this cycle 10 times a second, the


bulb will be lit as though it were connected to
a 4.5V battery (50% of 9V). We say that the
duty cycle is 50% and the modulating
frequency is 10Hz.

Embedded Systems Design: A Unified 39


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Example

• To achieve the desired effect of a dimmer (but always


lit) lamp, it is necessary to increase the modulating
frequency. The same is true in other applications of
PWM. Common modulating frequencies range from
1kHz to 200kHz.

Embedded Systems Design: A Unified 40


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Example

• Most loads, inductive and capacitive alike, require a


much higher modulating frequency than 10Hz.
Imagine that our lamp was switched on for five
seconds, then off for five seconds, then on again. The
duty cycle would still be 50%, but the bulb would
appear brightly lit for the first five seconds and off for
the next. In order for the bulb to see a voltage of 4.5
volts, the cycle period must be short relative to the
load's response time to a change in the switch state.

Embedded Systems Design: A Unified 41


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Pulse width modulator
• Generates pulses with specific
high/low times pwm_o
• Duty cycle: % time high clk
– Square wave: 50% duty cycle
25% duty cycle – average pwm_o is 1.25V
• Common use: control average
voltage to electric device pwm_o
– Simpler than DC-DC clk
converter or digital-analog
converter 50% duty cycle – average pwm_o is 2.5V.

– DC motor speed, dimmer


lights pwm_o
• Another use: encode clk
commands, receiver uses timer
75% duty cycle – average pwm_o is 3.75V.
to decode
Embedded Systems Design: A Unified 42
Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Controlling a DC motor with a PWM

% of Maximum
Input Voltage RPM of DC Motor
Voltage Applied
0 0 0

2.5 50 1840

3.75 75 6900

5.0 100 9200

Relationship between applied voltage and speed of the DC Motor

Embedded Systems Design: A Unified 43


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Internal PWM Structure

clk clk_div 8-bit counter counter < cycle_high,


( 0 – 254)
pwm_o = 1
8- bit
counter >= cycle_high,
controls
pwm_o = 0
how fast 8-bit
the counter comparator pwm_o
increments,
it used to
control 254/2=127 ofr7F->50%
period cycle_high duty cyle -> 18400 rpm
8-bit for
controlling
duty cycle

Internal Structure of PWM

Embedded Systems Design: A Unified 44


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Pseudo Code and connection to the DC
Motor
void main(void){ 5V

/* controls period */
PWMP = 0xff; // clk_div
/* controls duty cycle */
PWM1 = 0x7f; //cycle_high

while(1){}; From
} processor DC
MOTOR

The PWM alone cannot drive the


DC motor, a possible way to
implement a driver is shown
below using an MJE3055T NPN
transistor.

Embedded Systems Design: A Unified 45


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
LCD controller

E void WriteChar(char c){


communications
R/W bus
RS = 1; /* indicate data being sent */
RS
DATA_BUS = c; /* send data to LCD */
DB7–DB0 EnableLCD(45); /* toggle the LCD with appropriate delay */
8 }
microcontroller LCD
controller

CODES RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0 Description
I/D = 1 cursor moves left DL = 1 8-bit
0 0 0 0 0 0 0 0 0 1 Clears all display, return cursor home
I/D = 0 cursor moves right DL = 0 4-bit
S = 1 with display shift N = 1 2 rows 0 0 0 0 0 0 0 0 1 * Returns cursor home
S/C =1 display shift N = 0 1 row
Sets cursor move direction and/or
0 0 0 0 0 0 0 1 I/D S
S/C = 0 cursor movement F = 1 5x10 dots specifies not to shift display
R/L = 1 shift to right F = 0 5x7 dots ON/OFF of all display(D), cursor
0 0 0 0 0 0 1 D C B
ON/OFF (C), and blink position (B)
R/L = 0 shift to left
0 0 0 0 0 1 S/C R/L * * Move cursor and shifts display

Sets interface data length, number of


0 0 0 0 1 DL N F * *
display lines, and character font

1 0 WRITE DATA Writes Data

Embedded Systems Design: A Unified 46


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Interfacing LCD
(Liquid Crystal Display)
• Problem statement
– Interface a 2-line x 20 character
LCD module with the built-in
HD44780 controller to I/O ports of
the PIC18 microcontroller
• Multi-LCDs refer to LCDs with
different interfaces

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Converting to ASCII
• The LCD can represent characters in ASCII
• For example number 0x08  must be converted to 0x38
• To perform this:
– If W=0x08 then ASCII=XORLW 0x30W=38

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Interfacing LCD
• Hardware
– 20 x 2-line LCD displays (two lines
with 20 characters per line)
– LCD has a display Data RAM
(registers) that stores data in 8-bit
character code.
– Each register in Data RAM has its own
address that corresponds to its position
on the line. PICDEMO
• The address range for Line 1 is 00 to 0x38 0x00

13H and Line 2 is 40H to 53H.


0x39 0x013 8

0x014

8 2x20
0x38 0x040

Embedded Systems Design: A Unified 0x053

Hardware/Software Introduction, (c) 2000 Vahid/Givargis


Interfacing LCD

• Driver HD77480
– Three control signals:
• RS – Register Select (RA3)
• R/W – Read/Write (RA2)
• E – Enable (RA1)
– Three power connections
• Power, ground, and the variable register to control the brightness

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Interfacing LCD
• Can be interfaced either in the 8-bit mode or the 4-bit mode
– In the 8-bit mode, all eight data lines are connected for data transfer
– In the 4-bit mode, only four data lines (DB7-DB4 or DB3-DB0) are
connected and two transfers per character (or instruction) are needed
• Driver (HD77480) has two 8-bit internal registers
– Instruction Register (IR) to write instructions to set up LCD
– Data Register (DR) to write data (ASCII characters)

IR REGISTER
DR REGISTER
Embedded Systems Design: A Unified
Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Command and Instruction set for
LCD type HD44780

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Command and Instruction set for
LCD type HD44780

Embedded Systems Design: A Unified 53


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Interfacing LCD
• LCD Operation
– When the MPU writes an instruction to IR or data to DR, the controller:
• Sets the data line DB7 high as a flag indicating that the controller is busy
completing the operation
• Sets the data line DB7 low after the completion of the operation
– The MPU should always check whether DB7 is low before sending an
instruction or a data byte
– After the power up, DB7 cannot be checked for the first two
initialization instructions.

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Interfacing LCD
• Writing to or reading from LCD
• The MPU:
• Asserts RS low to select IR
• Reads from LCD by asserting the R/W signal high
• Asserts the E signal high and then low (toggles) to latch a data byte or an instruction

• Asserts RS high to select DR


• Writes into LCD by asserting the R/W signal low
• Asserts the E signal high and then low (toggles) to latch a data byte or an instruction

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Embedded Systems Design: A Unified 56
Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Embedded Systems Design: A Unified 57
Hardware/Software Introduction, (c) 2000 Vahid/Givargis
HD44780 Bus Timing

Read timing diagram

Embedded Systems Design: A Unified


Write timing diagram
Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Interfacing LCD (Write)

• Software
– To write into the LCD, the program should:
• Send the initial instructions (commands) before it can check DB7
to set up the LCD in the 4-bit or the 8-bit mode.
• Check DB7 and continue to check until it goes low.
• Write instructions to IR to set up the LCD parameters such as the
number of display lines and cursor status.
• Write data to display a message.

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Resetting LCD
• In 4-bit mode the data is sent in nibbles
– First we send the higher nibble and then the lower nibble.
• To enable the 4-bit mode of LCD, we need to follow special sequence of
initialization that tells the LCD controller that user has selected 4-bit mode
of operation:
– Wait for about 20mS
– Send the first init value (0x30)
– Wait for about 10mS
– Send second init value (0x30)
– Wait for about 1mS
– Send third init value (0x30)
– Wait for 1mS
– Select bus width (0x30 - for 8-bit and 0x20 for 4-bit
– Wait for 1mS

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
http://www.8051projects.net/lcd-interfacing/commands.php
Keypad controller

N1
N2
N3 k_pressed
N4

M1
M2
M3
M4 4
key_code key_code

keypad controller

N=4, M=4

Embedded Systems Design: A Unified 61


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Interfacing Key pad with Microcontroller

Embedded Systems Design: A Unified 62


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Normal Types

Embedded Systems Design: A Unified 63


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Underneath circuitry

Embedded Systems Design: A Unified 64


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
working

• Beneath each key is a membrane switch. Each


switch in a row is connected to the other switches in
the row by a conductive trace underneath the pad.
Each switch in a column is connected the same way –
one side of the switch is connected to all of the other
switches in that column by a conductive trace. Each
row and column is brought out to a single pin, for a
total of 8 pins on a 4X4 keypad:

Embedded Systems Design: A Unified 65


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
schematic for a 4X4 keypad

Embedded Systems Design: A Unified 66


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
• Pressing a button closes the switch between a column
and a row trace, allowing current to flow between a
column pin and a row pin.
• The schematic for a 4X4 keypad shows how the rows
and columns are connected:

Embedded Systems Design: A Unified 67


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
First step

• The Arduino detects which button is pressed by


detecting the row and column pin that’s connected to
the button.
• This happens in four steps:
• First, when no buttons are pressed, all of the column
pins are held HIGH, and all of the row pins are held
LOW:

Embedded Systems Design: A Unified 68


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
First step

Embedded Systems Design: A Unified 69


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Second Step

• When a button is pressed, the column pin is pulled


LOW since the current from the HIGH column flows
to the LOW row pin:

Embedded Systems Design: A Unified 70


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Third step

• The Arduino now knows which column the button is


in, so now it just needs to find the row the button is in.
It does this by switching each one of the row pins
HIGH, and at the same time reading all of the column
pins to detect which column pin returns to HIGH:

Embedded Systems Design: A Unified 71


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Fourth step

• When the column pin goes HIGH again, the Arduino


has found the row pin that is connected to the button:
• From the diagram above, you can see that the
combination of row 2 and column 2 could only mean
that the number 5 button was pressed.
• Source: http://www.circuitbasics.com/how-to-set-up-a-
keypad-on-an-arduino/

Embedded Systems Design: A Unified 72


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Embedded Systems Design: A Unified 73
Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Embedded Systems Design: A Unified 74
Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Embedded Systems Design: A Unified 75
Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Stepper motor controller

• Stepper motor: rotates fixed number Sequence


1
A
+
B
+
A’
-
B’
-
of degrees when given a “step” 2 - + + -
3 - - + +
signal 4 + - - +
– In contrast, DC motor just rotates when 5 + + - -

power applied, coasts to stop Vd 1 16 Vm


MC3479P 15
• Rotation achieved by applying
A’ 2 B
A 3 14 B’

specific voltage sequence to coils


4 13
GND GND
5 12
Bias’/Set 6 11 Phase A’

• Controller greatly simplifies this Clk


O|C
7
8
10
9
CW’/CCW
Full’/Half Step

Red A
White A’
Yellow B
Black B’

Embedded Systems Design: A Unified 76


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Stepper motor with controller (driver)
/* main.c */
void main(void){
MC3479P sbit clk=P1^1;
Stepper Motor Driver */turn the motor forward */
sbit cw=P1^0;
cw=0; /* set direction */
8051 clk=0; /* pulse clock */
void delay(void){
CW’/CCW int i, j;
delay();
10 CLK P1.0 clk=1;
for (i=0; i<1000; i++)
7 P1.1
for ( j=0; j<50; j++)
/*turn the motor backwards */
i = i + 0;
2 A’ B 15 cw=1; /* set direction */
3 A
}
B’ 14 clk=0; /* pulse clock */
delay();
clk=1;

The output pins on the stepper motor driver do not


Stepper provide enough current to drive the stepper motor. +V
Motor To amplify the current, a buffer is needed. One 1K
Q1
possible implementation of the buffers is pictured
A B
to the left. Q1 is an MJE3055T NPN transistor
and Q2 is an MJE2955T PNP transistor. A is Q2
connected to the 8051 microcontroller and B is
1K
connected to the stepper motor.

Embedded Systems Design: A Unified 77


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Stepper motor without controller (driver)
8051 /*main.c*/ /* counter clockwise movement */
sbit notA=P2^0; if(dir==0){
P2.4 GND/ +V
sbit isA=P2^1; for(y=0; y<=step; y++){
sbit notB=P2^2; for(z=19; z>=0; z - 4){
P2.3
sbit isB=P2^3; isA=lookup[z];
P2.2
sbit dir=P2^4; isB=lookup[z-1];
P2.1
notA=lookup[z -2];
P2.0
void delay(){ notB=lookup[z-3];
int a, b; delay( );
for(a=0; a<5000; a++) }
Stepper for(b=0; b<10000; b++) }
Motor a=a+0; }
} }
void main( ){
void move(int dir, int steps) { int z;
A possible way to implement the buffers is located int y, z; int lookup[20] = {
below. The 8051 alone cannot drive the stepper motor, so /* clockwise movement */ 1, 1, 0, 0,
several transistors were added to increase the current going if(dir == 1){ 0, 1, 1, 0,
to the stepper motor. Q1 are MJE3055T NPN transistors for(y=0; y<=steps; y++){ 0, 0, 1, 1,
and Q3 is an MJE2955T PNP transistor. A is connected to for(z=0; z<=19; z+4){ 1, 0, 0, 1,
the 8051 microcontroller and B is connected to the stepper isA=lookup[z]; 1, 1, 0, 0 };
motor. isB=lookup[z+1]; while(1){
+V
1K notA=lookup[z+2]; /*move forward, 15 degrees (2 steps) */
Q1 notB=lookup[z+3]; move(1, 2);
+V B
delay(); /* move backwards, 7.5 degrees (1step)*/
1K
} move(0, 1);
A Q2 } }
330 } }

Embedded Systems Design: A Unified 78


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Analog-to-digital converters

Vmax = 7.5V 1111 4 4


7.0V 1110
6.5V 1101 3 3

analog output (V)


analog input (V)
6.0V 1100
5.5V 1011
2 2
5.0V 1010
4.5V 1001
4.0V 1 1
1000
3.5V 0111
3.0V 0110 time time
t1 t2 t3 t4 t1 t2 t3 t4
2.5V 0101
2.0V 0100 0100 1000 0110 0101 0100 1000 0110 0101
1.5V 0011 Digital output Digital input
1.0V 0010
0.5V 0001
0V 0000

proportionality analog to digital digital to analog

Embedded Systems Design: A Unified 79


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Why ADC?

• Almost every environmental measurable parameter is


in analog form like temperature, sound, pressure, light,
etc. Consider a temperature monitoring system
wherein acquiring, analyzing and processing
temperature data from sensors is not possible with
digital computers and processors.
• Therefore, this system needs an intermediate device to
convert the analog temperature data into digital data in
order to communicate with the digital processors
like microcontrollers and microprocessors.
Embedded Systems Design: A Unified 80
Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Real World ----- Digital World

Embedded Systems Design: A Unified 81


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Analog to Digital Conversion Process

• Analog to Digital Converter samples the analog signal


on each falling or rising edge of sample clock. In each
cycle, the ADC gets of the analog signal, measures and
converts it into a digital value. The ADC converts the
output data into a series of digital values by
approximates the signal with fixed precision.

Embedded Systems Design: A Unified 82


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Factors affecting ADC

• In ADCs, two factors determine the accuracy of the


digital value that captures the original analog signal. 
• These are quantization level or bit rate and
sampling rate.
• Below figure depicts how analog to digital conversion
takes place. Bit rate decides the resolution of digitized
output and you can observe in below figure where 3-
bit ADC is used for converting analog signal.

Embedded Systems Design: A Unified 83


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Embedded Systems Design: A Unified 84
Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Factor 1: Quantization level and Bit Rate

• Assume that one volt signal has to be converted from


digital by using 3-bit ADC as shown below. Therefore,
a total of 2^3=8 divisions are available for producing
1V output. This results 1/8=0.125V is called as
minimum change or quantization level represented
for each division as 000 for 0V, 001 for 0.125, and
likewise upto 111 for 1V.

Embedded Systems Design: A Unified 85


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Factor 1: Quantization level or Bit Rate

• If we increase the bit rates like 6, 8, 12, 14, 16, etc. we


will get a better precision of the signal. Thus, bit rate
or quantization gives the smallest output change in the
analog signal value that results from a change in the
digital representation.

Embedded Systems Design: A Unified 86


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Example

• Suppose if the signal is about 0-5V and we have used


8-bit ADC then binary output of 5V is 256. And for
3V it is 133 as shown below.

Embedded Systems Design: A Unified 87


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Factor 2: Sampling Rate

• There is an absolute chance of misrepresenting the


input signal at output side if it is sampled at different
frequency than desired one. Therefore, another
important consideration of the ADC is the sampling
rate. 

Embedded Systems Design: A Unified 88


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Nyquist Theorem

• Niquest theorem states that the acquired  signal


reconstruction introduces distortion unless it is
sampled at (minimum) twice the rate of the largest
frequency content of the signal as you can observe in
the diagram. But this  rate is 5-10 times the maximum
frequency  of the signal in practical.

Embedded Systems Design: A Unified 89


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Factor 2: Sampling example

Sampling at 8X
Signal Frequency

Embedded Systems Design: A Unified 90


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Sampling example

Sampling at 2X
Signal Frequency

Embedded Systems Design: A Unified 91


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Sampling example

Sampling less
than 2X Signal
Frequency

Embedded Systems Design: A Unified 92


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Types of ADC

Some of the  types of analog to digital converters 


include:
•Dual Slope A/D Converter
•Flash A/D Converter
•Successive Approximation A/D Converter

Embedded Systems Design: A Unified 93


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Dual Slope ADC

• In this type of ADC converter comparison voltage is


generated by using an integrator circuit which is
formed by a resistor, capacitor and operational
amplifier combination. By the set value of Vref, this
integrator generates a sawtooth waveform on its output
from zero to the value Vref.

Embedded Systems Design: A Unified 94


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Dual Slope ADC

• When the integrator waveform is started


correspondingly counter starts counting from 0 to 2^n-
1 where n is the number of bits of ADC. When the
input voltage Vin equal to the voltage of the
waveform, then control circuit captures the counter
value which is the digital value of corresponding
analog input value. This Dual slope ADC is relatively
medium cost and slow speed device.

Embedded Systems Design: A Unified 95


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Source: https://www.youtube.com/watch?v=pzXZnvEKMXs

Embedded Systems Design: A Unified 96


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Flash ADC

 Also known as parallel ADC


 Elements
• Encoder – Converts output of
comparators to binary
• Comparators

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Flash ADC

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Flash ADC Example

Vin = 5.5V, Vref= 8V

0
Vin lies in between Vcomp5 & Vcomp6
Vcomp5 = Vref*5/8 = 5V
0
Vcomp6 = Vref*6/8 = 6V 1
1

Comparator 1 - 5 => output 1 1


Comparator 6 - 7 => output 0
1
Encoder Octal Input = sum(0011111) = 5 5.5V 1
Encoder Binary Output = 1 0 1
Embedded Systems Design: A Unified
Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Comparator is one use
of an Op-Amp
VIN
+ VOUT
VREF
If Output
-
VIN > VREF High
VIN < VREF Low

Embedded Systems Design: A Unified 100


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Flash ADC

Advantages Disadvantages
• Very fast • Needs many parts
(255 comparators
for 8-bit ADC)
• Lower resolution
• Expensive
• Large power
consumption
Embedded Systems Design: A Unified 101
Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Successive Approximation ADC

Embedded Systems Design: A Unified 102


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Successive Approximation

• Eg.: 5V analog voltage is to be search on 3 bit ADC.


• Initially compared analog voltage against Mid point of
3 bit ADC digital value which is 100 or 4v analog
value 5V H

4V

Embedded Systems Design: A Unified 103


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
5V 0
5V L

6V
5V

Embedded Systems Design: A Unified 104


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Digital-to-analog conversion using
successive approximation
Given an analog input signal whose voltage should range from 0 to 15 volts, and an 8-bit digital encoding, calculate the correct encoding for
5 volts. Then trace the successive-approximation approach to find the correct encoding.

5/15 = d/(28-1) Encoding: 01010101


d= 85

Successive-approximation method

½(Vmax – Vmin) = 7.5 volts 0 0 0 0 0 0 0 0 ½(5.63 + 4.69) = 5.16 volts 0 1 0 1 0 0 0 0


Vmax = 7.5 volts. Vmax = 5.16 volts.

½(7.5 + 0) = 3.75 volts 0 1 0 0 0 0 0 0 ½(5.16 + 4.69) = 4.93 volts 0 1 0 1 0 1 0 0


Vmin = 3.75 volts. Vmin = 4.93 volts.

½(7.5 + 3.75) = 5.63 volts 0 1 0 0 0 0 0 0 ½(5.16 + 4.93) = 5.05 volts 0 1 0 1 0 1 0 0


Vmax = 5.63 volts Vmax = 5.05 volts.

½(5.63 + 3.75) = 4.69 volts 0 1 0 1 0 0 0 0 ½(5.05 + 4.93) = 4.99 volts 0 1 0 1 0 1 0 1


Vmin = 4.69 volts.

Embedded Systems Design: A Unified 105


Hardware/Software Introduction, (c) 2000 Vahid/Givargis

You might also like