The code and the circuit in my post works with no problem in simulation.