lightdimmer

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

Post Reply
venturi
Flowcode V4 User
Posts: 9
Joined: Sun Apr 02, 2006 10:07 pm
Contact:

lightdimmer

Post by venturi »

I would like to make a lightcontrol device using flowcode.
Maybe to begin with an automatic fade in fade out dimmer with dipswitch on Port A for different speeds ? Has anyone done that already ?
I would like to use RB0 as zerocross detection.
I'm new in this so any help would be appreciated.
Target device is PIC16F628.
Thank you

Ian
Posts: 110
Joined: Thu Sep 29, 2005 10:53 am
Location: Matrix Multimedia
Been thanked: 1 time
Contact:

Post by Ian »

One item you may need that isn't on the PIC is a DAC - a Digital to anlaogue convertor, to control the lightbulb voltage.
You can get input from the dimmer as an analogue value, but all outputs are on or off digital logical levels.

You can input the dimmer anlogue signal, get the dip switch positions and calculate the signal level as a number (e.g. 0-255 on a single port) and send that signal via a port to the DAC chip to convert the number into an analogue signal to light the bulb.

I'm not too sure what you mean by Zerocross detection so can't suggest anything for that.
But RB0 can be configured as an interrupt if you need to react to a particular signal.

User avatar
Steve
Matrix Staff
Posts: 3424
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post by Steve »

You mention the need for "zero-cross detection". Does this mean you are implementing some kind of power-line control such as X-10?

We produce an X-10 E-Block (that connects to the powerline via an XM10U device, or equivalent) - this may be of interest to you as it takes some of the headaches away when connecting devices directly to the mains.

I know that Microchip have an X-10 application note which contains info about directly interfacing with the mains, plus they have some code.

As for a light-dimmer application, I think you can use PWM on a single output pin of a PICmicro connected to a TRIAC(?). Altering this output's mark-to-space ratio will set the brightness.

I've seen lots of info about this type of application on the web - I'm sure a few choice words in Google will send you in a good direction.

jimhumphries
Posts: 112
Joined: Wed Oct 12, 2005 6:29 pm
Location: USA
Been thanked: 1 time
Contact:

Post by jimhumphries »

Venturi:

If you really want zero crossing detection and you want to use RB0 you can full-wave rectify the output of a small transformer (like a 6.3 V fillament type) and "square" that signal with a saturated switch. Either the rising or the falling edge of the squared signal can be used to generate an interrupt at the zero crossing (or very close to it).

If you would rather use an A/D channel with polling, scale the rectified signal via a voltage divider to something below a peak value of + 5V (or whatever you're using for the A/D Vref). For zero crossing detection you can take successive samples on RB0 (A/D channel 0) and test to see if the second sample is less than the first sample. If yes, the AC waveform is heading for zero. The first time the second sample is larger than the first sample, you have crossed zero. The higher the sampling rate, the closer to zero your detection can be. How close you get to the actual zero crossing will also depend on the "granularity" of the A/D conversion. You may want to try this first in 8 bit mode (read high byte only) and see if it is fine enough. If not you can read successive high bytes and low bytes and when two successive high bytes are equal do the test using the low bytes.

This is a handy method generally because with it you can detect the rising edge of a waveform, the peak, the falling edge and zero crossings (if you full wave rectify).



Hope that helps somewhat.

Jim

jimhumphries
Posts: 112
Joined: Wed Oct 12, 2005 6:29 pm
Location: USA
Been thanked: 1 time
Contact:

Post by jimhumphries »

Woops - reference to RB0 in the second paragraph should be RA0.

Jim

venturi
Flowcode V4 User
Posts: 9
Joined: Sun Apr 02, 2006 10:07 pm
Contact:

Post by venturi »

Thank you all for thinking with me. I quiet understand what you all say but making a flowchart and get it to work is a different story.
About zerocrossdetection ik ok, i rectified the secondary of supply transformer and fed it to RB0 through a few transistors. Tne square pulse is about 10 usec and 5V high. The hardware is not the problem for me but the software is. I would like to make a 3 or 4 channel device with outputs on RB1,2,3 and/or RB4. Lets say i would have to make a fading unit that starts at channel 1 getting to max level then to min level starting channel 2 and so on. I haven't the slightest idea how to manege this.
Regards
Venturi
:?: :?: :?: :?: :?:

jimhumphries
Posts: 112
Joined: Wed Oct 12, 2005 6:29 pm
Location: USA
Been thanked: 1 time
Contact:

Post by jimhumphries »

Venturi:

Sounds like you've made a good start with the hardware and it sounds like you probably have an oscilloscope for looking at the PIC outputs.

Initialize two variables: ZERO and ON to 0 (create these two variables, drag two equation objects into the flow chart just below "Begin" and set ZERO = 0 and ON = 0 using those two objects). Without bothering with interrupts for now, let me suggest that you start your software with a simple loop (drag a loop object into the flow chart below the ON equate object). Inside the loop add an input object and read one bit of a port (your choice) and connect your zero crossing detect hardware to that port/bit. Make the output of the read the variable ZERO. The loop should end when the ZERO = 1 (the zero crossing is detected). This will be "loop while ZERO = 0" or "loop while ZERO < 1" or "loop while ZERO <> 1". Any of these should work for now but you'll learn later (on your own) which one to apply in various situations.

If you're comfortable getting that far - add an equate object to the flow chart just below the loop and set the variable ON = 1. Below the equate object add an output object. Pick a port/bit as an output and use ON as the value of that bit. That output will go high immediately after a zero crossing and stay high until your program tells it to go low. Create another variable - ON_TIME and initialize that to 0 by adding another equate object just below "Begin". OK so far (hope this isn't too simplistic)? Add another loop object below the output object and within the loop add an equate object - "ON_TIME = ON_TIME + 1". Create another variable - "OFF" and initialize it to 0. This second loop should end when the ON_TIME = OFF. Just below the second loop add a copy of the ON output object and change the value of the variable ON to 0. Below that add a copy of the ON_TIME initialization equate object.

