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

ADC *** Locked. 2004 thread.
Goto page 1, 2  Next
 
Post new topic   This topic is locked: you cannot edit posts or make replies.    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








ADC *** Locked. 2004 thread.
PostPosted: Mon May 24, 2004 9:54 am     Reply with quote

Hi,
the follow code doesn't work with emulator ice 2000 (MPLAB 6.40 & CCS 3.177) that isn't return the correct value. Why?

Thanks.


#include <18F452.h>
#DEVICE *=16 // Use 16 bit pointers to RAM
#DEVICE ADC=10
#include <STDLIBM.H>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=8000000)


void main()
{

int16 value;

setup_adc(ADC_CLOCK_DIV_8);
setup_port_a(RA0_ANALOG);
set_adc_channel( 0 );
while (TRUE)
{
delay_us(25);
value = read_adc();
}
}
Ttelmah
Guest







Re: ADC
PostPosted: Mon May 24, 2004 10:18 am     Reply with quote

Anonymous wrote:
Hi,
the follow code doesn't work with emulator ice 2000 (MPLAB 6.40 & CCS 3.177) that isn't return the correct value. Why?

Thanks.


#include <18F452.h>
#DEVICE *=16 // Use 16 bit pointers to RAM
#DEVICE ADC=10
#include <STDLIBM.H>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=8000000)


void main()
{

int16 value;

setup_adc(ADC_CLOCK_DIV_8);
setup_port_a(RA0_ANALOG);
set_adc_channel( 0 );
while (TRUE)
{
delay_us(25);
value = read_adc();
}
}

One obvious fault, is that you are clocking the ADC too fast. The maximum clock frequency allowed for ADC_CLOCK_DIV_8 on the chip's data sheet, is 5MHz.

Best Wishes
Guest








adc
PostPosted: Mon May 24, 2004 11:15 am     Reply with quote

OK, but with setup_adc(ADC_CLOCK_DIV_16) or setup_adc(ADC_CLOCK_DIV_32) or setup_adc(ADC_CLOCK_DIV_64) doesn't work.

Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 24, 2004 11:27 am     Reply with quote

Quote:
that isn't return the correct value. Why?

Tell us:

1. What voltage are you using for Vdd on your PIC ? (3.3v, 5v, etc. ?)

2. What is the voltage level at the A/D input pin ?
(Measure this with a voltmeter or oscilloscope).

3. What value do you expect to get from the read_adc() function ?

4. What is the actual value you get ?
xinyh



Joined: 23 Mar 2004
Posts: 11

View user's profile Send private message

PostPosted: Tue May 25, 2004 1:50 am     Reply with quote

read_adc() only return 8bits of the 10bits, so a new function is needed, anyone can help?
Ttelmah
Guest







PostPosted: Tue May 25, 2004 2:10 am     Reply with quote

xinyh wrote:
read_adc() only return 8bits of the 10bits, so a new function is needed, anyone can help?

Read_adc, as been returning values OK for a long time. I sugest you look elsewhere for the problem. A couple of comments apply:
1) You don't need *=16 on the 18 family chips. These default to using full size pointers.
2) How are you testing that the value is only 8 bits?. You do not show any code using the value, and a typical problem, would be to be using (for instance):
printf("%d"...);
Remember that a lot of the default arithmetic and operations in the PIC, are 8bit. suspect you are trimming the size yourself somewhere, without being aware of it..

Best Wishes
xinyh



Joined: 23 Mar 2004
Posts: 11

View user's profile Send private message

PostPosted: Tue May 25, 2004 2:30 am     Reply with quote

Hi Ttelmah,

Thanks for your reply!

My condition is like that, the analog voltage is 2.987V, for 10 bits, the hex value should be 2.987*(1024/5) = 263, but the value returned is 0063. The code is as below.

Best Regards!

#include <16f877.h>
#include <stdio.h>

#device ADC=10
#fuses HS,NOLVP,NOWDT,PUT, NOPROTECT
#use delay (clock=20000000)
#bit ADFM_BIT = 0x9F.7
#use rs232 (baud=9600, xmit=PIN_C6, rcv=PIN_C7)

void main() {
int value;
setup_adc_ports(RA0_ANALOG);
setup_adc(ADC_CLOCK_DIV_8);
ADFM_BIT = 1; // Right justify the A/D result

setup_spi(FALSE); //This function will initialize the SPI
setup_psp(PSP_DISABLED);
setup_counters(RTCC_INTERNAL,RTCC_DIV_2);//This function sets up the RTCC or WDT.
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_ccp1(CCP_OFF);
setup_ccp2(CCP_OFF);

SET_TRIS_A(0b0000001); //Set A0 as input
SET_TRIS_C(0b10000000); //Set C7 as input & C6 as output

// Read analog input voltages
set_adc_channel(0);
delay_ms(5);

do {
delay_ms(250);
value = Read_ADC();
printf("\n\r ADC-value: %4X \r\n", value);
} while (TRUE);
}
Haplo



