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

Unable to get 10-bit ADC to work on PIC16F877 - HELP

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



Joined: 09 Sep 2003
Posts: 52

View user's profile Send private message MSN Messenger

Unable to get 10-bit ADC to work on PIC16F877 - HELP
PostPosted: Tue Jul 22, 2003 2:00 pm     Reply with quote

I am writting code to do 10-bit ADC on the '877 and it does not do the job. It only do 8-bit (255) inteads of 10-bit (1024). I know that it does not do the job because instead of reading the LED pattern on PortD, I feed it through the RS 232, and use the Hyperterminal to read the max & min.

Please Help:

Here is my code. What did I do wrong?

=========================================
#include <16F877.h>
#fuses XT,NOWDT,NOPUT,NOBROWNOUT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
#use fast_io(d)
#bit ADFM_BIT=0X9F.7
// Inhale variables
#define KDI = 0;
#define KII = 0.001;
#define KPI = 1.0;
// Exhale variables
#define KDE = 0;
#define KIE = 0.001;
#define KPE = 1.0;



// ADC to long because it is 10-bit
typedef long uint16;
uint16 adc_result = 0;
//void adc_result( long int data )
int i,min,max;


main()
{
set_tris_a(0x3f); //set port a as input
set_tris_b(0x00); //set port b as output -- > inhale motor
set_tris_c(0x00); //set port c as output -- > exhale motor
set_tris_d(0x00); //set port d as output -- > ADC test pattern

setup_port_a( ALL_ANALOG );
//RA0 Analog pin
setup_adc( ADC_CLOCK_DIV_8 );
set_adc_channel( 0 );
ADFM_BIT=0;
delay_ms(10); // delay 10 mSec.
do
{ //take sample 30 and display min & max
min=1024;
max=0;
for (i=0; i<30; ++i)
{
delay_ms(100);
adc_result = read_adc();
output_d(adc_result);
if(adc_result min=adc_result;
if(adc_result>max);
max=adc_result;
}
printf("\n\rMin: \%4x MAX: \%4x",min,max);

}
while (1);
}
====================================================
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516256
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: Unable to get 10-bit ADC to work on PIC16F877 - HELP
PostPosted: Tue Jul 22, 2003 2:07 pm     Reply with quote

:=I am writting code to do 10-bit ADC on the '877 and it does not do the job. It only do 8-bit (255) inteads of 10-bit (1024). I know that it does not do the job because instead of reading the LED pattern on PortD, I feed it through the RS 232, and use the Hyperterminal to read the max & min.

But your "min" and "max" variables are only 8 bit values.
They cannot hold a 10-bit number.

:=
:=Please Help:
:=
:=Here is my code. What did I do wrong?
:=
:==========================================
:=#include <16F877.h>
:=#fuses XT,NOWDT,NOPUT,NOBROWNOUT,NOPROTECT,NOLVP
:=#use delay(clock=4000000)
:=#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
:=#use fast_io(d)
:=#bit ADFM_BIT=0X9F.7
:=// Inhale variables
:=#define KDI = 0;
:=#define KII = 0.001;
:=#define KPI = 1.0;
:=// Exhale variables
:=#define KDE = 0;
:=#define KIE = 0.001;
:=#define KPE = 1.0;
:=
:=
:=
:=// ADC to long because it is 10-bit
:=typedef long uint16;
:=uint16 adc_result = 0;
:=//void adc_result( long int data )

Remember that in CCS, an "int" is only 8 bits.
You should change these variables to be int16.

:=int i,min,max;
:=
:=
:=main()
:={
:= set_tris_a(0x3f); //set port a as input
:= set_tris_b(0x00); //set port b as output -- > inhale motor
:= set_tris_c(0x00); //set port c as output -- > exhale motor
:= set_tris_d(0x00); //set port d as output -- > ADC test pattern
:=
:= setup_port_a( ALL_ANALOG );
:= //RA0 Analog pin
:= setup_adc( ADC_CLOCK_DIV_8 );
:= set_adc_channel( 0 );
:= ADFM_BIT=0;
:= delay_ms(10); // delay 10 mSec.
:= do
:= { //take sample 30 and display min & max
:= min=1024;
:= max=0;
:= for (i=0; i<30; ++i)
:= {
:= delay_ms(100);
:= adc_result = read_adc();
:= output_d(adc_result);
:= if(adc_result
Here, you are putting a 16-bit word into an 8-bit variable.
:= min=adc_result;
:= if(adc_result>max);

Also, here:
:= max=adc_result;
:= }

You will also need to change this statement, so
it uses \%lx, to display the "long" (16-bit) values.
:= printf("\n\rMin: \%4x MAX: \%4x",min,max);
:=
:= }
:= while (1);
:=}
:=====================================================
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516257
Kenny



Joined: 07 Sep 2003
Posts: 173
Location: Australia

View user's profile Send private message

Re: Unable to get 10-bit ADC to work on PIC16F877 - HELP
PostPosted: Tue Jul 22, 2003 5:08 pm     Reply with quote

:=I am writting code to do 10-bit ADC on the '877 and it does not do the job. It only do 8-bit (255) inteads of 10-bit (1024). I know that it does not do the job because instead of reading the LED pattern on PortD, I feed it through the RS 232, and use the Hyperterminal to read the max & min.
:=
:=Please Help:
:=
:=Here is my code. What did I do wrong?
:=
:==========================================
:=#include <16F877.h>
:=#fuses XT,NOWDT,NOPUT,NOBROWNOUT,NOPROTECT,NOLVP
:=#use delay(clock=4000000)
:=#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
:=#use fast_io(d)
:=#bit ADFM_BIT=0X9F.7
:=// Inhale variables
:=#define KDI = 0;
:=#define KII = 0.001;
:=#define KPI = 1.0;
:=// Exhale variables
:=#define KDE = 0;
:=#define KIE = 0.001;
:=#define KPE = 1.0;
:=
:=
:=
:=// ADC to long because it is 10-bit
:=typedef long uint16;
:=uint16 adc_result = 0;
:=//void adc_result( long int data )
:=int i,min,max;
:=
:=
:=main()
:={
:= set_tris_a(0x3f); //set port a as input
:= set_tris_b(0x00); //set port b as output -- > inhale motor
:= set_tris_c(0x00); //set port c as output -- > exhale motor
:= set_tris_d(0x00); //set port d as output -- > ADC test pattern
:=
:= setup_port_a( ALL_ANALOG );
:= //RA0 Analog pin
:= setup_adc( ADC_CLOCK_DIV_8 );
:= set_adc_channel( 0 );
:= ADFM_BIT=0;
:= delay_ms(10); // delay 10 mSec.
:= do
:= { //take sample 30 and display min & max
:= min=1024;
:= max=0;
:= for (i=0; i<30; ++i)
:= {
:= delay_ms(100);
:= adc_result = read_adc();
:= output_d(adc_result);
:= if(adc_result := min=adc_result;
:= if(adc_result>max);
:= max=adc_result;
:= }
:= printf("\n\rMin: \%4x MAX: \%4x",min,max);
:=
:= }
:= while (1);
:=}
:=====================================================


Another possible problem is that you need to have the following line in 16F877.h to do 10 bit A/D

#device PIC16F877 ADC=10

Regards
Kenny
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516269
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