imranahmed
Advanced Member level 3
- Joined
- Dec 4, 2011
- Messages
- 822
- Helped
- 3
- Reputation
- 6
- Reaction score
- 3
- Trophy points
- 1,298
- Location
- Karachi,Pakistan
- Activity points
- 6,533
but when I tend to write 0.05 it cannot display but displays 0.49 , 0.10 is 0.99 and other
values are shown well.
What is happening can any one interested to tell me?
but when I tend to write 0.05 it cannot display but displays 0.49 , 0.10 is 0.99 and other
float f_Temp=0.0,temp=0; // Global Variable
unsigned int num=0, RTE_VAL=0; // Global Variable
void loop()
{
RTE_VAL=getRotary.Encoder(); //Get Rotary Encoder Values 0,1,2,3....n
temp = RTE_VAL * 0.01; // Make Rotary Encoder Values to decimal.
if(temp>=0.0 && temp<=9.9) // for printing decimal values
{
num = temp * 100;
one=num%10;
ten=(num/10)%10;
hundred=(num/100)%10;
lc.setdigit(0,2,(byte)one,0); //lc.setdigit(addr , digits , value , dp);
lc.setdigit(0,1,(byte)ten,0);
lc.setdigit(0,0,(byte)hundred,1);
}
else if(temp>=10.0 && temp<=99.9) // for printing decimal values
{
num = temp * 10;
one=num%10;
ten=(num/10)%10;
hundred=(num/100)%10;
lc.setdigit(0,2,(byte)one,0);
lc.setdigit(0,1,(byte)ten,1);
lc.setdigit(0,0,(byte)hundred,0);
}
else if(temp>=100 && temp<=999) // for printing decimal values
{
num = temp * 1;
one=num%10;
ten=(num/10)%10;
hundred=(num/100)%10;
lc.setdigit(0,2,(byte)one,1);
lc.setdigit(0,1,(byte)ten,0);
lc.setdigit(0,0,(byte)hundred,0);
}
}
int x;
int y;
float z;
x = 1;
y = x / 2; // y now contains 0, ints can't hold fractions
z = (float)x / 2.0; // z now contains .5
temp = (float)RTE_VAL * 0.01;
// I represente your RET_VAL
I= 0 Value=0.00 float2ascii temp=0.00
I= 1 Value=0.01 float2ascii temp=0.01
I= 49 Value=0.49 float2ascii temp=0.49
I= 50 Value=0.50 float2ascii temp=0.50
I= 99 Value=0.99 float2ascii temp=0.99
I= 100 Value=1.00 float2ascii temp=1.00
I= 101 Value=1.01 float2ascii temp=1.01
I= 990 Value=1.01 float2ascii temp=9.90 <- HERE Anomalie ????
I= 999 Value=9.90 float2ascii temp=9.99
I= 1000 Value=10.0 float2ascii temp=10.0
I= 1001 Value=10.0 float2ascii temp=10.0
I= 1010 Value=10.1 float2ascii temp=10.1
I= 9999 Value=99.9 float2ascii temp=99.9
I=10000 Value=100. float2ascii temp=100.
I=10001 Value=100. float2ascii temp=100.
I=10010 Value=100. float2ascii temp=100.
I=10100 Value=101. float2ascii temp=101.
I=65535 Value=655. float2ascii temp=655. Maxima else Overflow !!
unsigned char CRam1[32];
unsigned int num;
unsigned int one,ten,hundred;
float temp;
void Float2Ascii (float x, unsigned char *str,char precision)
{
/* converts a floating point number to an ascii string */
/* x is stored into str, which should be at least 30 chars long */
// unsigned char *adpt;
int ie, i, k, ndig;
double y;
//adpt=str;
ndig = ( precision<=0) ? 7 : (precision > 22 ? 23 : precision+1);
ie = 0;
/* if x negative, write minus and reverse */
if ( x < 0)
{
*str++ = '-';
x = -x;
}
/* put x in range 1 <= x < 10 */
if (x > 0.0) while (x < 1.0)
{
x *= 10.0; // a la place de =*
ie--;
}
while (x >= 10.0)
{
x = x/10.0;
ie++;
}
// in f format, number of digits is related to size
ndig += ie; // a la place de =+
//round. x is between 1 and 10 and ndig will be printed to
// right of decimal point so rounding is ...
for (y = i = 1; i < ndig; i++)
y = y/10.;
x += y/2.;
if (x >= 10.0) {x = 1.0; ie++;}
if (ie<0)
{
*str++ = '0'; *str++ = '.';
if (ndig < 0) ie = ie-ndig;
for (i = -1; i > ie; i--) *str++ = '0';
}
for (i=0; i < ndig; i++)
{
k = x;
*str++ = k + '0';
if (i == ie ) *str++ = '.';
x -= (y=k);
x *= 10.0;
}
*str = '\0';
//return (adpt);
}
void Affiche (unsigned int i)
{
temp = (float) i * 0.01 + 0.00001; // <-OK and best writing !
// temp = (float) i * 0.01; // <-- BAD
// temp = i * 0.01 + 0.00001; // <--- OK even without float casting
CRam1[4]=0;
if(temp>=0.0 && temp<=9.9) // for printing decimal values
{
num =(unsigned int) (temp * 100.0);
one=num%10;
ten=(num/10)%10;
hundred=(num/100)%10;
CRam1[3]=48+one; // lc.setdigit(addr , digits , value , dp);
CRam1[2]=48+ten; [COLOR=#0000FF][B] // add 48 to get an ascii value[/B][/COLOR]
CRam1[1]='.';
CRam1[0]=48+hundred;
}
else if(temp>=10.0 && temp<=99.99) // for printing decimal values
{
num =(unsigned int) ( temp * 10.0);
one=num%10;
ten=(num/10)%10;
hundred=(num/100)%10;
CRam1[3]=48+one; //lc.setdigit(addr , digits , value , dp);
CRam1[2]='.';
CRam1[1]=48+ten;
CRam1[0]=48+hundred;
}
else if(temp>=100.0 && temp<=999.999) // for printing decimal values
{
num =(unsigned int) temp;
one=num%10;
ten=(num/10)%10;
hundred=(num/100)%10;
CRam1[3]='.';
CRam1[2]=48+one; //lc.setdigit(addr , digits , value , dp);
CRam1[1]=48+ten;
CRam1[0]=48+hundred;
}
WordToStr(i,txt);
UART1_Write_CText("I="); UART1_Write_Text(txt);
UART1_Write_CText(" Value="); UART1_Write_Text(CRam1);
Float2Ascii (temp,CRam1,2);
k=strlen(CRam1) ;
// rajoute eventuel 0 non significatif
while (k<4)
{
CRam1[k]='0';CRam1[k+1]=0;
k=strlen(CRam1) ;
}
CRam1[4]=0; // limite long à 4
UART1_Write(TAB);
UART1_Write_CText("float2ascii temp="); UART1_Write_Text(CRam1);
CRLF();
}
// in main program
Affiche (0);
Affiche (1);
Affiche (49);
Affiche (50);
Affiche (99);
Affiche (100);
Affiche (101);
Affiche (990);
Affiche (999);
Affiche(1000);
Affiche (1001);
Affiche (1010);
Affiche (9999);
Affiche (10000);
Affiche (10001);
Affiche (10010);
Affiche (10100);
Affiche (65535);
Delay_ms(5000);
...but when I tend to write 0.05 it cannot display but displays 0.49 , 0.10 is 0.99 and other
values are shown well.
What is happening can any one interested to tell me?
getRotary.Encoder();
Dear All members,
I want that initially display 0.00 when I rotate Encoder(KY-040) the display looks like 0.01,0.02....0.09,0.10.....0.99..
..1.00 , 1.01.....1.99 , 2.00.............................9.90 , 9.91.....9.99..10.0.........99.0.......99.9.....100,101,102.....999.
then roll over.
I programmed it but some values missed during rotating like 9.99 when I reached 9.97 and rotate once the value
should be 9.98 then 9.99 but it but when I rotate at 9.97 there is no increase in value and second rotate no increase
third rotate no increase fourth rotate the value display 10.0.
Means the value 9.98 and 9.99 are missed during rotate and same occuring with other values.
How to do this?
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 #include "float2ascii.h" sbit Data7219 at LATD0_bit; sbit Load7219 at LATD1_bit ; sbit Clk7219 at LATD2_bit; sbit Data7219_Direction at LATD0_bit; sbit Load7219_Direction at LATD1_bit; sbit Clk7219_Direction at LATD2_bit; const unsigned char Font_B[16] = {0x7e,0x30,0x6d,0x79,0x33,0x5b,0x5f,0x70, 0x7f,0x7b,0x77,0x1f,0x4e,0x3d,0x4f,0x47 }; void Send7219 (char,char); void Send7219byte(char); void MAX7219init(); void Display(float); double adcVal = 0.0, prevVal = 0.0; void MAX7219init() { Data7219_Direction = 0; Load7219_Direction = 0; Clk7219_Direction = 0; Data7219 = 0; Load7219 = 0; Clk7219 = 0; Send7219(0x09, 0x00); //Decode Mode Send7219(0x0A, 0x05); //Brightness Send7219(0x0B, 0x07); //Scan limit Send7219(0x0C, 0x01); Send7219(0x0F, 0x00); } void Send7219(char Digit,char Data) { Send7219byte(Digit); Send7219byte(Data); Data7219 = 0; Load7219 = 1; Delay_us(20); Load7219 = 0; } void Send7219byte(char byte) { unsigned char i; for(i = 0; i < 8; i++) { if(byte & 0x80) Data7219 = 1; else Data7219 = 0; Clk7219 = 1; Delay_us(20); Clk7219 = 0; byte <<= 1; } } void Display(double fpNum) { unsigned char dispskip; // add 1 if "." found within string unsigned char i, dp, str[30]; Float2Ascii(fpNum, str, 2); //Convert float value to string Ltrim(str); //remove spaces padded to left of string LCD_Out(1,1,str); dispskip = strlen(str) - 1; if(dispskip >= 8)dispskip = 7; i = 0; dp = 0; while(i <= dispskip) { if((str[dispskip-i] >= 0x30) && (str[dispskip - i] <= 0x39) && (dp == 0)) { Send7219(i+1, (Font_B[str[dispskip-i] - 0x30]));//0-9 } if((str[dispskip-i] >= 0x30) && (str[dispskip - i] <= 0x39) && (dp)) { Send7219(i, (Font_B[str[dispskip-i] - 0x30]));//0-9 } if(str[dispskip-i] == '.') { Send7219(i+1, (Font_B[str[dispskip-(i+1)] - 0x30]) | 0x80);// i++; dp = 1; } i++; } while(i <= 8) { Send7219(i, 0); i++; } } void main() { TRISA = 0xFF; TRISB = 0x00; PORTB = 0x00; LATB = 0x00; TRISD = 0x00; PORTD = 0x00; LATD = 0x00; ANSELA = 0x01; ANSELB = 0x00; ANSELC = 0x00; ANSELD = 0x00; ANSELE = 0x00; CM1CON0 = 0x00; CM2CON0 = 0x00; MAX7219init(); while(1) { unsigned char i = 0; for(i = 0; i < 20; i++) { adcVal += ADC_Read(0); Delay_ms(20); } adcVal /= 20; if(prevVal != adcVal) { Display(adcVal); prevVal = adcVal; } } }
I am using 3 digit 7-seg display and tried.did you try to also display your raw RTE_VAL on LCD ?
did you fix your floating point display problem ?
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 while(i <= dispskip) { if((str[dispskip-i] >= 0x30) && (str[dispskip - i] <= 0x39) && (dp == 0)) { Send7219(i+1, (Font_B[str[dispskip-i] - 0x30]));//0-9 } if((str[dispskip-i] >= 0x30) && (str[dispskip - i] <= 0x39) && (dp)) { Send7219(i, (Font_B[str[dispskip-i] - 0x30]));//0-9 } if(str[dispskip-i] == '.') { Send7219(i+1, (Font_B[str[dispskip-(i+1)] - 0x30]) | 0x80);// i++; dp = 1; } i++; }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 while(i <= dispskip) { if((str[dispskip-i] >= 0x30) && (str[dispskip - i] <= 0x39)) { if(!dp)Send7219(i+1, (Font_B[str[dispskip-i] - 0x30]));//0-9 elseif(dp)Send7219(i, (Font_B[str[dispskip-i] - 0x30]));//0-9 } if(str[dispskip-i] == '.') { Send7219(i+1, (Font_B[str[dispskip-(i+1)] - 0x30]) | 0x80);// i++; dp = 1; } i++; }
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?