     PIC 16C63 Midi Controlled Light dimmer


     Two Source code files are included.  Midi12.c and Midi20.c
	Midi12.c works.  Midi20.c is a work in process.
	I was in process of improving the linearity at the ends of the range when I had to pause to support another customer.
	I have not happended to pick the project back up since.
	Feel free to finish up version 2.  If you make any progress, please let me know and I'll add a link to your page.
	



     I usually do projects about the same way every time.

	Product defination (10 min)
	Code outline       (10 min)
	Schematic generation
	Build prototype
	Write code all at one sitting  (This one took about 6 hours)
	Take a copy of the above file, and delete all but a small section.
	Debug the section, and put it back into the origonal file.   
	Repeat with other chunks of code until done.
	Test the program as a whole
	Mass produce and be famous (never got this far yet)
 
   Files in MIDI.ZIP
	midi12.C      Version 1.2 Source code for Midi Light dimmer
	midi20.C      Version 2.0 Source code for Midi Light dimmer  (Work in progress)
	MIDI.SCH      Schematic in OrCad Version IV (Dos version)
	MIDI.LIB      Library file for Orcad Schematic
	MIDI.DXF      Schematic in DXF Format 
	MIDI_sch.PdS  Schematic in Acrobat format
	Midi_Art.Zip  Gerber files for the PCB Vendor
	Midi_Art.pdf  Viewable files of the PCB artwork

	Please feel free to pick the code and electronics apart and tell me how inefficient it is. 
        (you can also comment on my lousy spelling too.)  




AC power control in firmware 

You will have to change one line of code if you wish to use the dimmer for 50Hz operation. 

    set_timer1(65208);       /* preset the TIMER1 to rollover in 328 counts */

The magic number 328 was determined by dividing the period of a 60Hz half cycle, by the PIC's TIMER1's 
period, and then by 127 (The number of distinct levels allower by MIDI), and subtracting that from the 
terminal count of the timer.  This divides the AC half cycle's period into 127 chunks.  

If you do the math you will see that I didn't let the counter time all the way to the end of the half 
cycle.  I built 10 units, and a few of them would go full bright if I commanded them to go full dim.
  This was because of errors in the zero crossing detector.  Different units reported zero crossing 
at different places on the AC waveform because of component tolerances.  If a unit reported zero 
crossing a little late, I would start my delay a little late, and when I try to fire the triac near 
the end of the half cycle, the zero crossing had actually already happened. The triac would be on for 
the whole cycle, instead of the tiny slice of the cycle that I had intended.  So instead of fixing the 
zero crossing detector, I just don't trigger near the dangerous zero crossing
area. Regular light bulbs can't go that dim anyway. A similar error happens if tolerances cause the 
zero crossing detector to report the zero crossing early.  If you command full on, you get full dim.

Try using 65485 as the timer1 preset.  Increase it until the max and min light commands behave properly
 on all of your units.  
