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.

8051 uC programming. Please help

Status
Not open for further replies.

Embedded_Geek

Full Member level 6
Joined
Jul 5, 2010
Messages
340
Helped
58
Reputation
116
Reaction score
56
Trophy points
1,318
Location
Germany
Activity points
2,948
How to detect a high to low pulse at any port pin of 8051. I mean how to check the condition whether a particular port pin is receiving a high to low pulse or not.
i.e., edge triggering not level triggering.

Like i want 8051 uC to blink led if it receives a high to low pulse at port pin 3.1 using embedded C.

thanks in advance[/b]
 

About 8051 uC

first detect a transition and then detect the level on that pin.

if the level is low, you have a falling edge on that pin.
 

i know how to detect level. But i m confused in the case of rising edge and falling edge detection.
 

the flow is as below:

read port pin 3.1
loop (or) wait until it is high.
once out of the above loop ,
loop again until it is zero.
at this stage you have detected a 1 to 0 transition.
 
But i m confused in the case of rising edge and falling edge detection.

the point was for you to detect a change on the port, either a rising or falling, and then detect if it is rising or falling.

there are many ways of detecting a change. polling is one of them, and the least efficient one as well.

many chips offer interrupt on change (on some pins / ports). you should read the datasheet for your particular chip to see if your mcu has it and how you can implement it.

again, you detect a change, and then determine if it is falling or rising.

Added after 12 minutes:

read port pin 3.1
loop (or) wait until it is high.
once out of the above loop ,
loop again until it is zero.
at this stage you have detected a 1 to 0 transition.

try something slightly different, assuming 8-bit port.

Code:
unsigned char port_get_falling(unsigned char port, unsigned char pins) {
  unsigned char tmp;
  static unsigned char pins_prev=0x00;  //previous key
  
  tmp=pins_prev; //keep the previous pin read
  pins_prev=port & pins;
  return (pins_prev ^ tmp) & tmp;
}

the routine will return a 1 if a pin has had a high-low transition since its last read.

for example, port_get_falling(PORTB, (1<<0) | (1<<4)) will return 0x01 if portb.0 has gone from high to low and portb.4 hasn't changed.

Added after 1 hours 6 minutes:

the above code can return different things by changing the "return" statement.

Code:
return (pins_prev ^ tmp);
returns 1 for any pin that has changed.

Code:
return (pins_prev ^ tmp) & pins_prev;
returns a 1 for any pin that has changed from low to high.
 
I was asked how one might use the routines. here is an example.

suppose we have to switches tied to KEY1 and KEY2, and we want to read their status, and activate two LEDs, LED1 and LED2 in the following way:

1) flip the LED1 on KEY1's falling edge;
2) if KEY1 or KEY2 is pressed (either rising or falling edge), flip LED2.

the code looks like this:
Code:
int main(void) {
	mcu_init();
	while (1) {
		if (key_get_f(KEY_PORT, KEYs) & KEY1) IO_FLP(LED_PORT, LED1);
		if (key_get_d(KEY_PORT, KEYs) & KEYs) IO_FLP(LED_PORT, LED2);
		//do something else
	}
}

where key_get_f(port, bits) returns the falling edge of bits on port, and key_get_d(port, bits) returns change of bits on port.

the sim confirms it.
 

in the above sim, P1.1 is KEY1, P1.5 is KEY2; P3.1 is LED1, P3.5 is LED2.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top