and that thread should give you an easy to implement solution
http://www.picbasic.co.uk/forum/showthread.php?t=190