簡昜小型PLC只有16個指令,這幾個是PLC基本功能,如需要其它功能可自行加上。
輸入輸出可自行更改位置點數(shù)。
內(nèi)部繼電器M,計數(shù)器,計時器,可增加所需要個數(shù)。
如果要在設(shè)備上運行。
需要在輸入,輸出,電源,每個部份要做好抗干擾功能。
連接人機介面HMI可成為小型控制系統(tǒng)。在小型設(shè)備使用很方便。
沒有實際在設(shè)備上測試,只在arduino LGT8F328 nulllab mini arduino nano板上測試過!
easy_mini_plc.ino
- //在arduino LGT8F328 nulllab mini arduino nano板上測試過,
- //這幾個都是PLC基本功能,如需要其它功能可自行加上。
- //------------------------------------------------------
- #include "macrodef.h"
- unsigned int T2ovfcount=10; //timers 100ms
- //-------------------------------------------------------
- void setup()
- {
- Serial.begin(115200);
- //----------------------------------------
- //更改自己的輸入輸出點
- //-----------------------------------------
- pinMode(A2, INPUT_PULLUP);
- pinMode(A3, INPUT_PULLUP);
- pinMode(4, INPUT_PULLUP);
- pinMode(5, INPUT_PULLUP);
- pinMode(6, INPUT_PULLUP);
- //----------------------------------------
- // fastioMode(6, OUTPUT);
- pinMode(7, OUTPUT);
- pinMode(8, OUTPUT);
- pinMode(9, OUTPUT);
- pinMode(13, OUTPUT);
- //----------------------------------------
- // T2 10ms Interrupt setup
- //----------------------------------------
- noInterrupts();
- TCCR2A =
- 1 << WGM20;
- TCCR2B =
- 1 << WGM22 |
- 1 << CS22 |
- 1 << CS21 |
- 1 << CS20;
- TIMSK2 =
- 1 << TOIE2;
- OCR2A = 78;
- interrupts();
- //--------------------------------
- count0=0;
- timer0=0;
- count1=0;
- timer1=0;
- count2=0;
- timer2=0;
- count3=0;
- timer3=0;
- }
- //-------------------------------
- void loop()
- {
- //---------------------------------------------
- // 其它程式
- //---------------------------------------------
- test_plc1();
- //test_plc2();
- //test_plc3();
- //test_plc4();
- //test_plc5();
- //---------------------------------------------
- PlcEnd();
- //---------------------------------------------
- //test output
-
- Serial.print(stb);
- Serial.print("\t");
- Serial.print(count0);
- Serial.print("\t");
- Serial.print(timer30);
- Serial.print("\t");
- Serial.print(Y3);
- Serial.print("\t");
- Serial.print(T30);
- Serial.println();
-
- }
-
- //---------------------------------------
- // ISR Timer Routine 10ms Interrup
- //---------------------------------------
- ISR(TIMER2_OVF_vect){
- T2ovfcount--;
- isrtimer(30); //10ms 0.01s timer
- isrtimer(31);
- if(T2ovfcount<=0){
- T2ovfcount=10;
- isrtimer(0); //100ms 0.1s timer
- isrtimer(1);
- isrtimer(2);
- isrtimer(3);
- isrtimer(4);
- }
- }
- //-------------------------------------
- // PLC io input output
- ///更改自己的輸入輸出點
- //-------------------------------------
- void PlcEnd() {
- digitalWrite(9,Y0);
- digitalWrite(7,Y1);
- digitalWrite(8,Y2);
- digitalWrite(13,Y3);
- X0=!digitalRead(A2);
- X1=!digitalRead(A3);
- X2=!digitalRead(4);
- X3=!digitalRead(5);
- X4=!digitalRead(6);
- stb=0;
- }
- //--------------------------------
- //X0,X1,其中一個接GND 生效
- //test plc
- //-------------------------------------------------------------------------------------
- void test_plc1(){
-
- LD(X0) // |----||----------|---|/|--------------------------( )-----|
- OR(X1) // | LD X0 | ANI M1 OUT T3,2 | 0.2s
- ANI(M1) // |----||----------| |
- OUTT(3,2) // | OR X1 |
- LD(T3) // |----||-------------------------------|-----------( )-----|
- ATL(0,Y3) // | LD T2 | ATL Y3 |
- OUT(M1) // | |-----------( )-----|
- LD(Y3) // | OUT M1 |
- OUTC(0,20) // |----||-------------------------------------------( )-----|
- LD(C0) // | LD Y3 OUT C0,20 | 20
- RSTC(0) // |----||-------------------------------------------( )-----|
- // | LD C0 RST C0
- }
- //-------------------------------------------------------------------------------------------
- void test_plc2(){
- LD (X4) // |-------||-------|--------||-------||-------|------( )----|
- LD (X0) // | LD X4 | LD X0 AND X1 | OUT Y3 |
- AND(X1) // | |--------||-------||-------| |
- LD (X2) // LD X2 AND X3
- AND(X3)
- ORB
- ANB
- OUT(Y3)
- }
- //-----------------------------------------------------------------------------------------
- void test_plc3(){
- LD (X0) // |------||------|------||------|--------------------( )-----|
- OR (X2) // | LD X0 | AND X1 | OUT Y3
- AND (X1) // |------||------| |
- LD (X3) // | ORI X2 |
- AND (X4) // |------||------||-------------|
- ORB // | LD X3 AND X4
- OUT(Y3)
- }
- //-------------------------------------------------------------------
- void test_plc4(){
- LD (X0)
- SET(M0)
- LD (M0)
- OUT(Y3)
- LD (X1)
- RST(M0)
- }
- //------------------------------------------------------------------
- void test_plc5(){
- LD (X2)
- ATL(0,Y3)
- }
- //------------------------------------------------------------------
復(fù)制代碼
macrodef.h
|