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

[Solved] ADC PIC16F1823 ..half-work

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



Joined: 23 Sep 2009
Posts: 55
Location: Ukraine

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

[Solved] ADC PIC16F1823 ..half-work
PostPosted: Wed Jul 18, 2018 8:33 am     Reply with quote

An incomprehensible problem arose: the reading of the channel AN3 gives only zero continuously. AN6 reads normally.

It seems to me that the channel does not turn on

Code:

#include <16F1823.h>
#device adc=10
#fuses INTRC_IO,NOWDT,NOPUT,PROTECT,NOIESO,NOFCMEN,WRT,NOSTVREN,NOLVP,NOMCLR,NODEBUG,BORV25
#use delay(clock=8000000)
#use fast_io(a)
#use fast_io(c)
#zero_ram
#use rs232(baud=9600,parity=N,xmit=PIN_C3,bits=8)

//----------------------------------------------//
void main(){
int16 Tmp16;
   setup_oscillator(OSC_8MHZ);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_4);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   set_tris_a(0b011110);
   set_tris_c(0b000100);
   port_a_pullups(0b110);
   port_c_pullups(false);
   setup_adc_ports(sAN3|sAN6|VSS_VDD);
   setup_adc(ADC_CLOCK_DIV_8);
   setup_vref(VREF_ON|VREF_ADC_1v024);
   setup_adc_reference(VSS_VDD);
   

for(;;){

   set_adc_channel(sAN3);
   delay_us(200);
   Tmp16 = read_adc();
   printf("nA = %lu \r",Tmp16);
   set_adc_channel(sAN6);
   delay_us(200);
   Tmp16 = read_adc();
   printf("nB = %lu \r",Tmp16);
   delay_ms(1000);

}
}



----------------
PCWHD v5.078


Last edited by Eddy71ua on Wed Jul 18, 2018 11:27 am; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: ADC PIC16F1823 ..half-work
PostPosted: Wed Jul 18, 2018 9:05 am     Reply with quote

Eddy71ua wrote:

set_adc_channel(sAN3);
delay_us(200);
Tmp16 = read_adc();
printf("nA = %lu \r",Tmp16);
set_adc_channel(sAN6);

That's not the correct way to set the A/D channel.
You must use the actual channel number, such as 3 or 6, as the parameter.
Ttelmah



Joined: 11 Mar 2010
Posts: 19546

View user's profile Send private message

PostPosted: Wed Jul 18, 2018 10:18 am     Reply with quote

a couple of other comments:

setup_wdt(WDT_OFF);

This is a wasted instruction. You have the WDT turned off in the fuses. When it is turned off this way, the setup_wdt instruction cannot control it....

Then you have:

setup_vref(VREF_ON|VREF_ADC_1v024);

Why?. You are using Vss to Vdd as your reference, so all this does is turn the Vref on, and waste power.

As a note, this is actually not a usable setting. The minimum Vref for the ADC is 2.048. From the data sheet:
Quote:

ADC Reference Voltage (REF+) is the selected input, VREF+ pin, VDD pin or the FVR Buffer 1. When the FVR is selected
as the reference input, the FVR Buffer 1 output selection must be 2.048V or 4.096V (ADFVR<1:0> = 1x).


The 1.048 setting is only really there for use with other peripherals.
Eddy71ua



Joined: 23 Sep 2009
Posts: 55
Location: Ukraine

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

Re: ADC PIC16F1823 ..half-work
PostPosted: Wed Jul 18, 2018 11:26 am     Reply with quote

PCM programmer wrote:

That's not the correct way to set the A/D channel.
You must use the actual channel number, such as 3 or 6, as the parameter.


Oops .. You're right, I was wrong.


Quote:
Then you have:

setup_vref(VREF_ON|VREF_ADC_1v024);

Why?.


Next, I measure the battery power.

Smile
Ttelmah



Joined: 11 Mar 2010
Posts: 19546

View user's profile Send private message

PostPosted: Wed Jul 18, 2018 12:13 pm     Reply with quote

You still can't use the 1.024 volt setting. The _minimum_ voltage allowed for the voltage reference on the ADC is 1.8v. As I said the 1.024v setting is for other peripherals that can use the Vref source.
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