Continue to Site

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.

  • 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.

Problem when interfacing servo motors with PIC18F452 MCU!

Status
Not open for further replies.

A_D_G

New Member
Hi, I need your help on the following problem.

I'm trying to use PIC18F452 microcontroller to drive a servo motor. The circuit I built is quite simple. It consists of a crytal oscillator with frequency of 4MHz with 2 22pf capacitors and 1 more capacitor and 1 resistor just for voltage supply purpose.

I programmed the uC using C18 compiler and MPLAB ICE2, the code I'm using is as follows:

#include <p18f452.h>
#include <pwm.h>
#include <timers.h>

void main(void)
{
TRISC = 0b11111011;
PORTCbits.RC2 = 0;

OpenPWM1 (0xF0);

OpenTimer2 (TIMER_INT_OFF & T2_PS_1_1 & T2_POST_1_1);

SetDCPWM1 (1024);
}

I want to send a pulse train of 20ms of period and 8% of duty cycle to port C (RC2) of the uC, but it seems that I cannot get the priod and duty cycle desired using the formula:

PWM period = ([period] + 1) * 4 * Tosc * prescaler...

Also when I try to see the signal on the oscilloscope, it could not get it. Do i have to include some kind of high resistor between PORTC and the oscilloscope probe?

I hope you could help me to figure this out. How to get the period desired and to display them properly to the oscillator? Is there anything that need to be added into the circuit i built. it's kind of simple but I dun know what to improve.

Thank you very much and hope to hear from you soon. :?

P.S. the circuit i built looks quite like this one:
https://www.imagesco.com/articles/picservo/02.html
 
Did you use any sort of external driver? A PIC pin only puts out 20mA or so and cannot drive a motor. Also a motor's inductance creates spikes with PWM, this might even cause latchup in your device if the motor is on the pin without a flyback diode. You need a bipolar or MOSFET transistor and a protection diode on it.
 
Sir, the servo motor I used has its own voltage supply, please refer to the below figure for the circuit:
**broken link removed**

The only difference is I'm using PIC18F452 instead of PIC16F84 and the output port is set to RC2 instead of RB0 as shown in the diagram.

Also a motor's inductance creates spikes with PWM, this might even cause latchup in your device if the motor is on the pin without a flyback diode. You need a bipolar or MOSFET transistor and a protection diode on it.

Could you tell me more specificly about this? For now I just use a resistor between the signal output and the signal wire of the motor. This work fine when I used a function generator to send the signal to the motor, but I cannot get the circuit do that job. I even cannot get the desired weaveform on the oscillator.

Pls help me figure this out. Thank you.
 
Hello...i wouldn't fix the problem but i can tell you some troubleshooting tips...
1. configure some pre-processor directives before burning your code like, #pragma congif OSC = HS, etc...they all are specific to each PIC18Fxxxx. THEY ARE NECESSARY.
2. Set up ur dmm in ammeter configuration and check how much the current flows when applying the signal from function generator. If it exceeds 25mA, you can use OPTO-COUPLER, Buffer IC or an OP-Amp with unity gain & if you are an electronic engineer think of devil POWER AMPLIFIERS(Class C the best) BETWEEN MCU and servo.
3. You should be known of torque(kg/cm) and current requirements of servo. Consider loading effect.
4. Before mounting a servo, mount an LED in sink configuration and check if it works.
YOU SHOULD ADJUST THE FREQ. & PULSEWIDTH TO OBSERVE ANY CHANGE ON IT.
5. Before burning, test your output on LOGIC ANALYSER.
6. THE PROGRAMMER VARIES FROM FAMILY TO FAMILY. Don't use PIC16xxxx programmer for PIC18xxxx programmer, even if it is 40pin package. The CRYSTAL frequency requirement varies. Soon I 'll upload a 40pin PIC18Fxxxx programmer.
 
Last edited by a moderator:
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top