Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Embedded C Programming for 8051

Status
Not open for further replies.

Help

Advanced Member level 2
Joined
Feb 15, 2005
Messages
617
Helped
7
Reputation
14
Reaction score
3
Trophy points
1,298
Activity points
7,065
Hai...

Anyone can explain to me........

--------------------------------------------------------------------------------

bit Read_Bit_P1(const unsigned char);

void main(void)
{
bit x;

while(1)
{
x = Read_Bit_P1(2); // why is Port 1???
Write_Bit_P1(5,x); // x is carry Hight or Low now????? why..
}
}

-------------------------------------------------------------------------------

Thanks.....
 

Hi Help,

As I'd told you in different topic you'll be helped. For sake of confidence, please mentioned from where did you get the code.
Thus, the members could view the function prototype declaration. Remember, members cannot guess what did you ask before in the forum.
I believe it's taken from here:


Your question is : "why is port 1"
If you look at the begining of the source code (page 66) you'll see an include statement:
#include <reg52.H>
Among all declaration inside reg52 header file is one that say:
sfr P1 = 0x90;
Now look at the function bit Read_Bit_P1(const unsigned char PIN) and tell me what's the value returned by function.
Yes, you guess is return (P1 & p). When the compiler generates the obj code it looks for p and found it declared as local variable unsigned char p
If for P1 don't find declarations neither inside function as local variable nor as global variable or external declaration, it looks inside the included file header.
There will find P1 and that's all the compiler needs to know.
If you want to return bit value according with P2 then change to return (P2 & p)

Your second question was "x is carry Hight or Low now????? why"
If you look inside function void write_Bit_P1(const unsigned char PIN, const bit VALUE) you'll see
an if statement. That it's a bitwise OR operation between P1 and local variable p if we want 1 output.
If we want 0 output at specified PIN, then you'll have to complement the local variable p and then perform a bitwise and with P1 variable (remember the reg52 header file )

In order to understand this two functions mentioned above it's a must to understand the hardware configuration of 8051 microcontroller.
With other words, the read-modify-wryte" instructions. Why some instructions that read a port read the latch and others read the pin.
Otherwise, I bet its quite confusing why the write_Bit_P1(const unsigned char PIN, const bit VALUE) appears inside function bit Read_Bit_P1(const unsigned char PIN) when all we want to do it's a input pin reading..

You can find good source of information in these three docs:
https://www.8052.it/download/architecture.pdf
https://www.8052.it/download/hardware_description.pdf
https://www.8052.it/download/programmers_guide.pdf
Most people are calling 8051 bible.

In the future please mention the source code, or if it's not available on a web page upload it to your post. Otherwise you can't be helped as you expected (quickly ?)

regards,
silvio
 

silvio said:
Hi Help,

As I'd told you in different topic you'll be helped. For sake of confidence, please mentioned from where did you get the code.
Thus, the members could view the function prototype declaration. Remember, members cannot guess what did you ask before in the forum.
I believe it's taken from here:
h**p://www.le.ac.uk/engineering/mjp9/pes1ohp_a4.pdf

Your question is : "why is port 1"
If you look at the begining of the source code (page 66) you'll see an include statement:
#include <reg52.H>
Among all declaration inside reg52 header file is one that say:
sfr P1 = 0x90;
Now look at the function bit Read_Bit_P1(const unsigned char PIN) and tell me what's the value returned by function.
Yes, you guess is return (P1 & p). When the compiler generates the obj code it looks for p and found it declared as local variable unsigned char p
If for P1 don't find declarations neither inside function as local variable nor as global variable or external declaration, it looks inside the included file header.
There will find P1 and that's all the compiler needs to know.
If you want to return bit value according with P2 then change to return (P2 & p)

Your second question was "x is carry Hight or Low now????? why"
If you look inside function void write_Bit_P1(const unsigned char PIN, const bit VALUE) you'll see
an if statement. That it's a bitwise OR operation between P1 and local variable p if we want 1 output.
If we want 0 output at specified PIN, then you'll have to complement the local variable p and then perform a bitwise and with P1 variable (remember the reg52 header file )

