LIST p=16f628A
include "p16f628A.inc"
__CONFIG _CP_OFF & _DATA_CP_OFF &_LVP_OFF & _BOREN_OFF &_MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTOSC_OSC_NOCLKOUT
ERRORLEVEL -302
cblock 0x20
Count
Count1 ;variables for Delay routines
Counta
Countb
endc
ORG 0x0000
movlw 0x07 ;like 16f84
movwf CMCON
Initialization
banksel TRISB ;select bank 1
movlw b'00100000'
movwf TRISB ;Make TRISB all outputs except RB5
movwf TRISA ;Make TRISA all outputs, except RA5 which is input only
banksel PORTB ;Select Bank 0
movwf PORTB ;Make PORTB all low with exception of RB5 which will be Hi
movwf PORTA ;Make PORTA all low with exception of RA5 which will be HI (using MCLRE for reset)
Turn180
movlw d'25'
movwf Count
Forward
bsf PORTB,2
call Delay3 ;delay for 2ms "clockwise motion"
bcf PORTB,2
;call Delay1
call Delay1
call Delay1 ;delay for 18ms
decfsz Count
goto Forward
Backward
movlw d'55'
movwf Count
bsf PORTB,2 ;Delay for 1ms (Counter Clockwise)
call Delay2
bcf PORTB,2
decfsz Count
goto Backward
nop
sleep
Delay1 movlw d'36' ;Delay 18ms(4MHz clock)
movwf Count1
d1 movlw d'100'
movwf Counta
movlw d'1'
movwf Countb
Delay_0
decfsz Counta,f
goto $+2
decfsz Countb,f
goto Delay_0
decfsz Count1,f
goto d1
retlw 0x00
Delay2 movlw d'2' ;delay 1ms (4MHz clock) forwards
movwf Count1
d2 movlw d'100'
movwf Counta
movlw d'1';
movwf Countb
Delay_1
decfsz Counta,f
goto $+2
decfsz Countb,f
goto Delay_1
decfsz Count1,f
goto d2
retlw 0x00
Delay3 movlw d'4' ;Delay 2ms (4MHz clock) backwards
movwf Count1
d3 movlw d'100'
movwf Counta
movlw d'1';
movwf Countb
Delay_2
decfsz Counta,f
goto $+2
decfsz Countb,f
goto Delay_2
retlw 0x00
END
Most servo's can rotate to that degree, standard servo signals are generally timed around 90 degrees each direction and the POT used in typical servo's can at best provide 120 degrees.sparky72 said:180 degrees clockwise and back 180 degrees counterclockwise
decfsz Count,F
list p=16F628A, r=dec, w=-302
include <P16F628A.INC>
__config _LVP_OFF & _BOREN_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT
cblock 0x20
Count
Count1 ;variables for Delay routines
Count2
Count3
endc
cblock 0x70
W_TEMP
STATUS_TEMP
PCLATH_TEMP
endc
org 0x0000
goto Start
org 0x004
movwf W_TEMP
swapf STATUS,W
banksel 0
movwf STATUS_TEMP
movfw PCLATH
movwf PCLATH_TEMP
;place holder for interrupt handler (in case needed)
ISRExit movfw PCLATH_TEMP
movwf PCLATH
swapf STATUS_TEMP,W
movwf STATUS
swapf W_TEMP,F
swapf W_TEMP,W
retfie
;place look up tables here (if applicable)
Start
movlw 0x07 ;disable on chip comparator
movwf CMCON
Initial
clrf PORTA ;Make PORTA all low (RA5 set high externally via pull up resistor)
movlw b'00100000'
movwf PORTB ;Make PORTB all low with exception of RB5 which will be Hi
banksel TRISA ;select bank 1
movlw b'00100000'
movwf TRISB ;Make TRISB all outputs except RB5
clrf TRISA ;RA0-RA7 outputs, RA5 external master clear (governed by config word setting)
banksel PORTA ;Select Bank 0
Turn180
movlw 25 ;initialize variable 'Count' to decimal 25
movwf Count
Forward
bsf PORTB,2 ;RB2 high
movlw 2
call Delay ;delay for 2ms "clockwise motion"
bcf PORTB,2
movlw 18
call Delay ;delay for 18ms
decfsz Count,F
goto Forward
movlw d'55'
movwf Count
Backward
bsf PORTB,2 ;Delay for 1ms (Counter Clockwise)
call Delay1mS
bcf PORTB,2
decfsz Count,F
goto Backward
nop
sleep
Delay1mS movlw 167
movwf Count1
movlw 2
movwf Count2
decfsz Count1,F
goto $-1
decfsz Count2,F
goto $-3
return
Delay movwf Count3
call Delay1mS
decfsz Count3,F
goto $-2
return
end
Thanks, Jon Wilder and Gobbledok for the reply to my request. Jon, I reprogrammed the pic with your .asm file and now the servo will only run counter clockwise from one end to the other when I connect power to the pic. The circuit is connected as per the circuit drawing that I attached to my first post. Does the circuit look correct. Pin # 4 has a 10K Ohm resistor to vdd and a momentary switch to ground. I am trying to get this servo to travel as close to 180 degrees clockwise as possible and then counter clockwise as close to 180 degrees as possible with a single push of the momentary switch, and then go to sleep until the momentary switch is pushed again. I really appreciate the help that you are giving me.
;*****************************************************************************************
;*****************************************************************************************
;** **
;** Header Information **
;** **
;*****************************************************************************************
;*****************************************************************************************
list p=16F628A, r=dec, w=-302
include <P16F628A.INC>
__config _LVP_OFF & _BOREN_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT
;*****************************************************************************************
;*****************************************************************************************
;** **
;** Variable Declarations **
;** **
;*****************************************************************************************
;*****************************************************************************************
cblock 0x20
Count
Count1 ;variables for Delay routines
Count2
Count3
endc
cblock 0x70 ;interrupt handler software stack
W_TEMP
STATUS_TEMP
PCLATH_TEMP
endc
;*****************************************************************************************
;*****************************************************************************************
;** **
;** I/O Pin Declarations **
;** **
;*****************************************************************************************
;*****************************************************************************************
#define BUTTON PORTB,0
#define SERVO PORTB,2
;*****************************************************************************************
;*****************************************************************************************
;** **
;** Reset Vector **
;** **
;*****************************************************************************************
;*****************************************************************************************
org 0x000
goto Start
;*****************************************************************************************
;*****************************************************************************************
;** **
;** Interrupt Handler **
;** **
;*****************************************************************************************
;*****************************************************************************************
org 0x004
movwf W_TEMP ;push W
swapf STATUS,W ;push STATUS
banksel 0 ;bank 0
movwf STATUS_TEMP
movfw PCLATH ;push PCLATH
movwf PCLATH_TEMP
btfsc 0x30,0 ;is backward flag set?
goto Backward ;yes, run servo backward
Forward
movlw 25 ;no, initialize variable 'Count' to decimal 25
movwf Count
bsf SERVO ;RB2 high
movlw 2 ;delay 2mS
call Delay
bcf SERVO
movlw 18
call Delay ;delay for 18ms
decfsz Count,F
goto $-7
bsf 0x30,0 ;set backward flag
goto Release
Backward
movlw d'55'
movwf Count
bsf SERVO ;Delay for 1ms (Counter Clockwise)
call Delay1mS
bcf SERVO
decfsz Count,F
goto $-4
bcf 0x30,0 ;clear backward flag
Release movlw 50
btfss BUTTON ;has button been released?
goto $-1 ;no, check again
call Delay ;yes, debounce
btfss BUTTON ;is button still released?
goto $-5 ;no, loop back and wait again
bcf INTCON,INTF ;clear RB0 interrupt flag
ISRExit movfw PCLATH_TEMP ;pop PCLATH
movwf PCLATH
swapf STATUS_TEMP,W ;pop STATUS
movwf STATUS
swapf W_TEMP,F ;pop W
swapf W_TEMP,W
retfie ;return
;*****************************************************************************************
;*****************************************************************************************
;** **
;** Initialization Routine **
;** **
;*****************************************************************************************
;*****************************************************************************************
Start
movlw 0x07 ;disable on chip comparator
movwf CMCON
clrf PORTA ;PORTA default low (RA5 set high externally via pull up resistor)
clrf PORTB ;PORTB default low
banksel TRISA ;select bank 1
clrf TRISA ;RA0-RA7 outputs, RA5 external master clear (governed by config word setting)
movlw b'00000001'
movwf TRISB ;Make TRISB all outputs except RB0
movlw b'00010001' ;RB weak internal pull ups on, interrupt on falling edge of RB0/INT
movwf OPTION_REG ;Timer 0 Clock source = Tcy, 1:4 prescale
banksel PORTA ;Select Bank 0
bcf INTCON,INTF ;clear RB0 external interrupt flag
bsf INTCON,INTE ;enable RB0 interrupt
bsf INTCON,GIE ;enable unmasked interrupts
;*****************************************************************************************
;*****************************************************************************************
;** **
;** Main Program **
;** **
;*****************************************************************************************
;*****************************************************************************************
sleep ;sleep while INT0 = 1
nop
goto $-2 ;place PIC in sleep mode upon return from interrupt
;*****************************************************************************************
;*****************************************************************************************
;** **
;** Delay Routines **
;** **
;*****************************************************************************************
;*****************************************************************************************
Delay1mS clrf TMR0
bcf INTCON,T0IF
btfss INTCON,T0IF
goto $-1
return
Delay movwf Count1
call Delay1mS
decfsz Count1,F
goto $-2
return
end
No, I did not have a resistor connected to RA5. The voltage at RB0 is minimal, around 5mv. However I did connect a 10k ohm resistor to RA5 and now when I push the reset button the servo will move a very short distance clockwise and stop, then when I push the reset button again the servo moves a very short distance counter clockwise and stops.
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?