|
|
View previous topic :: View next topic |
Author |
Message |
cxiong
Joined: 09 Sep 2003 Posts: 52
|
Unable to get 10-bit ADC to work on PIC16F877 - HELP |
Posted: Tue Jul 22, 2003 2:00 pm |
|
|
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
|
Re: Unable to get 10-bit ADC to work on PIC16F877 - HELP |
Posted: Tue Jul 22, 2003 2:07 pm |
|
|
:=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
|
Re: Unable to get 10-bit ADC to work on PIC16F877 - HELP |
Posted: Tue Jul 22, 2003 5:08 pm |
|
|
:=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 |
|
|
|
|
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
|