Hi
I am trying to display numbers on lcd from 1 to 100 continuously depending on adc values. I have used some arithmetic conversion equations. It is not working. I feel it is due to improper type of arithmetic variable used. Im Unsure what is the problem
Kindly help.
type casting
Re: type casting
Hi pls find the code with the above problem below
Code: Select all
unsigned int i;
unsigned int k;
long j;
long l;
double x;
double y;
long z;
void lcd(void);
void InitMain()
{
OPTION_REG = 0;
ADCON0 = 0X41;
ADCON1 = 0x84;
CMCON = 0X07; // Configure analog inputs and Vref
TRISA = 0x1F; // PORTA is input
TRISB = 0; // Pins RB7, RB6 are outputs
TRISC = 0; // PORTD is output
l = 0;
j = 0;
}
void main() {
InitMain();
Lcd8_Init(&PORTC, &PORTB); // Initialize LCD at PORTB and PORTC
Lcd8_Cmd(LCD_BLINK_CURSOR_ON);
Lcd8_Out(1, 3, text1);
portc = 0x09;
lcd();
}
void lcd()
{
while(1)
{
i = Adc_Read(0);
x = (5 * i)/1023 ;
y = 24.709 * x - 11.11 ;
z = (int) y + 0;
j = z/ 10 ;
l = z % 10 ;
lcd8_chr(1,1,j+48);
lcd8_chr(1,2,l+48);
}
}
Re: type casting
Hello pickrit, I'm not so sure if mikroC had a LongToString function, it will be helpful for you. I tell you that newest version of this compiler has that function.
Best Regards
Omar Galicia
Mexico City
Omar Galicia
Mexico City
-
- Posts: 619
- Joined: 05 Jul 2008 06:05
- Location: Thailand
Re: type casting
Hi Pickrit, this is an ideal example of why you should give your variables more meaningful names! Single letter variables may be easier to type but it makes your code very difficult for other people to read, single letters should only be used for counters (e.g. for i = 0; i < 8; i++). Try using names like ADCraw, ADCdec, etc. Can you also comment your code to show the meaning of each calculation, some are obvious but others are not.
Whilst I'm being pedantic, what are you trying to do here...
y = 24.709 * x - 11.11;
Do you mean y = (24.709 * x) - 11.11; or y = 24.709 * (x - 11.11);? In each case the results will be different.
It may be that the compiler will calculate the result as you expect but I find that it's always better to assert the meaning of each calculation so that you are guaranteed to get the correct result - and you will understand your code when you look at it 6 months later!
The LongToStr function exists in MikroC but I would be more inclined to use FloatToStr. The problem with this is that it will return an array of 13 characters regardless of the length of the result which can be difficult when you only have 16 characters on one LCD line. If you upgrade to MikroC PRO (it's free!) you can use FloatToStr and then use the ltrim and rtrim functions to remove the 'white spaces'.
There are other ways to achieve the same result but it depends on how you want to the result to appear, i.e. how many decimal places and so on, this is why the standard _ToStr functions may not be suitable for your purpose.
Whilst I'm being pedantic, what are you trying to do here...
y = 24.709 * x - 11.11;
Do you mean y = (24.709 * x) - 11.11; or y = 24.709 * (x - 11.11);? In each case the results will be different.
It may be that the compiler will calculate the result as you expect but I find that it's always better to assert the meaning of each calculation so that you are guaranteed to get the correct result - and you will understand your code when you look at it 6 months later!
The LongToStr function exists in MikroC but I would be more inclined to use FloatToStr. The problem with this is that it will return an array of 13 characters regardless of the length of the result which can be difficult when you only have 16 characters on one LCD line. If you upgrade to MikroC PRO (it's free!) you can use FloatToStr and then use the ltrim and rtrim functions to remove the 'white spaces'.
There are other ways to achieve the same result but it depends on how you want to the result to appear, i.e. how many decimal places and so on, this is why the standard _ToStr functions may not be suitable for your purpose.
- slavisa.zlatanovic
- mikroElektronika team
- Posts: 1321
- Joined: 07 Apr 2009 09:39
Re: type casting
Hi pickrit!
Little hint:
Instead of
you should write:
etc...
Best regards
Slavisa
Little hint:
Instead of
Code: Select all
x = (5 * i)/1023;
Code: Select all
x = (5 * (double)i)/1023. ; // or
x = (5 * i)/1023. ;
Best regards
Slavisa
Best regards
Slavisa
Slavisa