Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Servo Controlling using PIC16F877A

Status
Not open for further replies.
oh i see.. and by the way im using a hobby servo for this.. i should be able to get this now. how about the speed? how will i control it? thanks!!!

The speed of the servo depends on the model. the are usually quoted as 0.22 sec/60 degree at 4.8v, this is the rating for a futaba S3152. If you wish to control the speed from 1 position to the next, rather than just entering the end position and allowing the servo to operate at its normal speed, you will need to move the arm by slowly increasing the pulse width until it is in the required position.
 

Hi,

Well first things first..

Here is the code that Cuban posted earlier, with a couple of amendments so it will build ok.

So you need to connect your servo and run this code on it to prove it works.

By following those Servo tutorials mentioned earlier and changing the Delay Values you will start to see how you control the motor.

This method is "software" PWM - good and simple but limiting.

Get this done and proven then you can move on to the Hardware PWM method which means you can easily control 2 servos from each pic.

Look at controlling 5 motors when you have learnt how to do the above.

As you saying you do not know how to connect up the pic and motor - do you need a diagram ?

Let us know what hardware have you got, breadboard, soldering on a strip , psu ,programmer etc

Code:
	LIST P=PIC16f877A

	#include "p16f877A.inc"

	__CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _XT_OSC & _LVP_OFF & _CPD_OFF

	; Define variables used
	CBLOCK		0x20
			Delay
			Delay1
			Delay2
	ENDC

	ORG		0x00
init:

	banksel  	 ADCON1
	movlw		 0x06 		; all pins digital I/O			
	movwf		 ADCON1
	
	CLRF		PORTA

	Banksel		TRISA
	CLRF		TRISA		; All pins on porta are output
	banksel		PORTA




loop:	
	BSF			PORTA,1		;
	MOVLW		d'249'		; set delay of approx 1.5ms
	CALL		Delay_1x	; 
	BCF			PORTA,1		; Turn them off, no more pulse
	MOVLW		d'18'		; set delay of approx 18ms
	CALL		Delay_10x	; 
	GOTO 		loop		; Start over





;Setup up the delay routines that may be used
;delay of 1ms for 4mHz clock
;

Delay_1ms
	Movlw	D'166'	
Delay_1x                                 ;call here with W set allows diiferent delays
	Movwf	Delay
delay_loop
	Nop
	Nop
	Nop
	Decfsz	Delay,f
	goto 	delay_loop
	return




;delay of 10ms for 4mHz clock
Delay_10ms
	movlw	d'10'
Delay_10x                               ;call here with W set allows diiferent delays

	movwf	Delay2
Delay_Loop_10ms	
	Call 	Delay_1ms		; Call 1ms delay	
	Decfsz	Delay2,f
	goto 	Delay_Loop_10ms
	return





	end



hi. ive already done this program. but it doesnt move to a 45deg its just about 20degrees then goes back to initial position every 5seconds.. at which part of the program should i alter to change the angle or position of servo? and also to alter the 5 seconds delay?.. i understand why the delay is set to 1.5ms but i dont know why is it 18ms on the other part of the delay.. thank you very much.
 

Hi,

Well the code sends my motor 45 degrees , 1/8 of a full revolution, every 5 seconds.

Are you running it on actual hardware or simulation ?

What is the motor and model number you are using ?

What kind of 5v power supply have you used ?

The 18ms is the fixed interval between the control pulses 1-2 ms.
The servo must reveive the control pulses at regular intervals of approx 20ms.
 

Just to expand on what WP100 says. Try this link it explains the basics of servo operation.

**broken link removed**
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top