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

Problem with multiple ADC channels
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
pandu.ranganadh



Joined: 28 Apr 2008
Posts: 12
Location: hyd

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

Problem with multiple ADC channels
PostPosted: Tue Apr 29, 2008 7:30 am     Reply with quote

Hi,

I am new to CCS compiler and i am using pic demo plus 2 kit of
http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en010072&part=DM163022

I am unable to use multiple ADC channels,
If i can send ADC i/p through channel 0 (A0) then the below code is working, but remaining all channels its not working.


http://www.ccsinfo.com/forum/viewtopic.php?t=32168&start=1


My aim is i want to take ADC i/p's from all channels and selected channel ADC o/p display on the LCD interms of Voltage and current .
Please send me code for me
Matro
Guest







PostPosted: Tue Apr 29, 2008 7:52 am     Reply with quote

Compared to given code, you basically just have to change the "setup_adc_ports(AN0); " in order to connect all the channels you want to the ADC and to perform a "set_adc_channel(x)" to select the channel you want to convert.

Matro
pandu.ranganadh



Joined: 28 Apr 2008
Posts: 12
Location: hyd

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

ADC Problem
PostPosted: Wed Apr 30, 2008 12:52 am     Reply with quote

Hi,

Thanks for giving reply, I have selected other channel like you said.
setup_adc_ports(All_analog); then there is no output, if I can select
setup_adc_ports(All_analog); or setup_adc_ports(A0_analog); and set_adc_channel(0) then only o/p is coming, otherwise if any one of them I can change then there is no o/p.

Please fix this problem. Thanking you,
Matro
Guest







PostPosted: Wed Apr 30, 2008 1:29 am     Reply with quote

You have to add a "set_adc_channel(x)" before converting each x channel.

Matro
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Apr 30, 2008 1:36 am     Reply with quote

Quote:

I want to take ADC i/p's from all channels and selected channel ADC
o/p display on the LCD.

Does your board look exactly like the photo in your link above ?
If so, then you have the older version of the PicDem2-Plus. It uses pins
A1, A2, and A3 for the LCD, and pin A4 is used for switch S2.
pandu.ranganadh



Joined: 28 Apr 2008
Posts: 12
Location: hyd

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

Board detils for solving multiple ADC channel problem
PostPosted: Wed Apr 30, 2008 9:30 am     Reply with quote

Hi thanks for sending reply, Now I am very happy.

Yes, I am using PIC older version board, which is exactly shown in link available in

http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en010072&part=DM163022 .

if i can use as "setup_adc_ports(AN0); " then its giving error, its not accepting AN0, insted of AN0 i have given "all_analog or a0_analog" for channel-0, and "set_adc_channel(0)" then its working. But my job is i need to take multiple i/p's and process selected channel i/p only.

I have an other problem, actually my requirement is i need to send 0-30V as analog i/p's, but our board ADC bares up to 5V, so how to solve this problem, is there any alternative method available ?

Please fix this problem.
Thanking you,
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu May 01, 2008 10:33 am     Reply with quote

Quote:
If I use as "setup_adc_ports(AN0); " then its giving error.

1. Post the PIC that you're using.

2. Post your compiler version.
adamsya
Guest







is it like this?
PostPosted: Mon May 12, 2008 11:18 pm     Reply with quote

is it ok if i write the coding lik this for multiple ADC reading? it can be compile... but i dont test it on hardware or simulation yet...

Code:


#include <16F877A.H>
#device adc=10
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=2000000)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#include "lcd.c"

//============================
void main()
{
int16 adc_value0,adc_value1,adc_value2,adc_value3,adc_value4,adc_value5,adc_value6,adc_value7;
float volts;

lcd_init();

setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_DIV_8);

set_adc_channel(0); //a0
set_adc_channel(1); //a1
set_adc_channel(2); //a2
set_adc_channel(3); //a3
set_adc_channel(4); //a5
set_adc_channel(5); //e0
set_adc_channel(6); //e1
set_adc_channel(7); //e2


delay_us(20);

while(1)
  {

   adc_value0 = read_adc(0); //a0
   adc_value1 = read_adc(1); //a1
   adc_value2 = read_adc(2); //a2
   adc_value3 = read_adc(3); //a3
   adc_value4 = read_adc(4); //a5
   adc_value5 = read_adc(5); //e0
   adc_value6 = read_adc(6); //e1
   adc_value7 = read_adc(7); //e2


   volts = (float)(adc_value0 * 5)/1023.0;
   printf(lcd_putc, "\f%3.2f", volts);
   delay_ms(500);
  }
}

PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 12, 2008 11:46 pm     Reply with quote

Quote:
is it ok if i write the coding like this for multiple ADC reading

No.

Do it like this:

1. Set the desired channel.
2. Wait for 20 us.
3. Read the ADC.
4. Do the calculations.
5. Display the result.

The same steps are required for each channel.
Do it in a for(;;) loop.


Quote:
#use delay(clock=2000000)

Your clock is set for 2 MHz. That is probably not correct.

Quote:
setup_adc(ADC_CLOCK_DIV_8);

If your clock is 20 MHz, then the divisor should be 32, not 8.
arawana



Joined: 06 Apr 2008
Posts: 19
Location: SINGAPORE

View user's profile Send private message

PostPosted: Tue May 13, 2008 3:13 am     Reply with quote

i had change some.. but it is correct how i define set_adc_channel(0) to set_adc_channel(7)?

i want to use all the ADC channel at same time.. if there is a change or not at any ADC pins.. i want it to display at printf... it is correct?

iam using PIC16F877A, 20MHz and PCWH v3.40...

iam not very good in programing.. iam glad if anyone can help me~ thanks...


Code:


#include <16F877A.H>
#device adc=10
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=20000000) //20MHz
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)



void main()
{
int16 adc_value0,adc_value1,adc_value2,adc_value3,adc_value4,adc_value5,adc_value6,adc_value7;
float volt0,volt1,volt2,volt3,volt4,volt5,volt6,volt7;
int i;


setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_DIV_32);

set_adc_channel(0); //a0
set_adc_channel(1); //a1
set_adc_channel(2); //a2
set_adc_channel(3); //a3
set_adc_channel(4); //a5
set_adc_channel(5); //e0
set_adc_channel(6); //e1
set_adc_channel(7); //e2


delay_us(20);

while(1)
  {

for(i=0;i<100;i++)
   {
   adc_value0 = read_adc(0); //a0
   adc_value1 = read_adc(1); //a1
   adc_value2 = read_adc(2); //a2
   adc_value3 = read_adc(3); //a3
   adc_value4 = read_adc(4); //a5
   adc_value5 = read_adc(5); //e0
   adc_value6 = read_adc(6); //e1
   adc_value7 = read_adc(7); //e2


   volt0 = (float)(adc_value0 * 5)/1023.0;
   volt1 = (float)(adc_value1 * 5)/1023.0;
   volt2 = (float)(adc_value2 * 5)/1023.0;
   volt3 = (float)(adc_value3 * 5)/1023.0;
   volt4 = (float)(adc_value4 * 5)/1023.0;
   volt5 = (float)(adc_value5 * 5)/1023.0;
   volt6 = (float)(adc_value6 * 5)/1023.0;
   volt7 = (float)(adc_value7 * 5)/1023.0;
   
   
   printf("\f\n Volt0: %3.2f",volt0);
   printf("\f\n Volt1: %3.2f",volt1);
   printf("\f\n Volt2: %3.2f",volt2);
   printf("\f\n Volt3: %3.2f",volt3);
   printf("\f\n Volt4: %3.2f",volt4);
   printf("\f\n Volt5: %3.2f",volt5);
   printf("\f\n Volt6: %3.2f",volt6);
   printf("\f\n Volt7: %3.2f",volt7);
   delay_ms(500);
   } 
  }
}

arawana



Joined: 06 Apr 2008
Posts: 19
Location: SINGAPORE

View user's profile Send private message

PostPosted: Tue May 13, 2008 3:50 am     Reply with quote

what if i write like this? is it correct?

Code:


#include <16F877A.H>
#device adc=10
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=20000000) //20MHz
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)



