西門子PLC中使用SCL語言編程的技巧_第1頁
西門子PLC中使用SCL語言編程的技巧_第2頁
西門子PLC中使用SCL語言編程的技巧_第3頁
西門子PLC中使用SCL語言編程的技巧_第4頁
西門子PLC中使用SCL語言編程的技巧_第5頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

1、.在西門子 plc中使用 scl語言編程的技巧前言:兩年半前我就在工控網(wǎng)上發(fā)表了有關 scl編程的知識 , 但發(fā)表完后,即使我自己都從沒有把任何使用 scl編寫的程序用到實際控制中,當時的感覺是使用 scl編程到處受限,沒有stl語言靈活和強大。直到最近使用施耐德的 unity 軟件編程,并使用這種已經(jīng)國際標準化的文本語言(等同于西門子的 scl語言),才體會到它的優(yōu)點:、程序容易閱讀,便于診斷和維護;、程序容易在不同廠家之間的plc之間轉(zhuǎn)換。西門子的stl語言是強大,但難于閱讀,編寫程序也需要異常小心,其最強大的可能是它的寄存器尋址(類似于一些計算機高級語言中的地址指針),scl沒有這個功能

2、,那就多費一些程序代碼來實現(xiàn)同樣的功能,程序是否優(yōu)秀更應該看重程序的架構和提高程序生產(chǎn)效率的標準化,好的plc程序不應該只有自己明白,而是讓更多的人明白。 在西門子 plc中使用 scl語言的場合一般是編寫標準功能塊fb,其編程方式和西門子的其他編程語言,如梯形圖 lad、語句表 stl是完全不同的,同時為了實現(xiàn)程序的國際標準化,即為了方便的將程序代碼移植到不同廠家的 plc系統(tǒng)上,盡量不要在scl中使用西門子獨有的功能塊。.1、 在 fb 中使用 構 寫 fb 的準 ,就是其使用的內(nèi)部 量盡量與外部隔離,除了像plc的新啟 / 重啟 志,以及一些方波 / 脈沖波等全局 量可以在fb 中使用外

3、, 其他的任何全局 量都不 在fb內(nèi)部使用,即使是自定 構也 在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函數(shù)實現(xiàn): if fnadd &(button=false) thenpus1:=value1; else pus1:= value2; end_if;其等效于 pus1:= sel (g:= fnadd &(button=false), in0:=value2,in1:= value1); 使用該函數(shù)時注意兩點: 、參數(shù)名不能省略; 、當選擇條件 g為 true時,選擇后一個參數(shù)值 in1,為 false時,選擇前一個參數(shù)值 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、 脈沖沿檢測功能使用以下兩條語句替代脈沖上升沿檢測函數(shù),譬如檢測 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、 編寫脈沖發(fā)生器波峰持續(xù)時間僅為一個plc掃描周期的波形稱為脈沖波, 而波峰持續(xù)時間大于或等于兩個plc掃描周期的波形稱為方波,脈沖波可用于計數(shù)、定時,方波可用于控制信號燈的閃爍輸出,可以在西門子 plc的硬件配置中配置一個字節(jié)的各種時間的方波(波峰時間和波谷時間為 1:1 ),假設 fp_1sec 為這個字節(jié)中 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,同時當系統(tǒng)新啟動時,也需將其設為零。間隔更長時間的脈沖波編程都可以按照上面的方式編程。.6、盡量使用編程計數(shù)功能來替代定時器功能,這樣使程序更可靠和易于閱讀假設input_condition為輸入, output_delay 為通過定時處理后的輸出, timer_setpoint為時間設定點, timer_actual為當前時間計數(shù)的實際值,“impls_1sec ” 為系統(tǒng)編程產(chǎn)生的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;通過以上的編程方式可以實現(xiàn)任何定時器功能,而代碼卻可以為不同的plc系統(tǒng)所使用。7、使用編程計數(shù)功能來替代計數(shù)器在scl語言中

15、使用計數(shù)功能是最為簡單的,其關鍵是必須首先對輸入進行脈沖檢測假設input_imp為輸入脈沖, countimp 為輸入脈沖檢測 ,counter為計數(shù)值, factor 為計數(shù)因子(更詳細點就是每來一次脈沖,計數(shù)值增加多少)。(*- create impulse (impulse evaluation) -*) countimp:=.input_imp and (not countimp_old); countimp_old:= input_imp; (*- counter-*) if countimp then counter:=counter+factor; end_if;一個完整的 數(shù)

16、程序 有 數(shù)器復位功能以及 數(shù) 上限 條件(以防止 數(shù) 溢出)。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 入?yún)?shù), word 型, w0,w1,w15 位 量。.、通過西門子的 m變量實現(xiàn): 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、編程實現(xiàn) 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、面的 程。字取位的 合,一般用于 數(shù)據(jù)(譬如 器的狀 數(shù)據(jù)), 可能是字/ 整數(shù),此 就需要用到上面的 程。10、將位 合成字 相當于“字中取位”的反向操作, 也有兩種方法 , 一種方法是使用 m 量, 似于“字中取位”的方式 ,另一種也是 準 程,假 output_word 出參數(shù),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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論