Lecture13 PWM ADC DAC

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Lecture 13

Pulse Width Modulation,


ADC, DAC

Topics
 Pulse Width Modulation
 Analog to Digital Converter
 Digital to Analog Converter

2
Pulse Width Modulation
• A digital signal can be only 0 volt (logical 0) or 5 volt (logical 1).
• But an analog signal can be any value between 0 and 5 volts.
• Pulse Width Modulation (PWM) is a simulation method for
getting analog output results by using digital output signals.
• PWM can be implemented by hardware (PWM circuit) or by software.
• Digital signals are square waves that are switched between ON and OFF.
• ON/OFF operations can simulate intermediate voltages between
0 volts and 5 volts.
• The PWM program changes (modulates) the portion of the "ON time"
duration and "OFF time" duration in a loop.
• The duration of "ON time" is called the Duty Cycle (Pulse Width).
• To get varying analog output results, the pulse width is changed
repeatedly in a loop.

Examples of PWM usage


• Controlling brightness of an LED.
• Controlling rotation speed of a Direct Current (DC) Motor.
• Controlling temperature of a Heating Resistor.

4
Duty Cycle Duration
• The percentage of the time signal that is HIGH (ON)
as the fraction of Total Time is called the Duty Cycle
Duration (Pulse Width).

Digital Value
Duty Cycle Percentage= x 100
255

• The 8-bit digital value (0-255) directly controls how long


a 5V value (HIGH) is applied to the output period.
• This duration is called as Duty Cycle.

Examples of Duty Cycles


Digital Duty Cycle Duty Cycle Analog Voltage
Value Formula Percentage Equivalent

255 255 / 255 100% 5 Volt * 1.00 =5 Volt

191 191 / 255 75% 5 Volt * 0.75 = 3.75 Volt

127 127 / 255 50% 5 Volt * 0.50 = 2.5 Volt

63 63 / 255 25% 5 Volt * 0.25 = 1.25 Volt

0 0 / 255 0% 5 Volt * 0.00 =0 Volt

6
Example : Duty Cycle Duration
Suppose Duty Cycle is 75%
Total Dura on (One period) = 100 milli seconds
Duty (ON) Dura on = 75 milli seconds (Pulse Width)
OFF Dura on = 25 milli seconds

ON ON
5 volt

0 volt
OFF OFF

Duty (ON) OFF


Dura on Dura on
75% %25

Total Dura on One Period One Period


(One Period)

Examples of PWM Duty Cycles


The following graph shows a comparison of different Duty Cycles
and their corresponding analog voltage levels.

10%

50%

90%

8
Application Example: Controlling Speed of
5V DC Motor with PWM

Suppose a 5V DC Motor (Direct Current motor) is connected to
Port A of a 8255 peripheral interface chip as shown below.

The following port addresses will be used for 8255.
Port C address= 04h, Control Register address= 06h

+5V

Port A
PA0
8255 PA1
PA2
DC Motor

chip
Diode
PA3
PA4 Resistor
PA5
PA6 Transistor
PA7

GND
9

Components
Description
in Circuitry
It is a small DC (Direct Current) motor that rotates continiously.
DC Motor


It is powered by an external +5V voltage source (battery).

The transistor acts like a switch, controlling the power to
the DC motor.
The PA5 pin of Port-A in 8255 chip is used to turn the
Transistor

transistor on and off.



In order to prevent damage to 8255, because of too much
electrical current consumed, the transistor is used as a switch.
The resistor limits the current flowing into the transistor
Resistor

through the base.



The diode that is connected across the DC motor is used
to allow electricity to flow only in one direction.
When the power is turned off to a motor, a negative spike
Diode

of voltage may occur, that can damage the 8255 chip.



The diode protects the negative voltage, by shorting out reverse
current from the DC motor.

10
Application Program

Write an Assembly program that uses PWM method to control
the speed of a DC motor, through the 8255 interface chip.


Define the variables below.


TOTAL DURATION : 2 bytes
 Arbitrary value of Total time (mili seconds) for one period.

Initialize with 30000.


