// // Light level detector demonstration. // // Pin P1_4 is connected to one side of a cadmium sulfide photodetector. // Pin P1_5 is connected to the other side of the photodetector. // // There is a 1k ohm resistor between P1_5 and ground to form a voltage divider // with the photodetector. // // The low four pins of Port 1 are connected to LEDs whose other side is grounded // The low six pins of Port 2 are connected to LEDs the same way. // There are ten LEDs in a row, showing us the ten bit output of the analog // to digital converter, which is reading the voltage on pin P1_4. // // That voltage is between zero and the 2.5 volt reference. // There are 1,024 levels in ten bits, so each level is 2.4414065 millivolts. // void setup( void ) { P1DIR = 0b1111; // low four bits P2DIR = 0b111111; // low six bits (makes 10 bits total) // // Set the reference to 2.5 volts vs ground, turn the reference on, // and make it available on pin P1_4 (the reference output pin). // analogReference( SREF1 | REFON | REFOUT | REF2_5V ); } void show_value( unsigned value ) { P1OUT = value & 0b1111; P2OUT = (value >> 4) & 0b111111; } void loop( void ) { show_value( analogRead( A5 ) ); delay( 100 ); }