• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How to use PIC18F4550 to control a servo motor?

Status
Not open for further replies.

Innovator

New Member
Hi... I am new to microcontroller. Recently, I was given a task to use a PIC18F4550 to control a servo motor. How can this be done? I've damaged quite a few of them in my school lab.

I heard for a servo motor, the PWM on time for its centre position is 1.5ms. I am required to activate a laser pointer (at RC0) when it is at its centre position.

This servo motor will have to move back and forth for 5 times. I.e. it will act like as if it's performing a scanning.

I am not sure if a threshold is required; if it's needed then it'll be 3.5V.

Can somebody please help me out? I need a sample program to try things out. I’ve tried searching online but I couldn’t find any one of them for this particular PIC. I am in a desperate position because I've been trying doing this for a few weeks but I still couldn’t get it done and it's due soon. Really appreciate if you can help.

FYI u may want to use the following I/O ports:
Inputs
Start switch: RB0 (active low)
Pause switch: RB1
Photodiode sensor: RA0
Feedback from LDR to indicate if there is a hit: RA1

Outputs
Servo motor: RC2
Laser Pointer RC0
 

Russ Hensel

New Member
Use one for a different pic, but make the appropriate changes. Also note that the PIC18F4550 belongs to a family ( including the PIC18F2550, got to the microchip site for a complete list ) that will require very few changes. Generally do not use the PWM module which does not have enough resolution for this project.

Do the project in stages. Start with the blinking led and add one feature at a time until you get there.
 

Innovator

New Member
Hi, I have a problem here:

PWM Duty Cycle = (CCPRXL:CCPXCON<5:4>) • TOSC • (TMR2 Prescale Value)

1.5ms = (CCPRXL:CCPXCON<5:4>) • 1/(48MHz) • 16

(CCPRXL:CCPXCON<5:4>) = 4500

However, (CCPRXL:CCPXCON<5:4>) is only 10 bit. Therefore it can't be > 1024.

What should I do? Because 16 is the highest prescaler value selectable in TMR2
 

Innovator

New Member
Hi,

Because I was told by my schoolmate that this will work.

Do you have any suggestions on what are the alternative ways for this to work?
 

blueroomelectronics

Well-Known Member
Hi,

Because I was told by my schoolmate that this will work.

Do you have any suggestions on what are the alternative ways for this to work?
Yes it can work, it's very course as in only a few steps of resolution.

There is lots of PIC servo code around including JPUG #1 (on my site)
 

Mike - K8LH

Well-Known Member
You can use the PWM module and an interrupt driven software helper to link together several shorter PWM "frames" into a 20-msec servo period with extremely fine pulse width resolution but, unfortunately, the method is probably a bit advanced for a beginner (and some more experienced programmers as well).

Probably better to look at using the CCP module "special event trigger" mode for generating the 1.0 to 2.0 msec pulse and remainder of 20 msec servo period. I believe Pommie has a nice example floating around on the Forum somewhere. I will look for it.

Regards, Mike
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top