Click Here
and
Low Cos programmer
http://electronics-diy.com/avr_programmer.php
Homemade PLC
Make you own
Homemade PLC
Wednesday, 4 May 2011
Creating HEX File
o now we are fairly sure that the program works. At this point we are ready to generate actual code, and try it in the micro. First, exit simulation mode by choosing Simulate -> Simulation Mode, or by pressing Escape.
Next we must choose a microcontroller. We decided earlier that we would be using a PIC16F876, so choose Settings -> Microcontroller -> Microchip PIC16F876 28-PDIP or 28-SOIC. We also have to tell LDmicro what kind of crystal we will be using, and what the cycle time will be. Choose Settings -> MCU Parameters, and fill in our clock speed of 20 MHz. Leave the cycle time at 10 ms; that will usually be a good value.
Now we can assign pins to our inputs and outputs. Double-click `Xbutton' in the list at the bottom of the screen, and choose pin 14 of the PIC, which corresponds to MCU port RC3. (There is usually no reason for you to care which port you are using; just look at the pin number.)
Click `OK,' and then repeat the process for `Yled', which you can see from the schematic should go to pin 15. The other elements in the list are internal variables and bits in memory, so there is no need to assign pins to them. LDmicro will allocate memory for them when you compile.
So now you are ready to compile. Choose Compile -> Compile, and specify where you want to put the IHEX file. Then use whatever PIC programming equipment you have available to load that into your device, and you are ready to try it out.
This completes my tutorial. It is possible to write much more complex programs than that, of course. A program this simple uses only a very small fraction of your processor's memory, so there is room for many more rungs of logic. LDmicro also offers specialised instructions, for things like arithmetic, analog (A/D) inputs, PWM, and even text output to a character-based LCD. Consult the manual for details
Next we must choose a microcontroller. We decided earlier that we would be using a PIC16F876, so choose Settings -> Microcontroller -> Microchip PIC16F876 28-PDIP or 28-SOIC. We also have to tell LDmicro what kind of crystal we will be using, and what the cycle time will be. Choose Settings -> MCU Parameters, and fill in our clock speed of 20 MHz. Leave the cycle time at 10 ms; that will usually be a good value.
Now we can assign pins to our inputs and outputs. Double-click `Xbutton' in the list at the bottom of the screen, and choose pin 14 of the PIC, which corresponds to MCU port RC3. (There is usually no reason for you to care which port you are using; just look at the pin number.)
Click `OK,' and then repeat the process for `Yled', which you can see from the schematic should go to pin 15. The other elements in the list are internal variables and bits in memory, so there is no need to assign pins to them. LDmicro will allocate memory for them when you compile.
So now you are ready to compile. Choose Compile -> Compile, and specify where you want to put the IHEX file. Then use whatever PIC programming equipment you have available to load that into your device, and you are ready to try it out.
This completes my tutorial. It is possible to write much more complex programs than that, of course. A program this simple uses only a very small fraction of your processor's memory, so there is room for many more rungs of logic. LDmicro also offers specialised instructions, for things like arithmetic, analog (A/D) inputs, PWM, and even text output to a character-based LCD. Consult the manual for details
Simulation
Now we are ready to simulate our circuit. Choose Simulate -> Simulation Mode. The display will change; the ladder diagram will appear mostly greyed, but you won't see anything changing with time. That is because the PLC is not yet cycling. To start it cycling, choose Simulate -> Start Real-Time Simulation. Now you will see things happening: the oscillator is obviously running, but the LED (`Yled') is still off, which is what we want, because no one has pressed the button yet. To simulate pressing the button, double-click the text `Xbutton' in the list at the bottom of the screen. You have now simulated bringing the pushbutton input high; this is what would happen if someone depressed (but did not yet release) the pushbutton.
Ladder Diagram for the Program
First, we are going to need an oscillator to generate the `blinking' signal for the LED. There is a standard way to do this in ladder logic:
Also notice that we have chosen to use an internal relay (`Rfoo') instead of one attached to an I/O pin (`Yfoo' or `Xfoo'). This makes sense, because there is no particular reason to bring that signal out to a pin. LDmicro will automatically assign memory for the internal relay.
Our program will have three states: off, steady on, and blinking. The program should change its state on each rising edge of the signal from the pushbutton. This is a good application for a circular counter. We will say that `state 0' is `off,' `state 1' is `steady on,' and `state 2' is `blinking.' The counter counts 0, 1, 2, 0, 1, 2, ..., so if we just let the rung-in condition of the counter be the pushbutton input, then everything will work like we want:
|| Rosc Tosc_on Tosc_off Rosc || 1 ||-------] [--------[TON 250.0 ms]---[TOF 250.0 ms]---------(/)-------||This will flash at 1/((250+250) ms), or 2 Hz, or twice per second. The duty cycle will be 50%—250 ms on, then 250 ms off. This circuit can make any kind of oscillator, with whatever period or duty cycle you require, so it is a good one to remember.
Also notice that we have chosen to use an internal relay (`Rfoo') instead of one attached to an I/O pin (`Yfoo' or `Xfoo'). This makes sense, because there is no particular reason to bring that signal out to a pin. LDmicro will automatically assign memory for the internal relay.
Our program will have three states: off, steady on, and blinking. The program should change its state on each rising edge of the signal from the pushbutton. This is a good application for a circular counter. We will say that `state 0' is `off,' `state 1' is `steady on,' and `state 2' is `blinking.' The counter counts 0, 1, 2, 0, 1, 2, ..., so if we just let the rung-in condition of the counter be the pushbutton input, then everything will work like we want:
|| Xbutton Cstate || 2 ||-------] [---------------------------------------------{CTC 0:2}----||Now the only thing left is to use the program state to set the state of the LED. We can do it like this:
|| [Cstate ==] Yled || 3 ||---[ 1 ]-------------------+------------------------( )-------|| || | || || [Cstate ==] Rosc | || ||---[ 2 ]----------] [------+ ||It should be easy to convince yourself that this does what we want. If the program is in state 1, then the `Cstate == 1' instruction energizes `Yled', as desired. In state 2, the `Cstate == 2' instruction energizes `Yled', but only when `Rosc' is also true. Since `Rosc' is oscillating, that means that the LED will blink, as desired. Finally, in state 0, neither of the equals instructions will be true, so there is no way that `Yled' could ever turn on.
Microcontroller Selection and Schematic
We will be using a PIC16F876, which is easily available from Digikey or other online distributors. It comes in a number of different packages; I chose a DIP.
Note that as of Nov 2009, the PIC16F876 is no longer recommended for new design. This means that it will probably get discontinued at some point in the next few years. You may prefer to instead use a PIC16F886, which is pin-compatible. If you do, then make sure to specify the correct part when you compile, since the 'F886 is not code-compatible.
This is our schematic:
The microcontroller (IC1) is part number PIC16F876-20I/SP-ND at Digikey. Almost any three-terminal resonator (U1) will do; you might try a 535-9356-ND or an X909-ND.
The only thing that might confuse you is that the pushbutton goes to Vdd, and there is a pull-down. You might be more used to seeing a pushbutton to ground with a pull-up. For TTL, this mattered. For modern CMOS it does not, and I find this `active HIGH' arrangement less confusing than the traditional `active LOW' circuit.
Also, I chose to use a ceramic resonator with internal capacitors, U1, instead of a crystal and two ~20 pF caps. A crystal would work just as well and it would be more accurate, but it would be a little bit more expensive, and you would need more parts.
You could build this circuit in many different ways. I built it on a solderless breadboard, and it ended up looking like this:
Note that as of Nov 2009, the PIC16F876 is no longer recommended for new design. This means that it will probably get discontinued at some point in the next few years. You may prefer to instead use a PIC16F886, which is pin-compatible. If you do, then make sure to specify the correct part when you compile, since the 'F886 is not code-compatible.
This is our schematic:
The microcontroller (IC1) is part number PIC16F876-20I/SP-ND at Digikey. Almost any three-terminal resonator (U1) will do; you might try a 535-9356-ND or an X909-ND.
The only thing that might confuse you is that the pushbutton goes to Vdd, and there is a pull-down. You might be more used to seeing a pushbutton to ground with a pull-up. For TTL, this mattered. For modern CMOS it does not, and I find this `active HIGH' arrangement less confusing than the traditional `active LOW' circuit.
Also, I chose to use a ceramic resonator with internal capacitors, U1, instead of a crystal and two ~20 pF caps. A crystal would work just as well and it would be more accurate, but it would be a little bit more expensive, and you would need more parts.
You could build this circuit in many different ways. I built it on a solderless breadboard, and it ended up looking like this:
How It Works (Introduction)
PLCs are often programmed in ladder logic. This is because PLCs originally replaced relay control systems, and forty years later, we still haven't quite let go. A PLC, like any microprocessor, executes a list of instructions in sequence. Ladder logic tools abstract this; you can program the PLC by wiring up relay contacts and coils on-screen, and the PLC runtime will simulate the circuit that you've drawn. Some of the relay contacts can be tied to input signals from the real world; some of the coils can be tied to outputs. That way you can make your simulated circuit interact with other devices, and actually control things. That is the point.
Actually it's more general than that, because you can incorporate timers and counters and arithmetic operations that you couldn't (easily) perform with just relays. The circuit concept is still useful though, partly just because it's intuitive, but also because it abstracts the concurrency issues. It looks like this:
We close the loop with a simple hysteretic (bang-bang) controller. We have selected plus or minus 20 ADC units of hysteresis. That means that when the temperature falls below (setpoint - 20), we turn on the heater, and when it climbs above (setpoint + 20), we turn the heater off.
I chose to add a few small frills. First, there is an enable input: the heater is forced off when Xenable is low. I also added an indicator light, Yis_hot, to indicate that the temperature is within regulation. This compares against a threshold slightly colder than (setpoint - 20), so that the light does not flicker with the normal cycling of the thermostat.
This is a trivial example, but it should be clear that the language is quite expressive. Ladder logic is not a general-purpose programming language, but it is Turing-complete, accepted in industry, and, for a limited class of (mostly control-oriented) problems, surprisingly convenient.
Actually it's more general than that, because you can incorporate timers and counters and arithmetic operations that you couldn't (easily) perform with just relays. The circuit concept is still useful though, partly just because it's intuitive, but also because it abstracts the concurrency issues. It looks like this:
|| Xa Xb Yout || 1 ||-------] [------+-------] [------+-------( )-------|| || | | || || | Xc | || || +-------]/[------+ ||This is a simple piece of combinational logic. There are three input terms, Xa, Xb, and Xc. There is one output term, Yout. The expression is Yout := Xa and (Xb or (not Xc)). This makes sense if you think of Xa and Xb as normally open relay contacts, Xc as normally closed relay contacts, and Yout as a relay coil. Of course it gets more complicated than that:
|| || || Asetpoint || 1 ||-------------------------------------{READ ADC}----|| || || || Atemperature || ||-------------------------------------{READ ADC}----|| || || || || || || || || || {SUB min_temp :=} || 2 ||------------------------{ Asetpoint - 20 }--------|| || || || {ADD max_temp :=} || ||------------------------{ Asetpoint + 20 }--------|| || || || || || || || || ||[Atemperature >] Yheater || 3 ||[ max_temp ]+------------------------(R)-------|| || | || || Xenable | || ||-------]/[------+ || || || ||[Atemperature <] Xenable Yheater || ||[ min_temp ]--------] [--------------(S)-------|| || || || || || || || || || {SUB check_temp :=} || 4 ||-----------------------{ Asetpoint - 30 }-------|| || || || || || || || || ||[Atemperature >] Yis_hot || 5 ||[ check_temp ]-------------------------( )-------|| || || || || || || ||------[END]----------------------------------------|| || || || ||This is for a simple thermostat. There are two analog inputs; one of them is for the setpoint, so that it might, for example, be connected to a pot that the user turns to select the desired temperature. The other provides the temperature measurement; it might be a semiconductor temperature sensor, or a platinum RTD with suitable interfacing circuitry. There is a digital output, Yheater. That might control a heating element, through a suitable switch (a TRIAC, or a relay, or a solid-state relay, or whatever).
We close the loop with a simple hysteretic (bang-bang) controller. We have selected plus or minus 20 ADC units of hysteresis. That means that when the temperature falls below (setpoint - 20), we turn on the heater, and when it climbs above (setpoint + 20), we turn the heater off.
I chose to add a few small frills. First, there is an enable input: the heater is forced off when Xenable is low. I also added an indicator light, Yis_hot, to indicate that the temperature is within regulation. This compares against a threshold slightly colder than (setpoint - 20), so that the light does not flicker with the normal cycling of the thermostat.
This is a trivial example, but it should be clear that the language is quite expressive. Ladder logic is not a general-purpose programming language, but it is Turing-complete, accepted in industry, and, for a limited class of (mostly control-oriented) problems, surprisingly convenient.
Quick summary
This is a Microcontroller Based PLC that starts with a ladder diagram and generates native PIC16 or AVR code. Features include:
- digital inputs and outputs
- timers (TON, TOF, RTO)
- counters (CTU, CTD, `circular counters' for use like a sequencer)
- analog inputs, analog (PWM) outputs
- integer variables and arithmetic instructions
- easy-to-use serial communications, to a PC, LCD, or other device
- shift registers, look-up tables
- EEPROM variables, whose values are not forgotten when you lose power
- simulator, to test your program before you generate PIC/AVR code
Subscribe to:
Posts (Atom)