The total number of half-cycles I want to control is stored in the register TotalCount. Count holds the number of half-cycles I want voltage supplied to the load. So, the ratio of Count to TotalCount equals the duty cycle.

Each time the triac is fired, the value of nCount is decremented, until it reaches zero. When nCount equals zero, the triac is no longer fired. At the same time, the register nTotalCount is decremented each zero-crossing. So it keeps track of the number of half-cycles elapsed. Initially nTotalCount is loaded with the value of TotalCount so that nTotalCount keeps track of the number of half-cycles we want to control. After that number of half-cycles elapses, nTotalCount is reloaded with the value of TotalCount and nCount is reloaded with the value of Count. Pulse skipping modulation starts over again.

In our code, we want to control 10 half-cycles, which means 5 cycles. We want voltage to be supplied to the load for 3 cycles and off for 2 cycles. This is done, as nCount counts down from 6 to 0 while firing the triac, firing the triac 6 consecutive half-cycles. For the next 4 half-cycles, while nTotalCount is still counting down to zero, the triac is off. So, voltage is not supplied to the load. The duty cycle is 60%.

The triac is fired right after zero-crossing occurs. One advantage of this is that very little radio frequency interference is produced. However, care must be taken that an unequal number of positive and negative half-cycles must not be present. This will introduce a DC component that can damage motors and other devices. Cycle control also cannot be used with incandescent bulbs because of high unacceptable flickering.

This problem with the DC component is avoided by ensuring that equal numbers of positive and negative half-cycles are present. This is done by ensuring that the Count and TotalCount (in the code above) are both even numbers.

Now let’s take a look at my circuit setup and then the output waveform using this code: