' 4 Channel radio control encoder, by G.Kuhn ZS1NZ
' Change nx value for more or less channels, compensate with Frame
       	Device 16F877				' Using the 16F877
		Declare XTAL 4				' 4Mhz crystal
        symbol	pulse	PortB.0 	' Pulse train out pin is PortB bit-0
        Declare ADIN_TAD	FRC		' Choose the RC osc for ADC samples
        Declare ADIN_STIME	100    	' Allow 100us for charge time
        Dim adcval as Word			' Variable to store ADC result
		Dim nx as byte				' Variable for loop count
		Dim trim as word			' Part of pulse length calculation
		Dim out as word				' Part of pulse length calculation
		Dim init as word			' Part of pulse length calculation
		trisa = %11111111			' Set port A all inputs
		trisb = %11111110			' Set port B.0 as output
       	ADCON1 = %10000000			' Right justified result 10 bit value
		trim = 1700					' Fine tune pulse edge to edge length here
Again:	for nx = 0 to 3				' Loop to scan first 4 ADC's, can be 8
		set pulse	 				' Make out pin high
		adcval = adin nx			' Load AD value into variable 
		init = (((adcval+4)/2/2)*18)' Scale the result
		'init = (((adcval+4)/4)*18)	' Les, why doesn't this work ?  
		out = init - trim			' Calculate output pulse
		pauseus out	 				' Output pulse
		low pulse					' Make out pin low
		pauseus 400  				' Fixed off period for each stick
		next nx						' Do this four times
		set pulse					' Start of frame sync
Frame:	pauseus 14000				' Total frame length about 20 milliseconds
		low pulse					' End of frame sync
		goto again					' Repeat the scan
