Skip to navigation

Revs on the BBC Micro

Keyboard: GetADCChannel

Name: GetADCChannel [Show more] Type: Subroutine Category: Keyboard Summary: Read the value of an ADC channel (used to read the joystick)
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * ProcessDrivingKeys (Part 1 of 6) calls GetADCChannel * ProcessDrivingKeys (Part 3 of 6) calls GetADCChannel

This routine reads a joystick axis and returns a value with 0 representing the stick being at the centre point, and -127 and +127 representing the left/right or up/down values.
Arguments: X The ADC channel to read: * 1 = joystick X * 2 = joystick Y
Returns: A The high byte of the channel value, converted to an absolute figure in the range 0 to 127 X The sign of the result: * 0 = positive, i.e. right or down * 1 = negative, i.e. left or up C flag Clear if A < 10
.GetADCChannel LDA #128 \ Call OSBYTE with A = 128 to fetch the 16-bit value JSR OSBYTE \ from ADC channel X, returning (Y X), i.e. the high \ byte in Y and the low byte in X \ \ * Channel 1 is the x-axis: 0 = right, 65520 = left \ \ * Channel 2 is the y-axis: 0 = down, 65520 = up TYA \ Copy Y to A, so A contains the high byte of the \ channel value \ The channel value in A will be in the range 0 to 255, \ with 128 representing the stick being in the centre, \ so now we need to flip this around into the range 0 to \ 127, with the sign given in X LDX #1 \ Set X = 1, to denote a negative result (left or down), \ which we will change below if this is a positive \ result CLC \ Set A = A + 128, so in terms of 8-bit numbers, this ADC #128 \ does the following: \ \ * 0-127 goes to 128-255 \ \ * 128-255 goes to 256-383, i.e. 0-127 \ \ So A is now in the range 128 to 255 for low readings \ from the ADC (right or down), or 0 to 127 for high \ readings (left or up) BPL adcc1 \ If A is in the range 0 to 127, skip the following two \ instructions as the result is already in the correct \ range, 0 to 127, and X is set to 1 for left or up EOR #&FF \ Flip the value of A, so the range 128 to 255 flips to \ the range 127 to 0 DEX \ Set X = 0 to denote a positive result, right or down .adcc1 CMP #10 \ Clear the C flag if A < 10 RTS \ Return from the subroutine