fluctuating output of adc of pic18f4550.

Status
Not open for further replies.
View attachment 64489Hi friends,

I am using accelerometer for angle measurement. datasheet is given below....

**broken link removed**


I am using PIC18F4550. i am getting output from accelerometer and converting it in to digital using inbuilt adc of PIC (10 bit). My program works well and accelerometer too. but output difference of is 29mV adc of microcontroller even accelerometer is perfect steady. because of small difference in voltage my angle changes with 1.75 degrees. I know this is small difference but i have to suppress it for my application. i am giving you detailed readings. I am also attaching my code and my circuits too.

Code:
#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;



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;

    
    
  
	    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
*/
   

//---initialize the adc interrupt and enable them---
    ADC_INT_DISABLE();

//---sample and convert----
   
     while (d=1)
    {
      for ( i=0;i<3;i++)

     {
       
       if (i==0)
     {
         



      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);

   ADC_INT_DISABLE();


      Delay10TCYx(5);
  
    ConvertADC();
    
    while(BusyADC());
  
    Delay10TCYx(4);
	ADCResult = ReadADC();
    
    final_output = ADCResult;
   
	voltage_x = (final_output*5.0)/1023;

    
    theta_x = asin ((voltage_x-1.65)/0.8);
 
    degree_x = theta_x * 57.2957795;
    
	//PORTD = theta;

    CloseADC();         //turn off ADC
   
       Delay10TCYx(100);
     










     
     

     } 
      else if (i==1)
     {
  




 config1 = ADC_FOSC_16 | ADC_RIGHT_JUST | ADC_2_TAD ;
    config2 = ADC_CH1 | ADC_INT_OFF | ADC_REF_VDD_VSS ;
    portconfig = ADC_2ANA  ;
    OpenADC(config1,config2,portconfig);

   ADC_INT_DISABLE();


      Delay10TCYx(5);
    
    ConvertADC();
    
    while(BusyADC());
   
	//{ADCResult = (unsigned int) ReadADC();
  
    Delay10TCYx(4);
	ADCResult = ReadADC();
    
    final_output = ADCResult;
   
	voltage_y = (final_output*5.0)/1023;

    
    theta_y = asin ((voltage_y-1.65)/0.8);
 
    degree_y = theta_y * 57.2957795;
    
	//PORTD = theta;

    CloseADC();         //turn off ADC
    
   Delay10TCYx(100);







     }


    break;
    } 



     }

}



So, please help me to figure out this problem......a.meter.bmp
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…