Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

UART + ADC float problem

Status
Not open for further replies.

mostafa328

Newbie level 1
Joined
Oct 14, 2012
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,290
Hi

Im having trouble with my coding, i can't get my output to display as "00.0". Instead it displays via UART teminal as "27.99999", where it should display as "02.9".
I also tried sprintf instead of floattostr but it displays nothing. Unless i did something wrong. Im using pic18f4680 and mikroc pro. Can anyone point out what im doing wrong The following is my code:

unsigned int adc_rd;
float value;
char text[15];


void main() {
CMCON =7; //disable comprators
ADCON0 = 0b00001001; //select analog 2 and enable ADON
ADCON1 = 0b00001011; //Use VDD and VSS as reference voltage and pin 2 as analog
ADCON2 = 0b10001010; //Right justified, 2TAD, FOSC/32
TRISA = 0xFF; //TRIS A all inputs

OSCCON = 0b01110000; // 8Mhz internal oscillator
UART1_Init(9600); // baud rate at 9600bps
delay_ms(100);


while(1)
{
adc_rd = ADC_Read(2);
value =(adc_rd*50)>>10 ;
text[0] = value/100; //extract hundreds digit
text[1] = (value/10)%10; //extract tens digit
text[3] = value%10; //extract ones digit

floattostr(value,text);
// sprintf(text,"%u.%01u\n",d1,d2,d3);
UART1_Write_Text(text);

delay_ms(2000);

}
}
 

First of all you can not use floating point value with mod(%) .
you need to type cast before storing into the char array.
that is as below.

text[0] = (char) value/100; //extract hundreds digit
text[1] = (char)(value/10)%10; //extract tens digit
text[3] = (char) value%10; //extract ones digit

try this.

It may work.
If it does not work than come back here with your need error.

thankz,
androidbuddy
 

HI,

adc_rd = ADC_Read(2);
value =(float)(adc_rd*50)>>10 ;
value on LHS is float where as RHS is evaluated as int and then assigned to float so this may truncate the result
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top