H.2. windows c, H.3. pic18f4550, H.2. windows c 5.h.3. pic18f4550 – Pololu Maestro User Manual
Page 48

//const char * device = "/dev/cu.usbmodem00034567"; // Mac OS X
int fd = open(device, O_RDWR | O_NOCTTY);
if (fd == -1)
{
perror(device);
return 1;
}
#ifndef _WIN32
struct termios options;
tcgetattr(fd, &options);
options.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
options.c_oflag &= ~(ONLCR | OCRNL);
tcsetattr(fd, TCSANOW, &options);
#endif
int position = maestroGetPosition(fd, 0);
printf("Current position is %d.\n", position);
int target = (position < 6000) ? 7000 : 5000;
printf("Setting target to %d (%d us).\n", target, target/4);
maestroSetTarget(fd, 0, target);
close(fd);
return 0;
}
5.h.2. Windows C
For example C code that shows how to control the Maestro using its serial interface in Microsoft Windows,
download
(4k zip). This zip archive contains a Microsoft Visual C++ 2010
Express project that shows how to send a Set Target command and also a Get Position command. It can also be
compiled with MinGW. The Maestro’s serial mode needs to be set to “USB Dual Port” for this code to work. This
example is like the previous example except it does the serial communication using Windows-specific functions like
CreateFile and SetCommState. See the comments in the source code for more details.
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
. 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
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.
Pololu Maestro Servo Controller User's Guide
© 2001–2014 Pololu Corporation
5. Serial Interface
Page 48 of 73