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

A/D Converter

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







A/D Converter
PostPosted: Wed Apr 09, 2003 9:32 am     Reply with quote

I'm using PIC16F877 and I have the folling question.Is it possible to output data with the IO-Pin’s while the A/D Converter is working? If anybody has an idea can you send me an example code?
___________________________
This message was ported from CCS's old forum
Original Post ID: 13542
jdasari
Guest







Re: A/D Converter
PostPosted: Wed Apr 09, 2003 9:38 am     Reply with quote

:=I'm using PIC16F877 and I have the folling question.Is it possible to output data with the IO-Pin’s while the A/D Converter is working? If anybody has an idea can you send me an example code?

Note sure if this is what you're looking for....check out my previous post.

<a href="http://www.pic-c.com/forum/general/posts/13543.html" TARGET="_blank">http://www.pic-c.com/forum/general/posts/13543.html</a>
___________________________
This message was ported from CCS's old forum
Original Post ID: 13545
sar



Joined: 08 Sep 2003
Posts: 36

View user's profile Send private message

Re: A/D Converter
PostPosted: Wed Apr 09, 2003 9:51 am     Reply with quote

Try this example from: <a href="http://www.vermontficks.org/pic.htm" TARGET="_blank">http://www.vermontficks.org/pic.htm</a>
I changed the processer to 16F877


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

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

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

#include <16F877.h>
#DEVICE ICD=TRUE
//#DEVICE CCSICD=TRUE

#define 16F877 *=16 ADC = 10
#fuses XT, NOPROTECT, NOPUT, NOWDT

#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: 13546
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

Re: A/D Converter
PostPosted: Wed Apr 09, 2003 10:10 am     Reply with quote

:=I'm using PIC16F877 and I have the folling question.Is it possible to output data with the IO-Pin’s while the A/D Converter is working? If anybody has an idea can you send me an example code?

Do a search in this forum for an in depth discusion of the time spent performing analog conversions. You should also look at the data sheet in reguard to TAD.
___________________________
This message was ported from CCS's old forum
Original Post ID: 13548
sar



Joined: 08 Sep 2003
Posts: 36

View user's profile Send private message

Re: A/D Converter
PostPosted: Wed Apr 09, 2003 10:48 am     Reply with quote

Try this: 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>
#DEVICE ICD=TRUE
//#DEVICE CCSICD=TRUE

#define 16F877 *=16 ADC = 10
#fuses XT, NOPROTECT, NOPUT, NOWDT

#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 ) //DEBUGGER


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: 13551
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: A/D Converter
PostPosted: Wed Apr 09, 2003 3:01 pm     Reply with quote

:=I'm using PIC16F877 and I have the following question. Is it possible to output data with the IO-Pin’s while the A/D Converter is working? If anybody has an idea can you send me an example code?
----------------------------------------------------------

It only takes a few micro-seconds to do an A/D conversion.
Do you really need to change the i/o pins during that time ?
In other words, you can do this:

output_low(PIN_B1);
result = read_adc();
output_high(PIN_B1);

However, starting with vs. 3.148, the CCS compiler now gives
you the option to start an A/D conversion, and then read the
result later. This is in the README.TXT file.
___________________________
This message was ported from CCS's old forum
Original Post ID: 13559
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: A/D Converter
PostPosted: Wed Apr 09, 2003 3:26 pm     Reply with quote

:=I'm using PIC16F877 and I have the folling question.Is it possible to output data with the IO-Pin’s while the A/D Converter is working? If anybody has an idea can you send me an example code?

----------------------------------------------

See the README.TXT file for compiler versions 3.148 and later.
It has 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: 13560
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