Você está na página 1de 41

KY THUAT LAP TRNH Chng trnh ngi dung thng c chia nho thanh tng khoi logic theo

kieu chng trnh cau truc, giup cho viec lap trnh va sa loi thuan tien. Co nhieu loai khoi logic: Khoi to chc OB (Organization blocks) Khoi ham he thong SFB (System function blocks) va ham he thong SFC (system functions) tch hp trong PLC Khoi ham FB (Function blocks) trong th vien hay ngi dung t viet Ham FC (Functions) trong th vien hay ngi dung t viet Khoi d lieu Instance (Instance Data Blocks ) lien ket vi FB/SFB Khoi d lieu chia xe (Shared Data Blocks ) Khoi to chc OB la giao dien gia chng trnh ngi dung va he ieu hamh cua PLC. OB c goi bi he ieu hanh theo chu ky hay khi co ngat, co s co hay khi khi ong PLC. Co nhieu khoi OB va co u tien khac nhau, khoi OB co so u tien cao hn co the ngat khoi OB so u tien thap hn. Tuy theo loai CPU, so lng khoi OB s dung c se khac nhau, bang sau liet ke cac loai OB Loai OB Chng trnh chnh OB1 Ngat thi gian OB10, OB11, OB12, OB13 OB14, OB15, OB16, OB17 Ngat tr hoan OB20 OB21 OB22 OB23 Ngat chu ky OB30 OB31 OB32 OB33 OB34 OB35 OB36 OB37 OB38 Y ngha u tien c goi khi ket thuc khi ong hay ket 1 thuc OB1, thc hien theo chu ky Ngat theo thi gian trong ngay, thang, nam 2

3 4 5 6 Ngat chu ky (mac nh 5s) Ngat chu ky (mac nh 2s) Ngat chu ky (mac nh 1s) Ngat chu ky (mac nh 500ms) Ngat chu ky (mac nh 200ms) Ngat chu ky (mac nh 100ms) Ngat chu ky (mac nh 50ms) Ngat chu ky (mac nh 20ms) Ngat chu ky (mac nh 10ms) 7 8 9 10 11 12 13 14 15

Ngat cng OB40 OB41 OB42 OB43 OB44 OB45 OB46 OB47 OB60 OB70 OB72 OB 73 S co bat ong bo OB80 OB81 OB82 OB83 OB84 OB85 OB86 OB87 OB90

Goi bi SFC35 "MP_ALM" Loi I/O redundancy ( H CPU) Loi CPU redundancy (H CPU) Loi Communication redundancy (H CPU) S co chu ky quet Loi nguon Ngat chan oan Ngat do them bt module Loi phan cng CPU Loi chng trnh Loi module m rong Loi truyen thong Warm or cold restart or delete a block being executed in OB90 or load an OB90 on the CPU or terminate OB90 Khi ong am Khi ong nong Khi ong lanh

16 17 18 19 20 21 22 23 25 25 28 25 26, 28

29, 0

OB100 OB101 OB102 S co ong bo OB121 OB122

27

Sai lap trnh Sai I/O

u tien cua tac nhan gay ra s co

1/ Khoi OB1 OB1 c goi sau khi ket thuc qua trnh khi ong va sau khi ket thuc chnh no, moi OB tr OB90 co the ngat OB1. Khi OB1 a c thc hien, he ieu hanh gi i d lieu toan cuc. Trc khi goi lai OB1, he ieu hanh chuyen bo nh em ra module xuat , cap nhat bo em nhap va nhan d lieu toan cuc. Khi thc hien OB1, chng trnh trong khoi c thc hien, d lieu xuat ra module xuat c cap tam trong bo nh. Chng trnh trong OB1 co the goi cac ham hay khoi ham.

Thi gian thc hien OB1 goi la thi gian quet, he ieu hanh an nh thi gian quet toi a (150ms) va toi thieu, co the cai at bang Step 7. Neu chu ky quet keo dai th goi OB80 hay chuyen sang STOP, neu chu ky quet ngan qua th them tr hoan hay goi OB90. OB1 gom phan ma chng trnh, do ngi dung viet; bang bien cuc bo (local block) con goi la bang khai bao bien (variable declaration table) gom 20 byte

Cot th nhat la a ch trong vung bien cuc bo, cot th hai khai bao loai bien, temp ngha la tam thi, gia tr cua bien thay oi sau moi vong quet cua OB, cot th ba la cac ten cua d lieu, co y ngha nh sau (giai thch trong cot chu thch 6):
OB1_EV_CLASS: OB1_SCAN_1: gia tr B#16#11 co ngha OB1 tch cc B#16#01: hoan tat warm restart B#16#02: hoan tat hot restart B#16#03: hoan tat chu ky B#16#04: hoan tat cold restart gia tr 1 so OB la 1 d tr d tr thi gian vong quet trc (ms) thi gian vong quet ngan nhat thi gian vong quet dai nhat ngay gi OB1 bat au thc hien (8 byte)

OB1_PRIORITY: OB1_OB_NUMBR: OB1_RESERVED_1: OB1_RESERVED_2: OB1_PREV_CYCLE: OB1_MIN_CYCLE: OB1_MAX_CYCLE: OB1_DATE_TIME:

Cac gia tr tren ngi dung khong thay oi c, ngi dung co the them cac bien vao t a ch 20.0 tr i, cac bien nay la bien tam, thay oi sau moi vong quet. Cac bien them vao nay s dung cho lap trnh va goi cac chng trnh con FC, SFC, FB, SFB. Chng trnh STEP 7 dung e lap trnh cho PLC S7-300, S7-400. Chng trnh nay co version 5.0 dung cho Win 98, Version 5.1 va 5.3 dung cho Win XP. Khi kch chuot vao bieu tng Simatic Manager se xuat hien ca so Hnh , bam Next e chon loai CPU

Bam tiep Next e chon cac khoi OB, bat buoc la OB1, cac OB khac co the them vao sau.

Chon cach lap trnh STL, LAD hay FBD, trong luc lap trnh co the tuy y thay o i. Bam tiep Next at ten cho Project, sau o bam Finish, xuat hien ca so lap trnh

Na ca so ben trai sap xep dang th muc, kch chuot vao o e m ra cac muc con. Bam vao dong SIMATIC 300 STATION ben trai roi bam tiep vao Hardware ben phai e at cau hnh phan cng cua PLC (cong viec nay cung co the thc hien sau)

Ga s cau hnh n gian gom cac module DI/DO, AI/AO, ta kch chuot vao dong SIMATIC 300, SM- 300 , chon cac module phu hp, dung chuot keo vao cac slot cua Station t so 4 tr i, (slot 3 dung cho module IM), sau o vao menu Station Save roi Close. Ta se tr lai van e cau hnh muc

Tr lai Project, bam vao muc Blocks, ta thay xuat hien OB1, bam vao OB1 neu lap trnh tuyen tnh, ngha la khong dung cac khoi logic FC, FB t tao

Bam vao menu View, chon STL, LAD, FBD chon cach lap trnh. Khi lap trnh ta co the dung a ch tuyet oi ( I0.0, MW2, T5) hay a ch ky hieu (Start, Speed, Delay). a ch ky hieu giup chng trnh de hieu hn. Co hai loai la ky hieu cuc bo (bien cuc bo) va ky hieu toan cuc (hay chia xe) , ky hieu cuc bo khai bao trong bang khai bao bien cua khoi va ch co y ngha trong pham vi khoi o, ky hieu toan cuc khai bao trong bang ky hieu Symbols, co y ngha trong toan bo cac khoi cua project. Viec khai bao ky hieu toan cuc thc hien trc hay sau khi viet ma. Khoi logc co the co toi a 999 network, moi network co toi a 2000 hang , moi hang gom nhan , lenh, a ch va chu thch (sau //) Thu tuc lap ky hieu toan cuc nh sau: bam chuot vao oi tng Symbols (Xem hnh ).

Cac bien ky hieu c a vao tng dong mot, dai toi a 24 ky t ch so, ky t ac biet, tr dau nhay , khong phan biet ch hoa va ch thng. Bang ky hieu cha toi a 16380 ky hieu. Sau khi a bien tap xong, vao menu Symbol Table- Save e lu bang. Vao ca so bien tap cua khoi chon View- Display with - Symbolic Representation e nhn thay a ch ky hieu trong chng trnh, ky hieu toan cuc c ong khung bang dau , con ky hieu cuc bo co dau # ng trc. Neu dung View- Display with- Symbol Information th

hien th ca ky hieu, a ch tuyet oi , va chu thch i kem. Cac loai bien cho phep trnh bay trong bang , tam a ch cu the phu thuoc loai CPU
IEC I IB IW ID Q QB QW QD M MB MW MD PIB PQB PIW Description Input bit Input byte Input word Input double word Output bit Output byte Output word Output double word Memory bit Memory byte Memory word Memory double word Peripheral input byte Peripheral output byte Peripheral input word Peripheral output word PQW PID PQD T C OB FB DB FC SFB SFC VAT UDT Peripheral input double word Peripheral output double word Timer Counter Organization block Function block Data block Function System function block System function Variable table User defined data type Data Type BOOL BYTE, CHAR WORD, INT, S5TIME, DATE DWORD, DINT, REAL, TOD, TIME BOOL BYTE, CHAR WORD, INT, S5TIME, DATE DWORD, DINT, REAL, TOD, TIME BOOL BYTE, CHAR WORD, INT, S5TIME, DATE DWORD, DINT, REAL, TOD, TIME BYTE, CHAR BYTE, CHAR WORD, INT, S5TIME, DATE WORD, INT, S5TIME, DATE DWORD, DINT, REAL, TOD, TIME DWORD, DINT, REAL, TOD, TIME TIMER COUNTER OB FB DB, FB, SFB, UDT FC SFB SFC UDT Address Range 0.0 .. 65535.7 0 .. 65535 0 .. 65534 0 .. 65532 0.0 .. 65535.7 0 .. 65535 0 .. 65534 0 .. 65532 0.0 .. 65535.7 0 .. 65535 0 .. 65534 0 .. 65532 0 .. 65535 0 .. 65535 0 .. 65534 0 .. 65534 0 .. 65532 0..65532 0 .. 65535 0 .. 65535 1 .. 65535 0 .. 65535 1 .. 65535 0 .. 65535 0 .. 65535 0 .. 65535 0 .. 65535 0 .. 65535 0..511 0..511 0..1023 0..1023

0..1024

Chng trnh OB co the s dung cac bien tam di hnh thc ky hieu (co dau # trc) hay a ch L trong vung bang khai bao bien cua khoi , bat au t a ch 20.0 tr i. V du1: oan chng trnh sau s dung bien tam Enable khai bao trong bang bien cuc bo, a ch 20.0, chuyen sang dang STL, dung them bien phu L21.0 con trong ma khong can khai bao trong bang neu khong dung a ch ky hieu.

V d 2: lp trnh cho en bo hanh, bnh thng khi khong co yeu cau qua ng (I0.0, I0.1), en xanh xe (Q0.7) va en o bo hanh (Q0.0) sang. Khi co yeu cau en vang xe (Q0.6) sang trong 3s , sau o en o xe (Q0.5) sang va en xanh bo hanh (Q0.1) sang trong 10s, het thi gian nay en o bo hanh va o xe cung sang, sau 6s en vang xe va o xe cung sang va sau 3 s en xanh xe sang , xoa yeu cau qua ng
A( A( O O ) A O ) AN = AN = A L SD A AN = A A L SD A A( ON Q 0.5 S5T#10S T 3 Q 0.5 T 3 Q 0.1 //Bat en xanh bo hanh, thi gian 10s M 0.0 T 3 S5T#6S T 4 //Thi gian 6 s o xe va o bo hanh cung sang M 0.0 T 2

I I T M T M M Q

0.0 0.1 6 0.0

// Co yeu cau qua ng cua khach bo hanh

5 //xoa yeu cau 0.0 // ghi nhan yeu cau 0.0 // neu khong co yeu cau th 0.7 // en xanh xe sang

A L SD A A( ON O ) = A A =

M 0.0 S5T#3S T 2 M 0.0 T T Q M T Q 2 4 0.6 //en vang xe 3s 0.0 2 0.5 //en o xe sau 3s

O ) ON = A A L SD

T M Q

3 0.0 0.0 // Bat en o bo hanh

M 0.0 //Bat en o va vang xe T 4 S5T#3S T 5 //Chuyen sang xanh xe sau 3s

A Q 0.7 L S5T#1S SD T 6 //Thi gian tr hoan 1s e nhan yeu cau khi xanh xe va sang

Sau o lap bang ky hieu:

A( A( O "Switch_right" O "Switch_left" ) A "Ped_delay_green" O "Pedestrian_light" ) AN "Car_red_orange_phase" = "Pedestrian_light" AN "Pedestrian_light" = "Car_green" A "Pedestrian_light" L S5T#3S SD "Car_orange_phase" A "Pedestrian_light" A( ON "Car_orange_phase" O "Car_delay_red" ) = "Car_orange" A "Pedestrian_light" A( ON "Car_orange_phase" O "Car_delay_red" ) = "Car_orange"

A A = A L SD A AN = A A L SD A A( ON O ) ON = A A L SD A L SD

"Pedestrian_light" "Car_orange_phase" "Car_red" "Car_red" S5T#10S "Ped_green_phase" "Car_red" "Ped_green_phase" "Ped_green" "Pedestrian_light" "Ped_green_phase" S5T#6S "Car_delay_red" "Pedestrian_light" "Car_orange_phase" "Ped_green_phase" "Pedestrian_light" "Ped_red" "Pedestrian_light" "Car_delay_red" S5T#3S "Car_red_orange_phase" "Car_green" S5T#1S "Ped_delay_green"

Sau khi bien soan chng trnh ta co the chay mo phong khong can PLC nh phan mem S7 PLC Sim theo cac bc sau: - Vao menu Simatic Manager- Options- chon Simulate Modules. Ca so sau xuat hien

Vao menu PLC- Download e nap khoi chng trnh xuong PLC mo phong Vao ca so S7-PLCSIM menu Insert chon cac vung nh muon quan sat

Vao menu PLC- chon Power On, vao menu Execute chon Scan Mode Continuous Scan. - Chon RUN hay RUN P - Tac ong vao cac bit I 0.0, I0.1 e xem hoat ong cua chng trnh. - Tr lai Simatic Manager, chon View- Online, m khoi logic muon quan sat (OB1), bam Debug- Monitor Trong trng hp muon tap trung cac bien vao mot cho e de quan sat, ta dung bang khai bao bien VAT (Variable Table). Trong ca so Manager vao menu Insert- S7 Block- Variable Table (hay bam chuot phai Insert New Object- Variable Table) ta c khoi VAT1, m khoi nay ra va them vao cac a ch vung nh muon quan sat.

Vao menu PLC- Connect to Configured CPU. Vao menu Variable- Monitor. Vao menu Operating mode e ieu khien che o PLC.

Trng hp co san PLC, au tien ta phai ket noi may tnh vi PLC thong qua cap noi thch hp, vao menu PLC- Display Accessible Nodes, sau o PLC- Operating mode chon che o PLC la Stop, PLC- Download nap chng trnh xuong PLC. 2/ Cac khoi ngat Khoi OB1 c thc hien theo chu ky, va co the b ngat bi cac s kien khi ta cai at them cac khoi OB khac vao Project hoac khi xay ra cac s co. Cac khoi OB phu hp c goi e x ly ngat nh cac chng trnh con c cai at. Khoi OB u tien cao co the ngat khoi co u tien thap hn. Ta co the thay oi u tien cua OB trong S7-400 va S7-300CPU318. Them OB b ang cach bam chuot phai trong ca so Project- Insert New ObjectOrganization block, chon so OB, sau o m khoi OB va lap trnh

3/ Tao cac khoi logic Cac chng trnh ln thng c viet dang cau truc, gom khoi OB1, cac khoi chng trnh FC, FB, cac khoi chng trnh he thong SFC, SFB. S dung lap trnh cau truc giup chng trnh de quan ly va sa loi, thuan tien cho viec lap trnh theo nhom. Khoi OB1 va cac khoi FC, FB co the goi FC, FB, SFC, SFB bang lenh CALL

V du 3: lap trnh cho he thong tron hai chat long A va B (H ), ta chia qua trnh thanh nhieu khoi nho (H ) : bm chat A, bm chat B, bon tron va van xa. Ta nhan thay hai khoi bm lap trnh giong nhau, ch khac cac ngo vao/ra. Trc khi lap trnh ta phai co mo ta ky thuat cho hoat ong cua cac khoi. Khoi A/B gom co bm va van vao, van ra - Bm co cong suat 100KW, vong quay 1200 rpm, lu lng 400l/phut. Bm c ieu khien bi nut Start/Stop tren bang ieu khien, so lan start c hien th e tien bao tr. Bm c phep hoat ong khi:

o bon khong ay, o van xa ong, o nut emergency khong tac ong. Bm tat khi cam bien lu lng bao khong co dong chay sau 7 s ke t khi khi ong bm hay khi cam bien lu lng bao a ngng chay. - Van c ieu khien bi solenoid, m khi co ien vao van. Van phai m t nhat 1s sau khi bm chay. Khoi bon tron co ong c tron, cac cam bien mc. co cong suat 100KW, vong quay 1200 rpm, lu lng 400l/phut. ong c c ieu khien bi nut Start/Stop tren bang ieu khien, so lan start c hien th e tien bao tr. ong c c phep chay khi: o Mc chat long tren mc toi thieu , o Van xa ong o Nut Emergency khong tac ong ong c c tat khi van toc khong at nh mc sau khi khi ong 10s. Co ba cam bien mc dang contact . Cam bien ay thng ong, khi bon ay th h ra. Cam bien mc toi thieu thng h , khi mc chat long thap th ong lai. Cam bien can, h neu bon can Van xa c ieu khien t bang ieu khien. Van xa c hoat ong neu ong c tron ngng, cam bien mc bao bon cha can, nut emergency khong tac ong. Van xa ong neu cam bien mc bao bon can. Bang ieu khien dung e ieu khien va bao trang thai cac ong c, van xa, bao mc bon, bao bao tr va dng khan cap.

Co ba ong c co the lap trnh bang khoi logic chung (Hnh) . Sau ngo vao la hai nut nhan Sart/Stop, nut nhan Reset_Maint xoa en bao tr, tn hieu bao ong c chay, ngng hay bao co dong chay (Response), so hieu Timer (Timer No) va thi gian timer (Response_Time). Bon ngo ra la bao loi (Fault), en bao ong c chay, ngng (Start_Dsp, Stop_Dsp), bao bao tr (Maint), Tn hieu vao/ra la ieu khien Motor. Ngoai ra con mot bien la Starts cho biet so lan a khi ong ong c. Khoi logic nay lap trnh di dang khoi ham FB v can lu tr gia tr bien. Cac van cung c ieu khien bang khoi logic FC (Hnh) . Hai tn hieu vao la nut nhan m /ong van (Open/ Close) Tn hieu ra la en bao trang thai van ( Dsp_Open, Dsp_Closed). Tn hieu vao/ ra ieu khien van (Valve). Khoi nay khong co lu bien va thc hien bang FC. Cau truc chng trnh nh Hnh . Chng trnh chnh OB1 goi ham FB1 ieu khien ong c, co ba ong c ng vi ba khoi d lieu DB1, DB2, DB3. Ham FC1 c OB1 goi khi ieu khien van. Cac khoi FB va FC phai c lap trnh trc khoi OB. Vao ca so Project Symbols lap bang ky hieu cho cac bien (Bang )

Symbolic Name Feed_pump_A_start Feed_pump_A_stop Flow_A

Address I0.0 I0.1 I0.2

Data Type BOOL BOOL BOOL

Description Starts the feed pump for ingredient A Stops the feed pump for ingredient A Ingredient A flowing

Inlet_valve_A Feed_valve_A Feed_pump_A_on Feed_pump_A_off Feed_pump_A Feed_pump_A_fault Feed_pump_A_maint Feed_pump_B_start Feed_pump_B_stop Flow_B Inlet_valve_B Feed_valve_B Feed_pump_B_on Feed_pump_B_off Feed_pump_B Feed_pump_B_fault Feed_pump_B_maint Agitator_running Agitator_start Agitator_stop Agitator Agitator_on Agitator_off Agitator_fault Agitator_maint Tank_below_max Tank_above_min Tank_not_empty Tank_max_disp Tank_min_disp Tank_empty_disp Drain_open Drain_closed Drain Drain_open_disp Drain_closed_disp EMER_STOP_off Reset_maint Motor_block Valve_block DB_feed_pump_A DB_feed_pump_B DB_agitator

Q4.0 Q4.1 Q4.2 Q4.3 Q4.4 Q4.5 Q4.6 I0.3 I0.4 I0.5 Q5.0 Q5.1 Q5.2 Q5.3 Q5.4 Q5.5 Q5.6 I1.0 I1.1 I1.2 Q8.0 Q8.1 Q8.2 Q8.3 Q8.4 I1.3 I1.4 I1.5 Q9.0 Q9.1 Q9.2 I0.6 I0.7 Q9.5 Q9.6 Q9.7 I1.6 I1.7 FB1 FC1 DB1 DB2 DB3

BOOL BOOL BOOL BOOL BOOL BOOL BOOL BOOL BOOL BOOL BOOL BOOL BOOL BOOL BOOL BOOL BOOL BOOL BOOL BOOL BOOL BOOL BOOL BOOL BOOL BOOL BOOL BOOL BOOL BOOL BOOL BOOL BOOL BOOL BOOL BOOL BOOL BOOL FB1 FC1 FB1 FB1 FB1

Activates the inlet valve for ingredient A Activates the feed valve for ingredient A Lamp for feed pump ingredient A running" Lamp for feed pump ingredient A not running" Activates the feed pump for ingredient A Lamp for feed pump A fault" Lamp for feed pump A maintenance" Starts the feed pump for ingredient B Stops the feed pump for ingredient B Ingredient B flowing Activates the inlet valve for ingredient A Activates the feed valve for ingredient B Lamp for feed pump ingredient B running" Lamp for feed pump ingredient B not running" Activates the feed pump for ingredient B Lamp for feed pump B fault" Lamp for feed pump B maintenance" Response signal of the agitator motor Agitator start button Agitator stop button Activates the agitator Lamp for "agitator running" Lamp for "agitator not running" Lamp for agitator motor fault" Lamp for agitator motor maintenance" Sensor mixing tank not full" Sensor mixing tank above minimum level" Sensor mixing tank not empty" Lamp for "mixing tank full" Lamp for "mixing tank below minimum level" Lamp for "mixing tank empty" Button for opening the drain valve Button for closing the drain valve Activates the drain valve Lamp for "drain valve open" Lamp for "drain valve closed" EMERGENCY STOP switch Reset switch for the maintenance lamps on all motors FB for controlling pumps and motor FC for controlling the valves Instance DB for controlling feed pump A Instance DB for controlling feed pump B Instance DB for controlling the agitator motor

3.1 Lap trnh khoi FB

FB la khoi logc vi cac bien in, out, in_out, static va temp, c tao ra trong bang bien a phng i kem. Cac bien in, out, in_ out la cac tham so hmh thc co a ch cu the do chng trnh goi truyen en, bien static la bien trong chng trnh FB c lu lai khi ra khoi khoi FB, bien temp mat gia tr khi ra khoi khoi FB. Kem vi FB la khoi d lieu data block cha cac bien in, out, in_ out va static. Co the co nhieu data block cho mot FB khi mot FB dung cho cac nhiem vu khac nhau, goi la instance data block. Khi chng trnh goi FB can phai kem theo instance data block tng ng. Ta vao ca so Project bam chuot phai - Insert New Object Function block them vao khoi FB1. Bam chuot vao khoi FB1 e soan chng trnh cho khoi. Ta vao bang khai bao bien e khai bao cac bien hnh thc cho khoi theo th t in, out, in_out, static va temp. Vi v du tren, bang bien cua FB1 Motor_Block nh sau:
Address 0.0 0.1 0.2 0.3 2.0 4.0 6.0 6.1 6.2 6.3 8.0 10.0 12.0 14.0 16.0 Declaration Name IN Start IN Stop IN Response IN Reset_Maint IN Timer_No IN Response_Time OUT Fault OUT Start_Dsp OUT Stop_Dsp OUT Maint IN_OUT Motor STAT Time_bin STAT Time_BCD STAT Starts STAT Start_Edge Type Initial Value BOOL FALSE BOOL FALSE BOOL FALSE BOOL FALSE TIMER S5TIME S5T#0MS BOOL FALSE BOOL FALSE BOOL FALSE BOOL FALSE BOOL FALSE WORD W#16#0 WORD W#16#0 INT 0 BOOL FALSE

Cac bien STAT Time_ bin va Time_BCD lu thi gian timer, Starts lu so lan khi ong motor, neu qua 5000 th bao bao tr , Start_ Edge phuc vu cho lenh lay canh len
Network 1 Start/stop and latching A( O #Start O #Motor ) AN #Stop = #Motor Network 2 Startup monitoring A #Motor L #Response_Time SD #Timer_No AN #Motor R #Timer_No L #Timer_No T #Timer_bin LC #Timer_No T #Timer_BCD A #Timer_No AN #Response S #Fault R #Motor Network 4 Stop lamp AN #Response = #Stop_Dsp Network 5 Counting the starts A #Motor FP #Start_Edge JCN lab1 L #Starts +1 T #Starts lab1: NOP 0 Network 6 Maintenance lamp L #Starts L 5000 >=I = #Maint Network 7 Reset counter for number of starts A #Reset_Maint A #Maint JCN END L0

Network 3 Start lamp and fault reset A #Response = #Start_Dsp R #Fault

T #Starts END: NOP 0

Them khoi DB project vi cac ten DB1, DB2, DB3 loai Instance DB va thuoc FB1

Cac bien trong DB1 se t tao ra theo bang khai bao bien cua khoi FB1, gom cac bien loai in, out,in_out va stat, tng t cho cac DB2 va DB3. Khi khoi FB c goi se phai m kem theo DB tng ng

3.2 Lap trnh khoi FC Khoi FC co cac bien hnh thc in, out va in_ out do chng trnh goi cung cap cac a ch cu the, ngoai ra con co bien temp s dung noi bo, tuy nhien khong bat buoc phai dung tat ca cac loai bien nay. Khoi FC khong co bo nh nen d lieu mat i khi ra khoi khoi. Ta them vao project khoi FC1 va khai bao cac bien trong bang khai bao bien kem theo. Sau o lap trnh cho FC1
Address Declaration Name 0.0 IN Open 0.1 IN Close 2.0 OUT Dsp_Open Type Initial Value BOOL FALSE BOOL FALSE BOOL FALSE

2.1 4.0 Network 1 Open/close and latching A( O #Open O #Valve ) AN #Close = #Valve Network 2 Display "valve open" A #Valve = #Dsp_Open Network 3 Display "valve closed" AN#Valve = #Dsp_Closed

OUT IN_OUT

Dsp_Closed BOOL Valve BOOL

FALSE FALSE

Bc tiep theo la lap trnh cho OB1, ta khai bao cac bien cho OB1 t a ch 20 tr i
Address 0.0 1.0 2.0 3.0 4.0 5.0 6.0 8.0 10.0 12.0 20.0 20.1 20.2 20.3 20.4 20.5 Declaration TEMP TEMP TEMP TEMP TEMP TEMP TEMP TEMP TEMP TEMP TEMP TEMP TEMP TEMP TEMP TEMP Name OB1_EV_CLASS OB1_SCAN1 OB1_PRIORITY OB1_OB_NUMBR OB1_RESERVED_1 OB1_RESERVED_2 OB1_PREV_CYCLE OB1_MIN_CYCLE OB1_MAX_CYCLE OB1_DATE_TIME Enable_motor Enable_valve Start_fulfilled Stop_fulfilled Inlet_valve_A_open Inlet_valve_A_closed Type BYTE BYTE BYTE BYTE BYTE BYTE INT INT INT DATE_AND_TIME BOOL BOOL BOOL BOOL BOOL BOOL

Chng trnh OB1


Network 1 Interlocks for feed pump A A "EMER_STOP_off" A "Tank_below_max" AN "Drain" = #Enable_Motor Network 2 Calling FB Motor for ingredient A A "Feed_pump_A_start" A #Enable_Motor = #Start_Fulfilled A( O "Feed_pump_A_stop" ON #Enable_Motor ) = #Stop_Fulfilled CALL "Motor_block", "DB_feed_pump_A" Start :=#Start_Fulfilled Stop :=#Stop_Fullfilled Network 8 Delaying the valve enable ingredient B A "Feed_pump_B" L S5T#1S SD T 15 AN "Feed_pump_B" R T 15 A T 15 = #Enable_Valve Network 9 Inlet valve control for ingredient B AN "Flow_B" AN "Feed_pump_B" = #Close_Valve_Fulfilled CALL "Valve_block" Open :=#Enable_Valve Close :=#Close_Valve_Fulfilled Dsp_Open :=#Inlet_Valve_B_Open Dsp_Closed:=#Inlet_Valve_B_Closed

Response :="Flow_A" Reset_Maint :="Reset_maint" Timer_No :=T12 Reponse_Time:=S5T#7S Fault :="Feed_pump_A_fault" Start_Dsp :="Feed_pump_A_on" Stop_Dsp :="Feed_pump_A_off" Maint :="Feed_pump_A_maint" Motor :="Feed_pump_A" Network 3 Delaying the valve enable ingredient A A "Feed_pump_A" L S5T#1S SD T 13 AN "Feed_pump_A" R T 13 A T 13 = #Enable_Valve Network 4 Inlet valve control for ingredient A AN "Flow_A" AN "Feed_pump_A" = #Close_Valve_Fulfilled CALL "Valve_block" Open :=#Enable_Valve Close :=#Close_Valve_Fulfilled Dsp_Open :=#Inlet_Valve_A_Open Dsp_Closed:=#Inlet_Valve_A_Closed Valve :="Inlet_Valve_A" Network 5 Feed valve control for ingredient A AN"Flow_A" AN"Feed_pump_A" =#Close_Valve_Fulfilled CALL"Valve_block" Open:=#Enable_Valve Close:=#Close_Valve_Fulfilled Dsp_Open:=#Feed_Valve_A_Open Dsp_Closed:=#Feed_Valve_A_Closed Valve :="Feed_Valve_A" Network 6 Interlocks for feed pump B A "EMER_STOP_off" A "Tank_below_max" AN "Drain" = "Enable_Motor Network 7 Calling FB Motor for ingredient B A "Feed_pump_B_start" A #Enable_Motor = #Start_Fulfilled A( O "Feed_pump_B_stop" ON #Enable_Motor ) = #Stop_Fulfilled CALL "Motor_block", "DB_feed_pump_B" Start :=#Start_Fulfilled Stop :=#Stop_Fullfilled Response :="Flow_B" Reset_Maint :="Reset_maint" Timer_No :=T14 Reponse_Time:=S5T#7S Fault :="Feed_pump_B_fault" Start_Dsp :="Feed_pump_B_on"

Valve :="Inlet_Valve_B" Network 10 Feed valve control for ingredient B AN "Flow_B" AN "Feed_pump_B" = #Close_Valve_Fulfilled CALL "Valve_block" Open :=#Enable_Valve Close :=#Close_Valve_Fulfilled Dsp_Open :=#Feed_Valve_B_Open Dsp_Closed:=#Feed_Valve_B_Closed Valve :="Feed_Valve_B" Network 11 Interlocks for agitator A "EMER_STOP_off" A "Tank_above_min" AN "Drain" = #Enable_Motor Network 12 Calling FB Motor for agitator A "Agitator_start" A #Enable_Motor = #Start_Fulfilled A( O "Agitator_stop" ON #Enable_Motor ) = #Stop_Fulfilled CALL "Motor_block", "DB_Agitator" Start :=#Start_Fulfilled Stop :=#Stop_Fullfilled Response :="Agitator_running" Reset_Maint :="Reset_maint" Timer_No :=T16 Reponse_Time:=S5T#10S Fault :="Agitator_fault" Start_Dsp :="Agitator_on" Stop_Dsp :="Agitator_off" Maint :="Agitator_maint" Motor :="Agitator" Network 13 Interlocks for drain valve A"EMER_STOP_off" A"Tank_not_empty" AN"Agitator" = "Enable_Valve Network 14 Drain valve control A "Drain_open" A #Enable_Valve = #Open_Drain A( O "Drain_closed" ON #Enable_Valve ) = #Close_Drain CALL "Valve_block" Open :=#Open_Drain Close :=#Close_Drain Dsp_Open :="Drain_open_disp" Dsp_Closed :="Drain_closed_disp" Valve :="Drain" Network 15 Tank level display AN"Tank_below_max"

Stop_Dsp :="Feed_pump_B_off" Maint :="Feed_pump_B_maint" Motor :="Feed_pump_B"

="Tank_max_disp" AN"Tank_above_min" ="Tank_min_disp" AN"Tank_not_empty" = "Tank_empty_disp"

Bang sau cho biet so lng khoi /kch thc tong cong cac khoi logic cua cac loai cpu CPU 312 313, 314 315 316 318 412 414, 416 417 OB1 6KByte 8 KByte 16 KByte 16 KByte 64 KByte 64 KByte 64 KByte 64 KByte DB 63/6 Kbyte 127/8 Kbyte 255/16Kbyte 511/16 Kbyte 2047/64 Kbyte 512/64 Kbyte 4095/64 Kbyte 8192/64 Kbyte FB 32/6 KByte 128/8KByte 192/16KByte 256/16 KByte 1024/64 KByte 256/64Kbyte 2048/64 KByte 6144/64 KByte FC 32/6 KByte 128/8KByte 192/16KByte 256/16 KByte 1024/64 KByte 256/64Kbyte 2048/64 Kbyte 6144/64 KByte

4. S dng hm th vin Cac ham th vien do Siemens viet san thuoc cac loai FC, FB, SFC, SFB giup ngi dung thuan tien trong lap trnh. Muon dung cac ham th vien trong khoi logic nao th ta m khoi logic o ra, kch chuot vao cho goi ham th vien, vao menu Insert- Program Elements - Libraries chon cac ham th vien phu hp roi bam chuot kep vao o, hoac go CALL ten ham th vien neu ta nh ten ham. Lenh goi ham se c them vao kem theo cac bien in, out va in_out can trao oi

Cac ham th vien can chuyen gia tr cho cac bien hnh thc va ta phai cung cap cho ham goi theo qui nh cua ham th vien. Neu trong Project a co cac ham FB, FC trung ten vi ham th vien th can phai oi ten

Th vien chuan cua Step 7 gom co cac th vien con: System Function Blocks: khoi ham he thong (SFB) va ham he thong (SFC) S5-S7 Converting Blocks: ham chuyen oi chng trnh cua PLC S5 sang S7 TI-S7 Converting Blocks: cac ham a dung IEC Function Blocks: cac ham IEC Organization Blocks: khoi to chc (OB) PID Control Blocks : ham ieu khien PID Communication Blocks: cac ham mang Ngoai ra con co the cai them cac ham th vien khac nh fuzzy control, neuro system, motion control. Ngi dung co the t viet mot th vien gom cac ham thng dung e tien cho cong viec cua mnh. Cac bc tao th vien nh sau. - Simatic Manager- File New- Libraries at ten cho th vien- OK

Muon tao cac ham trong th vien ta bam File- Open- Libraries chon ten th vien OK

Vao ca so cua th vien Thanh- bam chuot phai- Insert New Objects- S7 Program

oi ten cho th vien con nay, v du Motion Control, sau o m th vien con nay va copy vao o cac ham t th vien khac hay t viet cac ham mi

Muon bo mot th vien vao Simatic Manager- File - Delete - Libraries chon ten th vien - OK

5 Bang cac ham SFB SFC va FC Bang sau liet ke cac ham th vien. Mot so ham se c khao sat cac chng sau
No. Short Name SFC 0 SET_CLK SFC 1 READ_CLK SFC 2 SET_RTM SFC 3 CTRL_RTM SFC 4 READ_RTM SFC 5 GADR_LGC SFC 6 RD_SINFO SFC 7 DP_PRAL SFC 9 EN_MSG SFC 10 DIS_MSG SFC 11 DPSYC_FR SFC 12 D_ACT_DP SFC 13 DPNRM_DG SFC 14 DPRD_DAT SFC 15 DPWR_DAT SFC 17 ALARM_SQ SFC 18 ALARM_S SFC 19 ALARM_SC Message SFC 20 BLKMOV SFC 21 FILL SFC 22 CREAT_DB SFC 23 DEL_DB SFC 24 TEST_DB SFC 25 COMPRESS SFC 26 UPDAT_PI SFC 27 UPDAT_PO SFC 28 SET_TINT SFC 29 CAN_TINT SFC 30 ACT_TINT SFC 31 QRY_TINT SFC 32 SRT_DINT SFC 33 CAN_DINT SFC 34 QRY_DINT SFC 35 MP_ALM SFC 36 MSK_FLT SFC 37 DMSK_FLT SFC 38 READ_ERR SFC 39 DIS_IRT SFC 40 EN_IRT SFC 41 DIS_AIRT SFC 42 EN_AIRT SFC 43 RE_TRIGR SFC 44 REPL_VAL SFC 46 STP SFC 47 WAIT SFC 48 SNC_RTCB SFC 49 LGC_GADR Function Set System Clock Read System Clock Set Run-Time Meter Start/Stop Run-Time Meter Read Run-Time Meter Query Logical Address of a Channel Read OB Start Information Trigger a Hardware Interrupt on the DP Master Enable Block-Related, Symbol-Related and Group Status Messages Disable Block-Related, Symbol-Related and Group Status Messages Synchronize Groups of DP Slaves Deactivation and activation of DP slaves Read Diagnostic Data of a DP Slave (Slave Diagnostics) Read Consistent Data of a Standard DP Slave Write Consistent Data to a DP Standard Slave Generate Acknowledgeable Block-Related Messages Generate Permanently Acknowledged Block-Related Messages Query the Acknowledgment Status of the last ALARM_SQ Entering State Copy Variables Initialize a Memory Area Create Data Block Delete Data Block Test Data Block Compress the User Memory Update the Process Image Update Table Update the Process Image Output Table Set Time-of-Day Interrupt Cancel Time-of-Day Interrupt Activate Time-of-Day Interrupt Query Time-of-Day Interrupt Start Time-Delay Interrupt Cancel Time-Delay Interrupt Query Time-Delay Interrupt Trigger Multicomputing Interrupt Mask Synchronous Errors Unmask Synchronous Errors Read Error Register Disable New Interrupts and Asynchronous Errors Enable New Interrupts and Asynchronous Errors Delay Higher Priority Interrupts and Asynchronous Errors Enable Higher Priority Interrupts and Asynchronous Errors Retrigger Cycle Time Monitoring Transfer Substitute Value to Accumulator 1 Change the CPU to STOP Delay Execution of the User Program Synchronize Slave Clocks Query the Module Slot Belonging to a Logical Address

SFC 50 SFC 51 SFC 52 SFC 54 SFC 55 SFC 56 SFC 57 SFC 58 SFC 59 SFC 60 SFC 61 SFC 62 SFC 63 SFC 64 SFC 65 SFC 66 SFC 67 SFC 68 SFC 69 Station SFC 72 SFC 73 SFC 74 Station SFC 79 SFC 80 SFC 81 SFC 90 No. SFB 0 SFB 1 SFB 2 SFB 3 SFB 4 SFB 5 SFB 8 SFB 9 SFB 12 SFB 13 SFB 14 SFB 15 SFB 16 SFB 19 SFB 20 SFB 21 SFB 22 SFB 23 SFB 29 SFB 30 SFB 32 SFB 33 SFB 34

RD_LGADR RDSYSST WR_USMSG RD_PARM WR_PARM WR_DPARM PARM_MOD WR_REC RD_REC GD_SND GD_RCV CONTROL AB_CALL TIME_TCK X_SEND X_RCV X_GET X_PUT X_ABORT I_GET I_PUT I_ABORT SET RSET UBLKMOV H_CTRL Short Name CTU CTD CTUD TP TON TOF USEND URCV BSEND BRCV GET PUT PRINT START STOP RESUME STATUS USTATUS HS_COUNT FREQ_MES DRUM ALARM ALARM_8

Query all Logical Addresses of a Module Read a System Status List or Partial List Write a User-Defined Diagnostic Event to the Diagnostic Buffer Read Defined Parameters Write Dynamic Parameters Write Default Parameters Assign Parameters to a Module Write a Data Record Read a Data Record Send a GD Packet Fetch a Received GD Packet Query the Status of a Connection Belonging to a Communication SFB Instance Assembly Code Block Read the System Time Send Data to a Communication Partner outside the Local S7 Station Receive Data from a Communication Partner outside the Local S7 Station Read Data from a Communication Partner outside the Local S7 Station Write Data to a Communication Partner outside the Local S7 Station Abort an Existing Connection to a Communication Partner outside the Local S7 Read Data from a Communication Partner within the Local S7 Station Write Data to a Communication Partner within the Local S7 Station Abort an Existing Connection to a Communication Partner within the Local S7 Set a Range of Outputs Reset a Range of Outputs Uninterruptable Block Move Control Operation in H Systems Function Count Up Count Down Count Up/Down Generate a Pulse Generate an On Delay Generate an Off Delay Uncoordinated Sending of Data Uncoordinated Receiving of Data Sending Segmented Data Receiving Segmented Data Read Data from a Remote CPU Write Data to a Remote CPU Send Data to Printer Initiate a Warm or Cold Restart on a Remote Device Changing a Remote Device to the STOP State Initiate a Hot Restart on a Remote Device Query the Status of a Remote Partner Receive the Status of a Remote Device Counter (high-speed counter, integrated function) Frequency Meter (frequency meter, integrated function Implement a Sequencer Generate Block-Related Messages with Acknowledgment Display Generate Block-Related Messages without Values for 8 Signals

SFB 35 ALARM_8P Generate Block-Related Messages with Values for 8 Signals SFB 36 NOTIFY Generate Block-Related Messages without Acknowledgment Display SFB 37 AR_SEND Send Archive Data SFB 38 HSC_A_B Counter A/B (integrated function) SFB 39 POS Position (integrated function) SFB 41 CONT_C 1) Continuous Control SFB 42 CONT_S 1) Step Control SFB 43 PULSEGEN 1) Pulse Generation * SFB29 "HS_COUNT" and SFB30 "FREQ_MES" only exist on the CPU 312 IFM and CPU 314 IFM. SFBs 38 "HSC_A_B" and 39 "POS" only exist on the CPU 314 IFM. For a detailed description, refer to. * SFBs 41 "CONT_C," 42 "CONT_S" and 43 "PULSEGEN" only exist on the CPU 314 IFM. No. FC1 FC2 FC3 FC4 FC5 FC6 FC7 FC8 FC9 FC10 FC11 FC12 FC13 FC14 FC15 FC16 FC17 FC18 FC19 FC20 FC21 FC22 FC23 FC24 FC25 FC26 FC27 FC28 FC29 FC30 FC31 FC32 FC33 FC34 FC35 FC36 FC37 FC38 FC39 FC40 Short Name AD_DT_TM CONCAT D_TOD_DT DELETE DI_STRNG DT_DATE DT_DAY DT_TOD EQ_DT EQ_STRNG FIND GE_DT GE_STRNG GT_DT GT_STRNG I_STRNG INSERT LE_DT LE_STRNG LEFT LEN LIMIT LT_DT LT_STRNG MAX MID MIN NE_DT NE_STRNG R_STRNG REPLACE RIGHT S5TI_TIM SB_DT_DT SB_DT_TM SEL STRNG_DI STRNG_I STRNG_R TIM_S5TI Function Add duration to a time Combine two STRING variables Combine DATE and TIME_OF_DAY to DT Delete in a STRING variable Data type conversion DINT to STRING Extract the DATE from DT Extract the day of the week from DT Extract the TIME_OF_DAY from DT Compare DT for equal Compare STRING for equal Find in a STRING variable Compare DT for greater than or equal Compare STRING for greater than or equal Compare DT for greater than Compare STRING for greater than Data type conversion INT to STRING Insert in a STRING variable Compare DT for less than or equal Compare STRING for less than or equal Left part of a STRING variable Length of a STRING variable Limit Compare DT for less than Compare STRING for less than Select maximum Middle part of a STRING variable Select minimum Compare DT for unequal Compare STRING for unequal Data type conversion REAL to STRING Replace in a STRING variable Right part of a STRING variable Data type conversion S5TIME to TIME Subtract two time values Subtract duration from a time Binary selection Data type conversion STRING to DINT Data type conversion STRING to INT Data type conversion STRING to REAL Data type conversion TIME to S5TIME

SFCs for Handling the System Clock Setting the Time with SFC 0 "SET_CLK"
Note SFC 100 "SET_CLKS" now replaces SFC 0 "SET_CLK". In new programs, you should only use SFC 100.

Description
With SFC 0 "SET_CLK" (set system clock), you set the time and the date of the CPU clock. The SFC 0 call starts the clock. The clock then runs starting from the set time and set date. If the clock is a master clock, the CPU also starts to synchronize the time when SFC 0 is called. You set the synchronization intervals using STEP 7.
Parameter Declaration Data Type Memory Area Description At the PDT input, you enter the date and If an error occurs during the execution of

PDT INPUT DT D,L time you want to set. RET_VAL OUTPUT INT I, Q, M, D, L the function, the return value contains an error code.

Date and Time


You enter the date and time as data type DT. As an example: for January 15th, 1995, 10:30 a.m. and 30 seconds you would enter: DT#1995-01-15-10:30:30. The time can only be entered with a precision of seconds. The day of the week is calculated by SFC 0 "SET_CLK" from the date. Remember that you must first create the data type DT with the FC "D_TOD_DT" before you can transfer input parameters to it (see time functions; FC 3, FC 6, FC7, FC 8, FC 33, FC 40, FC 1, FC 35, FC, FC 34).
Error Code (W#16#...) 0000 8080 8081 Explanation No error Error in date Error in time

Reading the Time with SFC 1 "READ_CLK" Description With SFC 1 "READ_CLK" (read system clock), you read the current date or current time of the system clock of the CPU. Parameter Declaration Data Type Memory Area Description RET_VAL OUTPUT INT I, Q, M, D, L If an error occurs during the execution of the function, the return value contains an error code. CDT OUTPUT DT D,L The current date and current time are output at the CDT output Example:

CALL "READ_CLK" RET_VAL:=#loi CDT :=#outtimedate A( L LB 25 L B#16#10 <=I ) = Q 0.0

Example: An organization block (OB1) will be programmed which calls the function FC10. FC10 will then set the time and read the current time dependent on the setting of the input I 0.1. The following SFCs and FCs will be used: SFC 0 "SET_CLK" Set the clock SFC 1 "READ_CLK" Read the clock FC 3 "D_TOD_DT" FC 6 "DT_DATE" FC 7 "DT_DAY" FC 8 "DT_TOD" NoteThe FCs 3, 6, 7, 8 must be copied manually from the IEC Function Blocks library to the block container before the user program is compiled. //Address Meaning //I 0.0 Input to read a variable time into MD 0 //I 0.1 Input to set the time //MW 0 Date, can be changed via STATUS VAR //MD 2 Time, can be changed via STATUS VAR //MW 6 Current date //MW 8 Current day //MD 10 Current time of day //*********************************************************************************** FUNCTION FC 10 TITLE = CPU- Set and read CPU time IN_TIME OUT_TIME IN_DATE OUT_DATE IN_TIME_DATE OUT_TIME_DATE CUR_RETVAL BEGIN NETWORK TITLE = Save the required time and date in the temporary variables L D#1997-1-20 ; //Specify date as constant T #IN_DATE ; L TOD#13:30:0.0 ; // Specify time as constant T #IN_TIME ; NETWORK TITLE = Combine date and time to the output variable (format DATE_AND_TIME) CALL FC 3 // Standard FC3 "D_TOD_DT" (conversion D and TOD to DT) IN1 := #IN_DATE, //Combines date from input variable (format DATE) IN2 := #IN_TIME, //and time from input variable RET_VAL := #IN_TIME_DATE); //(Format TIME_OF_DAY) //to form output variable format DATE_AND_TIME : TIME_OF_DAY ; //TOD actual parameter : TIME_OF_DAY ; //TOD actual parameter : DATE ; //DATE actual parameter : DATE ; //DATE actual parameter : DATE_AND_TIME ; //DT actual parameter : DATE_AND_TIME ; //DT actual parameter : INT ; //Current return value

NETWORK TITLE = Set the time CALL SFC 0 PDT RET_VAL NETWORK TITLE = Read time CALL SFC 1 RET_VAL CDT // SFC 0 "SET_CLK" (set time-of-day interrupt) := #IN_TIME_DATE // Current date and time := MW 200 // Contains error code (if SFC processing faulty)

// SFC 1 "READ_CLK" (read time) := #CUR_RETVAL , // Contains error code //(if SFC processing faulty) := #OUT_TIME_DATE); // Contains read time // and date (format DT)

NETWORK TITLE = Extract date from format DATE_AND_TIME CALL FC 6 //FC6 "DT_DATE" (conversion DT to DATE) IN := #OUT_TIME_DATE, // Read time and date //(format DT) RET_VAL := #OUT_DATE); // Output variable in format DATE //(current date) NETWORK TITLE = Display current date L #OUT_DATE ; T MW 6 ; // Display current date in MW 6 NETWORK TITLE = Extract day from format DATE_AND_TIME CALL FC 7 // FC7 "DT_DAY" (conversion DT to DAY) IN := #OUT_TIME_DATE, // Read time and date //(format DT) RET_VAL := MW 8 // Display current day in MW 8 NETWORK TITLE = Extract time from format DATE_AND_TIME CALL FC 8 // FC8 "DT_TOD" (conversion DT to TOD) IN := #OUT_TIME_DATE, //Read time and date //(format DT) RET_VAL := #OUT_TIME //Output variable in format TOD //(current time of day) NETWORK TITLE = Display current time L #OUT_TIME ; T MD 10 ; END_FUNCTION ORGANIZATION_BLOCK OB 1 CALL FC 10 ;

//Display current time of day in MD 10

Copying Memory Area with SFC 20 "BLKMOV"

Description You use SFC 20 "BLKMOV" (block move) to copy the contents of a memory area (= source area) to another memory area (= destination area). Permissible source areas are the following: Parts of data blocks Memory bits Process-image partition (part process image) for inputs Process-image partition (part process image) for outputs The source parameter can be a part of the data block in the load memory which is not relevant to program execution (DB compiled with the keyword UNLINKED). NoteIf your CPU contains SFC 83, you must use SFC 83 for reading data blocks that are not relevant to program execution. If you use SFC 20, error W#16#8092 is output. SRCBLK INPUT ANY I, Q, M, D, L Specifies the memory area to be copied (source area). Arrays of the data type STRING are not permitted. RET_VAL OUTPUT INT I, Q, M, D, L If an error occurs when the function is being executed, the return value contains an error code. DSTBLK OUTPUT ANY I, Q, M, D, L Specifies the memory area to which the data will be copied (destination area). Arrays of the data type STRING are not permitted.

Error Code (W#16#...) Explanation 0000 No error 8091 Nesting depth exceeded. 8092 SFC 20 "BLKMOV" cannot be executed because a unusable ( (write-protected, unrunnable on unloaded) data block was accessed. 8xyy General error information, Example: The 10 bytes from memory byte 100 are to be copied to the 10 memory bytes from memory byte 200. //I 0.0 Input to enable processing of SFC 20 //Q 4.0 Output to show whether the copy process was executed without error //***********************************************************************************

ORGANIZATION_BLOCK OB 1 BEGIN NETWORK CALL SFC20 // SFC 20 "BLKMOV" (copy variable) SRCBLK := P#M100.0 BYTE 10, RET_VAL := MW10, //10 bytes from MB 100 are copied to the 10 bytes := P#M200.0 BYTE 10; //from MB 200. //If an error occurs while processing the SFC, the SFC stores an error code in MW 10 and the value 0 in the BR bit. END_ORGANIZATION_BLOCK Example DSTBLK

FUNCTION FC10: VAR_TEMP Source : ANY; Target : ANY; END_VAR BEGIN LAR1 P#Source; Load the start address of the ANY pointer in AR1. L B#16#10; T LB[AR1,P#0.0]; L B#16#02; T LB[AR1,P#1.0]; L 10; T LW[AR1,P#2.0]; L 22; T LW[AR1,P#4.0]; L P#DBX11.0; T LD[AR1,P#6.0]; LAR1 P#Target; L B#16#10; T LB[AR1,P#0.0]; L B#16#02; T LB[AR1,P#1.0]; L 10; Load the start address of the ANY pointer in AR1. Load the syntax ID and transfer it to the ANY pointer. Load data type Byte and Load the syntax ID and transfer it to the ANY pointer. Load data type Byte and transfer it to the ANY pointer. Load 10 bytes and transfer them to the ANY pointer. Source is DB22, DBB11

T LW[AR1,P#2.0]; L 33; T LW[AR1,P#4.0]; L P#DBX202.0; T LD[AR1,P#6.0];

transfer it to the ANY pointer. Load 10 bytes and transfer them to the ANY pointer. Target is DB33, DBB202

CALL SFC 20 ( SRCBLK := Source, RET_VAL := MW 12, DSTBLK := Target Call the system function BLKMOV ); Evaluate the BR bit and MW12 END_FUNCTION Initializing a Memory Area with SFC 21 "FILL"
With SFC 21 "FILL," you can initialize a memory area (destination area) with the contents of another memory area (source area). The SFC copies the contents of the specified destination area until the memory area is completely full. Note: The source and destination field must not overlap.If the destination area to be initialized is not a whole multiple of the length of the input parameter BVAL, the destination area is nevertheless written up to the last byte. If the destination area to be initialized is smaller than the source area, the function only copies as much data as can be written to the destination area. If the destination or source area actually present is smaller than the size of the parameter set for the memory area of the source or destination area (BVAL, BLK parameters), no data will be transferred. If the ANY pointer (source or destination) is of the type BOOL, the length specified must be divisible by 8; otherwise the SFC will not be executed. Exceptions The following cannot be used as the source field: Counters Timers You cannot write values to the following using SFC 21: The following block types: FB, SFB, FC, SFC, SDB, Counters, Timers, Memory areas of the peripheral I/O area.

Parameter Declaration Data Type Memory Area Description BVAL INPUT ANY I, Q, M, D, L The parameter BVAL contains the value or description of the area whose contents will be used to initialize the destination area (source area). Arrays of the data type STRING are not permitted. RET_VAL OUTPUT INT I, Q, M, D, L If an error occurs while the function is being processed, the return value contains an error code.

BLK OUTPUT ANY I, Q, M, D, L The parameter BLK contains the description of the area to be initialized (destination area). Arrays of the data type STRING are not permitted.

1) CPU 314C-2 only Zn Counter n A, B Encoder signals HW gate Gate control Latch Store counter distance

Vn Comparator n Prob 0 Measuring probe 0 Bero 0 Reference point switch 0 R+, R- Directional signal Rapid Rapid traverse Creep Creep speed CONV_EN Power section enable CONV_DIR Directional signal (only with control type "voltage 0 to 10 V or current from 0 to 10 mA and directional signal") X Pin usable if not assigned to technology functions

Counting, Frequency Measurement and Pulse Width Modulation


- CPU - CPU - CPU

312C: 2 channels 313C, CPU 313C-2 DP/PtP: 3 channels

314C-2 DP/PtP: 4 channels You have only 2 channels (channel 2 and 3) available when using a positioning function. Counting frequency
- CPU - CPU - CPU

312C = 10 kHz max. 313C, CPU 313C-2 DP/PtP = 30 kHz max. 314C-2 DP/PtP = 60 kHz max.

Signals the CPU counts


o

- 24-V

incremental encoder, equipped with two tracks with 90 phase offset (rotary transducer).

- 24-V - 24-V

pulse generator with direction signal proximity switch (e. g. BERO or light barrier)

Project design
1)

- Via

parameter assignment screen forms 1) Quadrouple evaluation of the two signals will result in an internal quadrouple counting frequency

Counting
Counting mode.
- Infinite - Single

count count count

- Periodic

Gate function For starting, stopping and interrupting the count functions. Latch function You can use this function to save the current internal time value at the positive edge on the digital input. Reference value You can store a reference value in the CPU. Depending on the count and reference values, you can activate a digital output or generate a hardware interrupt. Hysteresis You can specify a hysteresis for the digital output. This prevents the digital output form dithering at every small jitter of the encoder signal when the count value lies within the reference value. Hardware interrupts

Frequency Measurement
Gate function Use the gate function for starting and stopping frequency measurements. High/low limit You can specify a high and low limit for frequency monitoring. You can activate a digital input and/or generate a hardware interrupt when one of the limits is reached. Hardware interrupts

Pulse Width Modulation (PWM)


Gate function Use the gate function to start/stop pulse width modulation. Hardware interrupts The count functions (counting, frequency measurement and pulse width modulation) are integrated in the CPU. The encoder, or a bounce-free switch, supplies the count pulses. Use the PG/PC to configure the CPU in the parameter assignment screen forms for the technological functions of the CPU.

to program CPU SFBs which you can implement directly in your user program. to commission and test the CPU with the help of the standard STEP7 interface (monitoring functions and variable table).

Você também pode gostar