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

ADCON1 register not set correctly

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



Joined: 02 Sep 2005
Posts: 49
Location: Nottingham, UK

View user's profile Send private message

ADCON1 register not set correctly
PostPosted: Wed Nov 09, 2005 4:05 am     Reply with quote

Dear all, I'm using v3.234 CCS compiler and working with a 18LF452.
The problem I am seeing is that CCS is not setting the ADCON1 register correctly. I want to use pin5 (RA3) as the reference for the A to D, this is the code I am using below. ADCON1 should be 0b10000011 but it is is being left at 0b10000111, bit2(PCFG2) should be 0 as I see it. The result is that the chip is using VCC as VREF and not the ref on RA3. If I uncomment the ADCON1 bit it works ok. I've asked CCS but they haven't replied yet. Am I doing something wrong ? or in the wrong order maybe ?
Code:

 
 setup_adc(adc_clock_div_16);
 setup_adc(AN0_AN1_AN2_AN4_VSS_VREF);    // Setup analog ports
 //ADCON1 = 0b10000011;
 set_adc_channel(1);             // Sample AN1
 delay_ms(50);
 value = read_adc();             // Read value of a/d

_________________
Tim
Ttelmah
Guest







PostPosted: Wed Nov 09, 2005 5:07 am     Reply with quote

The code you post, gives exactly the correct answer for me on 3.234.
Have a look in your 18F452.h file, to see what
'AN0_AN1_AN2_AN4_VSS_VREF' is actually defined as?.
The values for these, are just the numbers that get put into the ADCON register. On mine, it is set to 3, and '3' gets put into the register. The code generated is:
.................... setup_adc_ports(AN0_AN1_AN2_AN4_VSS_VREF);
032C: BSF FC1.0
032E: BSF FC1.1
0330: BCF FC1.2
0332: BCF FC1.3

Which sets bit 0, and 1, and clears bits 2 and 3.
Perhaps your #define has been changed?.

Best Wishes
TIMT



Joined: 02 Sep 2005
Posts: 49
Location: Nottingham, UK

View user's profile Send private message

ADCON1 problem
PostPosted: Wed Nov 09, 2005 6:36 am     Reply with quote

Thanks for replying,
This is the #define in my header : It SETS bits 0 and 1 but doesn't seem to CLEAR bit 2. Unless it's a problem with MPLAB v7.21 not displaying the adcon1 correctly. In a bit I'll deliberately set ADCON1 to 10000111 then see if setup_adc_ports sets it back to 0b100000011.
Code:

#define AN0_AN1_AN2_AN4_VSS_VREF             3    // A0 A1 A2 A5 VRefh=A3             

_________________
Tim
TIMT



Joined: 02 Sep 2005
Posts: 49
Location: Nottingham, UK

View user's profile Send private message

dilliberate mistake.
PostPosted: Wed Nov 09, 2005 7:15 am     Reply with quote

No one spotted the dillerate mistake :-

setup_adc(AN0_AN1_AN2_AN4_VSS_VREF);

should be:-

setup_adc_ports(AN0_AN1_AN2_AN4_VSS_VREF);

Sorry for wasting your time Ttelmah. No wonder CCS haven't replied
bet they think I'm a right wally!
! Embarassed
_________________
Tim
TIMT



Joined: 02 Sep 2005
Posts: 49
Location: Nottingham, UK

View user's profile Send private message

Example error
PostPosted: Wed Nov 09, 2005 7:24 am     Reply with quote

Having admitted my mistake, I think I made the error because the example on page 139 of the "An introduction to programming the Microchip PIC in CCS C" by Nigel Gardner uses the routine :-

setup_adc(ALL_ANALOG);

which should I beleive be :-

setup_adc_ports(ALL_ANALOG);

I think the example in the book is incorrect which came with my compiler. Thats my excuse anyway. Not all my fault! Smile
_________________
Tim
Ttelmah
Guest







PostPosted: Wed Nov 09, 2005 7:38 am     Reply with quote

Aargh!
At least you have a good 'reason'. :-)

Best Wishes
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