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

calculator program 8051

Discussion in '8051/8951' started by ametepe hounse, Mar 5, 2018.

  1. ametepe hounse

    ametepe hounse New Member

    Joined:
    Dec 30, 2017
    Messages:
    2
    Likes:
    0
    hey guys i need help
     
  2. ametepe hounse

    ametepe hounse New Member

    Joined:
    Dec 30, 2017
    Messages:
    2
    Likes:
    0
    i have code in c++ but when i compile the code it show something like this : 3 19 C:\Users\HP User\Documents\simple calculator code#1.cpp [Error] reg51.h: No such file or directory
    compilation terminated.
    please can you guys help me. the code is for a simple calculator

    Code (C):

    // interfacing Keypad and LCD with 8051 to design a simple calculator: reg51.h

    #include <reg51.h>
    #include <stdio.h>

    void init ();
    void command (unsigned int);
    void write_data(unsigned char);
    void dilay(unsigned char);
    void msDelay(unsigned int);
    char process_key(int, int, int, int, char, char, char, char);
    void num_generator(char, int*);
    void write_result(int, char);

    sbit ER = P3^2;  //Enable LCD
    sbit RN = P3^3;  //Read write
    sbit RS = P3^4;  //Register select

    void main()
    {
        int i, j, *operand, opereand1 = 0, operand2 = 0, result = 0;
        int i_arr[4][4] = {{0,1,1,1},{1,0,1,1},{1,1,0,1,},{1,1,1,0}};
        char pressed_key, operator1 = 'a', negative='N';
        char c_arr[4][4] = {{'7','4','1','c'},{'8','5','2','0'},{'9','6','3','='},{'/','*','-','+'}},error[10]={'M','A','T','H',' ','E','R','R','O','R'};
        }
        P1 = 0xff;
        operean = &operand1;
        while(1)
        {
            for(i=0; i<4; i++)
            {
                pressed_key = process_key(i_arr[I][0], i_arr[I][1], i_arr[I][2], i_arr[I][3], c_arr[I][0], c_arr[I][1], c_arr[I][2], c_arr[I][3]);
                if(pressed_key=='/'||pressed_key=='*'||pressed_key=='-'||pressed_key=='+')
                {
                    operanr = &opeerand2;
                    operator1 = pressed_key;
                }
            if((pressed_key != 'Z')&&(pressed_key!='=')&&(pressed_key!='/')&&(pressed_key!='*')&&(pressed_key!'-')&&(preesed_key!='+'))
                {
                    num_generator(pressed_key, operand);
                }
                //Make a function generator_result
                if(pressed_key == '=')
                {
                    // Make a function generator_result in which all mess below this line will be processed..
                    if(operator1=='*')
                    result = operand1*operand2;
                 
                    if(operator1=='/')
                    if(operand2==0)
                    {
                        command(0xC0);
                        for(j=0; j<10; j++)
                        write_data(error[j]);
                        msDelay(500);
                        init();
                    }
                    else
                    result = operand1/operand2;
                   
                    if(operator1=='-')
                    {
                        if(operand1>operand2)
                        result = operand1-operand2;
                        else
                        {
                            result = operand2-operand1;
                            command(0xC0);
                            negative ='Y';
                        }
                    }
                   
                    if(operator1=='+')
                    result = operand1+operand2;
                   
                    write_result(result,negative);
                }
             
               }
        }
     
        // LCD initialaizer function
        void init()
        {
            delay(3500);
            command(0x38);
            dealy(3500);
            command(0x38);
            dealy(3500);
            command(0x38);
            dealy(350);
            command(0x38);
            command(0x1C);
            command(0x0E);
            command(0x06);
            command(0x01);
            delay(3500);
            command(0x00);
            main ();
        }
     
        // Send different commands to LCD
        void command(unsigned int comm)
        {
            RW = 0;
            RS = 0;
            P2 = comm;
            EN = 1;
            delay(3500);
            EN = 0;
        }
     
        // Process and check the press of key
        char process_key(int a, int b, int c, int d, char A, char B, char C, char D)
        {
            char ch ='Z';
            P1_O = a;
            P1_1 = b;
            P1_2 = c;
            P1_3 = d;
         
            if(P1_4==0)
            {
                ch = A;
            }
            if(P1_5==0)
            {
                ch = B;
            }
            if(P1_6==0)
            {
                ch = C;
            }
            if(P1_7==0)
            {
                if(D =='0')
                init();
                else
                ch = D;
            }
            if(ch!= 'Z')
            {
                write_data(ch);
                msDelay(100);
            }
            return ch;
        }
     
        // Used to write data in to char on LCD
        void write_data(unsigned char ch)
        {
            RW = 0;
            RS = 1;
            P2 = ch;
            EN = 1;
            delay(3500);
            delay(3500);
            EN = 0;
        }
     
        void num_generator(char ch,int *operand)
        {
            int digit;
            digit = ch - '0';
            *operand = digit + (*operand*10);
        }
     
        void write_result(int num,char neg)
        {
            int i=0, j, rem;
            char rev_num[20];
            command(0xC0);
            if(neg == 'Y')
            {
                write_data('-');
            }
            do
            {
                rem = num%10;
                num = num/10;
                rev_num[I] = (char)rem+'0';
                i++;
            }
            while(num>0);
            for(j=i-1; j>=0; j--)
            {
                RW = 0;
                RS = 1;
                P2 = rev_num[j];
                EN = 1;
                delay(3500);
                EN = 0;
            }
        }
     
        // Function for generation of delay
        void delay(umsigned char c)
        {
            unsigned int i;
            unsinged char j;
            for(i=o; i<=3; i++)
            {
                for(j=0; j<=c; j++);
            }
        }
     
        // Generates delay in milli seconds
        void msDelay(unsigned int time)
        {
            TL0 = 0xEF;
            TH0 = 0xAF;
            TR0 = 1;
            while(time--)
            {
                while(TF0 == 0);
                TF0 = o;
                TL0 = 0xEF;
                TH0 = 0xAF;
            }
            TR0 = 0;
        }

     
     
    Last edited by a moderator: Mar 5, 2018
  3. Ian Rogers

    Ian Rogers User Extraordinaire Forum Supporter Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,802
    Likes:
    958
    Location:
    Rochdale UK
    What compiler are you using? It cannot fine the reg51.h file..... That is the first line so it will also mean the directory structure of the linker hasn't been set!!
     
  4. dave miyares

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    2
    Likes:
    -10


     
  5. Cicero

    Cicero Active Member

    Joined:
    Nov 21, 2014
    Messages:
    410
    Likes:
    40
    Location:
    UK
    Guaranteed this is homework, and this chap didn't write any of that code.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice