PIC Microcontroller

Tilt Sensor (ADXL203, 2-Axis, AnalogDivices社) Example Program

EP 기술연구소 2010. 5. 26. 23:52

/*****************************************************************
 Project  : Sub-Routine(GlassMain_ADCtl.c) of Glass Robot controller
 Version :
 Date    : 2010. 3. 21
 Author  : kornan-Park(kornanp@empal.com)
 Company : EU-Technology

 Version Change Notes :
 
*****************************************************************/
//#nolist

#define  X_Axis 0
#define  Y_Axis 1

#define  degree_0  0x2CC // 3.5V x 1023/5V
#define  degree_90  0x1FF // 2.5V x 1023/5V
#define  degree_180  0x132 // 3.5V x 1023/5V

#define  degree_45  0x296
#define  degree_135  0x16A
#define  degree_225  0x290

void AngleCheck()
{
 unsigned long i= 0, da;
   
    if(AD_Data[X_Axis] >= degree_45) i = 2;
    else {
     if(AD_Data[X_Axis] <= degree_135) i = 0;
     else {
      if(AD_Data[Y_Axis] >= degree_90) i = 3;
      else i = 1;
     }
    }
   
    switch(i){
     case 0:  // 45 - 135 : 2-status
      TestPin = ~TestPin;
      da = (degree_45 - AD_Data[Y_Axis]);
      for(i=0;i<90;i++) if(da <= Degeree[i]) break;
      CurrentAngle = (45 + i);
      break;
     case 1:  // 135 - 225 : 3-status
      da = (degree_45 - AD_Data[X_Axis]);
      for(i=0;i<90;i++) if(da <= Degeree[i]) break;
      CurrentAngle = (225 - i);
      break;
     case 2:  // 225 - 315 : 1-status
      da = (degree_45 - AD_Data[Y_Axis]);
      for(i=0;i<90;i++) if(da <= Degeree[i]) break;
      CurrentAngle = (315 - i);
      break;
     case 3:  // 270 - 45 : 4-Status
      da = (degree_45 - AD_Data[X_Axis]);
      for(i=0;i<90;i++) if(da <= Degeree[i]) break;
      CurrentAngle = (315 + i);
      CurrentAngle %= 360;
      break;
     default :
      CurrentAngle = 0xFFFF;
      TestPin = ~TestPin;
      break;
    }
   
  
}

/***** AD Converter Start & Check Routine *****/
void ADC_Start()
{
 unsigned char ch;
 
 if(++AD_Channel >= ADC_Size) AD_Channel = 0;
 ch = (AD_Channel << 2);
 ADCON0 = (ADC_VREF_TYPE | ch);
 AD_Start = 1;  // AD Conversion Start
}

//===============================================================
// ADC Complete Routine
void ADConverter()
{
 unsigned char i;
 //unsigned long int da;

 ADC_CompFlag = 0;
 
 if(++AD_Counter >= 16){
  AD_Counter = 0;
  for(i=0;i<ADC_Size;i++){
   AD_Data[i] = (ADC_Buffer[i] >> 4);
   ADC_Buffer[i] = 0;
  }
  AngleCheck(); // Current Angle Check Routine
  //
 }

}

#list