'{$STAMP BS2p} '{$PBASIC 2.5} 'Sample program for reading two joystick positions and two buttons 'and converting the results into a 7 channel PPM signal that a 'FUTABA transmitter will accept into it's trainer port. CS1 CON 0 'ADC CS1 pin is connected to I/O pin 0 CLK1 CON 1 'ADC CLK1 pin is connected to I/O pin 1 D01 CON 2 'ADC Data1 pin is connected to I/O pin 2 CS2 CON 3 'ADC CS2 pin is connected to I/O pin 3 CLK2 CON 4 'ADC CLK2 pin is connected to I/O pin 4 D02 CON 5 'ADC Data2 pin is connected to I/O pin 5 Temp VAR Byte 'Temporary result (value between 0 and 255) An1 VAR Word 'Analog joystick control position for Channel 1 An2 VAR Word 'Analog joystick control position for Channel 2 Sw1 VAR Word 'Digital switch control position for Channel 3 Sw2 VAR Word 'Digital switch control position for Channel 3 HIGH CS1 'Make sure the ADCs are not being used LOW CLK1 HIGH CS2 LOW CLK2 main: 'Main routine for measuring and outputting 'measurements 'Read Channel 1 Analog -> An1 LOW CS1 'Start the conversion process SHIFTIN D01,CLK1,2,[Temp\9] 'Read in 9 bits. The first bit is a 'dummy bit. The first clock cycle 'is used to initialize and convert 'the voltage into a digital form in 'the ADC0831. HIGH CS1 'Stop ADC LOW CLK1 'Reset clock line An1 = 750+(Temp*39/8) 'Converts the 1 - 2 ms pulse cycle 'into a 600 to 1600 us pulse width 'and using the appropriate BS2p 'timing conversion factors. 'Read Channel 2 Analog -> An2 LOW CS2 SHIFTIN D02,CLK2,2,[Temp\9] HIGH CS2 LOW CLK2 An2 = 750+(Temp*39/8) 'Read Channel 3, Digital (switch) -> Sw1 IF IN6 THEN 'Check to see if button was pressed Sw1 = 2000 'If Yes then output a 2 ms pulse 'position ELSE Sw1 = 750 'If No then output a 1 ms pulse ENDIF 'position 'Read Channel 4, Digital (switch) -> Sw2 IF IN7 THEN Sw2 = 2000 ELSE Sw2 = 750 ENDIF LOW 15 'End SYNC Delay PULSOUT 14, 337 'Pulse out to an unused pin allows 'for pauses less than 1 ms, and the '337 value equates to 400 us delay 'including calling instruction time. 'ch1 Write Channel 1 PULSOUT 15, An1 PULSOUT 14, 337 'ch2 Write Channel 2 PULSOUT 15, An2 PULSOUT 14, 337 'ch3 Write Channel 3 PULSOUT 15, Sw1 PULSOUT 14, 337 'ch4 Write Channel 4 PULSOUT 15, Sw2 PULSOUT 14, 337 'ch5 Write Channel 5 PULSOUT 15, 1375 PULSOUT 14, 337 'ch6 Write Channel 6 PULSOUT 15, 1375 PULSOUT 14, 337 'ch7 Write Channel 7 PULSOUT 15, 1375 PULSOUT 14, 337 'SYNC Start SYNC Delay HIGH 15 PAUSE 5 GOTO main