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

Binary to decimal

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



Joined: 21 Mar 2010
Posts: 37

View user's profile Send private message Yahoo Messenger

Binary to decimal
PostPosted: Thu May 27, 2010 11:10 pm     Reply with quote

How to output the binary result from the adc to decimal?
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Fri May 28, 2010 2:21 am     Reply with quote

Look at printf.

Best Wishes
feitanx



Joined: 21 Mar 2010
Posts: 37

View user's profile Send private message Yahoo Messenger

PostPosted: Fri May 28, 2010 7:54 pm     Reply with quote

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

View user's profile Send private message Yahoo Messenger

PostPosted: Mon May 31, 2010 1:16 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon May 31, 2010 4:18 am     Reply with quote

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
Razz Embarassed Rolling Eyes
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

View user's profile Send private message Yahoo Messenger

PostPosted: Mon May 31, 2010 5:32 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon May 31, 2010 1:34 pm     Reply with quote

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

View user's profile Send private message Yahoo Messenger

PostPosted: Mon May 31, 2010 7:20 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon May 31, 2010 7:28 pm     Reply with quote

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

View user's profile Send private message Yahoo Messenger

PostPosted: Mon May 31, 2010 7:48 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon May 31, 2010 8:28 pm     Reply with quote

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:
Quote:

3.00
3.00
3.00
feitanx



Joined: 21 Mar 2010
Posts: 37

View user's profile Send private message Yahoo Messenger

PostPosted: Tue Jun 01, 2010 7:22 am     Reply with quote

still has error. Best way to reduce them?
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Tue Jun 01, 2010 8:21 am     Reply with quote

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

View user's profile Send private message Yahoo Messenger

PostPosted: Wed Jun 02, 2010 7:54 am     Reply with quote

yes I connect the ground as well. but still i get some errors. it neccessary to parallel a capacitor on the input?
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