View previous topic :: View next topic |
Author |
Message |
feitanx
Joined: 21 Mar 2010 Posts: 37
|
Binary to decimal |
Posted: Thu May 27, 2010 11:10 pm |
|
|
How to output the binary result from the adc to decimal? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Fri May 28, 2010 2:21 am |
|
|
Look at printf.
Best Wishes |
|
|
feitanx
Joined: 21 Mar 2010 Posts: 37
|
|
Posted: Fri May 28, 2010 7:54 pm |
|
|
do you have yahoo messenger id so that I could contact you more often? I really need help because this is part of my thesis |
|
|
feitanx
Joined: 21 Mar 2010 Posts: 37
|
|
Posted: Mon May 31, 2010 1:16 am |
|
|
How to have a result in adc with decimal point value(e.g. 1.20...)and how to sed them in UART? I'am using 10 bit adc. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Mon May 31, 2010 4:18 am |
|
|
feitanx wrote: | do you have yahoo messenger id so that I could contact you more often? I really need help because this is part of my thesis |
How much are you willing to pay?
feitanx wrote: | How to have a result in adc with decimal point value(e.g. 1.20...) | The function for reading the ADC is read_adc(). Have you tried searching this forum for this function? You will find lots of examples.
feitanx wrote: | and how to sed them in UART? | Same answer as given by Ttelmah: "Look at printf."
We don't mind helping you, but we are not going to do your work. We like to see you have put in some effort, so post a small program demonstrating your results up till now and explain the part you have problems with.
Hey, we are doing this for fun. In our spare time. Expect nothing and be glad for any help you get. |
|
|
feitanx
Joined: 21 Mar 2010 Posts: 37
|
|
Posted: Mon May 31, 2010 5:32 am |
|
|
I know about read_adc(). My point is I have an ADC result and I want it to contain a decimal point value like 23.4589 and etc. So I use the adc formula (V/Vref)*1023. The problem is that UART contains only 8 bit and the resolution of the my ADC=10. Therefore I cannot send the whole data. I want to know to send the 10 bit data in UART.
HERE IS MY CODE:
Code: |
#include <16F877a.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOCPD,NOBROWNOUT
#device *=16 ADC=8
#use delay(clock=4000000)
#use rs232(uart1,baud=9600,xmit=PIN_C6,rcv=PIN_C7,PARITY=N,BITS=8,STOP=1)
#use STANDARD_IO(D)
#use STANDARD_IO(A)
#use STANDARD_IO(C)
#bit ADFM_BIT = 0x9F.7
void main(void){
int8 A;
int16 V2,V4,V6,B1,C1,A1;
float A2;
ADFM_BIT = 1;
setup_adc_ports(A_ANALOG_RA3_REF);
setup_adc(ADC_CLOCK_DIV_32);
for(;;){
V2=0;
V4=0;
V6=0;
for(A=1;A<=40;A++)
{
set_adc_channel(0);
delay_us(150);
V2+=read_adc();
set_adc_channel(1);
delay_us(150);
V4+=read_adc();
set_adc_channel(2);
delay_us(150);
V6+=read_adc();
delay_ms(150);
}
A1=V2/40;
B1=V4/40;
//output_d(A1);
C1=V6/40;
A2=((A1*5)/1023.00); ---------> this formula doesnt work; What are the other ways?
printf("%2.2f\n\r", A2);
}
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 31, 2010 1:34 pm |
|
|
Quote: |
#device *=16 ADC=8
A2=((A1*5)/1023.00);
|
You have set the ADC for 8-bit mode, but your equation is set for 10-bit
mode. That will produce an incorrect result. Change your equation
to use a divisor of 255.0 and it should work better. (Or, Ttelmah would
probably say to use 256.0).
Quote: |
setup_adc_ports(A_ANALOG_RA3_REF);
supply voltage is 5
Vref is 5
AN2 is 3v
|
If your Vref is the same as the supply voltage, then why use external
Vref ? Change that line to this:
Code: | setup_adc_ports(ALL_ANALOG);
|
Quote: |
#use delay(clock=4000000)
setup_adc(ADC_CLOCK_DIV_32);
|
The PIC data sheet recommends an ADC clock divisor of 8 if you use
a 4 MHz crystal. Change that line to this:
Code: | setup_adc(ADC_CLOCK_DIV_8);
|
I made all those changes and I put 3.00 volts on Pin A0, and grounded
the other two A/D input pins, and I got this result on the terminal window:
Quote: |
3.00
3.00
3.00
3.00
3.00
3.00
3.00 |
|
|
|
feitanx
Joined: 21 Mar 2010 Posts: 37
|
|
Posted: Mon May 31, 2010 7:20 pm |
|
|
Did you declare A2 as float? when I use the formula the answer is still incorrect, I already change the divisor to 255. Maybe there are some parameters to set. I did not ground the other AD pin by the way.
Last edited by feitanx on Mon May 31, 2010 7:46 pm; edited 1 time in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 31, 2010 7:28 pm |
|
|
Quote: |
Did you declare A2 as float? when I use the formula the answer is incorrect. Maybe there are some parameters to set.
|
Yes, 'A2' is declared as a float.
Quote: | I did not ground the other AD pin by the way.
|
What are the voltages on the other A/D input pins ? |
|
|
feitanx
Joined: 21 Mar 2010 Posts: 37
|
|
Posted: Mon May 31, 2010 7:48 pm |
|
|
No voltage on other input pins. When I use the formula the output is 0.00
HERE IS THE WHOLE CODE:
Code: |
#include <16F877a.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOCPD,NOBROWNOUT
#device *=16 ADC=8
#use delay(clock=4000000)
#use rs232(uart1,baud=9600,xmit=PIN_C6,rcv=PIN_C7,PARITY=N,BITS=8,STOP=1)
#use STANDARD_IO(D)
#use STANDARD_IO(A)
#use STANDARD_IO(C)
#bit ADFM_BIT = 0x9F.7
void main(void){
int8 A,A1;
int16 V2,V4,V6,B1,C1;
float A2;
ADFM_BIT = 1;
setup_adc_ports(A_ANALOG_RA3_REF);
setup_adc(ADC_CLOCK_DIV_8);
for(;;){
V2=0;
V4=0;
V6=0;
for(A=1;A<=40;A++)
{
set_adc_channel(0);
delay_us(100);
V2+=read_adc();
set_adc_channel(1);
delay_us(100);
V4+=read_adc();
set_adc_channel(2);
delay_us(100);
V6+=read_adc();
delay_ms(100);
}
A1=V2/40;
B1=V4/40;
C1=V6/40;
A2=((A1*5)/255.00);
printf("%2.2f\n\r", A2);
}
} |
sorry for inconvience but I think my mistake is A1, It should be int16?
The support tells me to use external ref because I use external volatge in my input.
Last edited by feitanx on Mon May 31, 2010 8:37 pm; edited 1 time in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 31, 2010 8:28 pm |
|
|
1. Change your setup_adc() statement to this:
Code: |
setup_adc_ports(ALL_ANALOG);
|
2. Change your voltage conversion formula to this:
Code: |
A2=((A1*5.0)/255.00);
|
3. Connect pin AN0 to 3.00 volts.
4. Connect AN1 and AN2 to ground.
Re-compile the program and re-program the 16F877A with the new code.
You should get the following output on the terminal window:
|
|
|
feitanx
Joined: 21 Mar 2010 Posts: 37
|
|
Posted: Tue Jun 01, 2010 7:22 am |
|
|
still has error. Best way to reduce them? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Tue Jun 01, 2010 8:21 am |
|
|
I ntre your comment about "The support tells me to use external ref because I use external voltage in my input". Do you mean that your input voltage is coming from an external source?. You do realise you have to connect the ground as well?...
The code with the mods from PCM Programmer _does_ work. You need to sort out your hardware.
Best Wishes |
|
|
feitanx
Joined: 21 Mar 2010 Posts: 37
|
|
Posted: Wed Jun 02, 2010 7:54 am |
|
|
yes I connect the ground as well. but still i get some errors. it neccessary to parallel a capacitor on the input? |
|
|
|