go_away

Author Topic: Having Big Problem in Controlling Servo Using PIC18F4221 Using C  (Read 1496 times)

0 Members and 1 Guest are viewing this topic.

Offline brianelite8787Topic starter

  • Beginner
  • *
  • Posts: 1
  • Helpful? 0
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

Offline arixrobotics

  • Full Member
  • ***
  • Posts: 119
  • Helpful? 3
Re: Having Big Problem in Controlling Servo Using PIC18F4221 Using C
« Reply #1 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.

Offline mugur

  • Beginner
  • *
  • Posts: 3
  • Helpful? 0
Re: Having Big Problem in Controlling Servo Using PIC18F4221 Using C
« Reply #2 on: September 12, 2013, 03:21:07 PM »
Try this code here http://rodroiddev.blogspot.ro/
It was written for pic16 but can be easy ported to pic18

 


Get Your Ad Here

data_list