CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Servo motor controller

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
maissa



Joined: 17 Feb 2012
Posts: 8

View user's profile Send private message

Servo motor controller
PostPosted: Fri Feb 17, 2012 6:46 pm     Reply with quote

Hi everyone,
I have to control a servomotor with two buttons, one to turn right and the the other to turn left. I'm using 18f2550. I began programming but it doesn't work, would you help me plz. This is my work:
Code:

#if defined(__PCH__)
#include <18f2550.h>
#fuses HS,NOWDT,NOPROTECT
#use delay(clock=20000000)

#define TIME 4
#define AV 40
#define AR 20
#define STOP 30
#define PORT_sevo1 PIN_B0

#use fast_io(a)
#use fast_io(c)

//declaration des bouttons poussoirs
#define RA0 Pin_A0
#define RA1 Pin_A1

#define WDT_18MS 360
#endif

int16 compteur1;
int compteur2,sens_servo1;
int1 signal;

#int_rtcc
void main_int(){
set_timer0(TIME);
if(++compteur1==360){
  compteur1=0;
  compteur2=0;
  signal=1;
  output_high(PORT_sevo1);
}
else{
  if(signal==1){
  if(compteur2++ == sens_servo1){
    signal=0;
    output_low(PORT_sevo1);
  }
}
}

}

void init_timer()
{
set_timer0(TIME);
setup_counters( RTCC_INTERNAL, RTCC_DIV_2 | RTCC_8_BIT);
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);
}


void main() {

sens_servo1 = STOP;

init_timer();

set_tris_a(0x0f);
set_tris_c(0x0f);
setup_adc_ports(NO_ANALOGS);

while (true)
{
if((input(Pin_A0)==0)){
  sens_servo1=AV;
}

if((input(Pin_A1)==0)){
  sens_servo1=AR;
 }
}
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Feb 17, 2012 9:15 pm     Reply with quote

If you are using a hobby servo (for R/C control), you can use the
CCS driver file, servos.c. Here's an example program which uses it:
http://www.ccsinfo.com/forum/viewtopic.php?t=34560&start=22

Your code is looking for a logic low level (0) when you press the switches.
Do you have pull-up resistors on the input pins for switches ? You need
them. You can use 4.7K resistors.
maissa



Joined: 17 Feb 2012
Posts: 8

View user's profile Send private message

PostPosted: Sat Feb 18, 2012 6:32 am     Reply with quote

Thanks for your answer, but i need more help, this is my dns file, will you plz help me how can i control the push buttons, what condition will i use?

Is this false?
Code:

while (true)
{
if((input(Pin_A0)==0)){
  sens_servo1=AV;
}

if((input(Pin_A1)==0)){
  sens_servo1=AR;
 }
}


temtronic



Joined: 01 Jul 2010
Posts: 9242
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Feb 18, 2012 7:02 am     Reply with quote

Looks like another Proteus simulation....

First you have to get the hardware correct....

1) you show two grounds !
a) Earth...switches and servo tied to it.
b) Signal... 100r and servo and _mclr.

2) xtal resistor not required

3) unknown crystal value

4) no decoupling between servo and PIC

5) PIC is unpowered( no +5 , grounds)

6) no filtering on servo power supply

7) no .1 caps at PIC power pins

8) _MCLR tied low, PIC never runs

9) SW AR_SERVO1 appears to be N/C not N/O like other switch

10) Servo signal lines not labeled

there, that's a start...
maissa



Joined: 17 Feb 2012
Posts: 8

View user's profile Send private message

PostPosted: Sat Feb 18, 2012 5:11 pm     Reply with quote

I'm sorry, but its still not very clear. Can you give me a simple example of controlling a servo motor with 2 pushbuttons because i still have to work for controlling 5 others. Thank you Embarassed
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Feb 19, 2012 2:52 pm     Reply with quote

Here is a sample push-button program to control the servo speed.
It has two buttons, for forward and reverse. In the test results below,
I pressed the forward button several times, so the speed goes up from
0 to 4, and then when I keep on pressing it, the speed stays at the top
limit of 4.
Then I pressed the Reverse button several times and it took the speed
down to 0, and I kept pressing it until the speed went down to the lower
limit of -4, and then I pressed it some more but it stays at the lower limit.

This is just the push-button code. You will have to add the servo control
code.

This output was displayed in a Terminal window (TeraTerm) on a PC:
Quote:

0
1
2
3
4
4
4
4
4
4
3
2
1
0
-1
-2
-3
-4
-4
-4
-4


Here's the program. The button.c code is at the following link:
http://www.ccsinfo.com/forum/viewtopic.php?t=23837
Code:

#include <18F4520.h>
#fuses INTRC_IO,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

// These are the "Bvar" variables.
int8 A4 = 0;   // For the push-button on pin A4
int8 B0 = 0;   // For the push-button on pin B0
#include <button.c>

#define MAX_FORWARD_SPEED 4
#define MAX_REVERSE_SPEED -4

//======================================
void main(void)
{
signed int8 speed; 

speed = 0;

while(1)
  {
   // Push the button on pin B0 to increase the forward speed.   
   if(button(PIN_B0, 0, 50, 10, B0, 1))
     {
      if(speed < MAX_FORWARD_SPEED)
         speed++;

      printf("%d\n\r", speed);
     }

   // Push the button on pin A4 to reduce forward speed or
   // to go into reverse, and increase the reverse speed.   
   if(button(PIN_A4, 0, 50, 10, A4, 1))
     {     
      if(speed > MAX_REVERSE_SPEED)
         speed--;

      printf("%d\n\r", speed);
     }


   delay_ms(10); // button() needs a 10ms loop delay
  }

 
}
maissa



Joined: 17 Feb 2012
Posts: 8

View user's profile Send private message

PostPosted: Mon Feb 20, 2012 2:31 pm     Reply with quote

Thank you so much Laughing
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group