sharp.c

Go to the documentation of this file.
00001 /*
00002  * Control code for Sharp GP2D120 Distance Measuring Sensor
00003  *
00004  * (c) 2007 by Matthias Arndt <matthias.arndt@tu-clausthal.de>
00005  *
00006  * sensor analog input connected to P1.0
00007  * sensor activation via P3.2
00008  *
00009  * Timer 1 is used to count 2 ticks of 25 ms to achieve a pause of 50ms when
00010  * the Sharp is about to be polled
00011  */
00013 #include "t89c51cc02.h"
00014 #include "datatypes.h"
00015 #include "adc.h"
00016 #include "sharp.h"
00017 
00019 volatile bit sharp_ready; 
00021 volatile bit wait_bit;  
00022 
00026 void Sharp_init()
00027 {
00028         ADCF |= 0x01;   /* program P1.0 for analogue input */
00029         Sharp_PowerOff();
00030         /* Sharp waiting timer is programmed upon poweron of Sharp sensor */
00031 }
00032 
00039 void Sharp_TimerInit()
00040 {
00041         TR1=0;          /* stop Timer 1 */
00042         TMOD = (TMOD & 0x0F); /* leave Timer 0 intact */
00043         TMOD |= 0x10;   /* Timer 1 Mode 1 */
00044         TF1=0;          /* clear timer flag */
00045 
00046         /* initial timing value */
00047         TH1=SHARP_TIMER_RELOAD_H;
00048         TL1=SHARP_TIMER_RELOAD_L;
00049         
00050         wait_bit=1;
00051         sharp_ready=0;
00052         TR1=1;          /* start Timer 1 */
00053         ET1=1;          /* activate Timer 1 interrupt */
00054 }
00055 
00064 void Sharp_Timer_interrupt(void) interrupt 3 using 2
00065 {
00066         if(wait_bit)
00067         {
00068                 /* reload Timer 1 once */
00069                 TR1=0;
00070                 TH1=SHARP_TIMER_RELOAD_H;
00071                 TL1=SHARP_TIMER_RELOAD_L;
00072                 TR1=1;
00073                 TF1=0;          /* acknowledge Timer 0*/
00074                 wait_bit=0;     /* next Timer ISR invocation will enable Sharp */
00075         } else {
00076                 sharp_ready=1;
00077                 TF1=0;
00078                 ET1=0; /* cut off Sharp Timer ISR as the Sharp is ready to be read to free CPU time */
00079         }
00080 }
00081 
00086 WORD Sharp_read()
00087 {
00088         /* wait until Sharp has been powered on */
00089         if(!SHARP_PWRCTL)
00090         {
00091                 /* Sharp is off */
00092                 return(0xffff);
00093         } else {
00094                 while(!sharp_ready);    /* wait until Sharp is ready...*/
00095                 return(ADC(0));
00096         }
00097 }

Generated on Wed Oct 24 12:57:24 2007 for SensorControl by  doxygen 1.4.6