DUTY CYCLE : 1 byte
 The value (between 0 and 100) will be used to control the speed of motor.
 Initialize with 75.


ON DURATION : 2 bytes

OFF DURATION : 2 bytes
 The values will be calculated by percentage formula.

11

Calculation Formulas

TOTAL DURATION∗DUTY CYCLE


ON DURATION =
100

OFF DURATION =TOTAL DURATION −ON DURATION

12
Program

Program uses PWM method to control speed of a DC motor.

The 8255 chip is used as peripheral interface.

;Define the port addresses of 8255.


PortA EQU 00h
KontrolPort EQU 06h

.model small

.stack
Part1 .data

;Define the variables (PWM parameters).

TOTAL_DURATION DW 30000
;Arbitrary value of Total time (mili seconds) for one period

ON_DURATION DW ?
OFF_DURATION DW ?

DUTY_CYCLE DB 75
;Arbitrary value of Time for Duty (ON)
13

Part2

.code
BASLA PROC
.STARTUP

;Perform calculations.
MOV AX, TOTAL_DURATION ; Copy variable to AX
MUL DUTY_CYCLE ; Multiply AX by the Duty Cycle
MOV BL, 100 ; For percentage calculation
DIV BL ; Divide AX by 100
MOV ON_DURATION, AX ; Copy register to variable

MOV AX, TOTAL_DURATION ; Copy variable to register


SUB AX, ON_DURATION ; Subtract variable from register
MOV OFF_DURATION, AX ; Copy register to variable

mov AL, 10000000b ; The control word of 8255


out KontrolPort, AL ; Initialize 8255 chip

14
Part3

MainDongu: ; Endless loop

;Turn on the motor during duty (on) time


MOV AL, 00100000b ; PA5 pin bit is 1
OUT PortA, AL ; Send to port
call WAIT_FOR_ON

;Turn off the motor during idle (off) time


MOV AL, 00000000b ; PA5 pin bit is 0
OUT PortA, AL ; Send to port
call WAIT_FOR_OFF

JMP MainDongu ; Goto endless loop

.EXIT
BASLA ENDP

15

Part4

; SUBROUTINES

WAIT_FOR_ON PROC
MOV CX, ON_DURATION ; CX is counter for local loop
LokalDongu1:
NOP
LOOP LokalDongu1 ; Decrements CX implicitly
RET ; Return from procedure
WAIT_FOR_ON ENDP

;=================================================
WAIT_FOR_OFF PROC
MOV CX, OFF_DURATION ; CX is counter for local loop
LokalDongu2:
NOP
LOOP LokalDongu2 ; Decrements CX implicitly
RET ; Return from procedure
WAIT_FOR_OFF ENDP
;=================================================

END BASLA ;End of file

16
Topics
 Pulse Width Modulation
 Analog to Digital Converter
 Digital to Analog Converter

17

ADC 0808
Analog to Digital Converter


The ADC is an input device to a microprocessor.

CPU sends an initializing signal to the ADC to start the analog signal
to digital conversion process.

The Start of Conversion (SOC) signal is a pulse of a specific duration.

Analog to digital conversion is a slow process and the processor has to
wait for the digital data until the conversion is over.

After the conversion, the ADC sends the End of Conversion (EOC)
signal to inform the processor, and the result is ready at the output
buffer of ADC.

The tasks of issuing SOC pulse to ADC, reading EOC signal from the
ADC and reading the digital output of the ADC are carried out by the
CPU using 8255 peripheral interface chip ports.

18
ADC 0808
Analog to Digital Converter

19

ADC 0808
Analog to Digital Converter

20
Internal Block Diagram of
ADC 0808

21

Interfacing diagram of
8255 and ADC 0808

Vref-

EOC Analog
Input
SOC Voltage

Control signals:
SOC: Start of Conversion
EOC : End of Conversion

Address Lines: A, B, C 22
Interfacing diagram
with 8255 Chip and ADC 0808 chip


Pin connections of ADC 0808 :

The analog input pin I/P2 is used for analog voltage input.

Address pins A, B, C should be 0,1,0 respectively to select
the I/P2 pin.

The OE pin and ALE pin are at +5V to select the ADC chip
and enable the outputs.


Pin connections of 8255 :

PC7 pin acts as the input port to receive the EOC signal.

PC0 pin acts as the output port to send SOC to ADC.

PA0-PA7 pins act as a 8-bit input data port to receive the
digital data output from the ADC.

23

Interfacing diagram
with 8255 Chip and ADC 0808 chip

24
Analog to Digital Converter
• Input of ADC : Analog input voltage between 0 Volt and +5 Volts.
• Output of ADC : 8-bit digital number between 0 and 255.
• Precision (P) of converted values are calculated approximately (5 − 0)
by the following formula. Precision is also called as resolution. P= =0.02
255

ADC Input ADC Output Binary Value


(Analog input (8-bit digital (Send to 8255
voltage) number) interface port)
0.00 0 0000 0000
0.02 1 0000 0001
0.04 2 0000 0010
0.06 3 0000 0011
0.08 4 0000 0100
.... ....
4.98 254 1111 1110
5.00 255 1111 1111

26
Application Program
using 8255 and ADC 0808


Write an 8086 Assembly program that reads 8-bit
digital values from 8255 interface chip.

Suppose 8255 ports will be used for interfacing
with a ADC 0808 chip.

Use the Port A of 8255 chip for transferring digital
data output of ADC to the 8086 CPU.

Use the Port C of 8255 for control signals.

Suppose that an analog input is present at the I/P2
pin of ADC.

27

Defining the Control Word


for 8255 Interface Chip

28
Defining the Control Word
for 8255 Interface Chip

D7 D6 D5 D4 D3 D2 D1 D0

1 0 0 1 1 0 0 0

Control word = 98h

D7=1; I/O Mode


D6=0 and D5=0; Port A Mode
D4=1; Port A is input port
D3=1; Port C (Upper) is input
D2=0; Port B Mode 0
D1=0; Port B is output
D0=0; Port C (Lower) is output

29

Program
Program reads 8-bit input data thru 8255 interface chip,
from an ADC 0808 chip (Analog to Digital Converter).

;Define the port addresses of 8255 chip.


PortA EQU 00h
PortB EQU 02h
PortC EQU 04h
KontrolPort EQU 06h
Part1 .model small

.code
BASLA PROC
.STARTUP

mov AL, 98h ;The control word of 8255 chip


out KontrolPort, AL ;Initialize 8255 chip

mov AL, 02h ;Select the I/P2 pin of ADC chip as analog input
out PortB, AL ;PortB of 8255 is connected to I/P2 pin of ADC chip

mov AL, 00h ;Send SOC pulse to the ADC (Start Of Conversion)
out PortC, AL ;PortC of 8255 is connected to SOC pin of ADC chip
30
BEKLE:
IN AL, PortC ;Check for EOC signal by reading PortC upper of 8255 chip
RCL AL,1 ;Rotate AL trough carry
Part2 JNC BEKLE ;Jump if Not Carry

IN AL, PortA ;If EOC signal exists, then read the 8-bit equivalent data in AL.

.EXIT
BASLA ENDP
END BASLA ;End of file

31

Topics
 Pulse Width Modulation
 Analog to Digital Converter
 Digital to Analog Converter

32
DAC 0830
Digital to Analog Converter


DAC 0830 is an 8-bit converter that transforms an 8 bit binary
number into an analog voltage output.

There are also other converters that convert from 10, 12, or 16 bit
binary numbers into analog voltages.

The number of voltage steps generated by the converter is equal to
the number of binary input combinations:
 An 8 bit converter generates 256 voltage levels (28).
 A 10 bit converter generates 1024 levels (210).

For a 8-bit converter, the analog output step is defined as VREF
(reference), divided by 255.

The step voltage is also called the resolution of the DAC converter.

33

DAC 0830
Digital to Analog Converter

34
Example: Controlling Speed of
12 Volt DC Motor with DAC 0830
Analog outputs labeled IOUT1 and IOUT2 are inputs to an external
Operational Amplifier.

DAC
0830

Opamp +12 V

Digital Analog Analog


GND GND GND

Address
Decoder 35

You might also like