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 10 bit with 18f458

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







A/D 10 bit with 18f458
PostPosted: Mon Jun 21, 2004 3:20 am     Reply with quote

i want to convert analog input from RA0. to show output at RC<1 0> and RD. How can i do this? Thank you for you help Surprised
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 21, 2004 1:48 pm     Reply with quote

Look at the CCS example file, EX_ADMM10.C, which is in this folder:
c:\Program Files\Picc\Examples
It shows how to setup and read the A/D converter, by using the
setup_adc(), set_adc_channel(), and read_adc() functions.
Also, make sure that you put in the "#device ADC=16" statement, just
as it shows in the example file.

The result of the A/D conversion will be in the upper 10 bits of the
'value' variable.

Then shift the bits in 'value' into the proper position and write
them to the port. For example, this line will write the upper 8 bits
of 'value' to Port D.

output_d(value >> 8);

You could get the other 2 bits by shifting, or by using the bit_test()
and output_bit() functions.
T_T
Guest







PostPosted: Mon Jun 21, 2004 11:07 pm     Reply with quote

thank you so much. i miss "#device ADC=16" as your suggestion. Smile
T_T
Guest







PostPosted: Tue Jun 22, 2004 11:23 am     Reply with quote

I don't have EX_ADMM10.C. I only have EX_ADMM.C. Anyway i post my code. The result when i give A0 5 Volt is 1111000000[c(1-0) d(7-0)] Could you suggest that where i miss. Thank you,again

Code:

#include <18f458.h>
#device ADC=16
#use delay(clock=10000000)

main() {
         int16 value;
         int8 value_h,value_l;
         setup_adc(  ADC_CLOCK_DIV_32  );
         setup_adc_ports( RA0_ANALOG );
         set_adc_channel(0);
         delay_ms(100);
         loop :
            value = read_adc();
            delay_ms(50);
            value_h = value >> 8;
            value_l = (int8) value;
            output_c(value_h);
            output_d(value_l);
            if (value==1023)
               output_high(pin_b0);
            else  output_low(pin_b0);
         goto loop;
         }
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jun 22, 2004 12:18 pm     Reply with quote

I wasn't sure which way you wanted to justify the 10-bit result
in the 16-bit word. But now I know that you want to right-justify it.
So change your #device statement to this:

#device adc=10
T_T
Guest







PostPosted: Sat Jun 26, 2004 12:34 pm     Reply with quote

thank you for your kindness
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