/*****************************************************************
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
'PIC Microcontroller' 카테고리의 다른 글
16F723 기본 프로그램 (0) | 2010.06.28 |
---|---|
PIC16F887 Basic Source Program (0) | 2010.06.14 |
Summary of CCS C compiler Access to PIC internal peripherals (0) | 2010.05.14 |
PIC Microchips ORCAD Library (0) | 2010.05.13 |
PIC18F4680 기본 프로그램 (0) | 2010.05.04 |