西门子PLC中使用SCL语言编程的技巧_第1页
西门子PLC中使用SCL语言编程的技巧_第2页
西门子PLC中使用SCL语言编程的技巧_第3页
西门子PLC中使用SCL语言编程的技巧_第4页
西门子PLC中使用SCL语言编程的技巧_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、.在西门子 plc中使用 scl语言编程的技巧前言:两年半前我就在工控网上发表了有关 scl编程的知识 , 但发表完后,即使我自己都从没有把任何使用 scl编写的程序用到实际控制中,当时的感觉是使用 scl编程到处受限,没有stl语言灵活和强大。直到最近使用施耐德的 unity 软件编程,并使用这种已经国际标准化的文本语言(等同于西门子的 scl语言),才体会到它的优点:、程序容易阅读,便于诊断和维护;、程序容易在不同厂家之间的plc之间转换。西门子的stl语言是强大,但难于阅读,编写程序也需要异常小心,其最强大的可能是它的寄存器寻址(类似于一些计算机高级语言中的地址指针),scl没有这个功能

2、,那就多费一些程序代码来实现同样的功能,程序是否优秀更应该看重程序的架构和提高程序生产效率的标准化,好的plc程序不应该只有自己明白,而是让更多的人明白。 在西门子 plc中使用 scl语言的场合一般是编写标准功能块fb,其编程方式和西门子的其他编程语言,如梯形图 lad、语句表 stl是完全不同的,同时为了实现程序的国际标准化,即为了方便的将程序代码移植到不同厂家的 plc系统上,尽量不要在scl中使用西门子独有的功能块。.1、 在 fb 中使用 构 写 fb 的准 ,就是其使用的内部 量尽量与外部隔离,除了像plc的新启 / 重启 志,以及一些方波 / 脉冲波等全局 量可以在fb 中使用外

3、, 其他的任何全局 量都不 在fb内部使用,即使是自定 构也 在fb 中 独定 ,在fb 中使用 构 在静 型 量中定 ,如下: var / static variables im:struct /data structure of internal flags h1_afcountimp:bool:=false; / aux flag counter impulse h1_countimp:bool:=false; / counter impulse h1_elcountmv:bool:=false; /endless counter maximum value end_struct; /o

4、ther data structure end_var在使用 些 构 ,可以按照如下方式: im. h1_countimp:=imp;2、 在 scl中替代 set/reset指令的方法 scl 中不存在 set/reset 指令,或者 也没有必要使用。在scl中,不使用排他条件 else 的条件 句就是一个set/reset 指令。.如下编程: if then variable name:=1; end_if;其等同于:(s)若加上 else 条件,如下: if then variable name:=1; else variable name:=0; end_if;则等同于:( )一条完整

5、的包含置位和复位的语句可以使用如下方式编程:if then variable name:=1; end_if; if then variable name:=0; end_if;其等效于 sr指令,若将上面的两个条.件语句的先后次序颠倒一下,则等效于rs指令。3、 简化程序指令 、尽量使用赋值语句替代那些不用于sr/rs指令的 bool型赋值条件语句,如下:if fnadd&(button=false) then pus1:=true; else pus1:=false; end_if;其等效于 pus1:= fnadd &(not button),这样使程序看起来更加简洁和容易阅读。、对于非

6、 bool型赋值语句则不能这如上简化,而是可以通过sel函数实现: if fnadd &(button=false) thenpus1:=value1; else pus1:= value2; end_if;其等效于 pus1:= sel (g:= fnadd &(button=false), in0:=value2,in1:= value1); 使用该函数时注意两点: 、参数名不能省略; 、当选择条件 g为 true时,选择后一个参数值 in1,为 false时,选择前一个参数值 in0, 这点与计算机 c语言等正好相反。 、xor指令有着比 and 和or更为复杂的表达,能使用xor的地方

