Help me with this code

Discussion in 'Embedded Systems and Microcontrollers' started by achm3d, Sep 25, 2012.

  1. achm3d

    Thread Starter New Member

    Sep 25, 2012
    1
    0
    Code ( (Unknown Language)):
    1.  
    2. // Program to interface Sharps distance sensor. Set vref =3v for ADC0804 #include<reg51.h>
    3.  #define port P3
    4.  #define adc_input P1
    5.  #define dataport P0
    6.  #define sec 100
    7.  sbit rs = port^0;
    8.  sbit rw = port^1;
    9.  sbit e = port^2;
    10.  sbit wr= port^3;
    11.  sbit rd= port^4;
    12.  sbit intr= port^5;
    13.  int test_final=0 ,shift=0;
    14.  void delay(unsigned int msec )
    15.  {
    16.  int i ,j ;
    17.  for(i=0;i<msec;i++)
    18.  for(j=0; j<1275; j++);
    19.  }
    20.  void lcd_cmd(unsigned char item) // Function to send command to LCD
    21.  {
    22.  dataport = item;
    23.  rs= 0;
    24.  rw=0;
    25.  e=1;
    26.  delay(1);
    27.  e=0;
    28.  return;
    29.  }
    30.  void lcd_data(unsigned char item) // Function to send data to LCD
    31.  {
    32.  dataport = item;
    33.  rs= 1;
    34.  rw=0;
    35.  e=1;
    36.  delay(1);
    37.  
    38.  e=0;
    39.  return;
    40.  }
    41.  void lcd_data_string(unsigned char *str) // Function to send string to LCD
    42.  {
    43.  int i=0;
    44.  while(str[i]!='\0')
    45.  {
    46.  lcd_data(str[i]);
    47.  i++;
    48.  delay(1);
    49.  }
    50.  return;
    51.  }
    52.  void convert()
    53.  {
    54.  int s;
    55.  lcd_cmd(0x81);
    56.  delay(2);
    57.  lcd_data_string("output:");
    58.  s=test_final/100;
    59.  test_final=test_final%100;
    60.  lcd_cmd(0x8a);
    61.  if(s!=0)
    62.  lcd_data(s+48);
    63.  else
    64.  lcd_cmd(0x06);
    65.  s=test_final/10;
    66.  test_final=test_final%10;
    67.  lcd_data(s+48);
    68.  lcd_data(test_final+48);
    69.  lcd_data(' ');
    70.  if(shift>16)
    71.  {
    72.  lcd_cmd(0xc0+(shift-1));
    73.  lcd_data_string("       ");
    74.  shift=0;
    75.  }
    76.  lcd_cmd(0xc0+(shift-1));
    77.  lcd_data(' ');
    78.  lcd_cmd(0xc0+shift);
    79.  lcd_data_string("CALIBRATE IT");
    80.  delay(30);
    81.  }
    82.  void main()
    83.  {
    84.  adc_input=0xff;
    85.  lcd_cmd(0x38);  //2 Line, 5X7 Matrix
    86.  lcd_cmd(0x0c);  //Display On, Cursor Blink
    87.  delay(2);
    88.  lcd_cmd(0x01);  // Clear Screen
    89.  delay(2);
    90.  lcd_cmd(0x81);
    91. delay(2);
    92.  lcd_data_string(" start");
    93.  delay(20);
    94.  while(1)
    95.  {
    96.    shift++;
    97.    delay(1);
    98.    rd=1;
    99.    wr=0;
    100.    delay(1);
    101.    wr=1;
    102.    while(intr==1);
    103.    rd=0;
    104.    test_final=adc_input;
    105.    delay(1);
    106.    intr=1;
    107.    convert();
    108.  }
    109.  }
    110. [/i][/i]


    // so here is my code but somehow i feel that may be i am missing an header file or something.can someone help me out to verify this code please. and if someone could convert this code into a .HEX file it would be of great help.

    *** microcontroller i am using is AT89C51

    THANKS IN ADVANCE! :)
     
    Last edited by a moderator: Sep 25, 2012
Loading...