×

Subscribe to newsletter

subscribe to our weekly newsletter to get notified with latest story and article Subscribe now!




PLC BLOG | Codesys Counter instruction
Share on Facebook Share On Twitter Share on LinkedIn Share on Whatsapp


codesys counter instruction

Counter are used for counting bit and increment and decrement of bits, generally two type of counter are used either UP counter or down counter. UP counter counting bits by adding 1 to its current value. for down dounter subtract 1 to its current value.


CTU - counter UP

up counter function block uses as Incrementer. each rising edge at the input (CU) of counter block, increment or add 1 to current value of counter. by default current value is set to 0. input and output of description of block:
    INPUT
  • Counter UP (CU): at each Rising edge, Incrementing CV by one data type bool.
  • RESET : Reset CTU, data type bool.
  • PRESET VALUE (PV) : maximum limit for incrementing , data type word.
  • OUTPUT
  • Q output: TRUE if CV >= PV, data type bool.
  • Current value (CV) : store current counter value.
  • instance name type CTU
    generaly in iec standard DATA type INT define for CV and PV, but codesys use word.

codesys CTU - counter UP instruction

data type for input variables input_ctu(CU) and (reset_CU) RESET and output variable out_ctu(Q) are data type BOOL, the input variable PV and the output variable ctu_cv (CV) are type WORD.
  • CV = 0 if reset is true.
  • if reset false and CU set to true (rising edge) then CV will be increment by 1. each rising edge increment CV by 1 untill CV is greater than or equal to the preset value PV.
  • Q will return TRUE when CV is greater than or equal to the preset value PV.


CTD counter down

CTD counter down, a Function Block Decrement of loaded Current value by one at each rising edge of CD. by default CV = 0 if load input is set to true then PV value transfer to CV.The input variables CD and LOAD as well as the output variable Q are BOOL data type, the input variable PV and the output variable CV are WORD data type.
    INPUT
  • Counter Down CD : data type BOOL, at each Rising edge Decrement CV by one.
  • LOAD : data type BOOL, if LOAD set to TRUE then PV transfer to CV.
  • Preset value PV : Start value for decrementing , data type word.
  • OUTPUT
  • Q output : data type BOOL, Q is TRUE when CV = 0.
  • current value CV : Current counter value.
  • instance type CTD.
    note: in codesys CV doesn't fall below 0.

codesys CTD counter down instruction

input variables input_cd(CU) and load_cd (LOAD), PV =4, and the output variable ctd_cv (CV) and out_ctds (Q). input variable
  • at first when input_cd true and load_cd false at output CV = 0 , because CV set to default value.
  • when load true and CD false then counter variable CV will be initialized with the upper limit PV. now set load input to false. if rising edge detected false to true at input CD , CV will be decrement by 1.
  • output Q returns TRUE when CV is equal 0.


CTUD Incrementer/Decrementer counter

CTUD perform increment as well as decrement counter. working and input is same for above counter. Rising edge at input CU, counter work as increment .Rising edge at CD counter work as Decrement.
  • CU data type BOOL, Rising edge Increment CV by one.
  • CD data type BOOL, Rising edge Decrement CV by one.
  • RESET data type BOOL when set to TRUE, Reset CV to 0.
  • LOAD data type BOOL if set to TRUE then value of PV set to CV.
  • PV data type WORD. Start value for decrementing or upper limit for incrementing.
  • Output QU data type BOOL, output for UP counting TRUE if CV ≥ PV .
  • Output QD data type BOOL, output for Down counting TRUE if CV = 0 .
  • Output QD data type BOOL, output for Down counting TRUE if CV = 0 .
  • CV data type WORD, Current counter value.

codesys CTUD Incrementer or Decrementer counter instruction

The input variables CU, CD, RESET, LOAD as well as the output variables QU and QD are data type BOOL, PV and CV are data type WORD.
  • If RESET is valid, the counter variable CV will be initialized with 0.
  • If LOAD is valid, CV will be initialized with PV.
  • If CU has a rising edge from FALSE to TRUE, CV will be increment by 1.
  • CD has a rising edge from FALSE to TRUE, CV will be decrement by 1.
  • QU returns TRUE when CV has become greater than or equal to PV.
  • QD returns TRUE when CV has become equal to 0.


counter example

in this counter example the value of counter PV set by Move instruntion, timer generate periodic pulse and these pulse counted by counter when current counter value CV is more than PV so counter up reset and again count from 0.

codesys counter example

in above program counter counter increment depend on timer output, each 2s gives a true bit and counter increment by 1. greater than comparison block compare CV and PV when CV > PV counter reset to 0.

 
Share on Facebook Share On Twitter Share on LinkedIn Share on Whatsapp


Suggested Post


 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

comment