View previous topic :: View next topic |
Author |
Message |
Jody
Joined: 08 Sep 2006 Posts: 182
|
PIC18F87J50 ADC value not correct??? (I think) |
Posted: Mon Oct 14, 2013 9:17 am |
|
|
Hello,
CCS compiler version 4.119.
I have a ADC value 65472 when I put 2V at the ADC input...
I thought that with the settings I use that 3,3V was the maxium reading... So at 2V a reading from almost maxium is a little too much.......
Anybody a idea????
Regards,
Jody
Included the code:
Code: |
#include "main_ADC.h"
#include "LCD_CCS.c"
int16 i;
unsigned int16 data;
#USE SPI (MASTER, SPI2, MODE=0, BITS=8, STREAM=SPI_2, MSB_FIRST)
void main()
{
setup_adc_ports(sAN0|sAN1|sAN2|sAN3|sAN4|sAN7|sAN10|sAN11|VSS_VDD);
setup_adc(ADC_CLOCK_DIV_2);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
set_timer1(0);
setup_timer_2(T2_DIV_BY_16,255,1); // 819 us overflow, 819 us interrupt
setup_timer_3(T3_INTERNAL|T3_DIV_BY_1);
setup_timer_4(T4_DISABLED, 0, 1);
setup_spi2(SPI_MASTER|SPI_XMIT_L_TO_H|SPI_SAMPLE_AT_MIDDLE|SPI_CLK_DIV_16);
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
lcd_init();
while (TRUE)
{
set_adc_channel(11);
delay_us(50);
data = read_adc();
lcd_gotoxy(1,1);
printf(lcd_putc,"Adc channel = %02Ld", 11);
lcd_gotoxy(21,1);
printf(lcd_putc,"Adc value = %Lu", data);
delay_ms(100);
}
} |
Header file:
Code: |
#include <18F87J50.h>
#device ICD=TRUE
#device adc=16
#FUSES DEBUG //Debug mode for use with ICD
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES NOSTVREN //Stack full/underflow will not cause reset
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES NOIESO //Internal External Switch Over mode enabled
#FUSES MSSPMSK5
#FUSES NOEASHFT //Address shifting disabled
#FUSES NOPMPEMB
#FUSES CCP2C1
#FUSES ECPLL
#FUSES NOCPUDIV
#FUSES PLL1 //Divide By 10(40MHz oscillator input)
#use delay(clock=40000000) |
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Mon Oct 14, 2013 9:25 am |
|
|
I don't use that PIC and haven't read it's datasheet but I suspect the adc_clock divisor is wrong.
I'd check the 'valid adc settings' chart in the ADC section to see what's allowed for a 3V PIC running at 40 MHz.
Also check the PIC spec to be sure you can run it at 40MHz at 3 Volts.
Either of these two conditions could give you wrong results...
hth
jay |
|
|
Jody
Joined: 08 Sep 2006 Posts: 182
|
|
Posted: Mon Oct 14, 2013 9:46 am |
|
|
When I use the internal oscillator at 4MHz and a
setup_adc(ADC_CLOCK_DIV_64| ADC_TAD_MUL_0);
I get the same result... |
|
|
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Mon Oct 14, 2013 10:57 am |
|
|
You have to set #device ADC= 8/10/12. Otherwise CCS the ADC are left justified instead of right.
Regards |
|
|
Jody
Joined: 08 Sep 2006 Posts: 182
|
|
Posted: Mon Oct 14, 2013 11:49 am |
|
|
like in the header file the third line?? |
|
|
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Mon Oct 14, 2013 12:06 pm |
|
|
Sorry missed that, but change to 10 bit, ADC are 10 bit on this chip.
Regards |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Mon Oct 14, 2013 12:40 pm |
|
|
One obvious comment. On this chip, Vss to Vdd, is _AVss_ to _AVdd_. Are these pins connected, and connected to 0v/3.3v. They need to be....
Best Wishes |
|
|
Jody
Joined: 08 Sep 2006 Posts: 182
|
|
Posted: Tue Oct 15, 2013 4:57 am |
|
|
AVDD -> 3.3V (pin 25) AVSS -> GND (pin 26) it is a 80 pins device...
At this moment the ADV value is not moving at all.... it is stuck at 33423 no mather what I am connecting to the adc pin.....
Am i doing something wrong??? or is it broken???
Second test: Have another board.... run the same software as above...
ADC value on all channels is 63371....
Even when I ground one... the reading is 63371.
Regards,
Jody |
|
|
Jody
Joined: 08 Sep 2006 Posts: 182
|
|
Posted: Tue Oct 15, 2013 6:44 am |
|
|
UPDATE:
I am trying to read AN11 at this moment (pin 14 F6) and after reading the registers has the following value (1V at the input):
Registers are:
ADCON0: 00101100 (0x2C)
that is reference AVSS to AVDD (GND to 3.3V),
Channel 11,
Conversion done,
AD Module disabled.
ADCON1: 00000000 (0x00)
left justified,
normal AD,
0 TAD,
FOSC/2.
ANCON0: 00000000 (0x00)
Analog channel.
ANCON1: 00000000 (0x00)
Analoge channel
Is this correct??? if so.. still no adc_data |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Wed Oct 16, 2013 1:25 am |
|
|
It has already been pointed out to you, that you are trying to clock the ADC much too fast.
Very first post from temtronic.
For a 40MHz device, the _maximum_ is Fosc/32, but you are still using Fosc/2.... |
|
|
Jody
Joined: 08 Sep 2006 Posts: 182
|
|
Posted: Wed Oct 16, 2013 1:42 am |
|
|
Oops sorry.. forgot to tell.. after the first message I picked a new board...
Used the internal oscilator on another board..... 8MHz...
But right now I have a 40MHz oscillator... and used the FOSC/64....
And the result are on all the channel the same...even when I left them float and when I connect 1V to the input...
Mine hair is turning grey...... |
|
|
Jody
Joined: 08 Sep 2006 Posts: 182
|
Solved.... but why???? |
Posted: Wed Oct 16, 2013 5:04 am |
|
|
Okee what I have done:
Setting all the ADC related bits by hand.....
And then it starts working....
Anybody knows why????
Code: |
#include "main_ADC.h"
unsigned int16 data0,data11;
#byte ADCON0 = 0xFC2
#bit VCFG1 = ADCON0.7
#bit VCFG0 = ADCON0.6
#bit CHS3 = ADCON0.5
#bit CHS2 = ADCON0.4
#bit CHS1 = ADCON0.3
#bit CHS0 = ADCON0.2
#bit ADCON0_GO = ADCON0.1
#bit ADON = ADCON0.0
#byte ADCON1 = 0xFC1
#bit ADFM = ADCON1.7
#bit ADCAL = ADCON1.6
#bit ACQT2 = ADCON1.5
#bit ACQT1 = ADCON1.4
#bit ACQT0 = ADCON1.3
#bit ADCS2 = ADCON1.2
#bit ADCS1 = ADCON1.1
#bit ADCS0 = ADCON1.0
void main()
{
setup_timer_4(T4_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
//****** Setup ADC ***//
VCFG1 = 0;
VCFG0 = 0;
CHS3 = 0;
CHS2 = 0;
CHS1 = 0;
CHS0 = 0;
ADCON0_GO = 0;
ADON = 1;
ADFM = 0;
ADCAL = 0;
ACQT2 = 1;
ACQT1 = 0;
ACQT0 = 1;
ADCS2 = 1;
ADCS1 = 1;
ADCS0 = 0;
while (TRUE)
{
set_adc_channel(0);
delay_ms(50);
data0 = read_adc();
delay_ms(10);
set_adc_channel(11);
delay_ms(50);
data11 = read_adc();
delay_ms(10);
}
} |
Code: |
#include <18F87J50.h>
#device ICD=TRUE
#device adc=10
#FUSES DEBUG //Debug mode for use with ICD
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES NOSTVREN //Stack full/underflow will not cause reset
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES NOIESO //Internal External Switch Over mode enabled
#FUSES MSSPMSK5
#FUSES NOEASHFT //Address shifting disabled
#FUSES NOPMPEMB
#FUSES CCP2C1
#FUSES ECPLL
#FUSES NOCPUDIV
#FUSES PLL1 //Divide By 10(40MHz oscillator input)
#use delay(clock=40000000) |
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Wed Oct 16, 2013 5:22 am |
|
|
well that's good news !!
Could be a compiler bug.
You should take your working program, copy it,save as 'ver2', edit to use the CCS ADC setup functions, compile and test. Then printout the listings to see what is different in the ADC setup configurations.
Please report back what you find, if it is a 'bug'( incorrect setup), contact CCS support and inform them.
hth
jay |
|
|
gaugeguy
Joined: 05 Apr 2011 Posts: 303
|
|
Posted: Wed Oct 16, 2013 7:12 am |
|
|
You changed from ADC_TAD_MUL_0 to ADC_TAD_MUL_8 (ACQT2 = 1)
This gave the ADC the correct acquisition time to give a good result. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Wed Oct 16, 2013 8:42 am |
|
|
gee... the old guy's first hunch was right !...
Jay |
|
|
|