|
|
View previous topic :: View next topic |
Author |
Message |
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
PIC 18Fxxxx 12 Bit A/D support |
Posted: Thu Nov 25, 2010 8:58 pm |
|
|
Just had to use a 12 bit internal PIC A/D on an upgrade-a 16F886 design.
Problem is - I can't make the compiler hand me over a proper 12bit conversion without working around the intrinsic A/D functions!
BTW-
#device ADC=12 does NOTHING
I did solve it but there is no syntax in the main compiler that I can make do ANYTHING except return an 8 BIT low order byte and nothing else.
Yes, solved, but its ugly.
Can anybody tell me why the compiler seems not to handle this part right?
Code: |
version 4.085 compiler
#include <18f2523.h>
#Fuses RC_IO,NOPROTECT
#Fuses BROWNOUT,NOWDT,BORV43
#Fuses PUT,NOCPD,STVREN,NODEBUG
#Fuses NOLVP,NOWRT,NOWRTD,NOIESO,NOFCMEN
#Fuses NOPBADEN,CCP2C1,NOWRTC,NOWRTB,EBTR
#Fuses NOEBTR,NOEBTRB,NOCPB,NOLPT1OSC,NOMCLR,NOXINST
// done in the INIT section already
// using 8 mhz int RC clock no multiplier
// A/D setup
setup_adc( ADC_TAD_MUL_8|ADC_CLOCK_INTERNAL ); // 4 usec
setup_adc_ports( AN0_TO_AN4|VSS_VDD); // R3
set_adc_channel( 3 ); // continuous read DC signal detector 0-5v
unsigned int16 adcout;
void adcll(void){ // get a 12 bit adc reading
unsigned int8 cpr=255;
#byte adhi = 0xFC4
#byte adlo = 0xFC3
read_adc(ADC_START_ONLY);
do {--cpr; } while (!adc_done() && cpr);
if (!cpr) baddie(88); // an error message reporter
else delay_cycles(4);
adcout = adlo + (adhi*256);
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Nov 25, 2010 10:42 pm |
|
|
Try a more simple program:
Code: |
#include <18F2523.h>
#device adc=16
#fuses INTRC_IO,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#byte ADCON2 = getenv("SFR:ADCON2")
#bit ADFM = ADCON2.7
#define right_justify_adc() ADFM = 1
//======================================
void main(void)
{
int16 result;
setup_adc_ports(AN0_TO_AN4 | VSS_VDD);
setup_adc(ADC_CLOCK_DIV_4);
right_justify_adc();
set_adc_channel(3);
delay_us(20);
while(1)
{
result = read_adc();
printf("%lx \n\r", result);
delay_ms(500);
}
} |
Also, the output impedance of your analog voltage source must be no
more than 2.5K ohms. |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
code flips |
Posted: Fri Nov 26, 2010 7:59 am |
|
|
new code to be tried later today
i am much more at home with hardware -oh for about 35 years -
the A/D port is fed from the output of a buffer op amp.
input Z not involved
the conversions results are dead-on accurate with the code shown -
but using CCS defaults -ONLY the low 8bits are passed back
more later |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
update |
Posted: Fri Nov 26, 2010 8:15 am |
|
|
Sorry I also already had the code below in my original init zone.
I was already doing what you suggested - but forgot to extract and show that bit of code.
I was using -
#bit ADFM 0xFC0.7 // adcon2 on the 18fxxxx
then globally
ADFM=1;
STILL the intrinsic A/D functions return ONLY the low 8 bits of the 12 bit conversion - into the 16 bit variable. If I use the CCS built-in call - 8 bits
with the direct register reading code - 12 good bits.
I think its bugs in the compiler version I'm using. Now more than ever.
And the fix I am using works fine - so I chalk it up to another bump in CCS device handling. |
|
|
|
|
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
|