OK, at this point you could go one pass through this program and the variable ON would go high for a very brief time then off and then the program would end. The time that ON is high depends on the instruction time (the inverse of your PIC clock frequency divided by 4) and number of instructions that are executed between ON = 1 and ON = 0. Not too useful but - add another loop and put everything except the initialization equate objects (just below Begin) inside it by dragging them into it.

Now you run the program and it should continue to "pulse" ON high then low then high, etc. with the rising edge of ON synchronized to the zero crossing (unless I screwed up).

Now, initialize OFF to some number greater than zero and see what effect that has on the high duration of ON. You will probably want OFF = 255 to equal an ON = 1 duration of just less than one-half cycle at your line frequency. You can tinker with that by adding a delay inside the second loop.

If this works you have the basics. You're synchronized to the AC line at the zero-crossing, you cause a bit to go high at that time and keep it high for some time that you control with the value of the variable OFF and if ON goes low in less than one half-cycle of AC line time (about 8 millisecond, USA) you'll be ready to detect the next zero crossing.

If the mechanics of assembling this simple program are unfamiliar to you you'll need to spend some time with the Flowcode demo or with the tutorials.

Hope that helps,

Jim

venturi
Flowcode V4 User
Posts: 9
Joined: Sun Apr 02, 2006 10:07 pm
Contact:

Post by venturi »

Hi Jim,
Thank you for the support.
wrote the flowchart and after a while i finally got it working.
I made some mistakes like placing the "loop while" at begin instead of at the end of loop and some other mistakes.
I have every zero cross 7 pulses of 50 us each so for the duration of 400 us.
But i cannot understand why there are 7 pulses.

But it seems to be working now.
Output is RB1 and is sinked so low activates led of optocoupler.
Any suggestions for how to go on ?
Thank you and best regards
Venturi

Ian
Posts: 110
Joined: Thu Sep 29, 2005 10:53 am
Location: Matrix Multimedia
Been thanked: 1 time
Contact:

Post by Ian »

venturi wrote: I have every zero cross 7 pulses of 50 us each so for the duration of 400 us.
But i cannot understand why there are 7 pulses.

Venturi
Are you counting 1-8 and getting 7 or 0-7 and getting 7?
I.e. are you accounting for 0 as a start number not 1 in the loops and counts etc.?

Also are you upping the count then resetting/quitting the loop, or resetting/quitting then upping the count?
I.e. are you skipping the last increment and not upping the count to 8 before going off and displaying it etc.?

If in doubt trace the logic flow (or a cut down test version) to see what happens when you get to 7 and 8...

jimhumphries
Posts: 112
Joined: Wed Oct 12, 2005 6:29 pm
Location: USA
Been thanked: 1 time
Contact:

Post by jimhumphries »

Venturi:

