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

ADC not working on simple 16F1705 program

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



Joined: 14 Feb 2016
Posts: 24

View user's profile Send private message

ADC not working on simple 16F1705 program
PostPosted: Tue Feb 14, 2017 8:33 am     Reply with quote

Hello,

I use 16f1705 on a large project, and i have strange behaviour from it's ADC. Returned value is always 1014(dec).
If i use ADC_CLOCK_DIV_2 instead of ADC_CLOCK_DIV_64, value is random (358, 851, ...). It is normal, due to bad sampling time. It seems working.
If i use TEMPERATURE_INDICATOR, value seems realistic.
But in normal configuration, reading AN3 returns 1014.
However i successfully used ADC on many pics and projects ...

Then, i try this simple program, with same hardware:
Code:

#include <16F1705.h>
#DEVICE ADC=10
#fuses INTRC_IO \
   ,NOWDT /* Disable WDT at start */ \
   ,PUT /* Enable power up timer. The Power-up Timer provides a nominal 64 ms time-out on POR or Brown-out Reset. The device is held in Reset as long as PWRT is active. The PWRT delay allows additional time for the VDD to rise to an acceptable level.*/ \
   ,NOMCLR,NOPROTECT \
   ,BROWNOUT_NOSL,BORV19 /* Brownout enabled during operation, disabled during SLEEP */ \
   ,NOCLKOUT,NOIESO,NOFCMEN, \
   ,NOWRT /* Program memory not write protected */ \
   ,NOPPS1WAY \
   ,ZCDDIS /* Zero-cross detect circuit is disabled at POR */ \
   ,NOPLLEN \
   ,NOSTVREN /* Stack full/underflow will cause reset */ \
   ,LPBOR /* Low-Power Brownout reset is enabled */ \
   ,NODEBUG,NOLVP
#use delay(clock=32MHZ)
#USE fast_io(A)
#USE fast_io(C)
// --------------------------------------------------------------------------------------
#define   IO_LEDR PIN_C2
#define   IO_LEDG PIN_C1
// --------------------------------------------------------------------------------------
#define TRIS_A    (0b00011111)
#define PULLUPS_A (0b00000000)
#define TRIS_C    (0b00011000)
#define PULLUPS_C (0b00000000)
// =========================================================================================================================================================
void main(void)
{
set_tris_a(TRIS_A);
set_tris_c(TRIS_C);
port_a_pullups(PULLUPS_A);
port_c_pullups(PULLUPS_C);

output_bit(PIN_C4,false);
output_bit(PIN_C5,false);
output_bit(IO_LEDR,true);
output_bit(IO_LEDG,true);

disable_interrupts(GLOBAL);

setup_adc_ports(sAN3,VSS_VDD); // sAN3 is RA4, pin 3
set_adc_channel(sAN3);
setup_adc(ADC_CLOCK_DIV_64);

while(true)
   {
   unsigned int16 tmp=read_adc();
   // if(0) -> then red, tested
   // if(1) -> then green, tested
   if(tmp>((unsigned int16)0))
      {
      output_bit(IO_LEDG,false);
      output_bit(IO_LEDR,true);
      }
   else
      {
      output_bit(IO_LEDR,false);
      output_bit(IO_LEDG,true);
      }
   }
}   


If apply 0V on pin 3 (read 11mv on voltmeter), led is red.
If apply 1.18V on pin 3 (read 1.181V on voltmeter), led is red.
With this simple program, read_adc return 0 every times.
I check with a brand new PIC on breadboard : same result.

Working voltage is 3.3V. CCS 5.043

I can't see why. I'm going absolutely crazy.
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Tue Feb 14, 2017 8:51 am     Reply with quote

set_adc_channel(sAN3);

Wrong. You don't use the define here, just the channel number.

set_adc_channel(3);

You are enabling sAN3, then trying to select ADC channel 32!....
(sAN3, is a 'bit mask', to enable the specific channel - happens to be 32)

So at present the input is not connected to anything. That you get apparently some result with clock/2, is just 'random', since the clock is then so fast that the ADC doesn't actually work....
gaugeguy



Joined: 05 Apr 2011
Posts: 303

View user's profile Send private message

PostPosted: Tue Feb 14, 2017 8:54 am     Reply with quote

Your test logic also appears flawed.
11mV with a 3.3V supply and 10 bit A/D is about 3.
1.18V is about 366.
Both of these are greater than 0 so the first test is true and IO_LEDG is false and IO_LEDR is true.
tssir



Joined: 14 Feb 2016
Posts: 24

View user's profile Send private message

PostPosted: Tue Feb 14, 2017 10:21 am     Reply with quote

YES !!!!
Thank you very much.
I replaced a macro (define) by sAN3 for my test. It was wrong.

This small program works with :
set_adc_channel(3);

Now i can compare and search why my main program not working (other problem in my code, my business ... ).

Thank you, again !
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