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

Reading input port pin of 8051 using embedded c

Discussion in '8051/8951' started by Mystogan, Mar 11, 2013.

  1. Mystogan

    Mystogan New Member

    Joined:
    Jan 23, 2013
    Messages:
    15
    Likes:
    1
    Hi, everyone.
    I have just started using embedded c (in keil c compiler).I have used Arduino in the past. Whenever we want to know the current status of the input pins in Arduino,we code something like,

    int pin1=8;

    void setup()
    {
    PinMode(pin1,INPUT);
    }
    void loop()
    {
    int x;

    x=digitalRead(pin1); reading current state of pin1(8)
    }

    My question is what is the analogus for 'digitalRead' in embedded c?
    I have heard that we have to initialize all input ports to 1 . Is it true?
    Also,is it something like input pins automatically get updated in embedded c type programming?
    Please ,help me how to use inputs in c prog.
    Give a sample code for regular reading of input pins in embedded c.
    thanks..
     
    Last edited: Mar 11, 2013
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,165
    Likes:
    910
    Location:
    Rochdale UK
    C is at a lower level then C++ programming..

    You can write your own pin selection functions .... On the 8051 series there is no DDR ( Data direction register) To write to a pin you just " P1.0 = 1;" or P1.0 = 0;"... Reading however!! Is different.. You must put the port in a reading state first.... If you write a "1" to pin P1.0, The output FET is switched off, then you can read the state of the same pin... Some ports have internal pullup .... If the pin is low and you apply a high on the port.. There will be magic smoke... as the output FET will be on...

    The best example is the reading / writing to an LCD screen... The data port has to be read and written..... The change must be made BEFORE you enable write mode on the LCD..
     
  3. Mystogan

    Mystogan New Member

    Joined:
    Jan 23, 2013
    Messages:
    15
    Likes:
    1
    Thank you very much for the information Mr.Ian Rogers.
    You reply cleared all my doubts.
    As a matter of fact I am working on speed control of dc motor using PWM(closed loop-feedback control-using IR sensor pair) on 8051.

    Once again thank you,Sir.
     
  4. dave

    Dave New Member

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


     
  5. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    ONLINE

    In C you can read a memory location (register) like this:

    // this reads the memory location 0x5555 to the variable x
    char x;
    x = (*((char *)0x5555));

    I would say that the ANSI C/C++ equivalent of that is something like:
    // Read bit number N from memory location 0xFF
    x = (*((char *)0xFF)) & (1<<N);
     
    Last edited: Mar 11, 2013
  6. Mystogan

    Mystogan New Member

    Joined:
    Jan 23, 2013
    Messages:
    15
    Likes:
    1
    Sorry misterT.
    I think I have to put my question in a better manner.
    Lets say if I write,(in embedded c compiled on keil )

    void main()
    {
    P1=0xff; //port 1 declared as input
    unsigned char mybyte;
    while(1) {
    mybyte= P1;
    MSDelay(500); //lets say i defined a delay function which gives a 500ms delay
    mybyte=P1;
    MSDelay(600);
    }
    }
    Now my question is,
    will the statement "mybyte =P1;" in the 3rd line of the while loop means that after 500ms 'mybyte' gets updated with new value of input?
    ( here I assume input port 'P1' gets data continuously from an external device ,say a sensor)
     
  7. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    ONLINE
    Are you using Keil C51? This stuff is all in the documentation and it is very easy to google (and try out!).
    http://www.keil.com/support/docs/74.htm
     
  8. Mystogan

    Mystogan New Member

    Joined:
    Jan 23, 2013
    Messages:
    15
    Likes:
    1
    Thanks misterT.
    your 'link' is really helpful.
    I got exactly what I wanted.
    Thank you very much Sir.
     

Share This Page