View previous topic :: View next topic |
Author |
Message |
T_T Guest
|
A/D 10 bit with 18f458 |
Posted: Mon Jun 21, 2004 3:20 am |
|
|
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](images/smiles/icon_surprised.gif) |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jun 21, 2004 1:48 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
T_T Guest
|
|
Posted: Mon Jun 21, 2004 11:07 pm |
|
|
thank you so much. i miss "#device ADC=16" as your suggestion. ![Smile](images/smiles/icon_smile.gif) |
|
![](templates/subSilver/images/spacer.gif) |
T_T Guest
|
|
Posted: Tue Jun 22, 2004 11:23 am |
|
|
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;
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jun 22, 2004 12:18 pm |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
T_T Guest
|
|
Posted: Sat Jun 26, 2004 12:34 pm |
|
|
thank you for your kindness |
|
![](templates/subSilver/images/spacer.gif) |
|