|
|
View previous topic :: View next topic |
Author |
Message |
Jack// ani Guest
|
Any probelm here.... |
Posted: Sun Apr 24, 2005 10:22 pm |
|
|
Hi all,
I've written this simple program, please see if this is correct.....
#include <16F877A.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
void main()
{
int16 value;
setup_port_a(ALL_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
while(1)
{
value=read_adc();
printf("Value is : %x ",value);
delay_ms(500);
}
}
Please tell me how to convert the integer value into floating decimal value?
Thanks |
|
|
Ttelmah Guest
|
|
Posted: Mon Apr 25, 2005 2:47 am |
|
|
First thing, 'value', is a long integer. The printf commad to print a 'long' in hexadecimal, is %Lx, not %x.
Add the line:
#device ADC=10
as the second line in the program (otherwise the ADC will only return an 8bit value), and there is no point in using a 'long' variable...
Then to get a 'float', something like this:-
Code: |
main() {
int16 value;
float volts;
setup_port_a(ALL_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
while(1) {
value=read_adc();
printf("Value is : %x /n",value);
//the actual arithmetic will depend on your Vref etc.
volts=(float)value*0.0048828125;
printf("Voltage is : %3.2f/n",volts);
delay_ms(500);
}
}
|
The 'cast' (adding the word 'float' in front of 'value'), is not strictly necessary, since the use of a float multiplier, _should_ cause automatic casting, but it is safer in CCS to be 'explicit' about this.
Best Wishes |
|
|
Jack// ani Guest
|
|
Posted: Mon Apr 25, 2005 11:29 pm |
|
|
Thanks for your helpful reply. Where should I add #device ADC=10, in the source file or header file? When I added this in the first line of the code it shows error "A #device required before this line", and when I replaced it with #device PIC16F877A ADC=10, the error is gone. Also when I place this line on the header file instead on source there is no error! Which one is correct way?
Thanks again |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Apr 26, 2005 12:09 am |
|
|
If you're using the CCS compiler, you should reading or at least
browsing all the posts in this forum every day. Many of the questions
that you have will asked by others every 2-3 days. For example,
your question about where to place the #device adc=10 statement
was just recently answered in this thread.
http://www.ccsinfo.com/forum/viewtopic.php?t=22646 |
|
|
|
|
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
|