In order to understand this two functions mentioned above it's a must to understand the hardware configuration of 8051 microcontroller.
With other words, the read-modify-wryte" instructions. Why some instructions that read a port read the latch and others read the pin.
Otherwise, I bet its quite confusing why the write_Bit_P1(const unsigned char PIN, const bit VALUE) appears inside function bit Read_Bit_P1(const unsigned char PIN) when all we want to do it's a input pin reading..

You can find good source of information in these three docs:
h**p://www.8052.it/download/architecture.pdf
h**p://www.8052.it/download/hardware_description.pdf
h**p://www.8052.it/download/programmers_guide.pdf
Most people are calling 8051 bible.

In the future please mention the source code, or if it's not available on a web page upload it to your post. Otherwise you can't be helped as you expected (quickly ?)

regards,
silvio

Hai...

1)
So, that mean the call function "Read_Bit_P1(0)" it didn't know what bit it are, only until finish the function "bit Read_Bit_P1(const unsigned char PIN)" then we know the bit is because the "return (P2 & p)". Am i right ???
2)
What's the difference i put
x = Read_Bit_P1(0); --> Read_Bit_P1(0);
why i cannot see the difference??
3)
Why the "return (P2 & p)" bitwase AND operation between P1 and local variable P??
4)
Still not understand.. function "void write_Bit_P1(const unsigned char PIN, const bit VALUE)" how the VALUE carry the value??

1st, when the main function call the "Read_Bit_P1(0)", inside of this function there have 1 statement which is "Write_Bit_P1(PIN,1);" then it will carry PIN=0 and 1 to VALUE for this "void Write_Bit_P1(const unsigned char PIN, const bit VALUE)" function izit..... so, if statement will be read
"if(VALUE ==1)" then output will be '1'..... izit

2nd, after that... return to "bit Read_Bit_P1(const unsigned char PIN)" then "return (P1 & p);", the question is what is it (return (P1 & p);) return to main function.... then what is "Write_Bit_P1(1,x);" x carry to "void Write_Bit_P1(const unsigned char PIN, const bit VALUE)"??? Why the VALUE become '0'...??

silvio said:
/*-------------------------------------------------------------------*-+
Bits2.C (v1.00)
----------------------------------------------------------------------
Reading and writing individual port pins
NOTE: Both pins on the same port
--- Generic version ---
+-*------------------------------------------------------------------*/
#include <reg52.h>

// Function prototypes
void Write_Bit_P1(const unsigned char, const bit);
bit Read_Bit_P1(const unsigned char);

/*--------------------------------------------------------------------*/

void main(void)
{
bit x;

while(1)
{
Read_Bit_P1(0); // Read Port 1, Pin 0
Write_Bit_P1(1,x); // Write to Port 1, Pin 1
}
}
/*--------------------------------------------------------------------*/
void Write_Bit_P1(const unsigned char PIN, const bit VALUE)
{

unsigned char p = 0x01; // 00000011

p <<= PIN; // Left shift appropriate number of places

if(VALUE ==1) // If we want 1 output at this pin
{
P1 |= p; // Bitwise OR
return;
}
p = ~p; // Complement
P1 &= p; // Bitwise AND
}
/*--------------------------------------------------------------------*/
bit Read_Bit_P1(const unsigned char PIN)
{
unsigned char p = 0x01; // 00000100

p <<= PIN; // Left shift appropriate number of places

Write_Bit_P1(PIN,1);// Write a 1 to the pin (to set up for reading)

return (P1 & p); // Read the pin (bitwise AND) and return
}
/*-------------------------------------------------------------------*-+
---------------------- END OF FILE ---------------------------------
+-*-----------------------------------------------------------------*/


Thanks for you explanation and your advice......

Thanks Silvio....



 

Hi Help,

From where did you get these:

bit x;

