LM35 temperature conversion problem

Discussion in 'Embedded Systems and Microcontrollers' started by PY01A0080, May 7, 2013.

  1. PY01A0080

    Thread Starter Member

    Mar 7, 2013
    42
    3
    HI to all,
    i found a code for interfacing 8051 with LM35 and LCD which is not working very well.
    1-The problem is: The temperature conversion is not good, for example when i ajdust LM35 to 89°C the LCD displays 39°C or for 30°C the LCD displays 09°C.

    2- When running Proteus, if i change the value of LM35, the LCD value do not change lively, i have to stop simulation and restart the simulation.

    Can you please check, i tried for all week-end and i did not fix.:(

    Thank you
    Have a nice day

    Here is the code found with google.

    Code ( (Unknown Language)):
    1. #include<reg51.h>
    2.  
    3.  
    4. #define port P3
    5. #define port0 P0
    6. #define adc_input P1
    7. #define dataport P2
    8. #define sec 100
    9.  
    10. sbit rs = port^0;
    11. sbit rw = port^1;
    12. sbit e = port^2;
    13.  
    14. sbit wr= port^3;
    15. sbit rd= port^4;
    16. sbit intr= port^5;
    17. int test_final=0,test_intermediate1[10],test_intermediate2[3]={0,0,0},test_intermediate3=0;
    18.  
    19. void delay(unsigned int msec )
    20. {
    21. int i ,j ;
    22. for(i=0;i<msec;i++)
    23.   for(j=0; j<1275; j++);
    24. }
    25.  
    26. void lcd_cmd(unsigned char item)  //Function to send command to LCD
    27. {
    28. dataport = item;
    29. rs= 0;
    30. rw=0;
    31. e=1;
    32. delay(1);
    33. e=0;
    34. return;
    35. }
    36.  
    37. void lcd_data(unsigned char item) //Function to send data to LCD
    38. {
    39. dataport = item;
    40. rs= 1;
    41. rw=0;
    42. e=1;
    43. delay(1);
    44. e=0;
    45. //delay(100);
    46. return;
    47. }
    48.  
    49. void lcd_data_string(unsigned char *str)  // Function to send string to LCD
    50. {
    51. int i=0;
    52. while(str[i]!='\0')
    53. {
    54.   lcd_data(str[i]);
    55.   i++;
    56.   delay(10);
    57. }
    58. return;
    59. }
    60.  
    61. void shape()     // Function to create the shape of degree
    62. {
    63. lcd_cmd(64);
    64. lcd_data(2);
    65. lcd_data(5);
    66. lcd_data(2);
    67. lcd_data(0);
    68. lcd_data(0);
    69. lcd_data(0);
    70. lcd_data(0);
    71. lcd_data(0);
    72. }
    73.    
    74. void convert()    // Function to convert the values of ADC into numeric value to be sent to LCD
    75. {
    76. int s;
    77. test_final=test_intermediate3;
    78. lcd_cmd(0xc1);
    79. delay(2);
    80. lcd_data_string("TEMP:");
    81. s=test_final/100;
    82. test_final=test_final%100;
    83. lcd_cmd(0xc8);
    84. if(s!=0)
    85. lcd_data(s+48);
    86. else
    87. lcd_cmd(0x06);
    88. s=test_final/10;
    89. test_final=test_final%10;
    90. lcd_data(s+48);
    91. lcd_data(test_final+48);
    92. lcd_data(0);
    93. lcd_data('c');
    94. lcd_data(' ');
    95. delay(2);
    96. }
    97.  
    98. void main()
    99. {
    100. int i,j;
    101. adc_input=0xff;
    102. lcd_cmd(0x38);
    103. lcd_cmd(0x0c);  //Display On, Cursor  Blinking
    104. delay(2);
    105. lcd_cmd(0x01);  // Clear Screen
    106. delay(2);
    107. while(1)
    108. {
    109.   for(j=0;j<3;j++)
    110.   {
    111.    for(i=0;i<10;i++)
    112.    {
    113.     delay(1);
    114.     rd=1;
    115.     wr=0;
    116.     delay(1);
    117.     wr=1;
    118.     while(intr==1);
    119.     rd=0;
    120.     lcd_cmd(0x88);
    121.     test_intermediate1[i]=adc_input/10;
    122.     delay(1);
    123.     intr=1;
    124.    }
    125.    for(i=0;i<10;i++)
    126.    test_intermediate2[j]=test_intermediate1[i]+test_intermediate2[j];
    127.    
    128.   }
    129.  
    130. test_intermediate2[0]=test_intermediate2[0]/3;
    131. test_intermediate2[1]=test_intermediate2[1]/3;
    132. test_intermediate2[2]=test_intermediate2[2]/3;
    133. test_intermediate3=test_intermediate2[0]+test_intermediate2[1]+test_intermediate2[2];
    134. shape();
    135. convert();
    136.                  
    137. }
    138.                                                    
    139. }[/i][/i][/i][/i]
     
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Sounds like you should try writing the code yourself, that way you might understand how it works, and understand any probable causes.

    This is probably a school assignment. As such, you will be hard pressed to find someone willing to do the work for you, because then you are left without an understanding of how it works, as evidenced by this post.
     
  3. PY01A0080

    Thread Starter Member

    Mar 7, 2013
    42
    3
    Thank you sir, but i'm not a student, i'm 40 year old and i want to learn 8051 by myself,
    it's not an assignement or anything else.
    But you are right, at this moment i already resolved my problem myself and it's working well.
    Thank you for your support
     
  4. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Good! It's usually much harder to try to figure out how someone did something instead of doing it yourself (as far as code is concerned)...
     
  5. MrChips

    Moderator

    Oct 2, 2009
    12,425
    3,359
    Separate the problem into two separate problems:

    1) Display readout
    2) Temperature conversion

    Adapt your program to output numbers from 0 to 99.
    Test that the program works correctly.

    After you have confirmed this is ok we can tackle the next problem.
     
Loading...