View previous topic :: View next topic |
Author |
Message |
Bhanu Watawana
Joined: 01 Jun 2011 Posts: 15
|
Servo Test Driver - SG 90 and MG 90 S micro servo |
Posted: Wed Jun 01, 2011 7:34 pm |
|
|
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
|
|
Posted: Mon Jun 13, 2011 9:32 pm |
|
|
ah ela ela |
|
|
Bhanu Watawana
Joined: 01 Jun 2011 Posts: 15
|
|
Posted: Mon Jun 13, 2011 9:34 pm |
|
|
Thanks bro ;-) _________________ ~~~~~~~~~~~~~~~~
Bhanu Watawana
Uva Wellassa University
Sri Lanka |
|
|
pacman91
Joined: 17 Jun 2011 Posts: 28 Location: Malaysia
|
|
Posted: Thu Jun 30, 2011 11:15 am |
|
|
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
|
Re: Pacman |
Posted: Sat Jul 02, 2011 6:23 am |
|
|
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 |
|
|
|