ADC problem with PIC16f877a

Thread Starter

MaxD

Joined May 5, 2012
11
ADCON0:
bit7 - ADCS1
bit6 - ADCS0
bit5 - CHS2
bit4 - CHS1
bit3 - CHS0
bit2 - GO/DONE
bit1 - unimplemented bit
bit0 - ADON
----------------------------------------------------------
ADCON1:
bit7 - ADFM
bit6 - unimplemented bit
bit5 - unimplemented bit
bit4 - unimplemented bit
bit3 - PCFG3
bit2 - PCFG2
bit1 - PCFG1
bit0 - PCFG0
---------------------------------------------------------
According to the registers above, I set RA3 as Vref+ and RA2 as analog input:
Rich (BB code):
TRISA = 0b11111;
TRISE = 0b111;

ADCON0 = 0b10010001;
ADCON1 = 0b10000001;

__delay_ms(2);
   ADGO=1;
   while(ADGO==1)
      continue;
   return(256*ADRESH+ADRESL);
I can't get any power supply from RA3, why? If there is no power supply from RA3, then why LCD didn't show 0 value? Any problem with my register setting?
 

nerdegutta

Joined Dec 15, 2009
2,684
I think
Rich (BB code):
TRISA = 0b11111;
Sets RA0-RA4 as input. To get RA3 as output, try this:

Rich (BB code):
TRISA = 0b11110111;
I might be wrong...
 

t06afre

Joined May 11, 2009
5,934
In a microcontroller world. You do want to avoid doing math unless needed. And in this case it would be much more efficient to use an union like this. The game is not to write code that looks compact on the paper. But use the instruction set of your PIC in the best way.
Rich (BB code):
union ad_res {
 unsigned int adc;
 unsigned char ad_bytes[2];
}ad_data;
main(void)
{
  unsigned int data;
  unsigned char lowByte, highByte;  
  data = 301;
  ad_data.adc = data;
  highByte = ad_data.ad_bytes[1];
  lowByte = ad_data.ad_bytes[0];  
 
  //or.
  HighByte = 0xf1;
  LowByte = 0x01;
  ad_data.ad_bytes[1] = highByte;
  ad_data.ad_bytes[0] = lowByte;
  data = ad_data.adc;
}
perhaps a more practical style
Rich (BB code):
typedef union 
   { 
   unsigned short u16; 
   unsigned char u8[2]; 
   } U16_U8;
 
 U16_U8 ptr;
 // Do something to set the variable ptr 
ptr.u16 = ?;
ptr.u8[0]=ADRESL; 
ptr.u8[1]=ADRESH;
 
Top