Pic all write 010c pic doc PWM.pdf

(12 KB) Pobierz
PWM.htm
;
;
;
;
;
;
;
;
;
;
;
;
PWM port (in w), pin, duty
Generates a bit stream of 256 1s and 0s with the specified
duty cycle. If this bit stream is run through a simple RC
integrator, the result is an analog voltage output of
(duty/255) * (PIC supply voltage). For example, if duty is
100 and the supply is 5 volts, then the integrated output of
pwm would be (100/255) * 5 = 1.96 volts. In many applications,
pwm output should be buffered with a unity-gain op-amp
circuit. In those cases, the pwm output pin should be set up
as an output when Pwm is called, then immediately switched to
input when Pwm is done. This prevents the steady-state output
of the pin from affecting the voltage set by Pwm.
org
ds
ds
ds
ds
8
1
1
1
1
duty
acc
index
pin
;
;
;
;
Pwm duty cycle.
Pwm accumulator.
Temporary counter for pwm.
Pin number to pulse (0-7).
; Device data and reset vector
device pic16c55,xt_osc,wdt_off,protect_off
reset
start
org
0
; Table to convert pin number (0-7) into bit mask (00000001b to 10000000b).
Pinz
jmp
pc+w
retw
1,2,4,8,16,32,64,128
start
mov
clr
clr
clr
mov
mov
CALL
inc
jmp
!ra, #0
ra
duty
index
pin,#2
w,#0
PWM
duty
:loop
;
;
;
;
;
;
;
;
;
All outputs.
Start with LED off.
Initial brightness = 0.
Clear loop counter for pwm.
Pin 2.
of port ra.
Send pwm to LED.
Turn up brightness.
Endless loop
:loop
;
;
;
;
Upon entry, the desired pin must already be set up as an output.
Variable "pin" contains the pin number (0-7). The w register contains a
number representing the output port (0-2) for RA through RC. The variable
duty contains the desired duty cycle from 0 to 255.
file:///D|/Electronice/Programe/Microcontrolere&PIC/Pic%20all%20write%20010c/pic_doc/PWM.htm (1 of 2)08.08.2003 03:52:39
PWM.htm
PWM
:loop
mov
add
mov
CALL
mov
add
mov
snc
OR
mov
sc
AND
djnz
ret
fsr,w
fsr,#RA
w,pin
Pinz
pin,w
acc,duty
w,pin
indirect,w
w,/pin
indirect,w
index,:loop
; Point to the port number.
; Add offset for port RA.
; Get bit mask from the table.
; Put the mask into pin.
; Let acc = acc + duty
; IF carry THEN pin = 1
; ELSE pin = 0.
; Repeat 256 times.
BACK
file:///D|/Electronice/Programe/Microcontrolere&PIC/Pic%20all%20write%20010c/pic_doc/PWM.htm (2 of 2)08.08.2003 03:52:39
Zgłoś jeśli naruszono regulamin