have a look at the tc_lib library for the Due it contains useful functions for measurning and generating signals