Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL
Help

The Flight > Expanding the EX16 Library - Pg. 181

It's an Analog World // main loop while( 1) { pot = ReadADC( POT_CH); 181 // select the POT input, convert // reduce the 10-bit result to a 3 bit value (0..7) // (divide by 128 or shift right 7 times pos = pot >> 7; // turn on only the corresponding LED // 0 2> leftmost LED.... 72> rigtmost LED PORTA = (0x80 >> pos); } // main loop } // main After the call to the initialization routine (to which we provide a mask that defines bit 5 as analog input), we initialize the TRISA register to make the pins connected to the LED bar digital outputs. Then in the main loop we perform the conversion on AN5 and we reformat the output to fit our special display requirements. As configured, the 10-bit conversion output will be returned as a right-aligned integer in a range of values between 0 and 1023. By dividing that value by 128 (or in other words shifting it right seven times) we can reduce the range to a zero to seven value. The final output though requires one more transformation