1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Problem in C coding of 89c51; urgent help required.

Discussion in '8051/8951' started by Qaisar Azeemi, Feb 28, 2012.

  1. Qaisar Azeemi

    Qaisar Azeemi Member

    Joined:
    Oct 24, 2011
    Messages:
    157
    Likes:
    1
    Location:
    South of Asia
    Hi all;

    i have interfaced lcd and adc with my 89c51 controller and want to make a digital thermometer, that display the temprature in both C and F scales. i write code for it but got the following problems.

    DVM(8): error C202: 'Port': undefined identifier
    DVM(9): error C202: 'Port': undefined identifier
    DVM(10): error C202: 'Port': undefined identifier
    DVM(11): error C202: 'Port': undefined identifier
    DVM(12): error C202: 'Port': undefined identifier
    DVM(13): error C202: 'Port': undefined identifier
    DVM(14): error C202: 'Port': undefined identifier
    DVM(35): error C202: 'RS': undefined identifier
    DVM(36): error C202: 'E': undefined identifier
    DVM(38): error C202: 'E': undefined identifier
    DVM(49): error C202: 'RS': undefined identifier
    DVM(50): error C202: 'E': undefined identifier
    DVM(52): error C202: 'E': undefined identifier
    DVM(59): error C141: syntax error near 'char', expected ')'
    DVM(62): error C202: 'str': undefined identifier
    DVM(64): error C202: 'str': undefined identifier
    DVM(190): error C202: 'ALE': undefined identifier
    DVM(191): error C202: 'SC': undefined identifier
    DVM(194): error C202: 'ALE': undefined identifier
    DVM(196): error C202: 'SC': undefined identifier
    DVM(199): error C202: 'ALE': undefined identifier
    DVM(201): error C202: 'SC': undefined identifier
    DVM(213): error C202: 'test_inermediate2': undefined identifier
    Target not created






    i searched it out but cant solve it.
    here is my code.......




    Code (text):
    #include <reg51.h>

    #define port P3 // control port
    #define adc_input P1
    #define data_port P0 // data from microcontroller to LCD


    sbit ADC_A=Port^0; //ADC
    sbit ADC_B=Port^1; //ADC
    sbit RS=Port^2; //LCD
    sbit E=Port^3; //LCD
    sbit SC=Port^4; //ADC
    sbit CLK=Port^5; //ADC
    sbit ALE=Port^6; //ADC

    int test_intermediate3=0, test_final=0;
    int test_intermediate1[10], test_intermediate2[3]={0,0,0};

    //------------------- . ------------------

    void delay(unsigned int msec)
    {
    int i,j;
    for(i=0; i=msec; i++)
    for(j=0; j=1275; j++);

    }

    //.................. LCD .................


    void lcd_cmd( unsigned int item )
    {
    data_port=item; //command on data port p0 of lcd
    RS=0; // selecting command register of lcd
    E=1;
    delay(1);
    E=0;
    return;


    }



    void lcd_data( unsigned int item )
    {
    data_port=item; //data on data port p0 of lcd
    RS=1; // selecting data register of lcd
    E=1;
    delay(1);
    E=0;
    return;


    }


    void lcd_data_string(usinged char *str) // sending string to LCD
    {
    int i=0;
    while (str[i] != '\0')
    {
    lcd_data(str[i]);
    i++;
    delay(10);
    return;

    }

    }


    //----------- custom character Generation for Degree symbol-----------

    void shape()
    {

    lcd_cmd(64); // first location in CGRAM
    lcd_cmd(2);
    lcd_cmd(5);
    lcd_cmd(2);
    lcd_cmd(0);
    lcd_cmd(0);
    lcd_cmd(0);
    lcd_cmd(0);
    lcd_cmd(0);

    return;
    }



    // Converting binary value of ADC to ASCII value of LCD

    void convert()
    {
    int s;
    lcd_cmd(0x81);
    delay(2);
    lcd_data_string("Temp: "); // Farnheit conversion

    test_final=(((9*test_intermediate3)/5)+32);

    s=test_final/100;
    test_final=test_final%100;

    lcd_cmd(0x88);
    if (s!=0)
    lcd_data(s+48);
    else
    lcd_cmd(0x06);



    s=test_final/10;
    test_final=test_final%10;

    lcd_data(s+48);
    lcd_data(test_final+48);
    lcd_data(0);
    lcd_data('F');
    lcd_data(' ');


    //------------------- for Celcius scale----------------

    test_final=test_intermediate3;

    lcd_cmd(0xC1); // set cursor to second line.
    delay(2);
    lcd_data_string("Temp: ");
    s=test_final/100;
    test_final=test_final%100;

    lcd_cmd(0xc8);
    if (s!=0)
    lcd_data(s+48);
    else
    lcd_cmd(0x06);



    s=test_final/10;
    test_final=test_final%10;

    lcd_data(s+48);
    lcd_data(test_final+48);
    lcd_data(0);
    lcd_data('C');
    lcd_data(' ');

    delay(2);

    }








    //.........--------.......Main Program.......-------........








    void main()
    {

    int i,j;
    adc_input=0xff ; // making p1 as input port
    lcd_cmd(0x38);
    lcd_cmd(0x0c);
    delay(2);
    lcd_cmd(0x01);
    delay(2);
    while(1)
    {
    for(j=0;j<3;j++)
    {
    for(i=0;i<10;i++)
    {
    delay(1);
    ALE=0;
    SC=0;
    delay(1);

    ALE=1;
    delay(1);
    SC=1;
    delay(1);

    ALE=0;
    delay(1);
    SC=0;
    delay(4);

    lcd_cmd(0x88);

    test_intermediate1[i]=adc_input/10;
    delay(1);

    }


    for(i=0;i<10;i++)
    test_inermediate2[j]+=test_intermediate1[i];
    }


    test_intermediate2[0]=test_intermediate2[0]/3;
    test_intermediate2[1]=test_intermediate2[1]/3;
    test_intermediate2[2]=test_intermediate2[2]/3;

    test_intermediate3=test_intermediate2[0] + test_intermediate2[1] + test_intermediate2[2];

    shape();

    convert();
    }
    }





    please help me to solve my problem.

    Thank you
     
    Last edited by a moderator: Feb 29, 2012
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,251
    Likes:
    911
    Location:
    Rochdale UK
    C is case sensitive..... When you defined... "port P3" you have use a lower case 'P' then used an uppercase to reference it.....
     
    • Like Like x 1
  3. cowboybob

    cowboybob Well-Known Member Most Helpful Member

    Joined:
    Oct 22, 2011
    Messages:
    3,039
    Likes:
    478
    Location:
    James Island, SC
    Plus one spelling error:

     
    Last edited: Feb 28, 2012
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,251
    Likes:
    911
    Location:
    Rochdale UK

    Finally
    Spelling error number two.
     
  6. Qaisar Azeemi

    Qaisar Azeemi Member

    Joined:
    Oct 24, 2011
    Messages:
    157
    Likes:
    1
    Location:
    South of Asia
    Thank you very much fellows....

    the problem is solved with your provided help.... Thanks a lot again.

    acctually this is my first time that i make a complete program in C... which is not pure my own... but it is my practice...



    The above program is successfully compiled but now the problem is that it is not simulating in proteous... outputs of the controller are quite still, no movement and no change in the output pin bits is seen.. every signal is quite still and stuck.... i have used infinite while loop in main and also scanning the ADC channel 0 continuously... but can't understand the problem???
     
  7. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,251
    Likes:
    911
    Location:
    Rochdale UK
    Reset?.... chip wont run in reset. OSC settings?... There are a few things that may be wrong.
     
  8. Qaisar Azeemi

    Qaisar Azeemi Member

    Joined:
    Oct 24, 2011
    Messages:
    157
    Likes:
    1
    Location:
    South of Asia
    you mean connecting resistora and capasitor to the reset pin of the controller.....

    if yes then hardware assembly is OK, i connect it with reset pin of 89c51 and it is running other programs. i think problem is in my code.
     
  9. Qaisar Azeemi

    Qaisar Azeemi Member

    Joined:
    Oct 24, 2011
    Messages:
    157
    Likes:
    1
    Location:
    South of Asia
    i make the following code to make a thermometer but i got the problem in the output at port 0 which is stuck and still at lcd_cmd(0x38) command in main program.

    following is the code.


    Code (text):


    #include <reg52.h>


     #define Port P3  // control port
     #define adc_input P1
     #define data_port P0  // data from microcontroller to LCD
     #define sec=100

     sbit ADC_A=Port^0; //ADC
     sbit ADC_B=Port^1; //ADC
     sbit RS=Port^2;        //LCD
     sbit E=Port^3;        //LCD
     sbit SC=Port^4;   //ADC
     sbit CLK=Port^5;  //ADC
     sbit ALE=Port^6;  //ADC

    int test_intermediate3=0, test_final=0;
    int test_intermediate1[10], test_intermediate2[3]={0,0,0};

             //------------------- . ------------------

            void delay(unsigned int msec)
            {
                int i,j;
                for(i=0; i=msec; i++)
                    for(j=0; j=1275; j++);

            }




            //---------------- timer 0 interrupt----------------

            void timer0() interrupt 1
            {
                   
                        CLK=~CLK;       // Toggle port bit p3.5
                        TR0=0;         // stop timer
                        TH0=0xff;       // 500hz clock for adc
                        TL0=0xff;
                        TR0=1;          // start timer
            }



            //.................. LCD .................


            void lcd_cmd( unsigned int item )
            {
                data_port=item;  //command on data port p0 of lcd
                RS=0; // selecting command register of lcd
                E=1;
                delay(10);
                E=0;
                return;
            }




            void lcd_data( unsigned int item )
            {
                data_port=item;  //data on data port p0 of lcd
                RS=1; // selecting data register of lcd
                E=1;
                delay(10);
                E=0;
                return;
            }



          void lcd_data_string(unsigned char *str)  // sending string to LCD
          {
                int i=0;
                while (str[i] != '\0')
                    {
                        lcd_data(str[i]);
                        i++;
                        delay(10);
                        return;

                    }
         
          }


          //----------- custom character Generation for Degree symbol-----------

          void shape()
          {
               
                lcd_cmd(64); // first location in CGRAM
                lcd_cmd(2);
                lcd_cmd(5);
                lcd_cmd(2);
                lcd_cmd(0);
                lcd_cmd(0);
                lcd_cmd(0);
                lcd_cmd(0);
                lcd_cmd(0);

                return;
          }



          // Converting binary value of ADC to ASCII value of LCD

          void convert()
          {
                int s;
                lcd_cmd(0x81);
                delay(2);
                lcd_data_string("Temp: ");   // Farnheit conversion

                test_final=(((9*test_intermediate3)/5)+32);
               
                s=test_final/100;
                test_final=test_final%100;

                lcd_cmd(0x88);
                if (s!=0)
                lcd_data(s+48);
                else
                lcd_cmd(0x06);



              s=test_final/10;
              test_final=test_final%10;

              lcd_data(s+48);
              lcd_data(test_final+48);
              lcd_data(0);
              lcd_data('F');
              lcd_data(' ');


            //------------------- for Celcius scale----------------

              test_final=test_intermediate3;

              lcd_cmd(0xC1);  // set cursor to second line.
              delay(2);
              lcd_data_string("Temp: ");
              s=test_final/100;
                test_final=test_final%100;

                lcd_cmd(0xc8);
                if (s!=0)
                lcd_data(s+48);
                else
                lcd_cmd(0x06);



              s=test_final/10;
              test_final=test_final%10;

              lcd_data(s+48);
              lcd_data(test_final+48);
              lcd_data(0);
              lcd_data('C');
              lcd_data(' ');

              delay(2);

          }








          //.........--------.......Main Program.......-------........








               void main()
               {

                    int i,j;
                    adc_input=0xff; // making p1 as input port
                    TMOD=0x01;     // timer 0,  mode 1
                    TH0=0xff;       // 500hz clock for adc
                    TL0=0xff;
                    TR0=1;         // timer start

                    IE=0x82;        // interrupt enable ; timer interrupt and globle interrupt.
                   
                    lcd_cmd(0x38);      // initializing LCD module
                       
                    lcd_cmd(0x0c);
                   
                    lcd_cmd(0x01);
                   
                    lcd_cmd(0x06);
                   
                    lcd_cmd(0x81);

                    lcd_data_string("Temp: ");

                    while(1)
                    {
                            for(j=0;j<3;j++)
                            {
                                for(i=0;i<10;i++)
                                {
                                       ADC_A=0;
                                       ADC_B=0; // channel 0 is selected

                                        delay(1);
                                        ALE=0;
                                        SC=0;
                                        delay(1);

                                        ALE=1;
                                        delay(1);
                                         SC=1;
                                         delay(1);

                                         ALE=0;
                                         delay(1);
                                         SC=0;
                                         delay(4);

                                         lcd_cmd(0x88);

                                         test_intermediate1[i]=adc_input/10;
                                         delay(1);

                                }


                                for(i=0;i<10;i++)
                                test_intermediate2[j]+=test_intermediate1[i];
                            }


                            test_intermediate2[0]=test_intermediate2[0]/3;
                            test_intermediate2[1]=test_intermediate2[1]/3;
                            test_intermediate2[2]=test_intermediate2[2]/3;

                            test_intermediate3=test_intermediate2[0] + test_intermediate2[1] + test_intermediate2[2];

                            shape();

                            convert();
                    }
               }
     
    and following is the immage of the ckt in proteous:

    View attachment 61563

    i am tring to solve it from many hours but still im faild... i cant understand what the problem is in my code??? i need urgent help plz... :-(
     
  10. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,251
    Likes:
    911
    Location:
    Rochdale UK
    Code (text):

    void delay(unsigned int msec)
            {
                int i,j;
                for(i=0; i=msec; i++)     ----------->> Whoops... this is the assignment operator.....You need to check for equality  '=='
                    for(j=0; j=1275; j++);
     
            }

     
    Change to
    Code (text):

    void delay(unsigned int msec)
            {
                int i,j;
                for(i=0; i==msec; i++)
                    for(j=0; j==1275; j++);
     
            }
     
    Or the variables will just sit at the same value.
     
    • Like Like x 1
  11. Qaisar Azeemi

    Qaisar Azeemi Member

    Joined:
    Oct 24, 2011
    Messages:
    157
    Likes:
    1
    Location:
    South of Asia
    Thank you Mr Ian thanks a lot... i did it :)

    problem solved after your recommended changes.

    but still some logical problems are there that i will post here tomarrow :)

    Thanks a lot again.
     
  12. Qaisar Azeemi

    Qaisar Azeemi Member

    Joined:
    Oct 24, 2011
    Messages:
    157
    Likes:
    1
    Location:
    South of Asia
    Can you please give me any routine to dispaly Temp: on my lcd screen... here in my program it is displaying only T... the code is as follows.

    Code (text):

    void lcd_data_string(unsigned char *str)  // sending string to LCD
          {
                int i=0;
                while (str[i] != '\0')
                    {
                        lcd_data(str[i]);
                        i++;
                        delay(10);
                        return;
     
                    }
     

    and to call this i am using :

    Code (text):

    lcd_data_string("Temp: ");
     
    please help.
     
  13. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,251
    Likes:
    911
    Location:
    Rochdale UK
    My routines are a tad shorter

    Code (text):

    void lcd_data_string(unsigned char *str)  // sending string to LCD
          {
          while (*str  != '\0')
            {
            lcd_data(*str++);
            delay(10);
            }
        }
     
    You don't need a return as the function is void.
     
    • Like Like x 1
  14. Qaisar Azeemi

    Qaisar Azeemi Member

    Joined:
    Oct 24, 2011
    Messages:
    157
    Likes:
    1
    Location:
    South of Asia
    Thank you Ian...
    and how to calculate the delay time of this function. my Xtal oscillator is 12MHz.

    Code (text):

    void delay(unsigned int msec)
            {
                int i,j;
                for(i=0; i==msec; i++)
                    for(j=0; j==1275; j++);

            }
     
     
  15. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,251
    Likes:
    911
    Location:
    Rochdale UK
    12mhz on a chip that has 12 clocks per instruction.... 1 million instructions per second.... 1/1000000 = 1uS, 1000 * 1uS = 1mS.... so you need to count 1000 times.... BUT!!!! the for loop will take quite a few clock cycles AND you are increasing a word not a byte... this will be about 6 clock cycles (possibly more) .. so 1000 / 6 = 167 ... Start with 167 on the J loop ( remember to deduct the same amount from the I loop ).. Pass 1 to the delay loop (have you a simulator?) and 1000 to the delay loop.. and check it.
     
  16. Qaisar Azeemi

    Qaisar Azeemi Member

    Joined:
    Oct 24, 2011
    Messages:
    157
    Likes:
    1
    Location:
    South of Asia
    i have proteous and multisim EWB...
    how to check the execution of code there...

    and from your explained post above i come to know that the above loop is for 1 msec if i pass 1 to the funtion. am i right???
     
  17. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,251
    Likes:
    911
    Location:
    Rochdale UK
    If you have proteus.... What compiler do you use?... If you can low level debug you can place a break point before the delay to test it.
     
  18. Qaisar Azeemi

    Qaisar Azeemi Member

    Joined:
    Oct 24, 2011
    Messages:
    157
    Likes:
    1
    Location:
    South of Asia
    i am using Keil microvision 4.
     
  19. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,251
    Likes:
    911
    Location:
    Rochdale UK
    I have just simulated the for loop and the for loop takes 7 clock cycles..... if j ==167 gives 1.17 mS So you'll have to go a tad smaller.... Say j == 143 (1.001mS) ... works out near enough... But remember the 'i' for loop takes 7uS to execute as well, so each iteration will need adjustment of 7uS per call, so 142 will be better.
     
    Last edited: Mar 5, 2012
  20. Qaisar Azeemi

    Qaisar Azeemi Member

    Joined:
    Oct 24, 2011
    Messages:
    157
    Likes:
    1
    Location:
    South of Asia
    how can i samulate it to know the execution times.. please post the steps and also what compilar and Simulation software are you using??
     
  21. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,251
    Likes:
    911
    Location:
    Rochdale UK
    You don't need to now... I've done it....I have MCU 8051 IDE with SDCC.... works pretty well... it doesn't chuck out a coff file for debugging but there is a simulator in the IDE...The hex file goes directly into proteus.
     

Share This Page