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

To ThomasB, re A/D Conversion

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



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

To ThomasB, re A/D Conversion
PostPosted: Wed Apr 09, 2003 3:30 pm     Reply with quote

This board does not allow replies to your original post.
Did you accidently click the box for "Disallow replies" ?
Anyway, here is my reply:

See the README.TXT file for compiler versions 3.148 and later.
It explains new parameters for the read_adc() function, which
allow you to start an A/D conversion and read the result later.
___________________________
This message was ported from CCS's old forum
Original Post ID: 13561
sar



Joined: 08 Sep 2003
Posts: 36

View user's profile Send private message

Re: To ThomasB, re A/D Conversion
PostPosted: Wed Apr 09, 2003 7:39 pm     Reply with quote

ThomasB try this revised for Pic16F877 from: <a href="http://www.vermontficks.org/pic.htm" TARGET="_blank">http://www.vermontficks.org/pic.htm</a>


/****************************************************************************

Reads 0-5V at pin 2 every 10mS, and writes the 10-sample averaged result
to a 9600 baud terminal screen.

***************************************************************************/

#include <16F877.h>
#define 16F877 *=16 ADC = 10
#fuses XT, NOPROTECT, NOPUT, NOWDT, NOLVP

#use delay (clock=4000000)
#use standard_io ( a )
#use standard_io ( b )
#use standard_io ( c )
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, BITS=8 )

float fx;
static char cADCflag; /* global variable */

void main( void )
{
char cCnt; /* 8-bit variables */
long cADCtotal, cADCval; /* 16-bit variables */

setup_adc_ports ( RA0_ANALOG ); /* these three statements set up the ADC */
setup_adc ( ADC_CLOCK_INTERNAL );
set_adc_channel ( 0 );

/* SET THE COUNTER TO ROLL EVERY 10mS */
setup_counters ( RTCC_INTERNAL, RTCC_DIV_64 ); /* timer increments every 64uS */
enable_interrupts ( INT_RTCC ); /* enable timer interrupt */
enable_interrupts ( GLOBAL ); /* enable global interrupts */
set_rtcc ( 100 ); /* start counter at 256-100 = 156 counts until wrap */

cADCtotal = 0;
cCnt = 0; /* count the number of ADC measurements */

while ( 1 ) /* do forever */
{
if ( cADCflag == 1 ) /* if interrupt turned flag on */
{
cADCflag = 0; /* turn flag off */

cADCval = read_adc(); /* read the ADC */
cADCtotal += cADCval; /* add it to total, for later averaging */

if ( cCnt++ == 10 ) /* increment count, is it ten counts yet? */
{
disable_interrupts ( INT_RTCC ); /* don't allow interrupts during display */
cCnt = 0; /* restart count */
cADCtotal /= 10; /* divide by 10 samples to get average */

// 0 to 5V input at pin 2 will display 0 - 1023.
// If you want to scale it to read 0.00 to 5.00 volts, the variable
// must be type FLOAT, such as fX in the three lines below...
//
fX = read_adc(); /* read the ADC here */
fX = fX / 5.00/10; /* scale it to 5V */
printf ( "\%1.2f\n\r volts ", fX); /* print it to the screen */
delay_ms (500); //sends to RS232 1/2 per second

//printf ( "\n\rMeasurement = \%f", fx ); /* send to RS232 */
cADCtotal = 0; /* restart total */
enable_interrupts ( INT_RTCC ); /* allow interrupts again */
}
}
}
}

