PIC18F4550 C code for a simples movement

Hello, fellows.

As mentioned in page 30 of Pololu Maestro Servo Controller User’s Guide, the following script applies a sequential movement of servo 0 into 5 different poisitions.

# Mover servo 0 a cinco posiciones diferentes, en el bucle.
begin
4000 0 servo # ajusta servo 0 a 1.00 ms
500 delay
5000 0 servo # ajusta a 1.25 ms
500 delay
6000 0 servo # ajusta a 1.50 ms
500 delay
7000 0 servo # ajmusta a 1.75 ms
500 delay
8000 0 servo # ajusta a 2.00 ms
500 delay
repeat

I need your help to translate this code into C source (CCS) for a PIC18F4550 microcontroller.
I also need some help in schematics. I know that the TTL signal pin 25 (PIC18F4550) direct inputs the correct value for RX (TTL serial input) of my micro maestro controller pin, which is not necessary a rs232 (MAX232) circuit.
The C source code in page 25 of the same manual, is a bit confused, if anyone can help me with the hex numbers in PUTC command for specifying a servo, and then moving it, I appreciate.

Thanks in advance.

Hello.

It’s pretty simple to connect your PIC to a Maestro. Please see the “Connecting to a Microcontroller” section of the Maestro User’s Guide. Your PIC is powered at 5V, right? If you’re not sure how to wire something up, feel free to ask a specific question or post a schematic here so we can check it.

For the C code, it’s best to get something simple working first and then gradually improve it. Did you see the serial example code in the “PIC18F4550” section of the Maestro User’s Guide? Could you try running that code, without modifying it, and see if it works for you?

–David

David, thanks for the answer.

What I am a little confused is about the Hex codification sent through putc command. Could you please explain me what the C code for PIC18F4550 in manual really do for one servo?
I need to control two servos, to make a 25x25 matrix movement. The servos are combined (arranged) to move in rows and columns. So, while the first one move one step, 4 degrees (25 movements x 4 degrees = 100 degrees of range), which represents the first column, the second servo moves 25 times to fill the first row (4 degrees too). The servo 1 moves again, for column two, and then servo 2 moves 25 times to fill the second row… And so on.

Could you help me to write a C code for PIC18F4550, for this application?
I think the schematics seems like as mentioned in manual.

I’ve tried to compile the PIC C code example in manual, but simply nothing happens. I checked the communication and all connections, but I can’t find what is going wrong. I really need some kind of intensive help, for this.

Thanks in advance.

Luciano L.

I can’t write your whole program for you, but I can at least get you to the point where you have a working C function for sending target positions to servos. Then its up to you to write the C code that calls it to perform the movements you want.

Yes. The PIC18F4450 code in the Maestro user’s guide sends a serial Set Target command to the Maestro, setting the target of channel 0 to 1000 us, and waits for a second. Then it sets the target of channel 0 to 1500us, and waits for a second. If you look in the Maestro Control Center while the PIC program is running, you should be able to see the slider bar for channel 0 moving back and forth every two seconds. If you have a servo connected to channel 0 and it is properly powered, you should see it moved back and forth every two seconds.

I’m sorry it’s not working for you. Could you please tell me what your Maestro’s serial settings are, and tell me how you are powering the servos? Also, you said your wiring is the same as in the manual, but it would be good to tell me exactly how you have wired everything just so I can double check it.

–David

As mentioned in manual, I connected pin 25/Tx/RC6 of PIC18F4550 into Rx MicroMaestro. Vin is powered in +5V (the same power source of PIC18F) and both PIC18F and microMaestro are grounded properly.
I connected a 10MHz oscillator crystal with two 22pF capacitors. MCLR is also enabled.
I made only this modification on C code:

#use delay(clock=10000000)

When a power the circuit, microMaestro takes 2 seconds to blink continuously red LED, so I guess something is wrong.
And it continues not working.

C Code:

#include<18f4550.H>
#fuses HSPLL, NOMCLR, PUT, BROWNOUT, BORV43, NOWDT, NOPROTECT, NOLVP
#fuses NODEBUG, USBDIV, PLL5, CPUDIV1, VREGEN, CCP2B3
#use delay(clock=10000000)
 
#define TTL_TX1 PIN_C6
#define TTL_RX1 PIN_C7
 
#use rs232(xmit=TTL_TX1, rcv=TTL_RX1, bits=8, parity=N)
 
void main() {     
    delay_ms(2000);
 
    while(true) {
        // Send a Set Target command using the Pololu protocol.
        putc(0xAA);  // Start Byte
        putc(0x0C);  // Device ID = 12
        putc(0x04);  // Command = Set Target
        putc(0x00);  // Channel = 0
        putc(0x20);  // Target position = 1000 us (typical minimum for servos)
        putc(0x1F);
        delay_ms(1000);
 
        // Send another Set Target command using the Pololu protocol.
        putc(0xAA);
        putc(0x0C);
        putc(0x04);
        putc(0x00);
        putc(0x70);  // Target position = 1500 us (typical neutral for servos)
        putc(0x2E);
        delay_ms(1000);
    }
}

If the Maestro blinks the red LED when it starts up, with the yellow LED off, it means there was a brown-out reset. You might have a power problem. Could you try disconnecting your servos and see if that behavior stops? How are you powering the servos? What serial mode is your Maestro configured to be in? Are you able to successfully control servos by plugging the Maestro into USB and using the Maestro Control Center?

–David

MicroMaestro:
As you can see the attached images, I’m using Micro Maestro. The same +5V applied to PIC18F4550 is applied to Maestro.
I took them making 2 jumpers (orange wires) on servo bus. One for GND, and other to Vin (+5V)
The servo is powered with +5V too.

