You are on page 1of 11

PicKit projects 2 of 3

By
Ibrahim N. Tansel
Example 4 Pulse with modulation (PWM)

Goal:

Create the analog voltage up to +5V at the selected output pin(s)


Example 4 Pulse with modulation (PWM)
Goal:
Create the analog voltage up to +5V at selected output pin(s)
Desired voltage is created by controlling the width of the pulses
Output (V)
Width of the pulses determines
the analog output voltage
5V

Voltage is
Constant
Analog voltage = Pulse height *
pulse duration/ (total duration on
and off)

Time (ms)
Example 4 Pulse with modulation (PWM)

Desired voltage is created by controlling the width of the pulses


Output (V)
Width of the pulses determines
the analog output voltage
5V

Analog
voltage 4.8V

Time (ms)
Example 4 Pulse with modulation (PWM)

Desired voltage is created by controlling the width of the pulses


Output (V)
Width of the pulses determines
5V the analog output voltage

Analog
voltage 0.5V
Time (ms)
Example 4 PWM
Generate 1V at RC2
Example programs: Generate 2.5V at RC1
START: START:
Generate 5V at RC0 PORTC.1=1 PORTC.2=1
Delay_ms=1 Delay_ms=1
START: PORTC.1=0 PORTC.2=0
PORTC.0=1 Delay_ms=1 Delay_ms=4
goto START goto START goto START

Output = 1 * 5V / 2 =2.5V Output = 1 * 5V / 5 =1V

The voltage on the right will be


smaller because the delay is bigger.
Example 4 PWM
The following program read the voltage of the potentiometer.

Consider the 4 LEDs on the DEMO board

LED3 (RC3) LED2 (RC2) LED1 (RC1) LED0 (RC0)

LED3 will be always ON

The brightness of the LED2 and LED3 will depend on the potentiometer setting. INDEX will have
values from 0 to 10 depending on the potentiometer setting. INDEX determines how many times the
LEDs will be on during the 1ms slices of 10 ms time segments. 2.5V will make the INDEX=5, LEDs will
be 5 times ON, 5 times OFF during 10 ms time segment. So, they will have half brightness.

The brightness of the LED0 will be always half since it depends on the COUNTER and it is set to 5
Example 4 PWM
program PWM

' This program is prepared by I.N Tansel to demonstrate the operation of PWM
' A/D will be read - According to the voltage LED1 and 2 intensity will
' change.
' LED0 and LED3 are REFERENCES. LEDO will stay at half power
' LED3 will have the FULL POWER

dim BRIGHTNESS as word


dim INDEX, COUNTER, I as integer
start:
TRISA=$FF ' Port A is all input
TRISC=$00 ' Port C is all output
ADCON1=$10 ' A2D Clock Fosc/8

ANSEL=$FF ' PortA is completely Analog input


Example 4 PWM
'Perform a/D conversion in the following lines
ADCON0= $01 'configure A2D for Channel 0 (RA0), Left justified
delay_ms(5) 'wait for the A/D to settle
ADCON0.1=1 'Start A/D conversion
' PORTC=$F0 ' to test if the program works
wait:
'
if ADCON0.1=1 then 'check if A/D conversion is over(ADCON0.1 will be 1
'when A/D conversion is completed
' PORTC=$F0 ' to test if the program works
goto wait 'if A/D conversion is not over, go back and wait
end if

'Read the value of the A/D conversion from two register the results went
' BRIGHTNESS = ADRESH 'take the lower byt to BRIGHTNESS
' BRIGHTNESS= (BRIGHTNESS<<8)+ADRESL 'move the content of this variable
'8 bits and add the lower byte
BRIGHTNESS = ADRESH 'take the lower byt to BRIGHTNESS
Example 4 PWM
INDEX= BRIGHTNESS/25 'INDEX will control the brightness
'our A/D converter is 4 bits
COUNTER = 5 ' This is to keep the LED0 half bright

' Do not use PORTC.1= because it will make LED1 ON all others OFF

for I =1 to 10

if INDEX >1 then ' Our adjustment lights 1 and 2 on


if COUNTER >1 then ' This is for ref 3 and 0
PORTC = %00001111 ' All the lights will be ON
else ' Half the time LED0 will be OFF
PORTC = %00001110 ' All ON EXCEPT LED0
end if
else ' LED0 will be off
Example 4 PWM
else ' LED0 will be off
if COUNTER >1 then
PORTC = %00001001 ' The adjusted lights will be off
else ' Half the time LED0 will be OFF
PORTC = %00001000 ' All the lights will be OFF exc 3
end if
end if

INDEX= INDEX-1 ' Reduce the INDEX by one


COUNTER=COUNTER - 1 ' Reduce the COUNTER by one
delay_ms(1)
next I

goto start

end.

You might also like