CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Why is A/D result reversed...?????

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ChrisL



Joined: 16 Apr 2007
Posts: 26
Location: Arizona

View user's profile Send private message Visit poster's website

Why is A/D result reversed...?????
PostPosted: Sat Apr 21, 2007 11:05 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Apr 21, 2007 12:17 pm     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Sat Apr 21, 2007 12:30 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Apr 21, 2007 12:49 pm     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Sat Apr 21, 2007 12:57 pm     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group