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 problem

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



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

ADC problem
PostPosted: Sun Mar 27, 2016 2:08 pm     Reply with quote

Hy,
I try to make reading from pin RB3 and not all result of commands are the same.
Controller is 16F1788.

in main:

Code:
setup_adc(ADC_CLOCK_DIV_8);
setup_adc_ports(ALL_ANALOG);
SETUP_ADC_REFERENCE (VSS_FVR);
setup_vref(VREF_ON|VREF_ADC_4v096);
output_bit(PIN_B3,0);
input(U1);
while(TRUE)
{
...
}


in header:

Code:
#device ADC=16       //10 and 12 are the same for reading in 10 bit ADC and for 12 bit ADC I use 16 (I don't know why?)
#use delay(internal=1MHz)
#define U1     PIN_B3
static unsigned int16 VAL


code testing:

Code:
set_adc_channel(9);
delay_us(100);
VAL=read_adc();
printf(LCD_SEND_C,"%Lu        ",VAL);


set_adc_channel(9);
this line show real value measured in 12bit but,

set_adc_channel(PIN_B3); //not working no error message

set_adc_channel(U1); //not working no error message and have same value as PIN_B3 and value is not true as applied to pin.

I want to use like U1 it help me for debugging in the future.
Can any body help me?
Ttelmah



Joined: 11 Mar 2010
Posts: 19375

View user's profile Send private message

PostPosted: Sun Mar 27, 2016 2:27 pm     Reply with quote

The set_adc_channel command accepts an ADC channel number.

It'll actually accept _any_ number, they just won't be right. PIN_B3, is a #define as a number, so will be accepted, it just will be wrong....

The pin you are reading will be floating, so 'of course' the values will change. Driving the pin, sets TRIS to 0, which disconnects the ADC from the pin. This is necessary, since otherwise you could damage the input (driving the pin high, would overdrive the ADC input if it was on the pin....). To drive a pin, then read it, you have to have something like a capacitor on it, then drive it, wait a while, then set the pin to float, select the adc channel and read. You read from the pin, so from that point TRIS is set to 1, and the pin is floating.

Put a pull down resistor on the pin if you want it to read 0.
nailuy



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

PostPosted: Sun Mar 27, 2016 11:44 pm     Reply with quote

Thank you.
What I'm understand is not so simple to read analog input as I like to do it. "U1"
Have you example code?
Best regards.
Ttelmah



Joined: 11 Mar 2010
Posts: 19375

View user's profile Send private message

PostPosted: Mon Mar 28, 2016 2:37 am     Reply with quote

You already are.

AN9, is the analog channel connected to pin B3.

That's why it works when you use 'set_adc_channel(9)....

If you want to give this a name, just define it. So something like:
Code:

#define ANALOG_ON_A0 (0)
#define ANALOG_ON_A1 (1)
#define ANALOG_ON_A2 (2)
#define ANALOG_ON_A3 (3)
#define ANALOG_ON_A5 (4)
#define ANALOG_ON_B2 (8)
#define ANALOG_ON_B3 (9)
#define ANALOG_ON_B4 (11)
#define ANALOG_ON_B0 (12)
#define ANALOG_ON_B5 (13)

//Then when you want to read from B3:
    set_adc_channel(ANALOG_ON_B3);


Normally however, since the mapping is chip specific, CCS leave it to you to connect the voltages to the pins in the order you want, and then just have an array or define(s) of the pin number(s) versus what you are actually connecting to them.

So if you were using this for a voltage from a current sensor:
Code:

#define CURRENT_INPUT (9)

//then you can read your current with:
    set_adc_channel(CURRENT_INPUT);
nailuy



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

PostPosted: Mon Mar 28, 2016 4:41 am     Reply with quote

Thank you.
I will test later new code, now I can't.

Also I don't know if is problem but now is working normal I think.
#device ADC=16 //10 and 12 are the same for reading in 10 bit ADC and for 12 bit ADC I use 16 (I don't know why is working like this?)
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