5.h.3. PIC18F4550

The following example code for the PIC18F4550 was submitted to us by customer Ney Palma Castillo. It is intended to be compiled with the PIC MCU C Compiler. The code shows how to control a Maestro via serial commands using pins C6 and C7. It first commands the channel 0 servo to go to its minimum position and then, one second later, to go to its neutral position. Please see Section 5.e for information on connecting the PIC to the Maestro and set the Maestro’s serial mode to “UART, detect baud rate.”

#include<18f4550.H>
#fuses HSPLL, NOMCLR, PUT, BROWNOUT, BORV43, NOWDT, NOPROTECT, NOLVP
#fuses NODEBUG, USBDIV, PLL5, CPUDIV1, VREGEN, CCP2B3
#use delay(clock=48000000)

#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);
    }
}