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

Simple ADC program doesn't work as expected (SOLVED)

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



Joined: 13 Apr 2011
Posts: 403

View user's profile Send private message

Simple ADC program doesn't work as expected (SOLVED)
PostPosted: Wed Jul 01, 2020 4:51 pm     Reply with quote

I'm trying to run this simple voltage comparator. I'm applying 5V to AN5/RA5 but always seems to measure 0V because I always get Pout high and Tout low.

My code:
Code:
#include <16f18326.h>
#DEVICE ADC=10
#FUSES RSTOSC_HFINTRC,NOEXTOSC,NOCLKOUT,NOWDT
#use delay (clock=8M)
//#use fast_io(ALL)

#define Pout PIN_C4
#define Tout PIN_C3
void main()
{
    unsigned int16 CurrentVolt;
   
    set_tris_a(0b100000);
    set_tris_c(0x00);
    setup_adc(ADC_CLOCK_DIV_8);
    setup_adc_ports(sAN5,VSS_VDD);
    set_adc_channel(sAN5);
   
    output_high(Pout);
    output_high(Tout);
    delay_ms(1500);
   
    while(1)
    {
        output_toggle(PIN_C2);
        CurrentVolt=read_adc();
       
        if(CurrentVolt>400)
        {
            output_high(Pout);
            output_high(Tout);
        }
        else
        {           
            if(CurrentVolt<65)
            {
                output_low(Tout);
                output_high(Pout);
            }
            else
            {
                output_low(Pout);
                output_high(Tout);
            }
        }
    }
}


I tried to manually put a value in CurrentVolt variable like 0x0098 but always fall inside the last "else".
What I'm doing wrong?
_________________
Electric Blue


Last edited by E_Blue on Wed Jul 01, 2020 6:16 pm; edited 1 time in total
temtronic



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

View user's profile Send private message

PostPosted: Wed Jul 01, 2020 5:00 pm     Reply with quote

quick comments...

Is this correct .... Since I don't use that PIC,

#FUSES RSTOSC_HFINTRC ??

Maybe try the 1Hz LED program to confirm the PIC runs properly ??

Jay
E_Blue



Joined: 13 Apr 2011
Posts: 403

View user's profile Send private message

PostPosted: Wed Jul 01, 2020 5:07 pm     Reply with quote

The PIC is running; I'm getting about 17KHz on pin C2.
I get RSTOSC_HFINTRC from 16f18326.h file.
_________________
Electric Blue
dluu13



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

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

PostPosted: Wed Jul 01, 2020 5:37 pm     Reply with quote

Instead of
set_adc_channel(sAN5);

I think it should be
set_adc_channel(5);
E_Blue



Joined: 13 Apr 2011
Posts: 403

View user's profile Send private message

PostPosted: Wed Jul 01, 2020 5:48 pm     Reply with quote

dluu13 wrote:
Instead of
set_adc_channel(sAN5);

I think it should be
set_adc_channel(5);


YES! Now it's alive!
I know that must be something really silly.
Awww! What a relief!
Now I can breath!
_________________
Electric Blue
dluu13



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

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

PostPosted: Wed Jul 01, 2020 5:53 pm     Reply with quote

E_Blue wrote:
dluu13 wrote:
Instead of
set_adc_channel(sAN5);

I think it should be
set_adc_channel(5);


YES! Now it's alive!
I know that must be something really silly.
Awww! What a relief!
Now I can breath!


You're welcome. I also found this system confusing. You have PIN_xx as the I/O pin, sANx for setting up ADC channel, and then x for selecting which ADC channel to measure... Three different numbers to know in order to set up each ADC pin...
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Jul 02, 2020 12:50 am     Reply with quote

Yes, it is difficult though to see how they could do it any other way, since
the values are very different. Even worse on some chips, where the ANx
numbers don't stay in the same sequence as the pins (a couple of the PIC's
that have other things mapped onto the high PortA pins).
One to keep in your 'memory bank'... Smile
temtronic



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

View user's profile Send private message

PostPosted: Thu Jul 02, 2020 4:42 am     Reply with quote

It's just another reason to pick a great PIC and stay with it. It allows you to concentrate on code and not the zillion 'background' details.
How CCS can code a compiler to work for 100s if not 1000s of different PICs has always amazed me.
dluu13



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

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

PostPosted: Thu Jul 02, 2020 5:14 am     Reply with quote

Oh yes, and on one of the PICs that I use, certain of the sAN ports need to be put into a second parameter in setup_adc_ports as opposed to just ORing them together.
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