void main()
{

int16 adc_value0,adc_value1,adc_value2,adc_value3,adc_value4,adc_value5,adc_value6,adc_value7;
float volt0,volt1,volt2,volt3,volt4,volt5,volt6,volt7;
int i;


setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_DIV_32);


while(1)
  {

for(i=0;i<100;i++)
   {
   
   set_adc_channel(0); //set to read channel a0 next
   delay_us(20);
   adc_value0 = read_adc(); //get 10 bit readig for channel 0
   volt0 = (float)(adc_value0 * 5)/1023.0;
   printf("\f\n Volt0: %3.2f",volt0);
   delay_ms(500);
   
   
   set_adc_channel(1); //set to read channel a1 next
   delay_us(20);
   adc_value1 = read_adc(); //get 10 bit readig for channel 1
   volt1 = (float)(adc_value1 * 5)/1023.0;
   printf("\f\n Volt1: %3.2f",volt1);
   delay_ms(500);


   set_adc_channel(2); //set to read channel a2 next
   delay_us(20);
   adc_value2 = read_adc(); //get 10 bit readig for channel 2
   volt2 = (float)(adc_value2 * 5)/1023.0;
   printf("\f\n Volt2: %3.2f",volt2);
   delay_ms(500);


   set_adc_channel(3); //set to read channel a3 next
   delay_us(20);
   adc_value3 = read_adc(); //get 10 bit readig for channel 3
   volt3 = (float)(adc_value3 * 5)/1023.0;
   printf("\f\n Volt3: %3.2f",volt3);
   delay_ms(500);


   set_adc_channel(4); //set to read channel a5 next
   delay_us(20);
   adc_value4 = read_adc(); //get 10 bit readig for channel 4
   volt4 = (float)(adc_value4 * 5)/1023.0;
   printf("\f\n Volt4: %3.2f",volt4);
   delay_ms(500);


   set_adc_channel(5); //set to read channel e0 next
   delay_us(20);
   adc_value5 = read_adc(); //get 10 bit readig for channel 5
   volt5 = (float)(adc_value5 * 5)/1023.0;
   printf("\f\n Volt5: %3.2f",volt5);
   delay_ms(500);


   set_adc_channel(6); //set to read channel e1 next
   delay_us(20);
   adc_value6 = read_adc(); //get 10 bit readig for channel 6
   volt6 = (float)(adc_value6 * 5)/1023.0;
   printf("\f\n Volt6: %3.2f",volt6);
   delay_ms(500);   
   
   
   set_adc_channel(7); //set to read channel e2 next
   delay_us(20);
   adc_value7 = read_adc(); ///get 10 bit readig for channel 7
   volt7 = (float)(adc_value7 * 5)/1023.0;
   printf("\f\n Volt7: %3.2f",volt7);
   delay_ms(500);
   
   
   }
  }
}


PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 13, 2008 11:13 am     Reply with quote

That will sort of work, but you don't know why. You also don't
know why it's not the best way.

You need to spend a few weeks studying the C language and
programming. The purpose of this forum is not to teach you
how to write a program. You should take a class to do that.
pandu.ranganadh



Joined: 28 Apr 2008
Posts: 12
Location: hyd

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

ADC Problem
PostPosted: Wed May 14, 2008 2:09 am     Reply with quote

Hi,

I have tested the above code, but its not working for other than the channel-0.

Except channel-0, remaining channels are not working.
There is no display on LCD.

I think "setup_adc_channel() " function is not pointing to remaining channels ?

Sorry if any wrong, its just my doubt. Please anybody check my problem.
Bye
Thanking you all.
rayleight
Guest







PostPosted: Tue Dec 15, 2009 10:51 am     Reply with quote

hye..
can we read adc from 2 channel at the same time Question Confused
i'm using 16f877a..
dyeatman



Joined: 06 Sep 2003
Posts: 1933
Location: Norman, OK

View user's profile Send private message

PostPosted: Tue Dec 15, 2009 11:26 am     Reply with quote

No.

If you look at the PIC16F877a datasheet on page 129 you will see that is not
possible. There is only ONE A/D module that is switched between inputs.
That's why set_adc_channel() is required.

You really need to read the datasheet to understand how the PIC functions.
_________________
Google and Forum Search are some of your best tools!!!!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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