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 support@ccsinfo.com

ADC trouble

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



Joined: 16 Jan 2019
Posts: 8

View user's profile Send private message

ADC trouble
PostPosted: Tue Jan 29, 2019 12:07 pm     Reply with quote

Hi all, and a preemptive thanks.

I'm new to ADC, and trying to use it on a PIC16F18345. The trouble seems to be that the pin I'm trying to use for ADC goes into a permanent pull-down state. Any voltage applied just gets sunk to near 0V and the ADC value doesn't change. But that pin works fine when I use it as a standard input. So the error seems to be me. Did try setup_adc_ports(sAN4, VSS_VDD), but that didn't help. Suggestions? Condemnations? Aspersions?

Code:

#include <16F18345.h>

#fuses PUT,RSTOSC_HFINTRC_PLL,BROWNOUT,NOMCLR,NOCLKOUT,NOEXTOSC,PROTECT
#use delay(internal=4000000)
 
#use RS232(baud=9600, xmit=PIN_A0, ERRORS)
 
#define LCD_INIT       22 
#define LCD_CLR_DSP    12 
#define TX PIN_A0

void main()
{   
    set_tris_a(00111010);
    set_tris_b(00000000);
    set_tris_c(00000000);
   
    int16 r;
   
    setup_adc_ports(sAN4);     
    setup_adc(ADC_CLOCK_DIV_8);       
    set_adc_channel(0);
       
   
   output_high(TX); // LCD handling stuff
   delay_ms(100);   // "" 
   putc(LCD_INIT);  // ""
   putc(LCD_CLR_DSP);  // ""
   delay_ms(5); // ""
     
   while(TRUE)
     {
      r =read_adc();         
      printf("AD is  %4LD \r\n", r);
      delay_ms(1000);   
     }
 }
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jan 29, 2019 12:32 pm     Reply with quote

Quote:
setup_adc_ports(sAN4);
setup_adc(ADC_CLOCK_DIV_8);
set_adc_channel(0);

You've set it up to use ch. 4 in the 1st line, but then you select channel 0.
You should select channel 4 in the 3rd line.
Drone601



Joined: 16 Jan 2019
Posts: 8

View user's profile Send private message

PostPosted: Tue Jan 29, 2019 2:15 pm     Reply with quote

Ah, right. Embarassed I had changed that in panicked random button pushing. Thanks!

And yes, that did help greatly. But...still have a problem with the pin pulling down the voltage too much. As soon as I connect to that pin...vooop. It's gone. I'd think I'd fried the port, but it works (or at least did last I checked) in non-ACD mode.
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Tue Jan 29, 2019 2:43 pm     Reply with quote

What is the nature of the source feeding the ADC?.
The maximum recommended impedance for any source is 10KR.
Drone601



Joined: 16 Jan 2019
Posts: 8

View user's profile Send private message

PostPosted: Tue Jan 29, 2019 3:05 pm     Reply with quote

It's a phototransistor. I *seem* to be connecting it as recommended by the manufacturer and some examples online. Surely it's just me being a dolt, trying to hammer it in with a rock or something.
newguy



Joined: 24 Jun 2004
Posts: 1900

View user's profile Send private message

PostPosted: Tue Jan 29, 2019 3:11 pm     Reply with quote

Can you post a schematic please?
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Tue Jan 29, 2019 3:20 pm     Reply with quote

Ttelmah makes a good point. You can use a opamp buffer to ensure that you have a nice low impedance output to drive your ADC pin. If you are expecting voltages close to zero, make sure you get a negative power rail for your opamp, or use one with rail to rail output.
temtronic



Joined: 01 Jul 2010
Posts: 9097
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Jan 29, 2019 3:26 pm     Reply with quote

Most (all) photoransistors are a very nonlinear device (actually log ) so you'll need to calibrate the PIC program for each device. Mfrs post the graphs with the device info. As stated using a in/out rail-to-rail opamp will help.

Jay
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