To bit-bang sound we toggle an output pin at the desired frequency. To play a musical note, we need to use specific frequencies and play the sound for a specific amount of time.
Generating the desired frequency big-bang style means figuring out what the period or cycle time of the desired frequency is. This is easy to calculate, it’s the reciprocal (1/F) of the frequency. As we found in part 2, 1KHz is 1000 cycles per second. So, one cycle takes 1 one-thousandth (1/1000) of a second or 0.001 seconds (1 millisecond). We need to toggle the pin at this rate so we want to keep it low for 1/2 the time then high for 1/2 the time. Therefore we want to set the I/O pin low for 500uS, then high for 500uS and do this over and over for as long as the duration requires.