list p=16f84
radix hex
#include <p16F84A.inc>
__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC
w EQU 0
f EQU 1
Counter EQU H'0C'
portb equ 0x06 ; port b equate
duty equ 0x0c ; length of duty cycle
temp equ 0x0d ; length of duty cycle
c equ 0
start bsf STATUS,RP0
movlw B'11010111'
movwf OPTION_REG
bcf STATUS,RP0
clrf PORTB
bsf STATUS,RP0
movlw B'11111110'
movwf TRISB
bcf STATUS,RP0
goto main
ACTA movlw 0x00 ; load W with 0x00 make port B output
tris portb ; copy W tristate to port B outputs
movlw 0x00 ; fill w with zeroes
movwf portb ; set port b outputs to low
rstrt movlw d'0'
movwf portb
movlw d'10' ; Duty cycle length
movwf duty
b0loop movf duty,w
movwf temp
bsf portb,0
pwma nop
nop
nop
decfsz temp
goto pwma
movlw d'255'
movwf temp
movf duty,w
subwf temp,f
bcf portb,0
pwmb nop
nop
nop
decfsz temp
goto pwmb
goto rstrt
return
WaitA movlw D'30'
movwf Counter
clrf TMR0
againA bcf INTCON,T0IF
loopA btfss INTCON,T0IF
goto loopA
decfsz Counter,f
goto againA
return
ACTB movlw 0x00 ; load W with 0x00 make port B output
tris portb ; copy W tristate to port B outputs
movlw 0x00 ; fill w with zeroes
movwf portb ; set port b outputs to low
rstrtB movlw d'0'
movwf portb
movlw d'200' ; Duty cycle length
movwf duty
b0loopB movf duty,w
movwf temp
bsf portb,0
pwmaB nop
nop
nop
decfsz temp
goto pwma
movlw d'255'
movwf temp
movf duty,w
subwf temp,f
bcf portb,0
pwmbB nop
nop
nop
decfsz temp
goto pwmbB
goto rstrtB
return
WaitP movlw D'30'
movwf Counter
clrf TMR0
againP bcf INTCON,T0IF
loopP btfss INTCON,T0IF
goto loopP
decfsz Counter,f
goto againP
return
main
call ACTA
call WaitA
call WaitP
call ACTB
call WaitA
call WaitP
goto main
END
list p=16f84A
radix hex
#include <p16F84A.inc>
__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC
w EQU 0
f EQU 1
Counter EQU H'0C'
portb equ 0x06 ; port b equate
duty equ 0x0c ; length of duty cycle
temp equ 0x0d ; length of duty cycle
c equ 0
start bsf STATUS,RP0
movlw B'11010111'
movwf OPTION_REG
bcf STATUS,RP0
clrf PORTB
bsf STATUS,RP0
movlw B'11111110'
movwf TRISB
bcf STATUS,RP0
goto main
ACTA nop
banksel TRISB
movlw 0x00 ; load W with 0x00 make port B output
movwf TRISB ; copy W tristate to port B outputs
banksel PORTB
movlw 0x00 ; fill w with zeroes
movwf portb ; set port b outputs to low
rstrt movlw d'0'
movwf portb
movlw d'10' ; Duty cycle length
movwf duty
b0loop movf duty,w
movwf temp
bsf portb,0
pwma nop
nop
nop
decfsz temp
goto pwma
movlw d'255'
movwf temp
movf duty,w
subwf temp,f
bcf portb,0
pwmb nop
nop
nop
decfsz temp
goto pwmb
goto rstrt
return
WaitA movlw D'30'
movwf Counter
clrf TMR0
againA bcf INTCON,T0IF
loopA btfss INTCON,T0IF
goto loopA
decfsz Counter,f
goto againA
return
ACTB nop
banksel TRISB
movlw 0x00 ; load W with 0x00 make port B output
movwf TRISB ; copy W tristate to port B outputs
BANKSEL PORTB
movlw 0x00 ; fill w with zeroes
movwf portb ; set port b outputs to low
rstrtB movlw d'0'
movwf portb
movlw d'200' ; Duty cycle length
movwf duty
b0loopB movf duty,w
movwf temp
bsf portb,0
pwmaB nop
nop
nop
decfsz temp
goto pwma
movlw d'255'
movwf temp
movf duty,w
subwf temp,f
bcf portb,0
pwmbB nop
nop
nop
decfsz temp
goto pwmbB
goto rstrtB
return
WaitP movlw D'30'
movwf Counter
clrf TMR0
againP bcf INTCON,T0IF
loopP btfss INTCON,T0IF
goto loopP
decfsz Counter,f
goto againP
return
main
call ACTA
call WaitA
call WaitP
call ACTB
call WaitA
call WaitP
goto main
END
You will find in all of the tutorials, including the one cited by wp100, that one of the very first projects is to flash an LED. That is PWM. A pin is turned on, delay, turned off, delay, repeat forever. The ratio of "on time" to total cycle time (on + off) is the duty cycle. One second divided by the cycle time is frequency.
For your choice of device, you may need to find a tutorial written for the 16F84A. Nigel Goodwin's (as cited above) is written for the newer 16F628. If all you need is something written for the 16F84A, let us know. If there is something else about PWM that you still are not sure of, please expand on that.
John
main call PWM25 ; 25% pwm for 5 seconds
call STOP ; stop for 1 second
call PWM50 ; 50% pwm for 5 seconds
call STOP
call PWM100
call STOP
goto main ; repeat forever
ORG 0x000 ; processor reset vector
goto main ; go to beginning of program
list p=16f84
radix hex
#include <p16F84A.inc>
__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC
;----------------------------------------------------------------------
portb equ 0x06 ; port b equate
duty equ 0x0c ; length of duty cycle
temp equ 0x0d ; length of duty cycle
lenght set d'157'
;---------------------------------------------------------------------
c equ 0 ; status bit to check after subtraction
;---------------------------------------------------------------------
org 0x000
start movlw 0x00 ; load W with 0x00 make port B output
tris portb ; copy W tristate to port B outputs
movlw 0x00 ; fill w with zeroes
movwf portb ; set port b outputs to low
rstrt movlw d'0'
movwf portb
movlw lenght ; Duty cycle length
movwf duty
b0loop movf duty,w
movwf temp
bsf portb,0
bsf portb,1
pwma nop
nop
nop
decfsz temp
goto pwma
movlw d'255'
movwf temp
movf duty,w
subwf temp,f
bcf portb,0
bcf portb,1
pwmb nop
nop
nop
decfsz temp
goto pwmb
goto rstrt
END
list p=16F84A
#include <p16F84A.inc>
__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC
w EQU 0
f EQU 1
Counter EQU H'0C'
ORG H'0000'
goto start
start bsf STATUS,RP0
movlw B'11010111'
movwf OPTION_REG
bcf STATUS,RP0
clrf PORTB
bsf STATUS,RP0
movlw B'11111000'
movwf TRISB
bcf STATUS,RP0
goto main
WaitA movlw D'150'
movwf Counter
clrf TMR0
againA bcf INTCON,T0IF
loopA btfss INTCON,T0IF
goto loopA
decfsz Counter,f
goto againA
return
WaitP movlw D'5'
movwf Counter
clrf TMR0
againP bcf INTCON,T0IF
loopP btfss INTCON,T0IF
goto loopP
decfsz Counter,f
goto againP
return
main bsf PORTB,0
bsf PORTB,1
bsf PORTB,2
call WaitA
bcf PORTB,0
bcf PORTB,1
bcf PORTB,2
call WaitP
bsf PORTB,0
bsf PORTB,1
call WaitA
bcf PORTB,0
bcf PORTB,1
call WaitP
goto main
END
list p=16f84
radix hex
#include <p16F84A.inc>
__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC
;----------------------------------------------------------------------
portb equ 0x06 ; port b equate
duty equ 0x0c ; length of duty cycle
temp equ 0x0d ; length of duty cycle
lenght set d'10'
cou set d'100'
;---------------------------------------------------------------------
c equ 0 ; status bit to check after subtraction
;---------------------------------------------------------------------
org 0x000
start movlw 0x00 ; load W with 0x00 make port B output
tris portb ; copy W tristate to port B outputs
movlw 0x00 ; fill w with zeroes
movwf portb ; set port b outputs to low
rstrt movlw d'0'
movwf portb
movlw lenght ; Duty cycle length
movwf duty
b0loop movf duty,w
movwf temp
bsf portb,0
bsf portb,1
pwma nop
nop
nop
decfsz temp
goto pwma
movlw d'255'
movwf temp
movf duty,w
subwf temp,f
bcf portb,0
bcf portb,1
pwmb nop
nop
clrf TMR0
againA bcf INTCON,T0IF
loopA btfss INTCON,T0IF
goto loopA
cou set cou-1
if cou==0
lenght set lenght+10
else
nop
decfsz temp
goto pwmb
goto rstrt
END
main call PWM ;subroutine which turning on led and motor at 50%pwm on RB0(led),RB1(motor)
call WaitA ; subroutine which shows how much time PWM subroutine will work
bcf PORTB,0 ;turn off the led
bcf PORTB,1 ;turn off the motor
bcf PORTB,2 ;set the direction
call WaitP ;small pause
bsf PORTB,0 ;turn the led ON in 100% power
bsf PORTB,1 ;turn the motor ON in 100% power
call WaitA
bcf PORTB,0
bcf PORTB,1
call WaitP
goto main
I think I am confused. There are two options :
1-st I am too stuped for this
2-nd I can't explain (in English) what I want to do ...
list p=16F84A
#include <p16F84A.inc>
__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC
w EQU 0
f EQU 1
Counter EQU H'0C'
c equ 0
portb equ 0x06 ; port b equate
duty equ 0x0c ; length of duty cycle
temp equ 0x0d ; length of duty cycle
cou set 0x64
cblock
d1
d2
endc
ORG H'0000'
goto start
start bsf STATUS,RP0
movlw B'11010111'
movwf OPTION_REG
bcf STATUS,RP0
clrf PORTB
bsf STATUS,RP0
movlw B'11111000'
movwf TRISB
bcf STATUS,RP0
goto main
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;deley for the pause between subroutines
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
WaitP movlw D'50'
movwf Counter
clrf TMR0
againP bcf INTCON,T0IF
loopP btfss INTCON,T0IF
goto loopP
decfsz Counter,f
goto againP
return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;delay for the time of Action on pwm100b and pwm100f
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
WaitA movlw D'100'
movwf Counter
clrf TMR0
againA bcf INTCON,T0IF
loopA btfss INTCON,T0IF
goto loopA
decfsz Counter,f
goto againA
return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;pwm 50% forward
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pwm50f movlw d'0'
movwf portb
movlw d'70' ; Duty cycle length
movwf duty
movf duty,w
movwf temp
bsf portb,0
bsf portb,1
pwma50f nop
nop
nop
decfsz temp
goto pwma50f
movlw d'255'
movwf temp
movf duty,w
subwf temp,f
bcf portb,0
bcf portb,1
pwmb50f nop
nop
nop
decfsz temp
goto pwmb50f
bcf INTCON,T0IF
btfss INTCON,T0IF
goto pwm50f
decfsz cou
clrf TMR0
return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;pwm 50% backward
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pwm50b movlw d'0'
movwf portb
movlw d'70' ; Duty cycle length
movwf duty
movf duty,w
movwf temp
bsf portb,0
bsf portb,1
bsf portb,2
pwma50b nop
nop
nop
decfsz temp
goto pwma50b
movlw d'255'
movwf temp
movf duty,w
subwf temp,f
bcf portb,0
bcf portb,1
bcf portb,2
pwmb50b nop
nop
nop
decfsz temp
goto pwmb50b
bcf INTCON,T0IF
btfss INTCON,T0IF
goto pwm50b
decfsz cou
clrf TMR0
return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;pwm 100% forward
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pwm100f bsf portb,0
bsf portb,1
call WaitA
bcf portb,0
bcf portb,1
return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;pwm 100% backward
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pwm100b bsf portb,0
bsf portb,1
bsf portb,2
call WaitA
bcf portb,0
bcf portb,1
bcf portb,2
return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
main call pwm100f
call WaitP
call pwm50f
call WaitP
call pwm100b
call WaitP
call pwm50b
call WaitP
goto main
END
Hi,
Will look at it properly in a few hours.
thx for that dude
I thing I solved the problem .... After "pwm50f" the value of "cou" = 0 , so if I set it to 100 again before "pwm50b" it will works .... I hope so ... but there is other problem ...... something on my board is burned and nothing happends when I turn ON the power suply ..... I will buy new parts monday and will made the schemе again ..... new week - new luck :roll:
Hi,
What has burned out ?
What are you using to interface the Pic to the Motor ?
Can you post a circuit diagram.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?