Continue to Site

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.

  • 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.

Reading input port pin of 8051 using embedded c

Status
Not open for further replies.

Mystogan

New Member
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:
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..
 
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.
 
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));

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

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:
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)
 
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)

Are you using Keil C51? This stuff is all in the documentation and it is very easy to google (and try out!).
https://www.keil.com/support/docs/74.htm
 
Status
Not open for further replies.

Latest threads

Back
Top