while(1)
{
P3 = x;
Read_Bit_P1(0); // Read Port 1, Pin 0
P3 = x;

I don't think I wrote that as you quoted.
And inside
appears something similar with what you wrote at the top of the page:

bit x;

while(1)
{
x = Read_Bit_P1(2);

That's why you don't see the difference:

2)
What's the difference i put
x = Read_Bit_P1(0); --> Read_Bit_P1(0);
why i cannot see the difference??

Look at the function prototype:
bit Read_Bit_P1(const unsigned char);

It must return a bit.
P3 = x it's illegal.

The best thing is to use a C compiler in order to see exactly what's going on behind the curtain.
Where variables are loaded in memory, how arguments are passed to a subroutine (call by value or call by reference), how function return a specific type of data, understanding very well the scope of a function.
A good C book it's a must.

Do you have a specific C compiler in mind ?
Just because you've to learn some difference from a genuine ANSI C compiler due to constraints of 8051 architecture.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
silvio said:
Hi Help,

From where did you get these:

bit x;

while(1)
{
P3 = x;
Read_Bit_P1(0); // Read Port 1, Pin 0
P3 = x;

I don't think I wrote that as you quoted.
And inside
h**p://www.edaboard.com/h**p://www.le.ac.uk/engineering/mjp9/pes1ohp_a4.pdf
appears something similar with what you wrote at the top of the page:

bit x;

while(1)
{
x = Read_Bit_P1(2);

That's why you don't see the difference:

2)
What's the difference i put
x = Read_Bit_P1(0); --> Read_Bit_P1(0);
why i cannot see the difference??

Look at the function prototype:
bit Read_Bit_P1(const unsigned char);

It must return a bit.
P3 = x it's illegal.

The best thing is to use a C compiler in order to see exactly what's going on behind the curtain.
Where variables are loaded in memory, how arguments are passed to a subroutine (call by value or call by reference), how function return a specific type of data, understanding very well the scope of a function.
A good C book it's a must.

Do you have a specific C compiler in mind ?
Just because you've to learn some difference from a genuine ANSI C compiler due to constraints of 8051 architecture.


Hi....

Sorry.. because i want to see the result..
Code:
while(1) 
{ 
P3 = x; 
Read_Bit_P1(0); // Read Port 1, Pin 0 
P3 = x;

Can you help me, because i am beginner. So i you all help....
What you mean "Do you have a specific C compiler in mind ?"??
Can you teach me how to use a C compiler (uVision2) in order to see exactly..?
Can you let me know the question that i posted??

Thank You....
 

Hai...

Anyone help me to explian......

1)
The call function "Read_Bit_P1(0)" it didn't know what bit it are, only until finish the function "bit Read_Bit_P1(const unsigned char PIN)" then we know the bit is because the "return (P2 & p)". Am i right ???
2)
What's the difference i put
x = Read_Bit_P1(0); --> Read_Bit_P1(0);
why i cannot see the difference??
3)
Why the "return (P2 & p)" bitwase AND operation between P1 and local variable P??
4)
Still not understand.. function "void write_Bit_P1(const unsigned char PIN, const bit VALUE)" how the VALUE carry the value??

1st, when the main function call the "Read_Bit_P1(0)", inside of this function there have 1 statement which is "Write_Bit_P1(PIN,1);" then it will carry PIN=0 and 1 to VALUE for this "void Write_Bit_P1(const unsigned char PIN, const bit VALUE)" function izit..... so, if statement will be read
"if(VALUE ==1)" then output will be '1'..... izit

2nd, after that... return to "bit Read_Bit_P1(const unsigned char PIN)" then "return (P1 & p);", the question is what is it (return (P1 & p);) return to main function.... then what is "Write_Bit_P1(1,x);" x carry to "void Write_Bit_P1(const unsigned char PIN, const bit VALUE)"??? Why the VALUE become '0'...??


Code:
/*-------------------------------------------------------------------*-+ 
Bits2.C (v1.00) 
---------------------------------------------------------------------- 
Reading and writing individual port pins 
NOTE: Both pins on the same port 
--- Generic version --- 
+-*------------------------------------------------------------------*/ 
#include <reg52.h> 

// Function prototypes 
void Write_Bit_P1(const unsigned char, const bit); 
bit Read_Bit_P1(const unsigned char); 

/*--------------------------------------------------------------------*/ 

void main(void) 
{ 
bit x; 

while(1) 
{ 
x=Read_Bit_P1(0); // Read Port 1, Pin 0 
Write_Bit_P1(1,x); // Write to Port 1, Pin 1 
} 
} 
/*--------------------------------------------------------------------*/ 
void Write_Bit_P1(const unsigned char PIN, const bit VALUE) 
{ 

unsigned char p = 0x01; // 00000011 

p <<= PIN; // Left shift appropriate number of places 

if(VALUE ==1) // If we want 1 output at this pin 
{ 
P1 |= p; // Bitwise OR 
return; 
} 
p = ~p; // Complement 
P1 &= p; // Bitwise AND 
} 
/*---------------------------------------------------------------------------*/ 
bit Read_Bit_P1(const unsigned char PIN) 
{ 
unsigned char p = 0x01; // 00000100 

p <<= PIN; // Left shift appropriate number of places 

Write_Bit_P1(PIN,1);// Write a 1 to the pin (to set up for reading) 

return (P1 & p); // Read the pin (bitwise AND) and return 
} 

/*-------------------------------------------------------------------------------*/

Thank You
 

1)
You know exactly what bit you want to read when function is called.
For bit 0 just call Read_Bit_P1(0)
For bit 1 just call Read_Bit_P1(1)
For bit 2 just call Read_Bit_P1(2)
and so on up to
For bit 7 just call Read_Bit_P1(7)
You see the difference ? I type this way just to be sure you got it !
What is "hard coded' if we can call so, is the port number.
We know is port P1 looking at the "return(P1 & p)'
If you want to read bit 5 from port P2 you must call as Read_Bit_P2(5) where the function looks like:

Code:
bit Read_Bit_P2(const unsigned char PIN) 
{ 
unsigned char p = 0x01; // 00000100 

p <<= PIN; // Left shift appropriate number of places 

Write_Bit_P2(PIN,1);// Write a 1 to the pin (to set up for reading)
              //you must change the write function according with port 2 

return (P2 & p); // Read the pin (bitwise AND) and return 
}

Do you see the difference ?

2)
x = Read_Bit_P1(0) means x receive the value returned by function Read_Bit_P1(0).
Since x is declared as bit, will receive the value of bit 0 of port P1.
When you type only Read_Bit_P1(0), the x preserve his value.
BTW, how are you expecting to see the difference. You use the debugger and watch the x variable contents ?

3) The value returned by function Read_Bit_P1(0) is of bit type.
local variable p is declared as unsigned char, thus 8 bits
P1 is sfr variable, 8 bits too.
How are you expecting to return a bit ?
p variable is the mask for port P1.
Inside p it's a walking 1 among 0's, whith his position in the byte according with the argument passed to the function.
Performing a bitwise operation you "extract" only the the bit you're looking for and return this bit as a results of calling function Read_Bit_P1(0).

4)The function's parameters PIN and VALUE are replaced wit arguments when you call the function.
Thus if you want to write a 0 on pin 3 you call Write_Bit_P1(3,0)
If you want to write a 1 on pin 3 you call Write_Bit_P1(3,1)
The function parameter PIN is replaced with argument 3 inside Read_Bit_P1(3) function.
You know that the port P1 will be involved in setting or reseting a specific bit.

English its not my native language, thus maybe I'll not explained very well, or at least you're expected to do it.
I'd learned C reading in english, from books written in english.
I can't claim I'm a C guru, but honestly I'm wonder what's your teacher doing in the class ?
As I told you before you need basic C knowledge.
I'm wonder how someone could explain how arguments are passed to a function in order to really understand it ?