/*****************************************************************************************************/
#INT_RTCC
void TIMER_INTERRUPT ( void )
{
/*
This interrupt occurs every 10mS (timer increments every 64uS, timer is
preset to 100 so it wraps 156 counts later at 256.) Adjust the restart
count to get the 10mS exactly.
*/

//OUTPUT_HIGH ( PIN_B1 ); /* test pulse */
//OUTPUT_LOW ( PIN_B1 );
set_rtcc ( 100 ); /* restart count */
cADCflag = 1; /* set flag to alert main program to read ADC */
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 13566
ThomasB
Guest







Re: To ThomasB, re A/D Conversion
PostPosted: Thu Apr 10, 2003 1:24 am     Reply with quote

<font face="Courier New" size=-1>Thank you for your answers but I have the problem that the 10 bits A/D conversion and writing the result to an external RAM should be done within of 30 µs. The A/D converter itselves nearly needs this time for a correct conversion. The second Problem is that this should be done 4000 times after each other so I think I can’t save so much data on the PIC directly.
Does anybody have solution for this problem?</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 13570
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

Re: To ThomasB, re A/D Conversion
PostPosted: Thu Apr 10, 2003 8:22 am     Reply with quote

:=<font face="Courier New" size=-1>Thank you for your answers but I have the problem that the 10 bits A/D conversion and writing the result to an external RAM should be done within of 30 µs. The A/D converter itselves nearly needs this time for a correct conversion. The second Problem is that this should be done 4000 times after each other so I think I can’t save so much data on the PIC directly.
:=Does anybody have solution for this problem?</font>

This was discused throughly in an earlier thread in this forum. What you want to do is posiable if you can write to your ram fast enough.
___________________________
This message was ported from CCS's old forum
Original Post ID: 13577
ThomasB
Guest







Re: To ThomasB, re A/D Conversion
PostPosted: Thu Apr 10, 2003 9:39 am     Reply with quote

:=:=<font face="Courier New" size=-1>Thank you for your answers but I have the problem that the 10 bits A/D conversion and writing the result to an external RAM should be done within of 30 µs. The A/D converter itselves nearly needs this time for a correct conversion. The second Problem is that this should be done 4000 times after each other so I think I can’t save so much data on the PIC directly.
:=:=Does anybody have solution for this problem?</font>
:=
:=This was discused throughly in an earlier thread in this forum. What you want to do is posiable if you can write to your ram fast enough.

Thank you for your replay. Can you remember when this was? I can't find it.
Thanks Thomas
___________________________
This message was ported from CCS's old forum
Original Post ID: 13582
R.J.Hamlett
Guest







Re: To ThomasB, re A/D Conversion
PostPosted: Thu Apr 10, 2003 3:14 pm     Reply with quote

:=<font face="Courier New" size=-1>Thank you for your answers but I have the problem that the 10 bits A/D conversion and writing the result to an external RAM should be done within of 30 µs. The A/D converter itselves nearly needs this time for a correct conversion. The second Problem is that this should be done 4000 times after each other so I think I can’t save so much data on the PIC directly.
:=Does anybody have solution for this problem?</font>
One solution, is to code the read adc yourself. Then you can trigger the AD, send the _last_ value to the memory, poll the flag to see if the conversion has completed, and when it has, read the value, then loop back to the start.
I use this approach where I need fast sequential AD reads.Then with suitable defintions for these bits/bytes (depends on which processor you are using), you can do the loop as:

for(count=0;count ADON=1;
ADGO=1;
store_to_RAM(result,count);
while(ADGO);
result=make16(adresh,adresl);
}

Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 13595
ThomasB
Guest







Re: To ThomasB, re A/D Conversion
PostPosted: Fri Apr 11, 2003 2:46 am     Reply with quote

:=:=<font face="Courier New" size=-1>Thank you for your answers but I have the problem that the 10 bits A/D conversion and writing the result to an external RAM should be done within of 30 µs. The A/D converter itselves nearly needs this time for a correct conversion. The second Problem is that this should be done 4000 times after each other so I think I can’t save so much data on the PIC directly.
:=:=Does anybody have solution for this problem?</font>
:=One solution, is to code the read adc yourself. Then you can trigger the AD, send the _last_ value to the memory, poll the flag to see if the conversion has completed, and when it has, read the value, then loop back to the start.
:=I use this approach where I need fast sequential AD reads.Then with suitable defintions for these bits/bytes (depends on which processor you are using), you can do the loop as:
:=
:=for(count=0;count := ADON=1;
:= ADGO=1;
:= store_to_RAM(result,count);
:= while(ADGO);
:= result=make16(adresh,adresl);
:=}
:=
:=Best Wishes

Thanks a lot
Your idea seems to be good. But how can I implement ADON and ADGO and also adresh and adresl to C code? I have to say that my knowledge about assembler programming is not very good.
___________________________
This message was ported from CCS's old forum
Original Post ID: 13601
R.J.Hamlett
Guest







Re: To ThomasB, re A/D Conversion
PostPosted: Fri Apr 11, 2003 4:25 am     Reply with quote

:=:=:=<font face="Courier New" size=-1>Thank you for your answers but I have the problem that the 10 bits A/D conversion and writing the result to an external RAM should be done within of 30 µs. The A/D converter itselves nearly needs this time for a correct conversion. The second Problem is that this should be done 4000 times after each other so I think I can’t save so much data on the PIC directly.
:=:=:=Does anybody have solution for this problem?</font>
:=:=One solution, is to code the read adc yourself. Then you can trigger the AD, send the _last_ value to the memory, poll the flag to see if the conversion has completed, and when it has, read the value, then loop back to the start.
:=:=I use this approach where I need fast sequential AD reads.Then with suitable defintions for these bits/bytes (depends on which processor you are using), you can do the loop as:
:=:=
:=:=for(count=0;count :=:= ADON=1;
:=:= ADGO=1;
:=:= store_to_RAM(result,count);
:=:= while(ADGO);
:=:= result=make16(adresh,adresl);
:=:=}
:=:=
:=:=Best Wishes
:=
:=Thanks a lot
:=Your idea seems to be good. But how can I implement ADON and ADGO and also adresh and adresl to C code? I have to say that my knowledge about assembler programming is not very good.

No assembler needed.
For a 16F877 (you will have to set the actual addresses to suit whatever chip you want), have the following:

#byte adresl=0x9e
#byte adresh=0x9f
#bit ADON=0x1f.0
#bit ADGO=0x1f.2

The ability to create direct register access variables for both bytes, and bits, is fundamental to this C, and allows you to code 95\% of the stuff that would normally need aseembler, using the C code. :-)

