CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

18f46k80 and ADC

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
-Terppa-



Joined: 08 Jan 2018
Posts: 59
Location: Finland

View user's profile Send private message

18f46k80 and ADC
PostPosted: Mon Apr 12, 2021 11:04 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Apr 12, 2021 11:23 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Apr 12, 2021 11:40 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Apr 12, 2021 3:11 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Apr 13, 2021 12:17 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Apr 13, 2021 3:42 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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