• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

servo trouble

Status
Not open for further replies.

thomasi

New Member
Hi,

I'm trying to control a servo to a central position using a PIC 16f84A. I am using the rudimentary code below to go to a central position when switch one is pushed, with switch two not really doing anything at the moment.
I have tested this on MPLAB IDE and the timings seem OK, a 1500 cycle pulse and a 20000 cycle total. Running with a 4MHz oscillator this is supposed to centralise the servo. However the servo just skews hard over each time.
Have I messed up the timings somewhere??


include "P16F84A.inc"



__CONFIG _WDT_OFF & _PWRTE_OFF & _HS_OSC & _CP_OFF

;==========================================================================
; Variable Definition
;==========================================================================
;INPUTS
SW1 EQU H'00' ;SW1 is triggering RA0
SW2 EQU H'01' ;SW2 is triggering RA1
SW3 EQU H'02' ;SW3 is triggering RA2
SW4 EQU H'03' ;SW4 is triggering RA3
TIMER1 EQU H'20' ;Used in delay routine
TIMER2 EQU H'21' ; " " "
PATERN EQU H'22' ;Pattern data for effect's


ORG 0 ;Reset vector address




;***********Set up the constants*********

COUNT1 equ 20h ;First counter for our delay loops
COUNT2 equ 21h ;Second counter for our delay loops

;*********Set up the port****


bsf STATUS,5 ;********Switch to bank 1****
movlw B'00000000'
movwf TRISB ;Set all port b to outputs
movlw B'11111111'
movwf TRISA ;set port A bit to input
bcf STATUS,5 ;switch back to bank 0


;****Start the program*******


Start movlw B'11111111'
movwf PORTB ;*****Set the ouput port B to high

; BTFSC PORTA, SW1
; goto Switch1

; BTFSC PORTA, SW2
; goto Switch2


; goto Start ;go back to Start and turn LED on again



Switch1

movlw B'11111111'
movwf PORTB ;*****Set the ouput port B to high

;do the offset part

movlw D'255'
movwf COUNT1
call DelayByFour ;Wait Four instruciton cycles * COUNT1

; do the position part
movlw D'118'
movwf COUNT1
call DelayByFour ;Wait Four instruciton cycles * COUNT1

;finish the PWM cycle
movlw B'00000000' ;invert lights
movwf PORTB ;'*****Set the output port B to low
movlw D'120' ;remaing time - 10 additional instructions
movwf COUNT1
call DelayByFour ; now up to 2ms

movlw D'18' ;Do the final 18ms loop
movwf COUNT2
movlw D'115'
movwf COUNT1
loop3
call DelayByFour ;Wait Four instruction cycles * COUNT1
decfsz COUNT2,1
goto loop3
; BTFSC PORTA,SW2
; goto Switch2
goto Switch1 ;go back to the start of the loop

Switch2

movlw B'11110111'
movwf PORTB ;*****Set the ouput port B to high
BTFSC PORTA,SW1
goto Switch1

goto Switch2

DelayByFour
NOP ;make instruction count 4
decfsz COUNT1,1 ;Subtract 1 from COUNT1
goto DelayByFour ;Go back to the start of our loop
NOP ;make final loop 4 instruction cycles

return


;****End of program*******

end
 
Last edited:

Pommie

Well-Known Member
Most Helpful Member
You have no

include file,
Config line,
Org,
etc.

Even when these are added, SW1 and SW2 aren't defined.
Can you post a file that will at least assemble.

Edit, sorry, I should be a little more tolerant of new posters. I'll have a look at your code.

Mike.
 
Last edited:

Pommie

Well-Known Member
Most Helpful Member
If you remove the switch testing it works as expected.

Code:
Start		movlw	B'11111111'
		movwf	PORTB		;*****Set the ouput port B to high

[COLOR="red"]	;	btfsc	PORTA, SW1
	;	goto	Switch1

	;	btfsc	PORTA, SW2
	;	goto	Switch2
	;	goto	Start		;go back to Start and turn LED on again
[/COLOR]


Switch1

		movlw	B'11111111'
		movwf	PORTB		;*****Set the ouput port B to high

;do the offset part 

		movlw	D'255'
		movwf	COUNT1
		call	DelayByFour	;Wait Four instruciton cycles * COUNT1

; do the position part 
		movlw	D'118'
		movwf	COUNT1
		call	DelayByFour	;Wait Four instruciton cycles * COUNT1

;finish the PWM cycle 
		movlw	B'00000000'	;invert lights
		movwf	PORTB		;'*****Set the output port B to low
		movlw	D'120'		;remaing time - 10 additional instructions
		movwf	COUNT1
		call	DelayByFour	; now up to 2ms

		movlw	D'18'		;Do the final 18ms loop
		movwf	COUNT2
		movlw	D'115'
		movwf	COUNT1
loop3
		call	DelayByFour	;Wait Four instruction cycles * COUNT1
		decfsz	COUNT2,1
		goto	loop3
[COLOR="Red"]	;	btfsc	PORTA,SW2
	;	goto	Switch2[/COLOR]
		goto	Switch1		;go back to the start of the loop
Mike.
 

thomasi

New Member
Thanks for your efforts however I'm getting the same result with the servo position unfortunately. I've put in all the code except for the include file (this is just a standard microchip file)
 
Last edited:

Mr RB

Well-Known Member
If the servo skews hard to one side it is receiving pulses ok, as if there was no pulse the servo would go limp.

The pulses are either too long or too short, or possibly just fixed high at 5v out.
Check your xtal speed and timer settings, its probably running at *2 or /2 speed etc.
 
Status
Not open for further replies.

EE World Online Articles

Loading
Top