With a suitable 'store_to_RAM' function, this should give what you want.

Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 13602
ThomasB
Guest







Re: To ThomasB, re A/D Conversion
PostPosted: Fri Apr 11, 2003 6:14 am     Reply with quote

:=:=:=:=<font face="Courier New" size=-1>Thank you for your answers but I have the problem that the 10 bits A/D conversion and writing the result to an external RAM should be done within of 30 µs. The A/D converter itselves nearly needs this time for a correct conversion. The second Problem is that this should be done 4000 times after each other so I think I can’t save so much data on the PIC directly.
:=:=:=:=Does anybody have solution for this problem?</font>
:=:=:=One solution, is to code the read adc yourself. Then you can trigger the AD, send the _last_ value to the memory, poll the flag to see if the conversion has completed, and when it has, read the value, then loop back to the start.
:=:=:=I use this approach where I need fast sequential AD reads.Then with suitable defintions for these bits/bytes (depends on which processor you are using), you can do the loop as:
:=:=:=
:=:=:=for(count=0;count :=:=:= ADON=1;
:=:=:= ADGO=1;
:=:=:= store_to_RAM(result,count);
:=:=:= while(ADGO);
:=:=:= result=make16(adresh,adresl);
:=:=:=}
:=:=:=
:=:=:=Best Wishes
:=:=
:=:=Thanks a lot
:=:=Your idea seems to be good. But how can I implement ADON and ADGO and also adresh and adresl to C code? I have to say that my knowledge about assembler programming is not very good.
:=
:=No assembler needed.
:=For a 16F877 (you will have to set the actual addresses to suit whatever chip you want), have the following:
:=
:=#byte adresl=0x9e
:=#byte adresh=0x9f
:=#bit ADON=0x1f.0
:=#bit ADGO=0x1f.2
:=
:=The ability to create direct register access variables for both bytes, and bits, is fundamental to this C, and allows you to code 95\% of the stuff that would normally need aseembler, using the C code. :-)
:=
:=With a suitable 'store_to_RAM' function, this should give what you want.
:=
:=Best Wishes

Thanks a lot
That was the solution. I can't say how happy I am that all works fine.
___________________________
This message was ported from CCS's old forum
Original Post ID: 13607
R.J.Hamlett
Guest







Re: To ThomasB, re A/D Conversion
PostPosted: Sat Apr 12, 2003 4:37 am     Reply with quote

:=:=:=:=:=<font face="Courier New" size=-1>Thank you for your answers but I have the problem that the 10 bits A/D conversion and writing the result to an external RAM should be done within of 30 µs. The A/D converter itselves nearly needs this time for a correct conversion. The second Problem is that this should be done 4000 times after each other so I think I can’t save so much data on the PIC directly.
:=:=:=:=:=Does anybody have solution for this problem?</font>
:=:=:=:=One solution, is to code the read adc yourself. Then you can trigger the AD, send the _last_ value to the memory, poll the flag to see if the conversion has completed, and when it has, read the value, then loop back to the start.
:=:=:=:=I use this approach where I need fast sequential AD reads.Then with suitable defintions for these bits/bytes (depends on which processor you are using), you can do the loop as:
:=:=:=:=
:=:=:=:=for(count=0;count :=:=:=:= ADON=1;
:=:=:=:= ADGO=1;
:=:=:=:= store_to_RAM(result,count);
:=:=:=:= while(ADGO);
:=:=:=:= result=make16(adresh,adresl);
:=:=:=:=}
:=:=:=:=
:=:=:=:=Best Wishes
:=:=:=
:=:=:=Thanks a lot
:=:=:=Your idea seems to be good. But how can I implement ADON and ADGO and also adresh and adresl to C code? I have to say that my knowledge about assembler programming is not very good.
:=:=
:=:=No assembler needed.
:=:=For a 16F877 (you will have to set the actual addresses to suit whatever chip you want), have the following:
:=:=
:=:=#byte adresl=0x9e
:=:=#byte adresh=0x9f
:=:=#bit ADON=0x1f.0
:=:=#bit ADGO=0x1f.2
:=:=
:=:=The ability to create direct register access variables for both bytes, and bits, is fundamental to this C, and allows you to code 95\% of the stuff that would normally need aseembler, using the C code. :-)
:=:=
:=:=With a suitable 'store_to_RAM' function, this should give what you want.
:=:=
:=:=Best Wishes
:=
:=Thanks a lot
:=That was the solution. I can't say how happy I am that all works fine.

Glad to hear it suits. :-)
You can also see how easy this method of working makes it to provide those extra 'tweaks' that the standard functions miss.


Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 13628
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