View previous topic :: View next topic |
Author |
Message |
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
Simple ADC program doesn't work as expected (SOLVED) |
Posted: Wed Jul 01, 2020 4:51 pm |
|
|
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: 9244 Location: Greensville,Ontario
|
|
Posted: Wed Jul 01, 2020 5:00 pm |
|
|
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: 417
|
|
Posted: Wed Jul 01, 2020 5:07 pm |
|
|
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
|
|
Posted: Wed Jul 01, 2020 5:37 pm |
|
|
Instead of
set_adc_channel(sAN5);
I think it should be
set_adc_channel(5); |
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Wed Jul 01, 2020 5:48 pm |
|
|
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
|
|
Posted: Wed Jul 01, 2020 5:53 pm |
|
|
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: 19545
|
|
Posted: Thu Jul 02, 2020 12:50 am |
|
|
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'... |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9244 Location: Greensville,Ontario
|
|
Posted: Thu Jul 02, 2020 4:42 am |
|
|
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
|
|
Posted: Thu Jul 02, 2020 5:14 am |
|
|
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. |
|
|
|