Joined: 06 Sep 2003
Posts: 659
Location: Sydney, Australia

View user's profile Send private message

PostPosted: Tue May 25, 2004 3:17 am     Reply with quote

I went through your code quickly and found two mistakes:

1. For a 20MHz crystal you need to set the ADC clock to 32TOSC, so you have to change that line in your code to:

setup_adc(ADC_CLOCK_DIV_32);


2. You've defined #device ADC=10, but you are using an 'int' variable to read the ADC (Mr.Hamlett had suspected right). Remember in CCS, 'int' is the same as 'int8' which is 8-bit variable. So change your code to:
int16 value;

And you'll also need to change your 'printf' statement as well to print out an int16 variable.
xinyh



Joined: 23 Mar 2004
Posts: 11

View user's profile Send private message

PostPosted: Tue May 25, 2004 3:45 am     Reply with quote

Hi Ali,

You are right! In my CCS Vision, there are not int8 and int16, so I use long int, but the problem is same, maybe I will change the format in printf(), appreciate it for give some example of printf() for long int. I use "%LX", but still cannot.
Haplo



Joined: 06 Sep 2003
Posts: 659
Location: Sydney, Australia

View user's profile Send private message

PostPosted: Tue May 25, 2004 5:00 am     Reply with quote

Did you try the setup_adc(ADC_CLOCK_DIV_32); ?
As others have pointed to you, according to the datasheet, the ADC clock should be set so it would guarantee a minimum of 19.2us for the conversion to complete. Your current setting is giving it less than 4us.

And also, what is the impedance on your analog pin? According to the datasheet, The maximum recommended impedance for analog sources is 10 kΩ.
Ttelmah
Guest







PostPosted: Tue May 25, 2004 5:51 am     Reply with quote

xinyh wrote:
Hi Ttelmah,

Thanks for your reply!

My condition is like that, the analog voltage is 2.987V, for 10 bits, the hex value should be 2.987*(1024/5) = 263, but the value returned is 0063. The code is as below.

Best Regards!

#include <16f877.h>
#include <stdio.h>

#device ADC=10
#fuses HS,NOLVP,NOWDT,PUT, NOPROTECT
#use delay (clock=20000000)
#bit ADFM_BIT = 0x9F.7
#use rs232 (baud=9600, xmit=PIN_C6, rcv=PIN_C7)

void main() {
int value;
setup_adc_ports(RA0_ANALOG);
setup_adc(ADC_CLOCK_DIV_8);
ADFM_BIT = 1; // Right justify the A/D result

setup_spi(FALSE); //This function will initialize the SPI
setup_psp(PSP_DISABLED);
setup_counters(RTCC_INTERNAL,RTCC_DIV_2);//This function sets up the RTCC or WDT.
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_ccp1(CCP_OFF);
setup_ccp2(CCP_OFF);

SET_TRIS_A(0b0000001); //Set A0 as input
SET_TRIS_C(0b10000000); //Set C7 as input & C6 as output

// Read analog input voltages
set_adc_channel(0);
delay_ms(5);

do {
delay_ms(250);
value = Read_ADC();
printf("\n\r ADC-value: %4X \r\n", value);
} while (TRUE);
}

Your answer is in the printf statement (as I suspected)... You have to use %4LX to print a 'long' the statement is only printing the low eight bits...

Best Wishes
Guest








PostPosted: Tue May 25, 2004 8:04 am     Reply with quote

sorry, for my late.
So:

1. What voltage are you using for Vdd on your PIC ? (3.3v, 5v, etc. ?)

VDD = 3V


2. What is the voltage level at the A/D input pin ?
(Measure this with a voltmeter or oscilloscope).

VINA/D=0.783V

3. What value do you expect to get from the read_adc() function ?

With ICD2 i get 0d273 (correct) but with ice2000 i get 0d159 (wrong)

4. What is the actual value you get ?

see point 3.

Besides, another problem is the type float. The variables float don't return the value update but strange value.

Thanks
hydrogene



Joined: 23 May 2004
Posts: 11
Location: Montreal

View user's profile Send private message

PostPosted: Tue May 25, 2004 8:55 am     Reply with quote

Hi,
I already saw this problem last month. I only forgot to put : setup_adc_ports(ALL_ANALOG).

Here my lines :
setup_port_a(ALL_ANALOG);
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);


It's supose to work like this Smile

David
Guest








PostPosted: Tue May 25, 2004 9:18 am     Reply with quote

Hi David,
thanks for your suggestion but it doesn't work.
About the type float can i help me?

Thanks in advance
Tony
hydrogene



Joined: 23 May 2004
Posts: 11
Location: Montreal

View user's profile Send private message

PostPosted: Tue May 25, 2004 9:30 am     Reply with quote

I don't know what you mean by float type.

read_adc return a 10 bits integer. with this, you must do this :

Float = value * 5 / 1024;

It's only what I understand... if it's not that, you can explain more please Smile

David
Display posts from previous:   
Post new topic   This topic is locked: you cannot edit posts or make replies.    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