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.

adc is not working pic16f887

Status
Not open for further replies.

ep.hobbyiest

Full Member level 4
Joined
Jul 24, 2014
Messages
212
Helped
1
Reputation
2
Reaction score
1
Trophy points
18
Activity points
1,487
Hi,
i have configured ADC of pic16f887's PIC but it is not working properly.
I m using XC8 compiler and 4MHz cystal.

Code:
void adc_init(void)
{
  ANSEL = 0x03;
    TRISA = 0x03;// 4 channels selected
  ADCON1 = 0x80;// right justified
  ADCON0 = 0x01;//FOSC/2 and ADC Enabled
}

uint16_t read_adc(uint8_t por)
{
  uint16_t dat;
  ADCON0bits.CHS = 0;
  ADCON0bits.CHS = por;
  ADCON0bits.GO = 1;
  __delay_ms(1);
  while(ADCON0bits.GO_DONE == 1);
  ADCON0bits.GO = 0;
  ADCON0bits.CHS = 
  dat = ADRESL;
  dat |= (ADRESH<<8);
  return dat;
}
 

The command "ADCON0bits.GO = 0" looks totally unnecessary.
The command "ADCON0bits.CHS =" seems like it was never compiled.
 

i added that part later after checking that ADC part is not working properly. It is giving same reading again and again for both channel.
 

Though your comment says "4 channels select", you have only enabled 2 (AN0 and AN1)

I also seem to remember reading (someplace) that you have to disable the ADC to change any configuration parameter. I can't find the reference but I do it, it works, so it can't hurt.

Your code should be:

Code:
  ADCON0bits.ADON = 0;     // Disable to change channel
  ADCON0bits.CHS3 = 0;
  ADCON0bits.CHS2 = 0;
  ADCON0bits.CHS1 = 0;
  ADCON0bits.CHS0 = por;  // Por = 0 for AN0, Por = 1 for AN1
  ADCON0bits.ADON = 1;    // Enable
 
Last edited:

yes. i forgot to update comment in code but in code and in post#3 i mentioned as 2 channels.

- - - Updated - - -

ok. i ll try as suggested , is it differ than what i used?
 

Code:
  ADCON0bits.CHS = 0;
  ADCON0bits.CHS = por;

Was setting ADCON0bits.CHS to 0 then to por. Not sure how it compiled that way, but still....
 

Use:
Code:
ADCON0 &= 0xC7;  // clear the channel select bits
ADCON0 |= por << 3; // select new channel
__delay_ms(1); // allow S&H to settle
 ADCON0bits.GO = 1;
 while(ADCON0bits.GO_DONE == 1);

that allows you to use any 'por' number, solves the problem of readings from one channel influencing another and it might return the result quicker.

Brian.
 

Use MCC it generates the ADC read function.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top