The 8051 has small hardware solutions that need to be known before going deeply into embedded C word for 8051.
If you are not been able to understand the C basic then it would be much harder to stick into embedded C.
In addition a good practice in assembler it's a must, though a lot of peoples thought NOT.

The learning "do it by practice" approach not works all the time.
Sometimes it's better and wise learning things even if seems a little bit abstract.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
silvio said:
1)
You know exactly what bit you want to read when function is called.
For bit 0 just call Read_Bit_P1(0)
For bit 1 just call Read_Bit_P1(1)
For bit 2 just call Read_Bit_P1(2)
and so on up to
For bit 7 just call Read_Bit_P1(7)
You see the difference ? I type this way just to be sure you got it !
What is "hard coded' if we can call so, is the port number.
We know is port P1 looking at the "return(P1 & p)'
If you want to read bit 5 from port P2 you must call as Read_Bit_P2(5) where the function looks like:

Code:
bit Read_Bit_P2(const unsigned char PIN) 
{ 
unsigned char p = 0x01; // 00000100 

p <<= PIN; // Left shift appropriate number of places 

Write_Bit_P2(PIN,1);// Write a 1 to the pin (to set up for reading)
              //you must change the write function according with port 2 

return (P2 & p); // Read the pin (bitwise AND) and return 
}

Do you see the difference ?

2)
x = Read_Bit_P1(0) means x receive the value returned by function Read_Bit_P1(0).
Since x is declared as bit, will receive the value of bit 0 of port P1.
When you type only Read_Bit_P1(0), the x preserve his value.
BTW, how are you expecting to see the difference. You use the debugger and watch the x variable contents ?

3) The value returned by function Read_Bit_P1(0) is of bit type.
local variable p is declared as unsigned char, thus 8 bits
P1 is sfr variable, 8 bits too.
How are you expecting to return a bit ?
p variable is the mask for port P1.
Inside p it's a walking 1 among 0's, whith his position in the byte according with the argument passed to the function.
Performing a bitwise operation you "extract" only the the bit you're looking for and return this bit as a results of calling function Read_Bit_P1(0).

4)The function's parameters PIN and VALUE are replaced wit arguments when you call the function.
Thus if you want to write a 0 on pin 3 you call Write_Bit_P1(3,0)
If you want to write a 1 on pin 3 you call Write_Bit_P1(3,1)
The function parameter PIN is replaced with argument 3 inside Read_Bit_P1(3) function.
You know that the port P1 will be involved in setting or reseting a specific bit.

English its not my native language, thus maybe I'll not explained very well, or at least you're expected to do it.
I'd learned C reading in english, from books written in english.
I can't claim I'm a C guru, but honestly I'm wonder what's your teacher doing in the class ?
As I told you before you need basic C knowledge.
I'm wonder how someone could explain how arguments are passed to a function in order to really understand it ?

The 8051 has small hardware solutions that need to be known before going deeply into embedded C word for 8051.
If you are not been able to understand the C basic then it would be much harder to stick into embedded C.
In addition a good practice in assembler it's a must, though a lot of peoples thought NOT.

The learning "do it by practice" approach not works all the time.
Sometimes it's better and wise learning things even if seems a little bit abstract.

Hai Silvio....

I already understand the program run already, THANKS.....
Please.. Can you help me the check the program explanation to make sure i am not miss understanding..., I use Watch and Call Stack Window check the x and I/O Port to check the result.
r u M'sian

Code:
#include <reg52.h>

 // Function prototypes
void Write_Bit_P1(const unsigned char, const bit);
bit Read_Bit_P1(const unsigned char);

sbit readbit = P3^0;

/*--------------------------------------------------------------------*/

void main(void)
{
	bit x;
	P0 = 0; // To set it all Low
	P1 = 0;
	P2 = 0;
	P3 = 0;

	while(1)
	{
		x=Read_Bit_P1(3); 
//				^_____ the Read_Bit_P1(const unsigned char PIN) return "1" or 
//					   "HIGH" to Read_Bit_P1(3). So, x=1.
//					   It posible "0" while return change it to (P1 ^ p) 		
						  /* - Read Port 1, Pin 3
						     - 3 decimal is value, it can be 0x03 in Hex	
						     - we always mention ....P1(3) is Port1 Pin3 or Port1 BIT 3,
							    but in program the 3 is not a BIT is NUMBER/VALUE.
				 			   So, this function ...P1(3) is carry value */

		P3=x;			      // debug 	
		Write_Bit_P1(1,x);  // Write to Port 1, Pin 1
	}
}

