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

Simply trying to read a analog signal

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



Joined: 15 Dec 2011
Posts: 20

View user's profile Send private message

Simply trying to read a analog signal
PostPosted: Thu Dec 22, 2011 8:51 am     Reply with quote

Hallo again.

Yesterday I had a serial question and today battling with a simple analog reading question.

Wiring was done correctly I think, I'm using a Pic16F690 and would like to read one analog signal from port_a Pin_An2 (Pin17 on the pic).

But battling to find some code that will only assign the one pin of Analog the rest of the A port are used for the PICKIT2 2 programmer and would like to keep it as is.

Yesterday everybody told me I must get rid of the TRIS function and let the compiler handle the pin assignments.

This is what I understand from the Analog, please let me know where I'm going wrong.

Using the test code from the library but would like to only assign one PIN. Code as follow:

void main() {

int i, value, min, max;

printf("Sampling:");

setup_port_a( ALL_ANALOG ); -(Done want to assign the entire channel
setup_adc( ADC_CLOCK_INTERNAL );
set_adc_channel( 0 );

do {
min=255;
max=0;
for(i=0; i<=30; ++i) {
delay_ms(100);
value = Read_ADC();
if(value<min)
min=value;
if(value>max)
max=value;
}
printf("\n\rMin: %2X Max: %2X\n\r",min,max);

} while (TRUE);
temtronic



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

View user's profile Send private message

PostPosted: Thu Dec 22, 2011 9:16 am     Reply with quote

It's all in the details !! You've got to read the ADC section of the data sheet for your PIC.
According to the ds, using the internal RC clock is ONLY to be used during 'sleep' for some combinations of processor speed, VDD,etc.
As for selecting only one ADC port,read the header for your PIC for info about 'legal' combinations of pin usage.
I don't use the PICKIT2 so cannot comment on what pins it uses .
Labjac



Joined: 15 Dec 2011
Posts: 20

View user's profile Send private message

PostPosted: Thu Dec 22, 2011 10:14 am     Reply with quote

Hi

Problem wasn't what pin to use, I knew I wanted to use Pin17 and the was An02, but the issue was I didn't know how to assign only one Pin (Meaning pin17).

But this worked not sure if it's correct but maybe you can comment on the correct wording?

Code below:
Code:

#include <16F690.h>
#fuses intrc,nowdt,noprotect,put
#use delay(clock=8M)
#use rs232(baud=9600,xmit=PIN_B7,rcv=PIN_B5,bits=8,ERRORS)

#define In1 PIN_C0
#define Op1 PIN_C1

void main()
{
int value;

set_adc_channel(2);
setup_adc( ADC_CLOCK_INTERNAL );
   
   while(1)
   {

      If (!Input(In1) == 1 )
      {
       //delay_ms(500);
       Printf("Die value is");
        delay_ms(20);
       output_high(Op1);
       delay_ms(value);
       output_low(Op1);
       delay_ms(value);
       value = Read_ADC();
       delay_ms(10);   

      }
   }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Thu Dec 22, 2011 10:58 am     Reply with quote

Not really.....

The key point is to read the data sheet.
Then look at the .h file for the chip, where there are the list of values for ADC pin selection, which correspond exactly to the values in the data sheet.

You need to select an ADC channel. Not selecting any, is the same as 'ALL_ANALOGS'. It'll work because when you drive a pin as output, it overrides the ADC setting, _but_ will result in digital noise from the output being coupled into the ADC multiplexer, extra power being drawn, and extra noise.
It is a bit like the joke about the lady who (on a car with a carburetor), pulls the choke knob out and hangs her [spam] on it. The car will work, but not right...

Look at the line starting #define sAN2 in the .h file.

Separately, there is the issue of clock selection. Though it won't stop the ADC from working, it makes accuracy less good if the internal RC clock is selected. This is a line from the data sheet:

"When the device frequency is greater than 1 MHz, the FRC clock source is only recommended if the conversion will be performed during Sleep.".

Now you are not performing your conversion during sleep, and your clock is faster than 1Mhz. So, select the right clock - look at table 9-1 in the data sheet. Look for the values in white squares, without a comment mark against them. Then again look at the .h file, and select the setting that gives this division.

You need to become totally 'picky' about detail, and the data sheet must be your first port of call for everything.

Best Wishes
Labjac



Joined: 15 Dec 2011
Posts: 20

View user's profile Send private message

PostPosted: Thu Dec 22, 2011 11:55 am     Reply with quote

Hi Ttelmah

Thanks for the reply, as I mentioned in my first post, I'm completly new in using PIC's. (Use to PLC programming), and still battling to understand the basics, eg. datasheets I can scroll through the .h files and see writing but what is all means is still to be tested.

Maybe a silly question, but you are talking about the #define sAN2 in the .h file, can you give me an example of the way you would done the definition of the program I pasted before please.

I manage to at least get a line to display on serial with the value changing in Hyperterminal when I turn the POT. Very Happy

But would like to learn the correct way from the start.

Thanks for your help.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Dec 22, 2011 12:37 pm     Reply with quote

This thread discusses using the A/D on the 16F690:
http://www.ccsinfo.com/forum/viewtopic.php?t=46559
Labjac



Joined: 15 Dec 2011
Posts: 20

View user's profile Send private message

PostPosted: Thu Dec 22, 2011 1:30 pm     Reply with quote

Thanks for the guidelines.

The code is done as follow and think this is correct.
Code:

#include <16F690.h>
#fuses intrc,nowdt,noprotect,put
#use delay(clock=8M)
#use rs232(baud=9600,xmit=PIN_B7,rcv=PIN_B5,bits=8,ERRORS)

#define In1 PIN_C0
#define Op1 PIN_C1

void main()
{
int value;

   setup_port_a(sAN2);
   setup_adc( ADC_CLOCK_DIV_16 );
 
   
   while(1)
   {

      If (!Input(In1) == 1 )
      {
       Printf("Die value is %u\n\r",value);      
        delay_ms(20);
       output_high(Op1);
       delay_ms(value);
       output_low(Op1);
       delay_ms(value);
       set_adc_channel(2);
         delay_ms(10);
       value = Read_ADC();
       delay_ms(10);   

      }
   }
}
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Thu Dec 22, 2011 1:35 pm     Reply with quote

One minor issue I see - the first time through, "value" has not been assigned a numeric value - only declared so odd things may happen :-) (I don't remember off hand just what delay_ms() does if the number you pass it is 0 either.

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
Labjac



Joined: 15 Dec 2011
Posts: 20

View user's profile Send private message

PostPosted: Fri Dec 23, 2011 6:37 am     Reply with quote

Thanks

Busy changing it... And so we learn from the clever people.

THanks again for all the help...
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