I can't help but wonder whether this could be done in software, maybe with limited hardware help. It'd be helpful if the existing code had more comments. They don't use any memory
I am thinking of trying this circuit, and wondering if it will work. It is from this thread:
Sine to square wave using op-amp - Page 2 - All About Circuits Forum