Seeking Help for Code Explanation

This is a discussion on Seeking Help for Code Explanation within the C Programming forums, part of the General Programming Boards category; Hi there, I am new to c programming and I am current studying the example codes. I am having difficulty ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    1

    Seeking Help for Code Explanation

    Hi there,
    I am new to c programming and I am current studying the example codes. I am having difficulty to understand the line
    Code:
    lcd_data(s+48);
    . I wish to know what does this line meas and what will it done.

    Here is the full codes:
    Code:
    //Program to interface IR sensor using ADC 0804. Set Vref =1.5v for ADC 0804
    
    
    #include<reg51.h>
    #define port P3
    #define adc_input P1
    #define dataport P0
    #define sec 100
    sbit rs = port^0;
    sbit rw = port^1;
    sbit e = port^2;
    
    
    sbit wr= port^3;
    sbit rd= port^4;
    sbit intr= port^5;
    
    
    int test_final=0 ,shift=0;
    
    
    void delay(unsigned int msec ) // Time delay function
    {
    int i ,j ;
    for(i=0;i<msec;i++)
      for(j=0; j<1275; j++);
    }
    
    
    void lcd_cmd(unsigned char item) // Function to send command to LCD
    {
    dataport = item;
    rs= 0;
    rw=0;
    e=1;
    delay(1);
    e=0;
    return;
    }
    
    
    void lcd_data(unsigned char item)  // Function to send data to LCD
    {
    dataport = item;
    rs= 1;
    rw=0;
    e=1;
    delay(1);
    e=0;
    return;
    }
    
    
    void lcd_data_string(unsigned char *str) // Function to send string to LCD
    {
    int i=0;
    while(str[i]!='\0')
    {
      lcd_data(str[i]);
      i++;
      delay(1);
    }
    return;
    }
    
    
    void convert()
    {
    int s;
    lcd_cmd(0x81);
    delay(2);
    lcd_data_string("output:");
    s=test_final/100;
    test_final=test_final0;
    lcd_cmd(0x8a);
    if(s!=0)
    lcd_data(s+48);
    else
    lcd_cmd(0x06);
    s=test_final/10;
    test_final=test_final;
    lcd_data(s+48);
    lcd_data(test_final+48);
    lcd_data(' ');
    if(shift>16)
    {
    lcd_cmd(0xc0+(shift-1));
    lcd_data_string("       ");
    shift=0; 
    }
    lcd_cmd(0xc0+(shift-1));
    lcd_data(' ');
    lcd_cmd(0xc0+shift);
    lcd_data_string("CALIBRATE IT");
    delay(30);
    }
    
    
    void main()
    {
    adc_input=0xff;
    lcd_cmd(0x38);  //2 Line, 5X7 Matrix
    lcd_cmd(0x0c);  //Display On, Cursor Blink
    delay(2);
    lcd_cmd(0x01);  // Clear Screen
    delay(2);
    lcd_cmd(0x81);  // Setting cursor to first position of first line
    delay(2);
    while(1)
    {
      shift++;
      delay(1);
      rd=1;
      wr=0;
      delay(1);
      wr=1;
      while(intr==1);
      rd=0;
      test_final=adc_input;
      delay(1);
      intr=1;
      convert();
    }
    }
    Please guide me through and I will appreciate it very much.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    5,852
    That code is highly specific to ADC 0804 and your IR sensor. You will need to read documentation for those to find out what the value 48 represents. The fact the code is a (bastardised) C does not mean that C programmers can explain what that code does.

    48 in that code is what is called a "magic number" - something that is meaningless voodoo unless you happen to be able to read the mind of the programmer who wrote the code.

    Adding 48 is presumably doing some mapping that makes sense if you have detailed knowledge of the ADC 0804 and your IR sensor.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Beginner1987 View Post
    Hi there,
    I am new to c programming and I am current studying the example codes. I am having difficulty to understand the line
    Code:
    lcd_data(s+48);
    . I wish to know what does this line meas and what will it done.
    A question... Are you studying C as a general programming language or as some specific dialect for this particular microcontroller chip?

    If you are studying generalized C, this is a truly horrible example.

    On the other hand if you are studying specifically about this uC chip it might be just what you need.


    As Grumpy (who is well named, btw) points out just because it says it's C code doesn't mean we magically understand it.

    The code in question calls the lcd_data function that begins on line 42 with a parameter value of s+48 ... So if s = 5 it calls the function with 53... Inside the function the value is assigned to dataport (which is defined as p0) and I have no idea what the rest of it is doing. My guess is that it displays a digit on the lcd display but without studying the uC's documentation I cannot be sure of anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. asking for a code explanation
    By hayview in forum C Programming
    Replies: 3
    Last Post: 10-07-2011, 10:33 PM
  2. Seeking explanation
    By Lonehwolf in forum C Programming
    Replies: 7
    Last Post: 11-29-2010, 12:39 PM
  3. Code explanation
    By Kong_Han_Dao in forum C Programming
    Replies: 4
    Last Post: 02-22-2010, 11:03 AM
  4. Code Explanation
    By Aliaks in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2009, 10:37 PM
  5. code explanation
    By chasingxsuns in forum C Programming
    Replies: 2
    Last Post: 02-04-2006, 12:47 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21