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.

Multi servo control using PIC16F877A with 16MHz Crystal

Status
Not open for further replies.

madhan2madhan

Junior Member level 3
Joined
Nov 30, 2010
Messages
26
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,482
Hi Everyone,
I'm doing my final year project as Bipedal Walking Robot,I'm using Futaba 3006 Servomotor and PIC 16F877A IC.I have to Control totally 20 Servomotors,I tried to generate the PWM signal from PORTB Its working fine for single servomotors,But for multi servomotors control the code is not running, Its getting hang after some movement of the servomotor.
Anyone please help me to do the control of multi servo at simultaneously,for different angles..
Here i given my simple program to control two servomotors.

<#include pic.h>
<#include htc.h>
__CONFIG(0x3f3a);
#define __XTAL_FREQ 16000000L
void main()
{
TRISB=0x00;
int x=0;
while(1)
{
for(x=0;x<50;x++)
{
RB1=1;
RB4=1;
__delay_us(1400);//neutal
RB1=0;
RB4=0;
__delay_ms(19);
}
for(x=0;x<50;x++)
{
RB1=1;
RB4=1;
__delay_us(1900);//left
RB1=0;
RB4=0;
__delay_ms(19);
}
}
}

This is my sample program for control two servos,Please help me to control more servos with different angles simultaneously please,Its really Urgent.I'm Waiting for Ur Timely help please.
 

Here are some links from another forum where there was almost a competition a few years back to drive the maximum number of servos. I have annotated the links. Although not all are in C code, I thought the discussion and approaches used might help.

