Newbie needs a bit of help with interrupts on m328

Well in short here is my problem, I’m trying to work with the PS/2 protocol but there is a hick up, I need to use an external interrupt on a pin that also handles output and the data sheet even says it’s possible how ever the interrupt also triggers when I use the pin to send data and because of PS/2 protocol I end up in an infinite loop.

Communication works over two pins like this;

(Both are held high on both ends for idle)

Device (keyboard) -> Host (Computer)
Device clock pin sets communication clock pulse, host reads Data pin on falling edge of clock.
Thats pretty straight forward however 

Host -> Device 
Host lowers clock pin for >100us (priority over any other communication) 
Then Device sets clock pulse and device reads on rising clock.

The problem that gives an infinite loop as the interrupt required for the Host communication init of 100us+ also triggers when I use the clock to pulse when sending data.

Hope I’ve made it clear enough for people to understand, so how can I do this? One thought I have is turn off that interrupt when not in idel mode and then too poll the state of the clock pin before every time the device lower it as that span is 30-50us and therefor should catch any 100us low, but is there an easier way that I’m missing? some setting to interrupt that can ignore states set by the MCU itself?

Did you consider starting with one of the many existing freeware implementations of the PS/2 protocol? Such as on AVRfreaks.net, projects section? Google will probably find quite a few.

The gist of the approach is to use the hardware timer in a mode that tells you the number of timer cycles since the last state change on the sensed bit (data line).

Thanks, I’ll take a look at those libs, mostly been reading PS/2 specifications but the idea is learning so I’m trying to avoid just copy/paste :slight_smile:

Which mode has that function? if you know? need to take another look at the data sheet cause I’ve must have missed that and thanks for pointing me in the right direction :slight_smile:

pulse width measurement - in most microprocessor timers - often just one of the timers can do this function.