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.