A Microcontroller Discussion Board
July 30, 2010, 12:04:56 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: If this is your first visit, be sure to check out the Help by clicking the link below.
 
   Home   Help Search Login Register  
Poll
Question: control RC servo motor using PWM
control stepper motor using PWM - 0 (0%)
modifier servor become DC motor - 1 (100%)
Total Voters: 1

Pages: [1]
  Print  
Author Topic: PWM control RC servo motor  (Read 1675 times)
0 Members and 1 Guest are viewing this topic.
king_diaw
Freshy
*
Posts: 11


View Profile
« on: April 07, 2008, 11:23:52 PM »

hello everyone, i'm just now finished programming to control servo motor (CS40S)
My project is running well, I'm can control the movement(0~180 degree) of servo motor by setting the duty cycle.
In this project I'm using PIC18F2550 with 20MHz oscillator.
 
Code:
//include the library
#include "p18f2550.h"
#include "delays.h"

#pragma config WDT=OFF
#pragma config LVP=OFF
#pragma config DEBUG=OFF
#pragma config PWRT=OFF

//define IO that will use in the Programming

#define sw1 PORTAbits.RA0
#define sw2 PORTAbits.RA1
#define sw3 PORTAbits.RA2
#define srm1 PORTAbits.RA3
#define srm2 PORTAbits.RA4
#define srm3 PORTAbits.RA5
#define srm4 PORTCbits.RC0
#define srm5 PORTCbits.RC1
#define srm6 PORTCbits.RC2
#define srm7 PORTCbits.RC4
#define srm8 PORTCbits.RC5
#define srm9 PORTCbits.RC6
#define srm10 PORTCbits.RC7
#define srm11 PORTBbits.RB0
#define srm12 PORTBbits.RB1
#define srm13 PORTBbits.RB2
#define srm14 PORTBbits.RB3
#define srm15 PORTBbits.RB4
#define srm16 PORTBbits.RB5
#define led PORTBbits.RB7

//Prototype function
void init_io(void);
void init_interrupt(void);
void blink(void);
void delay(unsigned long i);
void prog1(void);
void prog2(void);
void prog3(void);
void servo(unsigned char h);

//main function

void main()
{
unsigned char i2c_data;
init_io();
blink();
while(1)
{
if(sw1==0)   //sw1 is active low
servo(0);
else if(sw2==0)  //sw2 is active low
servo(1);
else if(sw3==0)  //sw3 is active low
servo(2);
}
while(1);
}

void init_io()
{
ADCON1=0x0F; //Configure ADC0:12 as Digital IO
TRISA=0b00000111;
TRISB=0X00;
TRISC=0X00;
PORTA=0;
PORTB=0;
PORTC=0;
}

void blink()
{
unsigned char i;
for(i=0;i<5;i++)
{
led=~led;
Delay10KTCYx(35);
}
led=1;
}
void servo (unsigned char h)
{
unsigned char i;
if(h==1) //forward
{
for(i=0;i<100;i++)
{
PORTA=0xff;
Delay100TCYx(30);//113 180degree~225us
Delay10TCYx(28);
PORTA=0;
Delay100TCYx(125);
}
blink();
}
else if(h==2)
{
for(i=0;i<100;i++)
{
PORTA=0xff;
Delay100TCYx(25);//25 90degree~500us
PORTA=0;
Delay100TCYx(125);
}
blink();
}
else if(h==0) //origin
{
for(i=0;i<100;i++)
{
PORTA=0xff;
Delay100TCYx(40);  //40 origin~800us
PORTA=0;
Delay100TCYx(125);
}
blink();
}

}
« Last Edit: January 17, 2009, 09:52:47 PM by king_diaw » Logged
A Microcontroller Discussion Board
« on: April 07, 2008, 11:23:52 PM »

 Logged
Doug
Administrator
Jr. Member
*****
Posts: 92



View Profile WWW
« Reply #1 on: October 24, 2008, 10:38:58 PM »

Actually, I wrote about this on my old blog but it is for 8051. Please see here

http://mcu-programming.blogspot.com/2006/09/servo-motor-control.html
« Last Edit: October 24, 2008, 10:40:34 PM by Doug » Logged

Microcontroller Programming Blog
http://www.mcuprogramming.com/blog
A Microcontroller Discussion Board
   

 Logged
Pages: [1]
  Print  
 
Jump to:  

Related Topics
Subject Started by Replies Views Last post
Servo Motor Control Tutorials mcuprogrammer 0 1453 Last post October 27, 2007, 11:06:25 PM
by mcuprogrammer
Motor Math Robotics richardv2 1 1146 Last post February 13, 2008, 04:26:25 AM
by KSHATRIYA
help req. for driving stepper motor with 8051..... 8051 sarang2502 1 831 Last post October 23, 2009, 09:33:17 AM
by shadowpucci
controlling servo motor (RC) using Atmel 89S51. 8051 minmanjoi 0 719 Last post October 29, 2009, 06:42:15 AM
by minmanjoi
servo Code/Schematics bhaskar0403 0 85 Last post June 29, 2010, 01:40:23 AM
by bhaskar0403
Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC | Sitemap Valid XHTML 1.0! Valid CSS!