EmbeddedRelated.com
Forums

PIC16F877A Code problems

Started by Devyn March 30, 2004
Greetings Everybody!

 I was trying to use the A/D module in PIC16F877A. I'm using a C
compiler (Hi-tech Picclite). The code i wrote passes values to PORTD.

The compiler declared the o/p of the ADC as ADRESH and ADRESL, "static
volatile unsigned char" type. The code I wrote was :-

char a,b;
//after adc conversion (which works)
//o/p right justified
a=ADRESH;
b=ADRESL;
PORTD=a; //works
PORTD=b; //doesnt work
PORTD=ADRESL; //works

What is happening? I suspected 'sign' problem, so i changed a,b to
"unsigned char", but the code still doesnt work. The register has MSB
= 1 usually (hence a sign problem?) Can anyone please help me out?

Thanks in Advance,
Devyn
I guess what I'd like to know is what you mean by:

PORTD=b; //doesnt work

You don't get the right value at all? Only the MSB is off? How do you know the
adc conversion works? Have you tried:
PORTD = ADRESH;


garykato@aol.com (Gary Kato) wrote in message news:<20040330142113.01433.00000355@mb-m15.aol.com>...
> I guess what I'd like to know is what you mean by: > PORTD=b; //doesnt work > You don't get the right value at all?
Sorry I didnt give much info. Hope this is better. When i give PORTD = b;, PORTD gives zero. When i do PORTD=ADRESL, i get the the right value.
>Only the MSB is off? How do you know the > adc conversion works? Have you tried: > PORTD = ADRESH;
Yes, I have and it 'works'. More info on that. I tested the ADC by writing the values of ADRESH and ADRESL to EEPROM. Funny thing is i wrote into eeprom using the char variables a and b. I check the eeprom contents by reading from my programmer. Sure enough, i get a non-zero and consistent value all the time. If i supply 1.4V to the ADC, i get 0x011E which is correct by my calculations. ( see, 5v->3ff, hence 1.4V->0x11e). So, i hope i've put everything across. Its an irritating problem, nothing serious. Regards, Devyn
 Is there any code between "b=ADRESL" and "PORTD=b"? There must be since PORTD
and ADRESL are in different banks.

I'm guessing that's the problem. Your variable b is probably in bank 0. You
switch to bank 1 then do the "b=ADRESL". Instead of setting b, it sets the same
address in bank 1 to the value. When you do "PORTD=b", a switch to bank 0 is
made and b is still 0. I don't use C so I have no idea if the C compiler
automatically generates the bank switching code, but you might want to look at
that.