|
发表于 2010-9-8 07:36:00
|
显示全部楼层
that's just way too complicated.
mine is here:
=========code============
#define PORT_TYPE unsigned char //for 8-bit ports
//#define PORT_TYPE unsigned long //for 32-bit ports
//set bits where the pins have gone through a low-high or high-low transition
PORT_TYPE port_get(PORT_TYPE port, PORT_TYPE pins) {
PORT_TYPE tmp;
static PORT_TYPE pins_prev=0x00;
tmp=pins_prev; //save pins_prev
pins_prev=port & pins; //read pins from port
return (pins_prev ^ tmp); //return rising or falling edge
//return (pins_prev ^ tmp) & tmp; //return falling edge
//return (pins_prev ^ tmp) & pins_prev; //return rising edge
}
===========end of code============
by choosing one of the return statement, the code can detect either port changes, or falling / rising changes.
the principle is really simple: tmp retains the value of the port during the last read, pins_prev retains the value of the port during the current read. so tmp ^ pins_prev is set for pins where the two reads differ.
by ANDing (tmp ^ pins_prev) with tmp, you have picked up the pins that changed and was 1 -> falling edge.
by ANDing (tmp ^ pins_prev) with pins_prev, you have picked the pins that changed and is 1 -> rising edge. |
|