Keep in mind that by doing this CMOS emulation stuff in code on a micro, you'll have to decide on the time base (sample rate) at which you refresh your variables ; and you'll get nasty aliasing problems unless you work at a very high sample rate.