View previous topic :: View next topic |
Author |
Message |
ChrisL
Joined: 16 Apr 2007 Posts: 26 Location: Arizona
|
Why is A/D result reversed...????? |
Posted: Sat Apr 21, 2007 11:05 am |
|
|
This code creates the correct result for an A/D conversion, but the answer is reversed in the result (Value). We would expect 0x00D6 in (Value), but we get 0xD600...!!! In the WATCH window ADRESH is 0x00 and ADRESL is 0xD6 which is correct.
#include <16F887.h>
#device adc=10
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=18432000)
#bit ADFM_BIT = 0x9F.7
long value=0;
//============================
void main()
{
set_tris_a(0xFF);
delay_ms(100);
setup_adc_ports(ALL_ANALOG | VREF_VREF);
setup_adc(ADC_CLOCK_INTERNAL );
ADFM_BIT = 1; // Left justify the result
while(1)
{
set_adc_channel(1); // AN1
delay_us(50);//wait to settle
value = read_adc();//get the conversion
}
} _________________ Thank you,
Chris |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Apr 21, 2007 12:17 pm |
|
|
Your comment on the ADFM bit is wrong. If you set it high, that will
right justify the result (not left justify it).
How do you know the result has the MSB and LSB swapped ? You said
it looks correct in the Watch Window. I don't see a printf statement
in your code. What method are you using to observe the result, other
than the Watch Window ? Your post implies that you have another
method.
1. Are you using Proteus ?
2. Post your compiler version.
3. If you're using an IDE (CCS or MPLAB), then post the IDE and
the version of it. |
|
|
ChrisL
Joined: 16 Apr 2007 Posts: 26 Location: Arizona
|
|
Posted: Sat Apr 21, 2007 12:30 pm |
|
|
PCM Programmer,
MPLAB 7.52
ICD-2
PCW.EXE - 2/15/2007
WATCH Window - is standard Watch Window in MPLAB, no other method used
WATCH (ADRESL=0xD6)
WATCH (ADRESH = 0x00)
No Proteus
Variable (Value) = 0xD600 after conversion (expect 0x00D6 to match ADRESH and ADRESL)
The conversion value is correct, just reversed...!
I just don't get it...!
Thank you for your assistance _________________ Thank you,
Chris |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Apr 21, 2007 12:49 pm |
|
|
Is it possible that this is an MPLAB issue ?
Either in user configuration or a bug in MPLAB ?
If I setup a Watch Window and add 'value' to it, and then right-click
on that line in the Watch Window, I get a Properties window.
In the Properties window, there is a setting for the Byte Order.
Do you have it set correctly ? |
|
|
ChrisL
Joined: 16 Apr 2007 Posts: 26 Location: Arizona
|
|
Posted: Sat Apr 21, 2007 12:57 pm |
|
|
PCM Programmer,
You nailed it, the "Byte Order" was the issue. Good catch on the incorrect comment on ADFM...
Thank you again for your expert help... _________________ Thank you,
Chris |
|
|
|