Professional Documents
Culture Documents
By
Ibrahim N. Tansel
Example 4 Pulse with modulation (PWM)
Goal:
Voltage is
Constant
Analog voltage = Pulse height *
pulse duration/ (total duration on
and off)
Time (ms)
Example 4 Pulse with modulation (PWM)
Analog
voltage 4.8V
Time (ms)
Example 4 Pulse with modulation (PWM)
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
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
'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
goto start
end.