PIC18F4550:
MCLR is enabled with +5V + resistor.
Pin C6 (25) is directly assigned to pin TTL-Rx in Maestro.
Power with +5V.
Crystal oscillator: 10MHz with 22pF capacitors.

Here is the code that I modified.

#include<18f4550.H>
//#fuses HSPLL, NOMCLR, PUT, BROWNOUT, BORV43, NOWDT, NOPROTECT, NOLVP
//#fuses NODEBUG, USBDIV, PLL5, CPUDIV1, VREGEN, CCP2B3

#fuses PUT, BROWNOUT, NOWDT, NOLVP
#use delay(clock=10000000)
 
//#define TTL_TX1 PIN_C6
//#define TTL_RX1 PIN_C7
 
#use rs232(xmit=PIN_C6, rcv=PIN_C7, bits=8, parity=N)
 
void main() {     
    delay_ms(2000);
 
    while(true) {
        // Send a Set Target command using the Pololu protocol.
        putc(0xAA);  // Start Byte
        putc(0x0C);  // Device ID = 12
        putc(0x04);  // Command = Set Target
        putc(0x00);  // Channel = 0
        putc(0x20);  // Target position = 1000 us (typical minimum for servos)
        putc(0x1F);
        delay_ms(1000);
 
        // Send another Set Target command using the Pololu protocol.
        putc(0xAA);
        putc(0x0C);
        putc(0x04);
        putc(0x00);
        putc(0x70);  // Target position = 1500 us (typical neutral for servos)
        putc(0x2E);
        delay_ms(1000);
    }
}






Within Control Center via USB, it works fine.
I can write scripts and for all servos I connect, they work normally.

What do you mean with MicroMaestro serial mode set in?

Please go to the Maestro Control Center’s “Serial Settings” tab and tell me what your settings are. That will include the serial mode.

–David

OK, serial mode is “USB Dual Port”.
This is when I can control the servo via USB. It works fine.

But, how do I configure this mode for an embedded application, such as the C code for PIC18F4550?

Also, I would recommend adding an LED to one of the PIC’s pins and blinking it so you can verify that your code is actually running. You could turn the LED on before the first serial command and off before the other one.

Also, do you have an oscilloscope available?

–David

It should be UART Mode. If you’re confident in the baud rate your PIC is generating, you should use fixed baud rate mode and enter the correct baud rate in the Maestro Control Center. If not, you should have the Maestro auto-detect it.

–David

Here is my customized code, which is working fine for PIC18F4550. I mean, the LEDs are blinking as in code, not serial commands. The servo continues not working!

Both two LEDs (red and yellow) blink for each routine, as seen below:

#include<18f4550.h>

#fuses HS,NOWDT,PUT,NOPROTECT
#use delay(clock=10000000)
 #use rs232(baud=2400,XMIT=PIN_C6,RCV=PIN_C7)
 
void main() {     
    while(true) {
		output_high(PIN_C2);
		delay_ms(1000);

        // Envia o comando 'Set Target' usando o protocolo do fabricante.
        putc(0xAA);  // Start Byte
        putc(0x0C);  // Device ID = 12
        putc(0x04);  // Command = Set Target
        putc(0x00);  // Channel = 0
        putc(0x20);  // Target position = 1000 us (typical minimum for servos)
        putc(0x1F);
        delay_ms(1000);

		output_low(PIN_C2);
		//delay_ms(1000);

		output_high(PIN_C1);
		delay_ms(1000);
 
        // Envia outro comando 'Set Target' usando o protocolo do fabricante.
        putc(0xAA);
        putc(0x0C);
        putc(0x04);
        putc(0x00);
        putc(0x70);  // Target position = 1500 us (typical neutral for servos)
        putc(0x2E);
        delay_ms(1000);

		output_low(PIN_C1);
		delay_ms(1000);		
    }

}

If the two LEDs blink, it seems that the putc commands work too. Serial mode is set to 2400 (baud) for microMaestro and above (#use rs232…).
I used the C1 and C2 pins for LEDs.
I conected a 9V battery to power the servo 0 (first connector), and the same +5V source for PIC18F4550 and Vin (in microMaestro).
Why it continues not working?

PS: When I connect the USB port, microMaestro board works perfectly for more than one servo.

Please, could you send me a tutorial with a simple schematic, which runs the C code in manual, and how I configure the Control Center, for an embeded application?

Thanks.

Hello. A 9V battery is not a good choice for powering servos because it can’t provide much current, and the voltage is probably too high for what your servos can handle.

However, you said you were able to control the servos fine within the Maestro Control Center, so your power is probably not a problem (yet).

Could you please save your Maestro settings file and post it here so I can double check it?

–David

Could you please send me a sample code (as the one in manual) for PIC18F4550, and its schematics and serial settings file (functional and working), so I can do some tests with these ones.

If they are OK, I can do some modifications in C code for two or more servos, and finally understand this Protocol.

Thanks.

I’m not sure what kind of sample code you want me to send you and why it would help. The code in the manual worked for another customer but it’s not working for you. I think there is something wrong with the serial connection between the Maestro and PIC.

There are two main possibilities:

  1. The PIC18F4550 isn’t sending the signals you expect it to be sending. Do you have an oscilloscope handy so you can look at the PIC’s TX line?

  2. The Maestro isn’t responding correctly to the signals. The main reason this would happen is if you configured your Maestro incorrectly. Once again, to help me figure this out, please save your Maestro settings file using the Maestro Control Center and post it here so I can double check all of your settings.

–David

Hi luciano, i am ney palma, i send the code for controlling maestro servo controller in C with pic18f4550, maybe i can help you with your problem?

Greetings