In early 2009 I picked up some PIC16F690s, and some PIC16F628s. I bought the former as it is what TwistedSymphony used for his Saturn controller to Xbox 360 adapter, and the latter for some other projects I read on the ‘net.
I flashed TwistedSymphony’s firmware to a PIC, but soon found I needed it to register button presses as active high, as opposed to his code which had button presses as active low.
I installed Microchip’s Assembly IDE, MPLAB, and started reading over the source. As I found I didn’t understand anything I was reading, I decided to learn about PIC ASM. I found great guides from Mike Stacey, and elsewhere and started learning PIC Opcodes. I learned by changing “movf” to “comf” I was able to invert the key presses from the registers called to the w(ork) register, and had what I needed.
Later, I learned that the PIC16F690 chips did not have enough available I/O pins to map out the Saturn’s Start button, so I decided to buy a chip with more I/O ports, a PIC16F737. As I had excess I/O pins with the new chip, I decided to put them to use. I wrote a routine that allowed Start+A+B+C (Saturn’s reset combo) to drive an individual pin high. I mapped this to the guide button on my Xbox 360 Controller version, and to the right trigger button on the original Xbox version. I also wrote a routine that would allow for the back button to be triggered.
I made an SNES adapter with similar functionality, before combing and optimizing the code into a PIC16F747.
( For more info on this project, check out my GameSX Build Log. )
After I was satisfied with the results of the Controller project, I decided I wanted to move onto home automation, so I went on to buy some hardware…

Hi.
Does the transformer induce some phase (delay) to the zero cross detection?
Juan, I’d imagine the Sine Wave looks the same after being stepped down, only with less power, so I don’t think a delay is being caused here, or the wave is being pushed out of phase.
The wave becomes rectified and drives a Transistor that in turn inverts the signal into a a square wave. i.e. when voltage is 0, the transistor passes no power, and a pull up drives the interrupt high. What I believe is happening is once the wave hits a low threshold, say 4v, for example, the transistor no longer functions, and the interrupt is triggered. I’d like to find a solution to this.
Hope that helps.
I know this has been idle for a few months but just to give a possible solution.
You have a processor, which allows you more flexibility that a transistor circuit monitoring a full wave rectifier.
If you use a single separate diode as a half wave rectifier,with some resistors you’ll get a square wave at the line frequency, that you could read with logic input.
Now if life were perfect,you’d be set, because all you would have to do is monitor that input for an edge. Rising or falling. That would be a zero crossing. It may even work just that simple. But possibly there is line noise on the AC that would give you several as the signal is near Vinputhigh. There is also a symmetry issue, where voltage crossing would be different on one phase from the other. Its the kind of thing you’d use a Schmidt trigger to read if you were using pure logic.
This is where the processor comes in. You can calculate how deep into the wave form the input crosses Vih, and when it will crass back again on the way down. From there you can calculate where the real zero crossing is.
With further elaboration you should be able to use that data make a phase locked loop in software that locks into the 60 Hz signal of the AC line. Using that as a time base, then you can determine your triac firing times.
On additional thought, you can really factor out the asymmetrical aspect of the half wave detector. Whatever Vih is, the peak of the AC powerline waveform is going to be half way between the rising edge and the falling edge. Likewise the nadir of the low part of the cycle will be half way between the falling edge and the next rising edge.
This PIC has a timer. So if (after deglitching) you take the times of the rising and falling edges, you should be able to calculate where the peaks are, and thereby the zero crossings.
I’d probably set it up so that a timer caused an interrupt when it rolled over, intending that to be a zero crossing. Reloading the timer count should take into account the error based on the the expected peak times. If the peak is not at 1/4 of a cycle, and the low at 3/4 of a cycle, calculate the error, and add that in when setting the timer count back 1/2 a cycle in the interrupt service routine.
You can even use an initialization process at the start of day to figure out how many timer ticks a cycle is, and not worry about an absolute scaled time base. just get an average for a few cycles.
Therefore with a bitof deglitching, you should be able to know
You can use another optocoupler on the transformer line in place of the transistor circuit instead. Look up the 4N26. The internal diode will half rectify the wave and the transistor will cause a more precise square wave you can base your timing from.
You must be Psychic. The last comment here was over a month ago, and I JUST had gotten done purchasing it’s sister chip, the H11AA1 when you commented. I was researching this earlier. The Renard PIC controller uses it, and I think I’ll adapt it also.