00001 /* 00002 * photo sensor control 00003 * 00004 * (c) 2007 by Matthias Arndt <matthias.arndt@tu-clausthal.de> 00005 * 00006 * control lines P3.3 - P3.7 00007 * analogue input P1.0 00008 * 00009 * 10kHz pulses generated via Timer 1 00010 */ 00013 #include "t89c51cc02.h" 00014 #include "datatypes.h" 00015 #include "photosensor.h" 00016 #include "adc.h" 00017 #include "sharp.h" 00018 #include <stdlib.h> 00019 00021 BYTE psensor; 00022 00024 const char psensor_select[]={0,0x08,0x10,0x20,0x40,0x80,0,0}; 00025 00031 void Photosensor_init() 00032 { 00033 ADCF |= 0x01; /* select P1.0 for analogue input */ 00034 psensor=0; 00035 Photosensor_PowerOff(); 00036 } 00037 00044 void Photosensor_toggle(void) 00045 { 00046 /* blink sender LED of selected photo sensor */ 00047 switch(psensor) 00048 { 00049 case 1: 00050 P3_3=!P3_3; 00051 break; 00052 case 2: 00053 P3_4=!P3_4; 00054 break; 00055 case 3: 00056 P3_5=!P3_5; 00057 break; 00058 case 4: 00059 P3_6=!P3_6; 00060 break; 00061 case 5: 00062 P3_7=!P3_7; 00063 break; 00064 default: 00065 /* disable photo sensors when no valid sensor has been selected */ 00066 Photosensor_select(0); 00067 Photosensor_PowerOff(); 00068 } 00069 00070 } 00071 00079 WORD Photosensor_read(BYTE sensornr) 00080 { 00081 WORD diff1,diff2; 00082 00083 Photosensor_select(sensornr); 00084 SHARP_PWRCTL=0; /* ensure Sharp is powered off so it won't give false readings */ 00085 /* now we read the sensor 2 times once with pulse and once without */ 00086 diff1=ADC(0); /* read */ 00087 Photosensor_PowerOff(); 00088 diff2=ADC(0); 00089 00090 return(abs(diff1-diff2)); 00091 }