View previous topic :: View next topic |
Author |
Message |
TIMT
Joined: 02 Sep 2005 Posts: 49 Location: Nottingham, UK
|
ADCON1 register not set correctly |
Posted: Wed Nov 09, 2005 4:05 am |
|
|
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
|
|
Posted: Wed Nov 09, 2005 5:07 am |
|
|
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
|
ADCON1 problem |
Posted: Wed Nov 09, 2005 6:36 am |
|
|
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
|
dilliberate mistake. |
Posted: Wed Nov 09, 2005 7:15 am |
|
|
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!
! _________________ Tim |
|
|
TIMT
Joined: 02 Sep 2005 Posts: 49 Location: Nottingham, UK
|
Example error |
Posted: Wed Nov 09, 2005 7:24 am |
|
|
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! _________________ Tim |
|
|
Ttelmah Guest
|
|
Posted: Wed Nov 09, 2005 7:38 am |
|
|
Aargh!
At least you have a good 'reason'. :-)
Best Wishes |
|
|
|