Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

fluctuating output of adc of pic18f4550.

Status
Not open for further replies.
Member level 5
Joined
Feb 22, 2012
Messages
82
Helped
25
Reputation
50
Reaction score
25
Trophy points
1,298
Activity points
0
Hi friends,

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

https://www.robokits.co.in/datasheets/Robokits%20ACC%20Module%201.5G.pdf


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......View attachment a.meter.bmp
 

Have you made sure that the reference voltage is steady and that it does not fluctuate?

Read the "TIPS AND TRICKS" section here: https://www.edaboard.com/blog/1574/

See if you have done everything mentioned. You may even try to use the interfacing circuit shown there.

Hope this helps.
Tahmid.

---------- Post added at 22:33 ---------- Previous post was at 22:31 ----------

If I remember correctly, you had posted a similar problem before. Did you try the suggestions provided before?
 
HI tahmid ... I am using internal Vref. I showed your article. it's really nice. Some times ago the same problem produced but at that time voltage difference was 200mV and this time around 20mV. I followed all the suggestions and due to that error reduced to 20mV.

Hi, wp100. I will check it out...
 
  • Like
Reactions: hd.

    hd.

    Points: 2
    Helpful Answer Positive Rating
i changed my Vref+ and Vref- from internal to external. But there is one problem. problem is when input voltage is given to pic it gives 0v as output and when no input is given, it gives maximum voltage at output... my code is also given.
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;


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

    }



so hep me to solve this error
 
  • Like
Reactions: hd.

    hd.

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top