type casting

General discussion on mikroC.
Post Reply
Author
Message
pickrit
Posts: 2
Joined: 06 Jul 2010 07:05

type casting

#1 Post by pickrit » 06 Jul 2010 07:19

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. :? 8)

pickrit
Posts: 2
Joined: 06 Jul 2010 07:05

Re: type casting

#2 Post by pickrit » 06 Jul 2010 07:21

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);

  }

  }

braus
Posts: 167
Joined: 25 Jul 2007 22:55
Location: Mexico, city.

Re: type casting

#3 Post by braus » 12 Aug 2010 22:55

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

Sobrietytest
Posts: 619
Joined: 05 Jul 2008 06:05
Location: Thailand

Re: type casting

#4 Post by Sobrietytest » 13 Aug 2010 09:16

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.

User avatar
slavisa.zlatanovic
mikroElektronika team
Posts: 1321
Joined: 07 Apr 2009 09:39

Re: type casting

#5 Post by slavisa.zlatanovic » 17 Aug 2010 11:12

Hi pickrit!

Little hint:

Instead of

Code: Select all

x = (5 * i)/1023; 
you should write:

Code: Select all

x = (5 * (double)i)/1023. ;  // or
x = (5 * i)/1023. ;
etc...

Best regards
Slavisa
Best regards
Slavisa

Post Reply

Return to “mikroC General”