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 support@ccsinfo.com

Servo Test Driver - SG 90 and MG 90 S micro servo

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
Bhanu Watawana



Joined: 01 Jun 2011
Posts: 15

View user's profile Send private message Send e-mail

Servo Test Driver - SG 90 and MG 90 S micro servo
PostPosted: Wed Jun 01, 2011 7:34 pm     Reply with quote

As you have already experienced, there is no data-sheet available for above servo motors. Below posted is a simple code that you can use for getting an idea about the pulse width of the servo.


Code:


#include <16F877.h>
#device adc=8
#FUSES NOWDT, HS, NOPUT, PROTECT, NOBROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG
#use delay(clock=20000000)



/************************************************************************************************
/  Used servo - SG 90 and MG 90 S micro servo
/  Pin configuration - RED(+5) BROWN (Ground) Orange/White(Data)
/  Servo SG90 min pulse length - 500 us max pulse length - 2500 us (best performed 800 to 2300)
/  Code by Bhanu Watawana
/  0778111887
/  Uva Wellassa University - Sri Lanka
/  Mechatronics
*/////////////////////////////////////////////////////////////////////////////////////////////////

/*

How servo works
Servo motor data pin value works as a voltage reference to the motor. Inbuilt preset which turns for motor turning works
as a voltage divider and allows the reference voltage(something like PWM) to be equal to the divider voltage.

The pulse should be sent in following manner

          ____                 ____
_________|    |_______________|    |__________
         
         |time|   20 ms low   |time|
Time is the pulse width for the servo ranging 500 us to 2500 us



*/



int16 time=600;                                 // Time variable defining with initial position value - can set as desired

#define LCD_TYPE 2                              // Using LCD
#include <lcd.c>


#define servo pin_C4                            // Servo data pin defined

#int_EXT                                        // External interrupt used for input button with a pull up at B0
void  EXT_isr(void)
{

   disable_interrupts(INT_TIMER1);              // Disabling timer interrupt to stop triggering while external interrupt works
time=time+100;                                  // Always add 100 to the time of the pulse - You can change this as you wish

printf(LCD_PUTC, "\f Time is %Lu",time);        // Printing the time to the LCD - for troubleshooting
delay_ms(1000);                                 // delay for debounce

   enable_interrupts(INT_TIMER1);               // Enabling timer interrupt
}

#int_TIMER1                                     // Timer interrupt
void  TIMER1_isr(void)
{
output_high(servo);                             // Putting the data pin high
delay_us(time);                                 // Delaying the pulse for 'time' period
output_low(servo);                              // Lowering the pin
}



void main()
{
   lcd_init();

   setup_timer_1(T1_INTERNAL|T1_DIV_BY_2);      // Timer set for 26.2ms overflow with 0.4 us resolution   
                                                // This should be 20ms, but works with 10-25 ms range
   enable_interrupts(INT_EXT);
   enable_interrupts(INT_TIMER1);
   enable_interrupts(GLOBAL);
   
   
printf(LCD_PUTC, "\f TEST \nInitial- 600us");                   // For LCD debugging
delay_ms(1000);


while(true){


}

}



LCD is not needed if you can count the pulse, or just you can use Serial communication instead.
_________________
~~~~~~~~~~~~~~~~
Bhanu Watawana
Uva Wellassa University
Sri Lanka


Last edited by Bhanu Watawana on Mon Jun 13, 2011 9:39 pm; edited 1 time in total
aruna1



Joined: 14 Oct 2008
Posts: 103

View user's profile Send private message

PostPosted: Mon Jun 13, 2011 9:32 pm     Reply with quote

ah ela ela
Bhanu Watawana



Joined: 01 Jun 2011
Posts: 15

View user's profile Send private message Send e-mail

PostPosted: Mon Jun 13, 2011 9:34 pm     Reply with quote

aruna1 wrote:
ah ela ela


Thanks bro ;-)
_________________
~~~~~~~~~~~~~~~~
Bhanu Watawana
Uva Wellassa University
Sri Lanka
pacman91



Joined: 17 Jun 2011
Posts: 28
Location: Malaysia

View user's profile Send private message Send e-mail

PostPosted: Thu Jun 30, 2011 11:15 am     Reply with quote

Quote:
setup_timer_1(T1_INTERNAL|T1_DIV_BY_2); // Timer set for 26.2ms overflow with 0.4 us resolution


20Mhz/4/2 = 0.4us

how to set the 26.2ms???
why 0.4us = 26.2ms
how to calculate
Bhanu Watawana



Joined: 01 Jun 2011
Posts: 15

View user's profile Send private message Send e-mail

Re: Pacman
PostPosted: Sat Jul 02, 2011 6:23 am     Reply with quote

Sorry for the delay,

Its so easy if you use the CCS PIC Wizard

http://www.ccsinfo.com/forum/viewtopic.php?t=22467

Read this and it will help you to get an idea about the timers.
_________________
~~~~~~~~~~~~~~~~
Bhanu Watawana
Uva Wellassa University
Sri Lanka
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library 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