找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2019|回复: 2
打印 上一主题 下一主题
收起左侧

Arduino简易小型PLC代码

  [复制链接]
跳转到指定楼层
楼主
ID:330820 发表于 2022-11-28 12:45 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
简昜小型PLC只有16个指令,这几个是PLC基本功能,如需要其它功能可自行加上。
输入输出可自行更改位置点数。
内部继电器M,计数器,计时器,可增加所需要个数。
如果要在设备上运行。
需要在输入,输出,电源,每个部份要做好抗干扰功能。
连接人机介面HMI可成为小型控制系统。在小型设备使用很方便。
沒有实际在设备上測试,只在arduino LGT8F328 nulllab mini arduino  nano板上测试过!

easy_mini_plc.ino
  1. //在arduino LGT8F328 nulllab mini arduino nano板上测试过,
  2. //这几个都是PLC基本功能,如需要其它功能可自行加上。
  3. //------------------------------------------------------

  4. #include "macrodef.h"  
  5. unsigned int T2ovfcount=10;               //timers 100ms
  6. //-------------------------------------------------------
  7. void setup()
  8. {
  9.   Serial.begin(115200);  
  10. //----------------------------------------
  11. //更改自己的输入输出点
  12. //-----------------------------------------
  13.   pinMode(A2, INPUT_PULLUP);
  14.   pinMode(A3, INPUT_PULLUP);
  15.   pinMode(4, INPUT_PULLUP);
  16.   pinMode(5, INPUT_PULLUP);
  17.   pinMode(6, INPUT_PULLUP);
  18. //----------------------------------------
  19. // fastioMode(6, OUTPUT);
  20.   pinMode(7, OUTPUT);
  21.   pinMode(8, OUTPUT);
  22.   pinMode(9, OUTPUT);
  23.   pinMode(13, OUTPUT);
  24. //----------------------------------------
  25. //  T2 10ms Interrupt setup
  26. //----------------------------------------
  27.   noInterrupts();
  28.   TCCR2A =
  29.     1 << WGM20;
  30.   TCCR2B =
  31.     1 << WGM22 |
  32.     1 << CS22 |
  33.     1 << CS21 |
  34.     1 << CS20;
  35.   TIMSK2 =
  36.     1 << TOIE2;
  37.   OCR2A = 78;
  38.   interrupts();
  39. //--------------------------------
  40.   count0=0;
  41.   timer0=0;
  42.   count1=0;
  43.   timer1=0;
  44.   count2=0;
  45.   timer2=0;
  46.   count3=0;
  47.   timer3=0;
  48. }
  49. //-------------------------------
  50. void loop()
  51. {
  52. //---------------------------------------------
  53. //  其它程式
  54. //---------------------------------------------
  55. test_plc1();
  56. //test_plc2();
  57. //test_plc3();
  58. //test_plc4();
  59. //test_plc5();
  60. //---------------------------------------------
  61. PlcEnd();
  62. //---------------------------------------------
  63. //test output
  64.       
  65.    Serial.print(stb);
  66.    Serial.print("\t");
  67.    Serial.print(count0);
  68.    Serial.print("\t");
  69.    Serial.print(timer30);
  70.    Serial.print("\t");
  71.    Serial.print(Y3);
  72.    Serial.print("\t");
  73.    Serial.print(T30);
  74.    Serial.println();
  75.    
  76. }

  77. //---------------------------------------
  78. // ISR Timer Routine 10ms Interrup
  79. //---------------------------------------
  80. ISR(TIMER2_OVF_vect){
  81.      T2ovfcount--;
  82.      isrtimer(30);    //10ms 0.01s timer
  83.      isrtimer(31);
  84.   if(T2ovfcount<=0){
  85.     T2ovfcount=10;   
  86.     isrtimer(0);     //100ms  0.1s timer
  87.     isrtimer(1);
  88.     isrtimer(2);
  89.     isrtimer(3);
  90.     isrtimer(4);
  91.   }
  92. }

  93. //-------------------------------------
  94. // PLC io input output
  95. ///更改自己的输入输出点
  96. //-------------------------------------
  97. void PlcEnd() {
  98. digitalWrite(9,Y0);  
  99. digitalWrite(7,Y1);  
  100. digitalWrite(8,Y2);  
  101. digitalWrite(13,Y3);  
  102.    X0=!digitalRead(A2);
  103.    X1=!digitalRead(A3);
  104.    X2=!digitalRead(4);
  105.    X3=!digitalRead(5);
  106.    X4=!digitalRead(6);
  107.    stb=0;
  108. }

  109. //--------------------------------  
  110. //X0,X1,其中一个接GND 生效
  111. //test plc
  112. //-------------------------------------------------------------------------------------
  113. void test_plc1(){
  114.                
  115. LD(X0)                //   |----||----------|---|/|--------------------------( )-----|
  116. OR(X1)                //   |   LD X0        |  ANI M1                      OUT T3,2  |  0.2s
  117. ANI(M1)               //   |----||----------|                                        |
  118. OUTT(3,2)             //   |   OR X1                                                 |
  119. LD(T3)                //   |----||-------------------------------|-----------( )-----|
  120. ATL(0,Y3)             //   |   LD T2                             |        ATL Y3     |
  121. OUT(M1)               //   |                                     |-----------( )-----|
  122. LD(Y3)                //   |                                              OUT M1     |
  123. OUTC(0,20)            //   |----||-------------------------------------------( )-----|   
  124. LD(C0)                //   |   LD Y3                                      OUT C0,20  |  20   
  125. RSTC(0)               //   |----||-------------------------------------------( )-----|
  126.                       //   |   LD C0                                      RST C0         
  127. }
  128. //-------------------------------------------------------------------------------------------
  129. void test_plc2(){  
  130. LD (X4)            //    |-------||-------|--------||-------||-------|------( )----|
  131. LD (X0)            //    |     LD X4      |     LD X0      AND X1    |  OUT Y3     |                     
  132. AND(X1)            //    |                |--------||-------||-------|             |
  133. LD (X2)           //                            LD X2      AND X3
  134. AND(X3)  
  135. ORB
  136. ANB
  137. OUT(Y3)  
  138. }   
  139. //-----------------------------------------------------------------------------------------
  140. void test_plc3(){
  141. LD  (X0)           //    |------||------|------||------|--------------------( )-----|
  142. OR  (X2)           //    |     LD X0    |    AND X1    |                 OUT Y3
  143. AND (X1)           //    |------||------|              |
  144. LD  (X3)           //    |     ORI X2                  |
  145. AND (X4)           //    |------||------||-------------|  
  146. ORB                //    |     LD X3   AND X4
  147. OUT(Y3)
  148. }
  149. //-------------------------------------------------------------------
  150. void test_plc4(){
  151. LD (X0)                           
  152. SET(M0)
  153. LD (M0)
  154. OUT(Y3)
  155. LD (X1)
  156. RST(M0)
  157. }
  158. //------------------------------------------------------------------
  159. void test_plc5(){
  160. LD (X2)
  161. ATL(0,Y3)  
  162. }      
  163. //------------------------------------------------------------------
