division and LCD

Discussion in 'Embedded Systems and Microcontrollers' started by nanoid, Feb 3, 2013.

  1. nanoid

    Thread Starter New Member

    Feb 3, 2013
    3
    0
    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:
    Code ( (Unknown Language)):
    1.  
    2. result=(float)portc/portd;
    3.  
    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!!

    Code ( (Unknown Language)):
    1.  
    2. sbit lcd_rs at rb2_bit;
    3. sbit lcd_en at rb3_bit;
    4. sbit lcd_d7 at rb7_bit;
    5. sbit lcd_d6 at rb6_bit;
    6. sbit lcd_d5 at rb5_bit;
    7. sbit lcd_d4 at rb4_bit;
    8. sbit lcd_rs_direction at trisb2_bit;
    9. sbit lcd_en_direction at trisb3_bit;
    10. sbit lcd_d7_direction at trisb7_bit;
    11. sbit lcd_d6_direction at trisb6_bit;
    12. sbit lcd_d5_direction at trisb5_bit;
    13. sbit lcd_d4_direction at trisb4_bit;
    14. int i,j,k,l;
    15. char txt[20];
    16. int operation;
    17. float result;
    18. char opsymb;
    19. char x[20];
    20. char y[20];
    21. char resultchr[20];
    22. void interrupt()
    23. {
    24. if(intcon.t0if==1&&(portd!=i||portc!=j||portb.b0!=k||portb.b1!=l))
    25. {inttostr(portc,x);
    26. inttostr(portd,y);
    27. operation=portb.b0+portb.b1;
    28. switch(operation)
    29. {
    30.   case 0:
    31.   result=portd+portc;
    32.   opsymb='+';
    33.   break;
    34.   case 1:
    35.   if(portb.b1>portb.b0)
    36.   {result=portd*portc;
    37.   opsymb='x';
    38.   }
    39.   else
    40.   {
    41.     result=(float)portc/portd;
    42.     opsymb='/';
    43.   }
    44.   break;
    45.   case 2:
    46.   result=portc-portd;
    47.   opsymb='-';
    48.   break;
    49. }
    50. lcd_cmd(_LCD_CLEAR);
    51. bytetostr(portc,x);
    52. bytetostr(portd,y);
    53. lcd_out(1,1,x);
    54. lcd_chr_cp(opsymb);
    55. lcd_out_cp(y);
    56. if(portd==0&&opsymb=='/')
    57. {
    58.    lcd_out_cp("error");
    59.    delay_ms(500);
    60.    lcd_cmd(_lcd_clear);
    61. }
    62. else
    63. {floattostr(result,resultchr);
    64. lcd_chr_cp('=');
    65. lcd_out(2,1,resultchr);
    66. }
    67. tmr0=0;
    68. intcon.t0if=0;
    69. i=portd;
    70. j=portc;
    71. k=portb.b0;
    72. l=portb.b1;
    73. }
    74. }
    75. void main() {
    76. trisd=0xff;
    77. trisc=0xff;
    78. lcd_init();
    79. intcon=0xA0;
    80. option_reg=0x07;
    81. tmr0=0;
    82. }
    83.  
     
    Last edited by a moderator: Feb 3, 2013
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    991
    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.
     
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Perhaps you could post some details of this mysterious floattostr(result,resultchr); function.
     
  4. nanoid

    Thread Starter New Member

    Feb 3, 2013
    3
    0
    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
     
  5. MrChips

    Moderator

    Oct 2, 2009
    12,432
    3,360
    Do the steps separately.

    Code ( (Unknown Language)):
    1.  
    2. float a;
    3. float b;
    4. float result;
    5.  
    6. a = 3;
    7. b = 2;
    8. result = a/b;
    9.  
     
    nanoid likes this.
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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.
     
    nanoid and thatoneguy like this.
  7. nanoid

    Thread Starter New Member

    Feb 3, 2013
    3
    0
    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?
     
  8. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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
     
Loading...