division and LCD

Thread Starter

nanoid

Joined Feb 3, 2013
3
hey guys, I'm having some trouble with the division part of pic16f877a calculator...
u see only the quotent part of the division part is shown on the LCD
what should i do?
here is the "effy" part:
Rich (BB code):
result=(float)portc/portd;
I'm actially taking the two numbers from ports C,D and I identified the result variable as float the pogram is c of course
thanks!!

Rich (BB code):
sbit lcd_rs at rb2_bit;
sbit lcd_en at rb3_bit;
sbit lcd_d7 at rb7_bit;
sbit lcd_d6 at rb6_bit;
sbit lcd_d5 at rb5_bit;
sbit lcd_d4 at rb4_bit;
sbit lcd_rs_direction at trisb2_bit;
sbit lcd_en_direction at trisb3_bit;
sbit lcd_d7_direction at trisb7_bit;
sbit lcd_d6_direction at trisb6_bit;
sbit lcd_d5_direction at trisb5_bit;
sbit lcd_d4_direction at trisb4_bit;
int i,j,k,l;
char txt[20];
int operation;
float result;
char opsymb;
char x[20];
char y[20];
char resultchr[20];
void interrupt()
{
if(intcon.t0if==1&&(portd!=i||portc!=j||portb.b0!=k||portb.b1!=l))
{inttostr(portc,x);
inttostr(portd,y);
operation=portb.b0+portb.b1;
switch(operation)
{
  case 0:
  result=portd+portc;
  opsymb='+';
  break;
  case 1:
  if(portb.b1>portb.b0)
  {result=portd*portc;
  opsymb='x';
  }
  else
  {
    result=(float)portc/portd;
    opsymb='/';
  }
  break;
  case 2:
  result=portc-portd;
  opsymb='-';
  break;
}
lcd_cmd(_LCD_CLEAR);
bytetostr(portc,x);
bytetostr(portd,y);
lcd_out(1,1,x);
lcd_chr_cp(opsymb);
lcd_out_cp(y);
if(portd==0&&opsymb=='/')
{
   lcd_out_cp("error");
   delay_ms(500);
   lcd_cmd(_lcd_clear);
}
else
{floattostr(result,resultchr);
lcd_chr_cp('=');
lcd_out(2,1,resultchr);
}
tmr0=0;
intcon.t0if=0;
i=portd;
j=portc;
k=portb.b0;
l=portb.b1;
}
}
void main() {
trisd=0xff;
trisc=0xff;
lcd_init();
intcon=0xA0;
option_reg=0x07;
tmr0=0;
}
 
Last edited by a moderator:

spinnaker

Joined Oct 29, 2009
7,830
1. Keep your "text talk" reserved for texting your friends. Most people won't tolerate it around here. If you can't take the time to present your problem clearly then we needn't take the time to answer.

2. When you post code use the code tags with formatting of the code.

3. I and I am sure others have no idea what you are asking. You need to narrow it down some more.
 

Thread Starter

nanoid

Joined Feb 3, 2013
3
Sorry guys I ddnt deliver the idea
Floattostr converts the value of the answer to a string value to be displayed on the lcd
Also the problem is when the two numbers are divided say 3/2 only 1 is displayed not 1.5 know what i mean?
Sorry again for the poor illustration
 

ErnieM

Joined Apr 24, 2011
8,415
Sorry guys I ddnt deliver the idea
Floattostr converts the value of the answer to a string value to be displayed on the lcd
Also the problem is when the two numbers are divided say 3/2 only 1 is displayed not 1.5 know what i mean?
Sorry again for the poor illustration
More like you can't take a hint. Let me go slowly.

Which is more likely? The C compiler gets an incorrect result when dividing one float number by another, or the routine you use to convert a float to a string is only converting the integer portion.

Want to take a poll? I'm laying odds here it's not the former.
 

Thread Starter

nanoid

Joined Feb 3, 2013
3
u think so???
but this is the only function I have in this compiler (MikroC)???
how can I separate the part after the point so ican convert the two parts separately?
 

t06afre

Joined May 11, 2009
5,934
How accurate must your result be. Floating point in micro controllers can be a mess. And take up huge amount of program memory. Can you instead do something like (PORTA*100)/(PORTB*100) and then adjust the decimal point in your program, then writing to the LCD
 
Top