#define USE_OR_MASKS
#include "delays.h"
#include "p18f4550.h"
#include "adc.h"
#include "math.h"
#if defined(_18F4550) //If the selected device if PIC18F46J11, then apply below settings else user will have to set
#pragma config OSC=HS, FCMEN=Off, WDT=OFF, IESO=OFF, XINST=OFF, LVP=OFF
#endif
unsigned int ADCResult=0;
float voltage_x=0;
float theta_x = 0;
float degree_x = 0;
float voltage_y=0;
float theta_y = 0;
float degree_y = 0;
//float voltage_z=0;
//float theta_z = 0;
//float degree_z = 0;
//
//char M[];
//char N[];
void main(void)
{
unsigned char channel=0x00,config1=0x00,config2=0x00,config3=0x00,portconfig=0x00,i=0;
unsigned int final_output,h,d,X=0;
d = 1;
// unsigned int i1=i2=i3=i4=i5=i6=i7=i8=i9=i10=0;
//unsigned int i1=0,i2=0,i3=0,i4=0,i5=0,i6=0,i7=0,i8=0,i9=0,i10=0;
h = 0;
TRISD = 0;
PORTD = 0xFF;
TRISAbits.TRISA0 = 1; //RA) as Analog i/p
PORTAbits.RA0 = 1;
CloseADC();
Delay10TCYx(4);
//--initialize adc---
/**** ADC configured for:
* FOSC/32 as conversion clock
* Result is right justified
* Aquisition time of 2 TAD
* Channel 0 for sampling
* ADC interrupt off
* ADC reference voltage from VDD & VSS
*/
// config1 = ADC_FOSC_16 | ADC_RIGHT_JUST | ADC_2_TAD ;
// config2 = ADC_CH0 | ADC_INT_OFF | ADC_REF_VDD_VSS ;
// portconfig = ADC_2ANA ;
//OpenADC(config1,config2,portconfig);
//---initialize the adc interrupt and enable them---
ADC_INT_DISABLE();
//---sample and convert----
//for(i=0;i<10;i++)
while (d=1)
{
config1 = ADC_FOSC_16 | ADC_RIGHT_JUST | ADC_2_TAD ;
config2 = ADC_CH0 | ADC_INT_OFF | ADC_REF_VREFPLUS_VREFMINUS ;
portconfig = ADC_1ANA ;
OpenADC(config1,config2,portconfig);
ADC_INT_DISABLE();
Delay10TCYx(5);
ConvertADC();
while(BusyADC());
Delay10TCYx(4);
ADCResult = ReadADC();
final_output = ADCResult;
voltage_x = (final_output*2.0)/1023;
theta_x = asin ((voltage_x-1.65)/0.8);
degree_x = theta_x * 57.2957795;
CloseADC(); //turn off ADC
Delay10TCYx(100);
}
}