/*--------------------------------------------------------------------*/
void Write_Bit_P1(const unsigned char PIN, const bit VALUE)
{
	unsigned char p = 0x03; // 00000011
		
	p <<= PIN;   // Left shift appropriate number of places
				 /* p = p << PIN;
					 p = 00000011 << 3 
					 p = 00011000  	   */
	
	if(VALUE ==1) // If we want 1 output at this pin
	{
		P1 |= p;  // P1 = P1 | p; (Bitwise OR)
				    /* P1 = 00000000 | 00011000
			  			  = 00011000			  */	
		return;
	}
	p = ~p;		 // Complement
	P0 = p;
	P1 &= p;	   // Bitwise AND
	P2 = P1;
}
/*--------------------------------------------------------------------*/
bit Read_Bit_P1(const unsigned char PIN)
{
	unsigned char p = 0x03; // 00000011
	P0 = p;				     // P0 = 00000011, 3(dec) 
	p <<= PIN;			     // p = p << PIN;
						         //                    bits 7654 3210 
						         // 0x03, LEFT shift 3 bit; 0000 0011-->0001 1000
						         // this function for "<<" or ">>" is shift bit
	P2 = PIN;			      // P2 = 00000011,  3(dec), 0x03(hex) <--- is value
	P3 = p;				     // P3 = 00011000, 18(dec), 0x18(hex) <--- is value
	Write_Bit_P1(PIN,1);    // PIN = 0x03 is carry by call-function
						         // Write a 1 to the pin (to set up for reading)
	
	return (P1 & p);    // return = P1 & p;
						     // Read the pin (bitwise AND) and return 
						     /* compare bit(P1) by bit(p)
								(P1)	   00011000		
								(p)		 00011000
								------------------		bits 76543210
								(return)  00011000 <-------- 00011000
							  at bit 3 and 4 is High that mean return 
							  function is "1" or "HIGH" (return bit 1)	*/
						     // It can return "0" change it to return (P1 ^ p);
						 
						
}
/*------------------------ END OF FILE --------------------------------------*/

Thanks....
 

I hope that the only purpose changing from

unsigned char p = 0x01; of the first topic example

to

unsigned char p = 0x03;

was only to proove you understand how program it's working and for using debuger.

Otherwise it would not works since defeats the purpose of reading or writing specific port pin.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
silvio said:
I hope that the only purpose changing from

unsigned char p = 0x01; of the first topic example

to

unsigned char p = 0x03;

was only to proove you understand how program it's working and for using debuger.

Otherwise it would not works since defeats the purpose of reading or writing specific port pin.

Hai...

What you mean changing from unsigned char p = 0x01; of the first topic example to unsigned char p = 0x03; was only to proove you understand how program it's working and for using debuger.
You mean the first topic example is Hello Embedded World
How to proove i understand ??
Previously i post the program explanation is it correct??

I have one question, 3) The value returned by function Read_Bit_P1(0) is of bit type. This is very important because we must know it is return in bit 0 or 1 because previously i thought if P1 & p is 00011000 (24 dec) so the return (P1 &p); will return 24 decimal to main function.
Let say... example if i want it to return value/decimal to main function what should i change it at bit Read_Bit_P1(const unsigned char PIN)...??

sorry... may i ask you how you creat the helped me icon .... :)


Thanks..

Added after 2 hours 12 minutes:

Help said:
Hi Silvio,

Thanx very much :D :D

I would like to thank you for you helping me alot so i will donate you with 10 points for Kongsi information and 10 point for your Kindness :p

Thanx again.......

Help
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top