(8 servos, post#8, Assembly):
Multitask servos with PIC

33 Servos (Assembly):
Drive 33 Servos With One PIC + USART

18 Servos (C code, Post#2; see other posts in thread for more options and more servos):
18 servo controller

John
 
hi jpanhalt,
Thank you for imtly reply,The links was useful.And also I'm Using the mplab compiler for develope my program,Is my above code have any problem?,Because whenever i start my code from my uc its getting stuck,after i pressed the reset key then only its getting correct flow and also some rotation in the servo its also getting stuck,please help me for this bug. Once again thanks for urs help.
 

I wish I could help you, but i am illiterate in C.

John
 

hi John,
Please help me to complete my Final Year Project.Im doing a Bipedal walking Robot with 20 Servomotor.Please give some solution to clear my above mentioned problems.
 

To genarate pulse for servo, need PWM at 50Hz. PIC16 can't use PWM module.
Use delay function also, not good for many servo.

Other way,
you can generate PWM using Timer Interrupt. See example servo.
or
You can use Servo Controller that use UART to set servo position like this product, servo controller.
 
Hi engshahrul,
The Example Servo program was useful,But there it was discussed with single servomotor only Please give me a solution to control multi servomotors simultaneously with different position angle,I'm Waiting for your valuable reply,Thank You
 

Hi engshahrul,
The Example Servo program was useful,But there it was discussed with single servomotor only Please give me a solution to control multi servomotors simultaneously with different position angle,I'm Waiting for your valuable reply,Thank You
Just edit in the interrupt
Code:
if(TMR0IF==1){
	TMR0IF=0;
	counter++;
	if(counter==position1) SERVO1=0;
	if(counter==position2) SERVO2=0;
	if(counter==position3) SERVO3=0;
	if(counter==position4) SERVO4=0;
	if(counter==390){
		counter=0;
		SERVO1=1;
		SERVO1=2;
		SERVO1=3;
		SERVO1=4;}
}
 

Hi engshahrul,
Thank You For Reply,I will Try this and i let you know very shorty.

---------- Post added at 10:49 ---------- Previous post was at 10:42 ----------

Hi engshahrul,
Thank You For Reply,I will Try this and i let you know very shorty.
 

Hi,
Can You Please explain the folloe=wing Block in the servo example code,

int read_a2d(unsigned char channel)
{
ADCON0=0b00000001; //Turn on A/D module
ADCON1=0b10000000; //Right Justify
if(channel<=7) ANSEL=ANSEL|(0b00000001<<channel);
else ANSELH=ANSELH|(0b00000001<<(channel-8));
__delay_ms(2);
GO=1; //initiate conversion on the selected channel
while(GO==1) continue; //wait until conversion done
return(256*ADRESH+ADRESL);



And Also this line,

position=(int)(0.9775*(max-min)*read_a2d(0)/1000+min);

How the Position is calculated here.
Because in my project Initially I want to check only the simultanious servo running thatsy.
Thank You in Advance.
 

Hi,
Can You Please explain the folloe=wing Block in the servo example code,

int read_a2d(unsigned char channel)
{
ADCON0=0b00000001; //Turn on A/D module
ADCON1=0b10000000; //Right Justify
if(channel<=7) ANSEL=ANSEL|(0b00000001<<channel);
else ANSELH=ANSELH|(0b00000001<<(channel-8));
__delay_ms(2);
GO=1; //initiate conversion on the selected channel
while(GO==1) continue; //wait until conversion done
return(256*ADRESH+ADRESL);
You check the PIC16F887 datasheet and you will know the function address ADCON0, ADCON1, ANSEL, ANSELH

And Also this line,

position=(int)(0.9775*(max-min)*read_a2d(0)/1000+min);

How the Position is calculated here.
Because in my project Initially I want to check only the simultanious servo running thatsy.
Thank You in Advance.
That range to get signal for servo about 0.5ms - 2.5ms
 
Hi evyone,
Here I given a code for control the multiple servomotor i got from some website and made some changes on it.My requirement is have to control the multiplee servomotors simultaniously with different positions.Please help me,Its Very Urgent,This is my final year project.

Here I attached my code,
Code:
#include<pic.h>
#include<htc.h>
__CONFIG(FOSC_HS &			//External Crystal at High Speed
	 WDTE_OFF &			//Disable Watchdog Timer
	 PWRTE_ON  &			//Enable Power Up Timer
	 BOREN_OFF &			//Disable Brown Out Reset
	 /*MCLRE_ON &*/			//MCLR function is enabled
	 LVP_OFF);			//Disable Low Voltage Programming

#define SERVO1		RB1
#define SERVO2		RB2
#define SERVO3		RB3
#define SERVO4		RB4
#define _XTAL_FREQ 	16000000
#define	min			10
#define	max			50

void pic_init(void);
int read_a2d(unsigned char channel);

int counter,position1=28,position3=12,position4=24;
int position2=36,x;

static void interrupt isr(void)
{
if(TMR0IF==1)
{
	TMR0IF=0;
	counter++;
	if(counter==position1) 
		SERVO1=0;
	if(counter==position2) 
		SERVO2=0;
	if(counter==position3) 
		SERVO3=0;
	if(counter==position4) 
		SERVO4=0;
	if(counter==390)
	{
		counter=0;
		SERVO1=1;
		SERVO2=1;
		SERVO3=1;
		SERVO4=1;
	}
}
}
main()
{
pic_init();							//initialize PIC
for(;;)
{
	position1=36;	//36->+90deg,12->-90deg,24->Neutral
}
}
void pic_init(void)
{
TRISB=0b00000000;
TRISD=0b00000000;
OPTION_REG=0b00001000;		//Internal instruction clock			
PORTB=0b00000000;
INTCON=0b10100000;			//Enable Global, TMR0 Interrupt
PIE1=0b00000000;			//Disable TMR1, TMR2 Interrupt
PIE2=0b00000000;
}

Im Using Hi-Tech c v9.83 and MPLAb software,My microcontroller is PIC 16F877A with 16Mhz Clock.

Please help me Soon.
Thanks in Advance.
 
Last edited by a moderator:

What your result? If you are using Crystal 16MHz, the min and max value will be change instead of using 20MHz.
 

Hi,
But in my code im not use that min max values,I just used the constatnt values only,The Min max values are used for previous program only,Sorry I forgot to remove that.In the given code I want to modify for run the multiple servo.This code running and moving the Servos to corresponding position but i dnt knw how to change the pos during run time by using the position variable.

My Another problem is when i Deploy this code into PIC, My program is getting starting trouble i.e the Servos are getting struck.Im using 5V reg IC and 1A adapter to give a power for both PIC and Servo is it is sufficient power source for My project.
Thank You for Your Quick Reply.
 

The power supply not enough. You may use 3A power supply or use battery ex LiPo.
 

hi,
Thank you,Is My power source is the problem for my Code getting stuck.Can I give a Same 3A power Source to my PIC Also Knw.

Now Im Using PIC development Board,For that im Giving a Power By using Adaptor which will provide a 12V 1A power Source.Can I change the Adaptor alone for 12V 3A,to increse the current rate.

For giving the power to servo which one is the better way,

Giving power from which the power for apply both PIC and Servo or Giving power alone to the servo.

Please Give Some Suggestion for my above Clarifications.

Thanks in Advance.
 
Last edited:

Hi Friends,Any one please help me I want to control my 11 servomotors with slow motion using the timer intruupt.Im using PIC 16f877A with 4Mhz clock and hitec c compilr.Its very urget.Thanks in advance.
 

hi engshahrul,
I read this docus already,Actually When I did this kind of code its working fine for Single servomotor.When I include 11 servos at same time time servo is not moving to the exact postion.Instead of that its simply moving to maximum postion or minumum postion of the servo that is (-90 and +90).Actually my application is Arm based Robot.It has a 11 DOF.For that I also need to move my servo with slow motion.Please help to find some programs.
 

You need to consider your power to the servo. Not share servo supply with PIC supply. Use seperate voltage regulator.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top