Wow amazing took me sometime to understand but I am willing to learn
Thanks eric
Here is the codes
Code:[COLOR="Red"]banksel PORTA banksel TRISB[/COLOR] movlw 0 ;set PORTB as all outputs movwf TRISB banksel PORTB movf CCP1CON,W ;set CCP1 as PWM andlw 0xF0 iorlw 0x0C movwf CCP1CON
Again why Banksel PortA do u think its not needed?
movlw .126 ;set highest PWM value
banksel PR2 ;over this (127) is permanently on
movwf PR2
Can someone explain these lines? Why .126 not .128??
The 126 [7Eh], as explained in the program comments, at 127 [ 7Fh] 0111,1111 the motor is running forward, permanently ON.
We set 126? why 127 is still motor running forward?
Because with 126 there is a very short 'space' in the 'mark/space' in the PWM signal.
With 127 [7Fh, 01111111] there is no 'space' at all
Another question what is the different between postscaler and prescaler?
This is described in the datasheet, there is a block diagram showing how the
postscaler and prescaler are interconnected with the Timer.
Can someone show me a flow diagram how to set the period to (1/280hz) & duty cycle to 22%? any idea? using PIC16F818.
Please post the program you have written for this servo motor.
Thanks in advance
; 16F818 PWM example code
;
; Device 16F818
list p=16F818 ; list directive to define processor
#include <p16F818.inc> ; processor specific variable definitions
errorlevel -302, -207
__config H'3F10'
cblock 0x20 ;start of general purpose registers
count ;used in delay routine
count1 ;used in delay routine
counta ;used in delay routine
countb ;used in delay routine
temp ;temp storage
;rate ;adc value
endc
Fwd1 equ 0x00 ;pin for left motor reverse
Rev1 equ 0x01 ;pin for left motor forward
;pins 1 and 2 are the 1 PWM channel
org 0x0000
nop ;for bootloader compatibility
nop
nop
goto START
org 0x0010
START call Initialise
MainLoop:
movlw d'64'
call Speed
call Long_Delay
movlw d'192'
call Speed
call Long_Delay
movlw d'10'
call Speed
call Long_Delay
movlw d'228'
call Speed
call Long_Delay
goto MainLoop
Initialise:
banksel OSCCON
movlw b'01100000' ;uae1
movwf OSCCON
banksel ADCON1 ;turn off A2D
movlw 0x0E; AN0 analog, rest digital, left justified [255]
movwf ADCON1
banksel PORTA
banksel TRISB
movlw 0 ;set PORTB as all outputs
movwf TRISB
banksel PORTB
movf CCP1CON,W ;set CCP1 as PWM
andlw 0xF0
iorlw 0x0C
movwf CCP1CON
movlw .126 ;set highest PWM value
banksel PR2 ;over this (127) is permanently on
movwf PR2
banksel TMR2
movf T2CON,W ;set prescaler to 4;;;16
andlw 0xF8 ;PWM at 2500HZ
iorlw 0x01;;;2
movwf T2CON
movf T2CON,W ;set postscaler to 1
andlw 0x07
iorlw 0x00
movwf T2CON
clrf CCPR1L ;set PWM to zero
bsf T2CON,TMR2ON ;and start the timer running
return
Speed: ;use value in W to set speed (0-127)
movwf temp
btfsc temp,7 ;if more than 128 set speed in reverse
call ReverseMtr ;so '1' is very slow forward
btfss temp,7 ;and '129' is very slow reverse
call ForwardMtr
andlw 0x7F
movwf CCPR1L
return
ReverseMtr:
bsf PORTB,Rev1 ;set pins for reverse
bcf PORTB,Fwd1
return
ForwardMtr:
bcf PORTB,Rev1 ;set pins for forward
bsf PORTB,Fwd1
return
;Delay routines
Long_Delay
return
movlw .50 ;delay 5 seconds
call Delay100W
return
Delay100W movwf count ;delay W x 100mS
d2 call Delay100 ;maximum delay 25.5 seconds
decfsz count ,f
goto d2
return
Delay255 movlw 0xff ;delay 255 mS
goto d0
Delay100 movlw .100 ;delay 100mS
goto d0
Delay50 movlw .50 ;delay 50mS
goto d0
Delay20 movlw .20 ;delay 20mS
goto d0
Delay10 movlw .10 ;delay 10mS
goto d0
Delay1 movlw .1 ;delay 1mS
goto d0
Delay5 movlw 0x05 ;delay 5.000 ms (4 MHz clock)
d0 movwf count1
d1 movlw 0xE7
movwf counta
movlw 0x04
movwf countb
Delay_0 decfsz counta, f
goto $+2
decfsz countb, f
goto Delay_0
decfsz count1 ,f
goto d1
return
;end of Delay routines
end
Here are the codes written for driving a servo motor.
Btw, The servo motor didn't rotate?
I am actually still confuse how to set the period and the off and on time of the square wave.
Here are the codes written for driving a servo motor.
Btw, The servo motor didn't rotate?
I am actually still confuse how to set the period and the off and on time of the square wave.
Thanks in advance
;*******************************************************************
; 16F877 PWM Program
;*******************************************************************
include "p16f877.inc"
errorlevel -302
radix dec
__CONFIG _CP_OFF & _DEBUG_ON & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _HS_OSC & _WRT_OFF & _LVP_OFF & _CPD_OFF
cblock 20h
OnTime:2
OffTime:2
endc
cblock 71h
int_work
int_status
int_pclath
endc
org 0h
nop
goto start
nop
nop
interupt movwf int_work
swapf STATUS,W
movwf int_status
bcf STATUS,RP0
bcf STATUS,RP1
movfw PCLATH
movwf int_pclath
clrf PCLATH
btfsc PORTB,0
goto TurnOff
; turn on the output and write the on time
nop; delay to make both path identical
bsf PORTB,0;<<<<<<<<<< Break Point
movfw OnTime+1
movwf CCPR1H
movfw OnTime
movwf CCPR1L
goto DonePWM
TurnOff bcf PORTB,0;<<<<<<<<<< Break Point
movfw OffTime+1
movwf CCPR1H
movfw OffTime
movwf CCPR1L
DonePWM
bcf PIR1,CCP1IF; reset special event trigger interupt
movfw int_pclath
movwf PCLATH
swapf int_status,W
movwf STATUS
swapf int_work,F; swap to file
swapf int_work,W; swap to work
retfie
start bsf STATUS,RP0
bcf STATUS,RP1
bsf STATUS,IRP; all indirest access is to 100h - 1ffh
movlw (0<<NOT_RBPU|0<<INTEDG|0<<T0CS|0<<T0SE|0<<PSA|B'000'<<PS0)
movwf OPTION_REG
movlw b'11111110'
movwf TRISB
bcf STATUS,RP0
movlw (b'01'<<T1CKPS0|0<<T1OSCEN|0<<NOT_T1SYNC|0<<TMR1CS|1<<TMR1ON)
movwf T1CON; enable timer 1
movlw low(45000)
movwf CCPR1L
movwf OnTime
movlw high(45000)
movwf CCPR1H
movwf OnTime+1
movlw low(5000)
movwf OffTime
movlw high(5000)
movwf OffTime+1
movlw (0<<CCP1X|0<<CCP1Y|b'1011'<<CCP1M0); enable special event trigger on CCP1
movwf CCP1CON;
bsf STATUS,RP0
bsf PIE1,CCP1IE; enable CCP1 interupt
bcf STATUS,RP0
movlw (1<<GIE|1<<PEIE|0<<T0IE|0<<INTE|0<<RBIE|0<<T0IF|0<<INTF|0<<RBIF)
movwf INTCON; enable Peripheral interupts
Loop
goto Loop
END
Error[113] C:\MPLAB\16F877 SERVO MOTOR\SENSOR.ASM 10 : Symbol not previously defined (_WRT_OFF)
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?