mostafa328
Newbie level 1
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);
}
}
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);
}
}