Software > Software
servo motor pic source code!!
junior000:
hi i am using a pic 18f4550 microcontroller.
can anyone post source code for controlling a servo motor.
hazzer123:
Which language are you using?
junior000:
i am using c language...c18 compiler
and the microcontroller is 18f4550
airman00:
All the servo code is doing is the following
Pause 150 milliseconds ( to make it center)
make the servo signal high
make it low
Loop it 3 times
paulstreats:
include the standard "Delays.h" into the top of your code
then something like:
--- Code: ---
include <pic.h>
include<Delays.h>
//////////////////////////
//add fuse bit settings here
/////////////////////
define SERVO1 PortDbits.rd0 // connect servo to rdo
TRISD = 0b00000000;
SERVO1 = 0; //make sure servo port start low
void move_servo(unsigned int duty);
void main(){
while(1){
move_servo(163); //163 is value for center at 10Mhz - adjust as necessarry
//at 10mhz 200 is 45deg left and 125 is 45 degrees right
//double these values for 20Mhz
//you could try using something to convert degrees into the timing units
//if you like
}
}
void move servo(unsigned int duty){
SERVO1=1;
Delay10TCx(duty); //delay 10 instruction cycles for each int +1 value
Delay10TCx(duty);
SERVO2=2;
Delay100TCx(200); //delay 20000 instruction cycles
Delay100TCx(200);
}
--- End code ---
I'm currently working with the PIC18 series so if you get stuck with anything I will probably be able to help.
One quick tip: Always keep the LVP fuse bit as off if possible because this causes erratic behaviour including erasing your code if you touch the top of the PIC?
Navigation
[0] Message Index
[#] Next page
Go to full version