7、应该尽量使用if(condition1and(not condition2)or(condition2and( not condition1) then result:=true; else result:=false; end_if;其等效于 result:= condition1 xor.condition2; xor功能就是两条件不同输出true,相同输出 false4、 脉冲沿检测功能使用以下两条语句替代脉冲上升沿检测函数,譬如检测 button_input上升沿的代码如下: puls:=button_input&(not button_last); button_last:= but

8、ton_input;同样的下降沿脉冲检测如下:puls:= ( not button_input) & button_last;button_last:= button_input;5、 编写脉冲发生器波峰持续时间仅为一个plc扫描周期的波形称为脉冲波, 而波峰持续时间大于或等于两个plc扫描周期的波形称为方波,脉冲波可用于计数、定时,方波可用于控制信号灯的闪烁输出,可以在西门子 plc的硬件配置中配置一个字节的各种时间的方波(波峰时间和波谷时间为 1:1 ),假设 fp_1sec 为这个字节中 1 秒的方波,则: 、间隔 1 秒的脉冲波“ impls_1sec ” 如下编程: “impls_

9、1sec ” := fp_1sec and (not “ impls_1sec_aux”); “impls_1sec_aux” := fp_1sec ; 、间隔 10 秒的脉冲波“ impls_10sec ” 如下编程: if ( “impls_10sec ” )thencount_ actual:=0;“ impls_10sec ”:=0; elseif ( “impls_1sec ” )then count_ actual:= count _ actual +1; end_if; “impls_10sec ” := count_ actual=10; end_if; count_ actu

10、al 的初始值为0,同时当系统新启动时,也需将其设为零。间隔更长时间的脉冲波编程都可以按照上面的方式编程。.6、尽量使用编程计数功能来替代定时器功能,这样使程序更可靠和易于阅读假设input_condition为输入, output_delay 为通过定时处理后的输出, timer_setpoint为时间设定点, timer_actual为当前时间计数的实际值,“impls_1sec ” 为系统编程产生的1 秒脉冲。 、在输入条件满足的情况下,延时输出的定时器:if (not input_condition) then timer_actual:= 0; output_delay:=0; el

11、se if (“ impls_1sec ” and not output_delay) then timer_ actual:= timer_ actual +1; end_if; output_delay:=timer_actual = timer_setpoint; end_if;.、有记忆的延时输出定时器,即在延时过程中,若输入条件终止,不影响延时,这种定时器必须使用其它的信号复位。ifinput_condition then output_aux:=1; end_if; if (not output_aux) then timer_actual:= 0; output_delay:=0

12、; else if( “impls_1sec ” and not output_delay) then timer_ actual:= timer_ actual +1; end_if; output_delay:= timer_actual =timer_setpoint; end_if;若想终止 output_delay 的输出,必须在后面追加一条条件语句,用于复位output_aux.、立即输出,延时断开的定时器if input_condition then timer_actual:= 0; output_aux:= 0; output_delay:=1; /立即输出else if (

13、 “impls_1sec ” and not output_aux) then timer_ actual:= timer_ actual +1; end_if; output_aux:= timer_actual =timer_setpoint; end_if; if output_aux then output_delay:=0; /延时断开 end_if;、在检测到一个上升沿脉冲后,立即输出,并开始计时,在时间到达后断开。 if input_conditionthenoutput_aux:=1;end_if;if (not output_aux) then timer_actual:=

14、0; timer_arrived := 0; else if (not timer_ arrived and“impls_1sec ” ) then.timer_actual:=timer_actual+1; end_if; timer_ arrived:= timer_actual= timer_setpoint;end_if; if timer_ arrivedthenoutput_aux:=0; end_if; output_delay:= output_aux;通过以上的编程方式可以实现任何定时器功能,而代码却可以为不同的plc系统所使用。7、使用编程计数功能来替代计数器在scl语言中

15、使用计数功能是最为简单的,其关键是必须首先对输入进行脉冲检测假设input_imp为输入脉冲, countimp 为输入脉冲检测 ,counter为计数值, factor 为计数因子(更详细点就是每来一次脉冲,计数值增加多少)。(*- create impulse (impulse evaluation) -*) countimp:=.input_imp and (not countimp_old); countimp_old:= input_imp; (*- counter-*) if countimp then counter:=counter+factor; end_if;一个完整的 数

16、程序 有 数器复位功能以及 数 上限 条件(以防止 数 溢出)。8、 新故障 / 新警告的 一个完整的 fb 能 故障 / 警告,以及新故障 / 新警告,假 input1,input2 inputn 故障的 入(有信号表示 ok), fault1, fault2 faultn 故障位, nfault1, nfault2 nfaultn 新故障位, flt 和 nflt 分 合的故障和新故障, ackn 故障 答 入, 常开点, mute 新故障消除 入 (或者称 蜂 器沉寂) , 常开点: fault1:= notinput1 or(fault1 andnotackn); nfault1:=

17、fault1 and(mute ornfault1); fault2:= notinput2 or(fault2 andnotackn);nfault2:= fault2 and (mute or nfault2); flt := fault1 or fault2 or faultn nflt :=(fault1 and not nfault1) or (fault2and not nfault2) or (faultn and not nfaultn) nflt就是最 的新故障 出指示,新警告的 与之 似。9、字中取位字中取位有两种方式,一种是通 西 子所特有的字取位方式 ,一种是通 算机

18、程的 准方式 ,假 input_word 入参数, word 型, w0,w1,w15 位 量。.、通过西门子的 m变量实现: temp_aux:=mw10; mw10:=input_word; w0:=m11,0; w1:=m11,1; w2:=m11,2; w3:=m11,3; w4:=m11,4; w5:=m11,5; w6:=m11,6; w7:=m11,7; w8:=m10,0; w9:=m10,1; w10:=m10,2; w11:=m10,3; w12:=m10,4; w13:=m10,5; w14:=m10,6; w15:=m10,7; mw10:=temp_aux;、通过标准

19、编程实现 w0:=(input_word & 16#1)=16#1; w1:=(input_word & 16#2)=16#2; w2:=(input_word & 16#4)=16#4; w3:=(input_word & 16#8)=16#8; w4:=(input_word & 16#10)=16#10; w5:=(input_word & 16#20)=16#20; w6:=(input_word &16#40)=16#40; w7:=(input_word & 16#80)=16#80; w8:=(input_word & 16#100)=16#100; w9:=(input_wor

20、d & 16#200)=16#200;w10:=(input_word & 16#400)=16#400; w11:=(input_word & 16#800)=16#800; w12:=(input_word & 16#1000)=16#1000;w13:=(input_word & 16#2000)=16#2000; w14:=(input_word & 16#4000)=16#4000; w15:=(input_word & 16#8000)=16#8000;.使用方式 1 会更加 和容易理解一些,但方式2 具有更加 广的 用 合,更加 准化,即使是当今的 算机 程在取位操作 也 似于上

21、面的 程。字取位的 合,一般用于 数据(譬如 器的状 数据), 可能是字/ 整数,此 就需要用到上面的 程。10、将位 合成字 相当于“字中取位”的反向操作, 也有两种方法 , 一种方法是使用 m 量, 似于“字中取位”的方式 ,另一种也是 准 程,假 output_word 出参数,word 型,w0,w1,w15 位 量。 、通 西 子的 m 量 : temp_aux:=mw10; m11,0 := w0; m11,1 := w1; m11,2 := w2; m11,3 := w3; m11,4 := w4; m11,5 := w5; m11,6 := w6; m11,7 := w7;m1

22、0,0 := w8; m10,1 := w9; m10,2 := w10; m10,3 := w11; m10,4 := w12; m10,5 := w13; m10,6 := w14; m10,7 :=w15; output_word:=mw10; mw10:=temp_aux; 、通 准 程 if w0 then output_word:=output_word or 16#1; else output_word:=output_word and (not 16#1); end_if; if w1 then output_word:=output_word or 16#2; elseout

23、put_word:=output_word and (not 16#2); end_if; if w2 then output_word:=output_word or 16#4; else output_word:=output_word and (not 16#4); end_if; if w3 then output_word:=output_word or 16#8; else output_word:=output_word and (not 16#8); end_if; if w4 then output_word:=output_word or 16#10; else outpu

24、t_word:=output_word and (not 16#10); end_if; if w5 then output_word:=output_word or 16#20; elseoutput_word:=output_word and (not 16#20); end_if; if w6 then output_word:=output_word or 16#40; elseoutput_word:=output_word and (not 16#40); end_if; if w7 then output_word:=output_word or 16#80; elseoutpu

25、t_word:=output_word and (not 16#80); end_if; if w8 then output_word:=output_word or 16#100; else.output_word:=output_word and (not 16#100); end_if; if w9 then output_word:=output_word or 16#200; else output_word:=output_word and (not 16#200); end_if; if w10 then output_word:=output_word or 16#400; else output_word:=output_word and

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论