I think this is because I forgot to reset ZERO after a zero crossing is detected (but why you get seven pulses I don't know). That causes the zero crossing detect loop to be skipped after the first pass through.

Try cutting and pasting the ZERO = 0 equate object to just above the input object inside the loop. That will reset ZERO before each read of the input port. Let me know if that fixes the seven pulse problem

Also - I hope you're having fun with this and are exploring Flowcode. It is relatively easy to learn and is a very powerful tool once you get the hang of it.

Jim

venturi
Flowcode V4 User
Posts: 9
Joined: Sun Apr 02, 2006 10:07 pm
Contact:

Post by venturi »

After taking a closer look with 2 channels i discovered 2 things.
1. What i mentioned earlier about zerocross pulwidth of a few usec is not true. This is 450 usec.

2. The PIC is just pulsing 8 pulses during this zerocross pulse and those are about 50 micros long each. The last pulse occures just after zerocross detection goes low again.

Ignition pulses should be generated just after detection so i must have screwed up somethining in the flowchart.
But i'm happy anyway because it is doing something.

Venturi

venturi
Flowcode V4 User
Posts: 9
Joined: Sun Apr 02, 2006 10:07 pm
Contact:

Post by venturi »

Jim,

i did what you suggested but no change.
Please could you give me some directions to get the triac to do what it is supposed to do ?
Indeed it is fun to work with flowcode and i hope to get the hang of it soon or at least to begin to understand.some basics.
There is another thing, the zerocross detection should only work with the high to low transition because the complete pulse consists of two halfs.
The first half is the end of the previous cycle and the second half the beginning of the cycle we want to cut.
Thank you again
Venturi

jimhumphries
Posts: 112
Joined: Wed Oct 12, 2005 6:29 pm
Location: USA
Been thanked: 1 time
Contact:

Post by jimhumphries »

Venturi:

I also forgot to reset ON_TIME but that shouldn't have been the problem.

I built the flow code I described above and attached a single push button object connected to "single bit" 0 of (input) port B. That simulates your zerro crossing hardware. I also added an LED object and connected a single LED to "single bit" 0 of (output) port A. I then ran the Flowcode in simulation mode and, as far as I can tell, it works like it is supposed to.

Send your email address to me at jimhumphries@sbcglobal.net and I will reply with the actual Flowcode attached so that you can run the simulation yourself and then try the compiled code on your target system.

After we get the basic system going I'd be happy to help with general details of the actual dimmer.

Jim

venturi
Flowcode V4 User
Posts: 9
Joined: Sun Apr 02, 2006 10:07 pm
Contact:

Post by venturi »

Thank you for flowchart, it works indeed.
Changing value of OFF changes width of ignitionpulse.
Value of 255 gives a pulse of 2.8 usec. But wen looking at lampoutput it is difficult to see any difference in the intensity.
Now i'm trying to make the pulse longer using a switch on RA0 but sofar without luck.
Venturi

venturi
Flowcode V4 User
Posts: 9
Joined: Sun Apr 02, 2006 10:07 pm
Contact:

Post by venturi »

Sorry, pulsewidth should be 2.8 msec.

jimhumphries
Posts: 112
Joined: Wed Oct 12, 2005 6:29 pm
Location: USA
Been thanked: 1 time
Contact:

Post by jimhumphries »

Venturi:

Glad we got that going.

If I'm not mistaken SCRs and TRIACs will turn "on" (very quickly) and stay "on" as long as current is flowing through the load (turning an SCR or TRIAC "on" requires driving the gate until load current begins to flow). Once the SCR or TRIAC is "on" the gate drive can go to zero and current will continue to flow through the load (your lamp) until the next zero-crossing - where the the line voltage and load current go to 0.

If that's correct, you'll want to turn the SCR or TRIAC "on" at the end of the program's ON_TIME delay. Current will then flow through the lamp until the next zero-crossing.

To produce a brief gate drive pulse you can move the ON = 1 equate object and the ON to A0 output object to just above the ON = 0 equate object. Then, when the program comes out of the ON_TIME loop, A0 will go briefly high and then low. If the ON pulse is too short to trigger your TRIAC you can add a 1 mS delay just before the ON = 0 equate object (or if 1 mS is too long add another loop there that ends when some new variable equals a preset number - just like the ON_TIME loop). There are a lot of ways to generate a brief pulse in hardware but this lets you do it in software

Try that and see if you can then control the brightness. The brightness of the lamp will be proportional to the "on" time of the SCR or TRIAC so the lamp should be brightest when the variable OFF is smallest (1) and least bright when OFF is largest (255).

If that works you should have the basic elements that you need to take it from there.

Jim

Post Reply