Society of Robots - Robot Forum

Software => Software => Topic started by: brianelite8787 on August 27, 2009, 01:05:54 PM

Title: Having Big Problem in Controlling Servo Using PIC18F4221 Using C
Post by: brianelite8787 on August 27, 2009, 01:05:54 PM
I am doing a RC car with adjustable Spoiler. My design is sense the speeds of the  car using IR sensor then input to my pic to control my servo.

My problem is i dont know how to use PWM. I am totally new to  PIC programming. I been look through the data sheet but still cant figure out.
The main is how i send  the pulse(e.g. 1.5ms to have certain angle) to the servo . what i need to set up for the coding. Can someone write a example code to turn the servo which is able to turn in clockwise and ccw in certain angle in C using PIC18F4221 .

Someone help me plz ..i been trying this for whole week.



Code: [Select]
#include <pic18.h>
#define _XTAL_FREQ 20000000

main()
{
TRISC=0;
PR2=0xFF;   [color=yellow]//is this the correct PR2 value? and y we nd to configure this value?[/color]
CCP1M3=1;
CCP1M2=1;
T2CON=0b00000111;
CCPR1L=0;      [color=red] //anything i miss out here or incorrect?[/color]



while(1)
{
-----> what should i include here <--------


}


}




thanks
Title: Re: Having Big Problem in Controlling Servo Using PIC18F4221 Using C
Post by: arixrobotics on August 29, 2009, 09:43:43 AM
hi,

does your code even compile correctly? does MPLAB gives any error messages upon compiling?

I've never used PIC18, but I've used dsPIC with C. My first guess is that you can't set CCPxMy directly like you did in your code. I think you need to set CCPxCON instead, because that is the name of the register. So, instead of;
Code: [Select]
CCP1M3=1;
CCP1M2=1;

I 'think' you should do this instead;
Code: [Select]
CCP1CON = 0b1100;

And for a start, try testing your PWM on an LED, instead of a servo straight away. So with an LED, you should be able to vary the brightness of it with different PWM values. Once this works, then try on a servo.

See if this gives any difference.
Title: Re: Having Big Problem in Controlling Servo Using PIC18F4221 Using C
Post by: mugur on September 12, 2013, 03:21:07 PM
Try this code here http://rodroiddev.blogspot.ro/ (http://rodroiddev.blogspot.ro/)
It was written for pic16 but can be easy ported to pic18