; ; utility routine to listen to an RC servo channel ; - asumptions - P3.2 (INT0*) is disconnected from it's pyro channel ; and wired to the white wire on the servo, the red wire (power) ; is not connected and the black wire is connected to ground. ; ; calling conventions: ; always clear carry on return ; result is returned in r1:r0 width of pulse in (11/12)uS ; or 0 for no signal ; ; ; the servo signal is a 1-2mS (depending on the stick positioon) ; pulse every 20mS or so - we measure the pulse by waiting for it ; to start then turning on the timer, and using the signal to gate ; the result. ; ; low values return ~0x3c0 high ones ~0x630 ; ; uses timer #0 as a side effect, leaves it idle ; ; Current time spent in here is any where from 1mS to 10mS - we time out ; at 20mS ; ; for the record with a 11MHz rock there are 916.66666 timer clocks/mS ; get_rcin: setb p3.2 ; make it an input clr tr0 ; turn off timer #0 clr ie0 clr it0 mov a, tmod ; save tmod anl a, #0xf0 mov r1, a jnb p3.2, rc1 ; is it curently active mov tl0, #(0xffff-(3*917))&0xff ; wait 3mS mov th0, #(0xffff-(3*917))>>8 mov a, r1 ; get tmod orl a, #0x01 ; mode 1 16-bit counter mov tmod, a clr tf0 setb tr0 rc2: jnb p3.2, rc3 ; loop until it goes away or it times out jnb tf0, rc2 ; rc6: clr tr0 mov r0, #0 ; return timeout mov r1, #0 clr c ret rc3: clr tr0 ; it is now 0 rc1: ; wait for it to go hi mov tl0, #(0xffff-(20*917))&0xff ; wait 20mS mov th0, #(0xffff-(20*917))>>8 mov a, r1 ; get tmod orl a, #0x01 ; mode 1 16-bit counter mov tmod, a clr tf0 setb tr0 rc4: jb p3.2, rc5 ; loop until it goes away or it times out jnb tf0, rc4 ; ajmp rc6 rc5: clr tr0 ; it is now 0 mov tl0, #0 ; set the counter to 0 mov th0, #0 mov a, r1 ; get tmod orl a, #0x09 ; mode 1 16-bit counter with gate mov tmod, a clr tf0 setb tr0 rc7: jnb p3.2, rc8 jnb tf0, rc7 ; wait for the end of the pulse ajmp rc6 rc8: clr tr0 ; stop the timer mov r1, th0 ; read the result mov r0, tl0 clr c ret