View previous topic :: View next topic |
Author |
Message |
-Terppa-
Joined: 08 Jan 2018 Posts: 59 Location: Finland
|
18f46k80 and ADC |
Posted: Mon Apr 12, 2021 11:04 am |
|
|
Hello! I have interesting problem. I try to read AD- value from pin A0. Everything is look's good but when input voltage is higher than 3,0v then ADC- value goes down..
If i have input pin at 3,0v i can get value 652, if voltage is 4,0v value is only 566 and so on.
I have internal clock selected 64Mhz and MCU is F- version not LF
(Test program/setup)
Code: |
//device adc setup
#device ADC=10
#ZERO_RAM
#nolist
FUSES:
#fuses INTRC_IO, PLLEN, NOWDT, PUT, BROWNOUT, MCLR, BORV30, VREGSLEEP
ADC setup:
setup_adc(ADC_CLOCK_DIV_32);
setup_adc_ports(sAN0|sAN1);
SET_ADC_CHANNEL(0);
Somewhere in main read ADC and print:
int16 val;
val=ReadADC(channel);
fprintf(USEADCPRINTF,"\n\rADCraw:%lu",val);
|
Why i cannot see maximum value 1023? and why ADC- value goes down after 3.0v?
Compiler PCWHD 5.092
MPLAB 8.92 + plugin
WIN 10/64bit
Thank you for your help! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Apr 12, 2021 11:23 am |
|
|
What is the Vdd of your PIC ?
What is the output impedance of your voltage source ?
Quote: | val=ReadADC(channel); |
Post your ReadADC function. This function is not in the CCS manual.
Also, what is channel set to ? Is it possible you are reading the wrong
channel ? |
|
|
-Terppa-
Joined: 08 Jan 2018 Posts: 59 Location: Finland
|
|
Posted: Mon Apr 12, 2021 11:40 am |
|
|
Vdd is 5v (wall adapter) and ADC- source is feeded direct from laboratory power supply and both GND's is connected.
Now ReadADC is almost empty, only this is left:
Code: |
unsigned int16 val=0;
val=READ_ADC();
return val;
|
Channel not change anything.. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Apr 12, 2021 3:11 pm |
|
|
I was able to make it work with the following test program. This was
tested in hardware, with the DIP-40 package PIC, at 5v, with CCS
vs. 5.103. I can turn a 5K trimpot to the max (5v output) and see 1023
on TeraTerm.
Code: |
#include <18F46K80.h>
#device ADC=10
#use delay(internal=64M)
#use rs232(UART1, baud=9600, ERRORS)
//================================= //
void main()
{
int16 val;
setup_adc(ADC_CLOCK_DIV_64 | ADC_TAD_MUL_20);
setup_adc_ports(sAN0);
set_adc_channel(0);
delay_us(3);
while(TRUE)
{
val = read_adc();
printf("\n\rADCraw:%lu", val);
delay_ms(1000);
}
}
|
|
|
|
-Terppa-
Joined: 08 Jan 2018 Posts: 59 Location: Finland
|
|
Posted: Tue Apr 13, 2021 12:17 am |
|
|
Thank you very much your help Mr. PCM programmer! I'll try your code and i also build new hardware just in case. |
|
|
-Terppa-
Joined: 08 Jan 2018 Posts: 59 Location: Finland
|
|
Posted: Tue Apr 13, 2021 3:42 am |
|
|
It works! I'm build new PCB and put that code in it. That do a trick:
setup_adc(ADC_CLOCK_DIV_64|ADC_TAD_MUL_20);
If i change that ADC_TAD_MUL_20 say ADC_TAD_MUL_0 it shows max value ~660 |
|
|
|