Help: LM35 & ADC0831 interfacing with 8051

Discussion in 'Embedded Systems and Microcontrollers' started by hashim5003, Jan 16, 2014.

  1. hashim5003

    Thread Starter Member

    Apr 29, 2013
    32
    0
    Hello everyone!
    I am trying to use LM35 for temprature reading with 8051. I need to know that how to receive serial data from ADC0831 with 8051 and then send it to LCD? I have wrote some code but can't do the receiving part? Should I create an array to save the bytes received from adc or something else?
     
  2. ericgibbs

    AAC Fanatic!

    Jan 29, 2010
    2,503
    380
  3. hashim5003

    Thread Starter Member

    Apr 29, 2013
    32
    0
    I'm using c language.
     
  4. ActivePower

    Member

    Mar 15, 2012
    155
    23
    You need a single 8-bit variable (char) to read in the 8-bit value, say result. Create a loop to run 8 times and in each iteration read in a bit, shift the result by one and OR it with the bit you just read. No need to use an array.
     
  5. hashim5003

    Thread Starter Member

    Apr 29, 2013
    32
    0
    What's the reason for ORing?
     
  6. ActivePower

    Member

    Mar 15, 2012
    155
    23
    I meant bitwise OR, unless you already got that. We need it to keep 'adding' the incoming bits to the variable storing the result.

    For example,
    Code ( (Unknown Language)):
    1.  
    2. int a = 2;         // Binary = 010
    3. int b = a << 1;    // Binary = 100
    4. b = b | 1;        // Binary = 101
    5.  
     
  7. hashim5003

    Thread Starter Member

    Apr 29, 2013
    32
    0
    @ActivePower
    I have wrote the code from what I have understand:
    Is it correct?

    Code ( (Unknown Language)):
    1.  
    2. unsigned char read_data ()
    3. {
    4.     unsigned char i;
    5.     char result = 0;
    6.     char read;
    7.     for ( i = 0; i < 8; i++ )
    8.     {
    9.         read = result << 1;    //shift variable by one bit
    10.         clk = 1;                  //clock pulse
    11.         delay(5);
    12.         clk = 0;
    13.         delay(5);
    14.         result = result | read;
    15.     }
    16.     return read;                   //return data stored in variable
    17. }
    18.  
     
    Last edited: Jan 16, 2014
  8. hashim5003

    Thread Starter Member

    Apr 29, 2013
    32
    0
    ActivePower
    I have worked on the code. plz check if it is correct or not? thanks

    Code ( (Unknown Language)):
    1.  
    2. #include<reg51.h>
    3.  
    4. void delay (int);
    5. void lcd_init (void);
    6. void lcd_cmd (char);
    7. void lcd_data (char);
    8. //char read_data (char);
    9.  
    10. sbit RS = P1^0;
    11. sbit EN = P1^1;
    12. sbit CS = P1^2;
    13. sbit CK = P1^3;
    14. sbit DO = P1^4;
    15. char value;
    16.  
    17. void delay ( int a )
    18. {
    19.     int i,j;
    20.     for ( i = 0; i < a; i++ )
    21.         for ( j = 0; j < 1275; j++ );
    22. }
    23.  
    24. void lcd_init ()
    25. {
    26.     lcd_cmd(0x38);       
    27.     delay(5);
    28.     lcd_cmd(0x0F);      
    29.     delay(5);
    30.     lcd_cmd(0x0C);
    31.     delay(5);
    32.     lcd_cmd(0x80);
    33.     delay(5);
    34. }
    35.  
    36. void lcd_data ( char y )
    37. {
    38.     RS = 1;
    39.     P2 = y;
    40.     EN = 1;
    41.     delay(5);
    42.     EN = 0;
    43.     delay(5);
    44. }
    45.  
    46. void lcd_cmd ( char z )
    47. {
    48.     RS = 0;
    49.     P2 = z;
    50.     EN = 1;
    51.     delay(5);
    52.     EN = 0;
    53.     delay(5);
    54. }
    55.  
    56. unsigned char read_data()
    57. {
    58.     unsigned char i;
    59.     char result;
    60.    
    61.         CS = 1;
    62.         delay(5);
    63.         CS = 0;
    64.    
    65.         CK = 1;
    66.         delay(5);
    67.         CK = 0;
    68.         delay(5);
    69.         CK = 1;
    70.         delay(5);
    71.         CK = 0;
    72.         delay(5);
    73.    
    74.         for ( i = 0; i < 8; i++ )
    75.         {
    76.             CK = 1;
    77.             delay(5);
    78.             CK = 0;
    79.             delay(5);
    80.             result = result << 1;
    81.             result = result | DO;
    82.             delay(5);
    83.         }
    84.     return result;
    85. }
    86.  
    87. void main ()
    88. {
    89.     lcd_init();
    90.  
    91.     while(1)
    92.     {
    93.         value = read_data;
    94.         lcd_data (value);
    95.     }
    96. }
    97.  
     
  9. ActivePower

    Member

    Mar 15, 2012
    155
    23
    1. You didn't initialize the `result` variable in your final code and it will end up getting an arbitrary value. It's better to initialize it to 0x00.

    2. I think you intended to write read_data() in your main().

    3. Use comments. While your application is small enough not to require a lot of them, it is advisable to comment your code especially in sections where you got stuck before. You will be surprised to see how less sense the same tricky part makes months from now.

    4. Refrain from using magic numbers. The 1275 in your for() loop appears out of nowhere. It is better to have a define'd constant getting that value from a one-line computation. Again, this is more a friendly advice than a rule set in stone.

    5. Why do you need the global `value` variable. You're returning the result from your readADC function and writing it to the LCD, why introduce a global in the middle. You could get away with an automatic variable or a call like this:

    Code ( (Unknown Language)):
    1. lcd_data(read_data())
    Having a global complicates things - it gets hard to keep track of what's accessing what global really soon as your program grows.

    Otherwise, based on a quick skim it looks okay. Good work!
     
    Last edited: Jan 20, 2014
  10. hashim5003

    Thread Starter Member

    Apr 29, 2013
    32
    0
    Thanks Mr ActivePower for your help.
    I couldn't do that with adc0831.
    Although it worked but there were some complications.

    SO, I am going to use adc0804 with LM35 now.
    I will let you know if I need help with adc0804.
     
  11. Arm_n_Legs

    Active Member

    Mar 7, 2007
    183
    10
    If you like to use the MCP3002 instead, I have ready codes.
     
Loading...