Home
Articles
Futaba servo signal interface
4 Programming the Device
Previous
1
2
3
4
A
B
C
D
Next
Although complete, this device is a dumb circuit until a program is put into it. To do this we use the PULSIN command to measure the length of the servo pulse. Different execution statements can then be compared to constants stored in the program to activate an event. These constants are as follows.
150 = servo neutral position
200 = servo max position
100 = servo min position
Because the PIC is operating at 4MHz we get a Pulse measurement resolution of 10us. Standard servos use pulse widths of 1ms, 1.5ms and 2ms for their operation. Because the measurement resolution of the PIC is in 10us we end up with a multiplication factor of 100. This is because 100us is equal to 1ms. Therefore 1.5ms is equal to 150us which results in the pulse widths reported by the PULSIN command.
Here is an example of controlling a motor forwards, backwards and off depending on the pulse width detected. Using the first servo input and the first two relay drivers we end up having to do the following.
First the ports have to be identified as inputs or outputs. To do this we use the TRIS command followed by the port letter. The pins can be individually set up as inputs or outputs, and to do this we use a binary constant in order to better see the pins as they are set. 1 = input and 0 = output.
For example to set the entire port D as inputs as we will for this device you would enter;
The "%" symbol tells the PIC that you are displaying a binary value. There are eight digits following it because port D has a resolution of 8-bits (8 pins).
Just as port D was set as inputs the port connected to the relay drivers respectively needs to be set as outputs. This port is port B.
At this point it may also be a good idea to make sure port B is set to logic low before the program starts to avoid complications. To do this simply add portb = 0.
The TRIS commands need to be made at the beginning of the program, before any coding using the ports is executed or the program will not function properly. Its is good practice to place the TRIS commands at the beginning of the program even before you define your global variables.
Next any variables and constants you will be using need to be defined. First we will define the variable for storing the pulse width as W0 (standing for width on pin 0)
Next the constants for the servo will be defined. You may want the program to trigger the event before the control on the radio controller is fully set to its high or low position for more sensitivity. You can do this by making the Min constant slightly higher than 100 and the Max constant slightly higher than 200. We will use a Min of 135 and a Max of 165.
Now that the constants and variables are defined we can start the actual coding of the device. First we will make a label. This label will be used to get the program to jump back to it, via a GOTO command during operation to create loops. I prefer this type of looping because the DO LOOP command can sometimes cause the program to become stuck in loops. We will call the label "Start" as it will be used to reference to the start of the actual program. To create a label, simply add a colon after its name.
Now that the Start label is made we will begin with the PULSIN command. (see appendix A for information on how PULSIN works). Since the pin we are using is Pin0 on Port D and we are measuring the length of the logic high segment of the pulse and storing it to W0, our PULSIN command will look as follows.
We are now ready for the decision making section of the program. A few simple IF statement will do fine. Since we want the PIC to set portb.0 to high and portb.1 to low during one selection, and vice versa in the other and for both pins to drop low when the controller is in neutral position we would do as follows.
The first IF statement sets the pins low if the pulse width is between the min and max servo constants. This tells the device that the servo signal is at its neutral position. It then jumps the program back to start to prevent the next two statements from being executed.
The second statement will set pin0 high and pin1 low if the servos maximum is reached or carried beyond.
The third statement sets pin0 low and pin1 high if the servos minimum is reached or carried beyond.
Finally all that is required is a simple GOTO statement to send the program back to the beginning to keep the program checking and comparing the servo pulse widths.
That's all there is to it. More of these statements can control more motors, on different pins of course. Other commands can be used to create a semi intelligent system, and the portA input pins can be used to monitor limit switches or sensors. Just don't forget to set TRISA = %11111111. Also keep in mind that the port A pins are High at default because of the pull up resistors. Keep this in mind when designing sensors for the port and when coding for the port.
The program that you have just been walked through looks as follows when all together.
NOTE: See Appendix C for the code used in the main drive circuit of the Skills Robot.
NOTE: See Appendix D for information on connecting motors to the interface board.
Previous
1
2
3
4
A
B
C
D
Next
Articles
Futaba servo signal interface
4 Programming the DeviceFutaba servo signal interface
4 Programming the Device
Although complete, this device is a dumb circuit until a program is put into it. To do this we use the PULSIN command to measure the length of the servo pulse. Different execution statements can then be compared to constants stored in the program to activate an event. These constants are as follows.
150 = servo neutral position
200 = servo max position
100 = servo min position
Because the PIC is operating at 4MHz we get a Pulse measurement resolution of 10us. Standard servos use pulse widths of 1ms, 1.5ms and 2ms for their operation. Because the measurement resolution of the PIC is in 10us we end up with a multiplication factor of 100. This is because 100us is equal to 1ms. Therefore 1.5ms is equal to 150us which results in the pulse widths reported by the PULSIN command.
Here is an example of controlling a motor forwards, backwards and off depending on the pulse width detected. Using the first servo input and the first two relay drivers we end up having to do the following.
First the ports have to be identified as inputs or outputs. To do this we use the TRIS command followed by the port letter. The pins can be individually set up as inputs or outputs, and to do this we use a binary constant in order to better see the pins as they are set. 1 = input and 0 = output.
For example to set the entire port D as inputs as we will for this device you would enter;
| TRISD = %11111111 |
The "%" symbol tells the PIC that you are displaying a binary value. There are eight digits following it because port D has a resolution of 8-bits (8 pins).
Just as port D was set as inputs the port connected to the relay drivers respectively needs to be set as outputs. This port is port B.
| TRISB = %00000000 |
At this point it may also be a good idea to make sure port B is set to logic low before the program starts to avoid complications. To do this simply add portb = 0.
| Portb = 0 |
The TRIS commands need to be made at the beginning of the program, before any coding using the ports is executed or the program will not function properly. Its is good practice to place the TRIS commands at the beginning of the program even before you define your global variables.
Next any variables and constants you will be using need to be defined. First we will define the variable for storing the pulse width as W0 (standing for width on pin 0)
| W0 VAR BYTE |
Next the constants for the servo will be defined. You may want the program to trigger the event before the control on the radio controller is fully set to its high or low position for more sensitivity. You can do this by making the Min constant slightly higher than 100 and the Max constant slightly higher than 200. We will use a Min of 135 and a Max of 165.
| SERVOMIN CONST 135
SERVOMAX CONST 165 |
Now that the constants and variables are defined we can start the actual coding of the device. First we will make a label. This label will be used to get the program to jump back to it, via a GOTO command during operation to create loops. I prefer this type of looping because the DO LOOP command can sometimes cause the program to become stuck in loops. We will call the label "Start" as it will be used to reference to the start of the actual program. To create a label, simply add a colon after its name.
| Start: |
Now that the Start label is made we will begin with the PULSIN command. (see appendix A for information on how PULSIN works). Since the pin we are using is Pin0 on Port D and we are measuring the length of the logic high segment of the pulse and storing it to W0, our PULSIN command will look as follows.
| PULSIN portd.0, 1, W0 |
We are now ready for the decision making section of the program. A few simple IF statement will do fine. Since we want the PIC to set portb.0 to high and portb.1 to low during one selection, and vice versa in the other and for both pins to drop low when the controller is in neutral position we would do as follows.
| IF W0 < SERVOMAX AND W0 > SERVOMIN THEN
Portb.0 = 0 Portb.1 = 0 GOTO Start ENDIF IF W0 > SERVOMAX THEN Portb.0 = 1 Portb.1 = 0 ENDIF IF W0 < SERVOMIN THEN Portb.0 = 0 Portb.1 = 1 ENDIF |
The first IF statement sets the pins low if the pulse width is between the min and max servo constants. This tells the device that the servo signal is at its neutral position. It then jumps the program back to start to prevent the next two statements from being executed.
The second statement will set pin0 high and pin1 low if the servos maximum is reached or carried beyond.
The third statement sets pin0 low and pin1 high if the servos minimum is reached or carried beyond.
Finally all that is required is a simple GOTO statement to send the program back to the beginning to keep the program checking and comparing the servo pulse widths.
| GOTO Start |
That's all there is to it. More of these statements can control more motors, on different pins of course. Other commands can be used to create a semi intelligent system, and the portA input pins can be used to monitor limit switches or sensors. Just don't forget to set TRISA = %11111111. Also keep in mind that the port A pins are High at default because of the pull up resistors. Keep this in mind when designing sensors for the port and when coding for the port.
The program that you have just been walked through looks as follows when all together.
| TRISD = %11111111
TRISB = %00000000 Portb = 0 W0 VAR BYTE SERVOMIN CONST 135 SERVOMAX CONST 165 Start: PULSIN portd.0, 1, W0 IF W0 < SERVOMAX AND W0 > SERVOMIN THEN Portb.0 = 0 Portb.1 = 0 GOTO Start ENDIF IF W0 > SERVOMAX THEN Portb.0 = 1 Portb.1 = 0 ENDIF IF W0 < SERVOMIN THEN Portb.0 = 0 Portb.1 = 1 ENDIF GOTO Start |
NOTE: See Appendix C for the code used in the main drive circuit of the Skills Robot.
NOTE: See Appendix D for information on connecting motors to the interface board.


