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

Cant read PIC16F15345 ADC

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



Joined: 30 Jun 2023
Posts: 3

View user's profile Send private message

Cant read PIC16F15345 ADC
PostPosted: Fri Jun 30, 2023 4:12 am     Reply with quote

Hello,

Can anybody help me with this problem? I am trying to read a DC voltage going from 0.5 to 3.5V on pin A2 of a pic16F15345. For some reason all I can read is the max value of 1023 or thereabouts. I can watch the voltage change on pin A2 on the scope.

I've chopped the code back to what I think should be the bare minimum.
I tried setting the tris and all the other (probably) stupid other thing I can think of.

Any comments..

Compiler Version 5.115
Code:

#include <16F15345.h>
#device ADC=10   // 10 bit ADC values
#use delay(internal=16000000)

#fuses NOWDT

void main()
{

   int16 reading = 0;
   
   #pin_select NULL=PIN_A2 

   // Tries setting TRIS. Didn't work.'
   set_tris_a(0b110100);
   
   // Set pins A2 as analogue input.
   set_analog_pins (pin_a2);

   setup_adc_ports(sAN2|VSS_VDD);
   setup_adc(ADC_CLOCK_INTERNAL);
   
   // This is to control a fet to let DC voltage through to A2
   output_high(pin_c3);

   while(TRUE)
   {
      reading = 0;
     
      set_adc_channel(sAN2);
      delay_us(100);
      reading = read_adc();
      delay_ms(1);  // Just setting a point to stop code at and read whats in reading
      reading = 0;     
      //TODO: User Code
   }
}

Any help appreciated.

SandyW
Ttelmah



Joined: 11 Mar 2010
Posts: 19503

View user's profile Send private message

PostPosted: Fri Jun 30, 2023 6:57 am     Reply with quote

One key thing wrong:

set_adc_channel(sAN2);

set_adc_channel, does not take sAN2, it just takes a channel number.
So:

set_adc_channel(2);

sAN2 is defined as 0x04000000, so this will be trying to select channel
67million!.... Not going to work....

Also better generally to always use the clock derived from Fosc, not
Frc. So ADC_CLOCK_DIV_16. This will always give slightly more accurate
results than the RC oscillator.
sandyw



Joined: 30 Jun 2023
Posts: 3

View user's profile Send private message

PostPosted: Fri Jun 30, 2023 8:08 am     Reply with quote

Thanks Ttelmah,

You are a gental man and a scholar.

Problem solved. when pointed out it is obvious.


Sandyw
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jul 01, 2023 11:50 am     Reply with quote

One more small bug. You have two params joined with an OR symbol:
Quote:
setup_adc_ports(sAN2 | VSS_VDD);

But the ccs manual and the .h file show the params separated by a comma.
This is the correct way to do it:
Code:
void setup_adc_ports(int32 pins, int32 reference);

Your method doesn't cause harm because VSS_VDD is defined as 0.
But the correct method is the 2nd one.
sandyw



Joined: 30 Jun 2023
Posts: 3

View user's profile Send private message

Cant read PIC16F15345 ADC - Solved
PostPosted: Mon Jul 03, 2023 2:04 am     Reply with quote

Thanks for this PCMProgrammer. It is wrong on the posted code. A typo on my part due to cut and paste etc. Embarassed
It is right in the main code that I am using but thank you, it made me go and check.

- SandyW
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