复制代码



macrodef.h


  1. //----------------------------------------------------------
  2. //计时器 中斷 时间 -- isrtimer(0)---
  3. //------------------------------------------------------------
  4. #define  isrtimer(x)\
  5.   if (startt##x){\
  6.       if(!T##x){\
  7.         if(!timer##x){\
  8.            T##x=1;\
  9.         }\
  10.         else{\
  11.           timer##x--;\
  12.         }\
  13.       }\
  14.     }
  15. //-------------------------------------------------------
  16. //计时器 OUT x=计时器号,k=计时器时间 ----OUTT(2,5)----
  17. //-------------------------------------------------------
  18. #define OUTT(x, k)\
  19.           stb=0;\
  20.     if (st0) {\
  21.        if(!T##x){\
  22.         if(!startt##x){\
  23.            startt##x=1;\
  24.            timer##x=k;\
  25.            }\
  26.         }\
  27.     }\
  28.     else {\
  29.             timer##x=0;\
  30.            startt##x=0;\
  31.            T##x=0;\
  32.            }
  33.                            
  34. //-----------------------------------------------------------
  35. //按一次键ON 又按一次键OFF ATL(a,d) a=ATL号 d=输出  --ATL(0,Y3)---
  36. //-----------------------------------------------------------
  37. #define ATL(a,d)\
  38.                if(st0){\
  39.                   if (!atl##a){\
  40.                   d =(st0^d);\
  41.                  atl##a=1;\
  42.                   }\
  43.                }\
  44.               else{\
  45.                 atl##a=0;\
  46.                }   
  47.                                                
  48. //---------------------------------------------------
  49. //counter 计数器 x=计数器号 k=计数器数值----OUTC(0,20)---
  50. //---------------------------------------------------------
  51. #define OUTC(x,k)\
  52.            stb=0;\
  53.         if(C##x==0){\
  54.             if(st0){\
  55.               if(cset##x==0) { count##x=k; cset##x=1;}\
  56.                 if(cst##x==0){\
  57.                       cst##x=1;\
  58.                          count##x--;\
  59.                           if(count##x==0){\
  60.                          C##x=1;\
  61.                        }\
  62.                   }\
  63.               }\
  64.                 else{\
  65.                   cst##x=0;\
  66.               }\
  67.     }
  68. //---------------------------------------------------------
  69. //reset 计数器 x=计数器号
  70. //------------------------------------------------
  71. #define RSTC(x)\
  72.        if(st0){\
  73.               C##x=0;\
  74.               cst##x=0;\
  75.               cset##x=0;\
  76.               count##x=0;\
  77.        }
  78. //----------------------------------------------------------
  79. //reset timer 计时器  ---RSTT(0)
  80. //-------------------------------------------------------------
  81. #define RSTT(d)\
  82.    if(st0){\
  83.       timer##x=0;\
  84.       startt##x=0;\
  85.       T##d=0;\
  86.    }      
  87. //-------------------------------------------------------      
  88. #define SET(d)   if(st0){d=1;}                     
  89. #define RST(d)   if(st0){d=0;}                     
  90. #define LD(d)    stu[stb]=st0;stb++; st0=d;        
  91. #define LDI(d)   stu[stb]=st0;stb++; st0 =!d;
  92. #define OUT(d)   d=st0;stb=0;
  93. #define ANB      stb--;st0=(st0 & stu[stb]);
  94. #define AND(d)   st0=(st0 & d);
  95. #define ANI(d)   st0=(st0 &!d);
  96. #define ORB      stb--;st0=(st0 | stu[stb]);
  97. #define OR(d)    st0=(st0 | d);   
  98. #define ORI(d)   st0=(st0 |!d);      

  99. //------------------------------------------------
  100. //內部状态使用
  101. //-------------------------------------------------
  102. bool stu[8]{};
  103. unsigned int stb;
  104. bool st0;
  105. //------ATL------
  106. bool atl0;
  107. bool atl1;
  108. bool atl2;
  109. bool atl3;
  110. bool atl4;
  111. //-----------------------------------------
  112. //PLC 输入
  113. //----------------------------------------------
  114. bool X0;
  115. bool X1;
  116. bool X2;
  117. bool X3;
  118. bool X4;
  119. //---------------------------------------------
  120. //PLC 输出
  121. //----------------------------------------------
  122. bool Y0;
  123. bool Y1;
  124. bool Y2;
  125. bool Y3;
  126. bool Y4;
  127. //--------------------------
  128. //PLC 內部继电器
  129. //---------------------------
  130. bool M0;
  131. bool M1;
  132. bool M2;
  133. bool M3;
  134. bool M4;
  135. //----------------------------
  136. //计时器状态使用
  137. //------------------------------

  138. unsigned int timer0;
  139. bool startt0;
  140. bool T0;
  141. //------------------------
  142. unsigned int timer1;
  143. bool startt1;
  144. bool T1;
  145. //------------------------
  146. unsigned int timer2;
  147. bool startt2;
  148. bool T2;
  149. //------------------------

  150. unsigned int timer3;
  151. bool startt3;
  152. bool T3;
  153. //------------------------
  154. unsigned int timer4;
  155. bool startt4;
  156. bool T4;
  157. //------------------------
  158. unsigned int timer30;
  159. bool startt30;
  160. bool T30;
  161. //------------------------
  162. unsigned int timer31;
  163. bool startt31;
  164. bool T31;
  165. //----------------------------
  166. //计数器状态使用
  167. //------------------------------
  168. unsigned int count0;
  169. bool C0;
  170. bool cset0;
  171. bool cst0;
  172. //--------------------------------
  173. unsigned int count1;
  174. bool C1;
  175. bool cset1;
  176. bool cst1;
  177. //-------------------------------
  178. unsigned int count2;
  179. bool C2;
  180. bool cset2;
  181. bool cst2;
  182. //-----------------------------------
  183. unsigned int count3;
  184. bool C3;
  185. bool cset3;
  186. bool cst3;
  187. //----------------------------------------
  188. unsigned int count4;
  189. bool C4;
  190. bool cset4;
  191. bool cst4;
  192. //---------------------------------------
  193. unsigned int count30;
  194. bool C30;
  195. bool cset30;
  196. bool cst30;
  197. //-----------------------------------------
复制代码


评分

参与人数 2黑币 +55 收起 理由
zwk34 + 5 赞一个!
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏5 分享淘帖 顶2 踩
回复

使用道具 举报

沙发
ID:857155 发表于 2023-2-28 14:16 | 只看该作者
楼主厉害!&#128077;&#128077;&#128077;请问这个怎么实现
回复

使用道具 举报

板凳
ID:53978 发表于 2023-3-2 20:10 | 只看该作者
这个厉害呀,应该会有实用性。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表