View previous topic :: View next topic |
Author |
Message |
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
[SOLVED] ADC value wrong |
Posted: Thu Oct 03, 2013 11:35 pm |
|
|
Hi All.
dsPIC33FJ12MC202
CCS 4.141 & 5.012
The following code produce a wrong ADC value. The ADCBUF val are OK, but when CCS read the value they divide by 4. Now I wonder why and where am I setting wrong.
Code: | #include <33FJ12MC202.h>
#device ADC=10
#FUSES NOWDT //No Watch Dog Timer
#FUSES CKSFSM //Clock Switching is enabled, fail Safe clock monitor is enabled
#FUSES NOJTAG //JTAG disabled
#device ICSP=1
#use delay(internal=20000000)
void main()
{
unsigned int16 VinADC;
setup_adc_ports(sAN0 , VSS_VDD);
setup_adc(ADC_CLOCK_DIV_8);
set_adc_channel(0);
while(TRUE) {
VinADC = read_adc();
}
} |
The lst file for the read_adc
Code: | 022C: BCLR.B 320.0
022E: BSET.B 320.1
0230: BTSS.B 320.0
0232: BRA 230
0234: MOV 300,W0
0236: LSR W0,#2,W0
0238: MOV W0,802 |
On line 0236: the register get divided by 4. If I remove the #device ADC = 10, then it does not divide by 4. What am I missing.
Regards
Last edited by alan on Sat Oct 05, 2013 12:04 am; edited 1 time in total |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Thu Oct 03, 2013 11:41 pm |
|
|
The CCS code defaults to configuring the ADC for single sample and hold.
Setup like this the ADC returns 12bit values, not 10bit values.
So to give the ten bit value you are asking for, the result has to be divided by 4.
Best Wishes |
|
|
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Thu Oct 03, 2013 11:43 pm |
|
|
Thanks Thelmah.
But the value in the ADCBUF are 10 bit as all the ADC registers are setup for 10 bit. Thus the value I get are off by a factor of four.
Regards |
|
|
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Fri Oct 04, 2013 12:26 am |
|
|
OK.
ADC doesn't seem to work when no #device ADC specified. So will go for 12 bit operation now. That works OK.
Regards |
|
|
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Fri Oct 04, 2013 5:18 am |
|
|
The plot thickens. Selecting ADC=12, the ADC are setup as 10-bit and reading back are a 10 bit value. Wondering what would the setup be for actual 12 bit operation.
I contacted CCS regarding this so waiting response from them and will then post it.
Regards |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Fri Oct 04, 2013 7:23 am |
|
|
I was going to say you should contact them. I think they have got confused, and assumed the ADC is 12bit all the time. They are actually setting up the registers to return 10bit values, and then assuming the result is 12bit. Hence if you specify ADC=12 it works, but gives the specified 10bit value, while if you specify ADC=10, they think they have to divide by 4...
As the code currently 'is', to get 12bits, you'd have to turn off the ADC, and set the AD12B bit yourself, then turn it back on.
The division by 4, is 'standard' on chips with 12bit ADC's, if you specify ADC=10. The error is in configuring the ADC for 10bit mode, then treating it as if it is 12bit....
Best Wishes |
|
|
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Fri Oct 04, 2013 12:51 pm |
|
|
Here are the answer from CCS:
Quote: | The problem you having looks to be an error in our database for this device. If you let me know what version of the compiler you have I can send you an updated database that will fix the problem. |
Regards |
|
|
|