background image

Sample Programs

Aside from actually applying a DI-159 PLC in your control application, the best way to under-
stand how easy the instrument is to use is by example. The following just scratches the surface,
but should give you a solid understanding of the range of DI-159 PLC control possibilities.
Note that explanatory comments appear in these examples on the same line as the code to con-
serve space. Since the BASIC engine supports comments using the familiar REM statement,
comments would actually appear as program lines.

Example #1 Boilerplate Code

Object

Boilerplate code that must be included at the beginning of every program that uses the specified
I/O points. *

Code

10 dim c0 as pin Ch0 for analog input

20 dim c1 as pin Ch1 for analog input

30 dim c2 as pin Ch2 for analog input

40 dim c3 as pin Ch3 for analog input

50 dim c4 as pin Ch4 for analog input

60 dim c5 as pin Ch5 for analog input

70 dim c6 as pin Ch6 for analog input

80 dim c7 as pin Ch7 for analog input

90 dim i0 as pin Di0 for digital input

100 dim i1 as pin Di1 for digital input

110 dim i2 as pin Di2 for digital input

120 dim i3 as pin Di3 for digital input

130 dim o0 as pin Do0 for digital output

140 dim o1 as pin Do1 for digital output

150 dim o2 as pin Do2 for digital output

160 dim o3 as pin Do3 for digital output

170 dim push_button as pin Pb for digital input

180 dim led0 as pin Led0 for digital output

190 dim led1 as pin Led1 for digital output

200 rem your program starts here

'map analog input 0 to BASIC variable "c0"

'map analog input 1 to BASIC variable "c1"

'map analog input 2 to BASIC variable "c2"

'map analog input 3 to BASIC variable "c3"

'map analog input 4 to BASIC variable "c4"

'map analog input 5 to BASIC variable "c5"

'map analog input 6 to BASIC variable "c6"

'map analog input 7 to BASIC variable "c7"

'map digital input 0 to BASIC variable "i0"

'map digital input 1 to BASIC variable "i1"

'map digital input 2 to BASIC variable "i2"

'map digital input 3 to BASIC variable "i3"

'map digital output 0 to BASIC variable "o0"

'map digital output 1 to BASIC variable "o1"

'map digital output 2 to BASIC variable "o2"

'map digital output 3 to BASIC variable "o3"

'map pushbutton to BASIC variable "push_button"

'map LED0 to BASIC variable "led0"

'map LED1 to BASIC variable "led1"

'

Comment

These instructions map the various DI-159 PLC analog input and digital I/0 points so the
BASIC program can use them. You can rename them as necessary (e.g. change "c0" to "Motor-
Voltage"), and you can even omit those that will not be used by your program. In this example
all I/O points have been mapped to the variable names that immediately follow the "dim" state-
ment.

* Note that for clarity this code will not be shown in all other examples, so all subsequent pro-
gramming examples begin with line 200.

71

Содержание DI-159

Страница 1: ...company The distribution of this material outside the company may occur only as authorized by the company in writing Portions Copyright 2008 2011 All rights reserved http www cpustick com DATAQ Instr...

Страница 2: ...o products that have been repaired or altered by persons other than DATAQ Instruments employees or products that have been subjected to misuse neglect improper installation or accident DATAQ Instrumen...

Страница 3: ...ortable con trol module that communicates through your computer s USB port Power is derived from the interface port so no external power is required while the instrument remains tethered to a PC An op...

Страница 4: ...via down load at http www dataq com 159 StickOS TM The embedded programming environment StickOS provides BASIC language applications See StickOS in this documentation for more information DATAQ PLC T...

Страница 5: ...Number of Channels 4 Pull up value 47 K Isolation none Input high voltage threshold 1 8 V minimum Input low voltage threshold 1 4 V maximum Absolute maximum values 30 VDC ADC Characteristics Resolutio...

Страница 6: ...g Temperature 0 C to 35 C 32 F to 95 F Operating Humidity 0 to 90 non condensing Storage Temperature 20 C to 45 C 4 F to 113 F Storage Humidity 0 to 90 non condensing Physical Characteristics Enclosur...

Страница 7: ...r unit must for any reason be sent back to DATAQ Instruments Installing Windows Drivers USB Drivers for the DI 159 PLC can be installed via a downloadable executable directly from the DATAQ Instrument...

Страница 8: ...r PC and connect via your chosen terminal program Go to DATAQ Terminal Emulator for instructions installing and running the DATAQ Instru ments PLC terminal program Connecting the Instrument to Your Co...

Страница 9: ...age to the instrument Mini B USB Connection Use the supplied USB cable to connect and power the instrument through your computer s USB port Connecting Input Signals All input signal connections are ma...

Страница 10: ...eral purpose digital inputs bits 0 3 Digital Outputs General purpose digital outputs bits 0 3 Connect Analog Input Channel 0 Use the following diagram to connect Analog Input Channel 0 Connecting Sign...

Страница 11: ...ion Do not over tighten 3 Tug gently on the signal lead to ensure that it is firmly secured Digital Inputs The DI 159 contains 4 general purpose digital inputs Valid signals are switch closures or dis...

Страница 12: ...atus and notification Led0 and Led1 General purpose LEDs Heartbeat Indicates the device is powered slow heartbeat at once per second or when a program is running fast heartbeat at about 4 times per se...

Страница 13: ...FOR WHILE DO and GOSUB constructs Use any terminal program that can hook a com port to connect to and pro gram the DI 159 PLC Quick Reference Guide Command Line StickOS Commands Help Command Entering...

Страница 14: ...DI 159 PLC Hardware Manual StickOS Read Data Statements Conditional Statements Looping Conditional Statements Subroutines Timers Digital I O Analog Input Frequency Output Other Statements 14...

Страница 15: ...es load name load saved program memory print memory usage new erase code ram and flash memories purge name purge saved program renumber line renumber program lines and save run line run program save n...

Страница 16: ...mod add subtract shift right left inequalities equal not equal bitwise or xor and logical or xor and Strings V is a null terminated view into a byte array v string statements dim input let print vpri...

Страница 17: ...ore label restore data pointer sleep expression s ms us delay program execution stop insert breakpoint in code vprint var dec hex raw expr print to variable Modes analog millivolts set analog voltage...

Страница 18: ...rence array variable indices start at 0 v is the same as v 0 except for input print statements ram variables dim var n dim var n as byte short flash parameter variables dim varflash n as flash pin ali...

Страница 19: ...t will always be called out explicitly either by example or by text rather than nomenclated with the traditional Command line editing is enabled via the terminal keys key function move cursor left mov...

Страница 20: ...used to control the StickOS BASIC program Unlike BASIC program statements StickOS commands cannot be entered into the StickOS BASIC program with a line number Help Command Entering Programs Running Pr...

Страница 21: ...rl C auto line clear flash cls cont line delete line line s ubname download slave Hz dir edit line help topic list line line s ubname load name memory new profile line line s ubname purge name renumbe...

Страница 22: ...e name undo upgrade uptime for more information help modes renumber program lines and save reset the DI 159 PLC run program save code ram to flash mem ory undo code changes since last save upgrade Sti...

Страница 23: ...d statements in RAM are seam lessly merged with the previously saved statements in flash memory to give the appearance of a single current program at a slight performance penalty When the newly edited...

Страница 24: ...ity To set the line numbering mode use the command numbers on off To display the line numbering mode use the command numbers Note that unnumbered listings are useful to paste back in to the auto comma...

Страница 25: ...program by 10 s and save the BASIC program permanently to flash memory use the command renumber To delete all lines from the BASIC program use the command new Examples 10 dim a 20 for a 1 to 10 auto 3...

Страница 26: ...DI 159 PLC Hardware Manual StickOS 20 for a 1 to 10 30 print a 40 next end renumber list 10 rem this is a comment 20 dim a 30 for a 1 to 10 40 print a 50 next end new list end _ 26...

Страница 27: ...BASIC program from a specific line number use the com mand cont line To set the autorun mode for the saved BASIC program use the command autorun on off This takes effect after the next DI 159 PLC rese...

Страница 28: ...DI 159 PLC Hardware Manual StickOS 40 endwhile save run Ctrl C STOP at line 40 print a 5272 cont Ctrl C STOP at line 30 print a 11546 autorun off autorun on _ 28...

Страница 29: ...unaffected by all other StickOS commands than these To display the list of currently stored named programs use the command dir To store the current program under the specified name use the command sa...

Страница 30: ...nsert one or more conditional breakpoints in the program with the following statement line assert expression When the program execution reaches line expression is evaluated and if it is false i e 0 th...

Страница 31: ...llows you to modify the value of variables with an immediate mode statement like let variable expression Note that if an immediate mode statement references a pin variable the live DI 159 PLC pin is e...

Страница 32: ...trace mode for the BASIC program use the command trace While trace mode is on the program will display all executed lines and variable modifications while running Examples 10 dim a sum 20 for a 1 to 1...

Страница 33: ...o 10000 30 let sum sum a 40 next 50 print sum end run 10 dim a sum STOP at line 10 cont 20 for a 1 to 10000 let a 1 STOP at line 20 Enter 30 let sum sum a let sum 1 STOP at line 30 Enter 40 next let a...

Страница 34: ...isplay the StickOS memory usage use the command memory To reset the DI 159 PLC as if it was just powered up use the command reset Note that the reset command inherently breaks the USB or Ethernet conn...

Страница 35: ...used save memory 0 ram code bytes used 1 flash code bytes used 19 ram variable bytes used 0 flash parameter bytes used 1 variables used clear memory 0 ram code bytes used 1 flash code bytes used 0 ram...

Страница 36: ...executed in immediate mode at the command prompt without a line number just as if the program had encountered the statement at the cur rent point of execution Variable Declarations System Variables Va...

Страница 37: ...s specified in a dimension statement integer is assumed if no as is specified a RAM variable is assumed Array RAM variables Array RAM variables can be dimensioned with the following statements dim var...

Страница 38: ...one run of a program to another even if power is lost between runs unlike RAM variables which are cleared to 0 at the start of every run Note that since flash memory has a finite life 100 000 writes...

Страница 39: ...70 next 80 for b 0 to 3 90 print array b 100 let led led 110 next 120 print potentiometer is potentiometer 130 print volatile is volatile 140 print persistent is persistent 150 let persistent persiste...

Страница 40: ...state These variables are all read only analog analog supply millivolts getchar most recent console character keychar most recent keypad character msecs number of milliseconds since boot seconds numb...

Страница 41: ...evaluates to an array index between 0 and the length of the array minus one and the second expression is assigned to the specified array element String variables are assigned with the following state...

Страница 42: ...taking one argument on the right the logical and bitwise not oper ators are unary All binary operators evaluate from left to right all unary operators evaluate from right to left Note that the operat...

Страница 43: ...s will be removed from the expression based on the precedence rules above Examples 10 print 2 3 4 20 print 2 3 4 list 10 print 2 3 4 20 print 2 3 4 end run 14 14 print 3 4 7 print 3 2 1 print 0 1 prin...

Страница 44: ...variable start length variable substring string concatenation operator A string may be tested in a conditional statement with a condition of the form if string relation string then while string relati...

Страница 45: ...StickOS DI 159 PLC Hardware Manual 40 print a i 1 50 next run hello h e l l o new 10 dim a 10 20 input a 30 if a y then 40 print yes 50 else 60 print no 70 endif run aya yes run aaa no 45...

Страница 46: ...string dec hex raw expression If the expression specifies an array its entire array contents are printed If the expression ref erences an input pin variable the corresponding DI 159 PLC input pin is s...

Страница 47: ...s can be used to print integer expressions using either a decimal or hex adecimal output radix or printing raw ASCII bytes vprint variable dec hex raw expression Or strings vprint variable string Or v...

Страница 48: ...ences an output pin variable the corresponding DI 159 PLC output pin is immediately updated When the input statement is serviced StickOS prints a prompt to the console _ And the user enters integer or...

Страница 49: ...bles at runtime use the read statement read variable read variable variable If a read is attempted when no more data exists the program stops with an out of data error A line may be labeled and the cu...

Страница 50: ...if and else clauses and their corresponding statements are optional Alternately the string form of this statement is if string relation string then statements elseif string relation string then statem...

Страница 51: ...StickOS DI 159 PLC Hardware Manual 4 is even _ 51...

Страница 52: ...ements within the loop execute At the end of the loop if the incremented loop variable would still be within the range inclusive of the first and second expression the loop variable is incremented by...

Страница 53: ...of the innermost loop Additionally multiple nested loops can be exited prematurely together using the statement break n Which causes program execution to immediately jump to the statements following t...

Страница 54: ...rs 0 thru a is sum 110 let a a 1 120 endwhile run sum of integers 0 thru 0 is 0 sum of integers 0 thru 1 is 1 sum of integers 0 thru 2 is 3 sum of integers 0 thru 3 is 6 sum of integers 0 thru 4 is 10...

Страница 55: ...lues of the caller s gosub expressions Simple variable caller s gosub expression s how ever are passed to sub param s by reference allowing the sub to modify the caller s variables all other caller s...

Страница 56: ...ntegers 0 thru 1 is 1 sum of integers 0 thru 2 is 3 sum of integers 0 thru 3 is 6 sum of integers 0 thru 4 is 10 sum of integers 0 thru 5 is 15 sum of integers 0 thru 6 is 21 sum of integers 0 thru 7...

Страница 57: ...s to execute when it is delivered specified with the statement on timer n statement If statement is a gosubsubname then all of the statements in the corresponding sub are executed when the timer inte...

Страница 58: ...DI 159 PLC Hardware Manual StickOS 90 sub fast 100 let ticks ticks 1 110 endsub run slow slow slow ticks is 14 _ 58...

Страница 59: ...digital output channel Led1 Pb digital input channel for the pushbutton Digital input 0 is configured and the variable i0 is bound to the Di0 pin with the following state ment dim i0 as pin Di0 for d...

Страница 60: ...the program Line 10 configures the dtin0 pin for digital output and creates a variable named square whose updates are reflected at that pin Line 20 starts an infinite loop typically DI 159 PLC program...

Страница 61: ...run in a program can also be run in immediate mode at the command prompt For example after having run the above program the square variable and dtin0 pin remain configured so you can type print squar...

Страница 62: ...le c0 is bound to the Ch0 pin with the following statement dim c0 as pin Ch0 for analog input A pin is configured for analog input and a variable bound to that pin with the following state ment dim va...

Страница 63: ...otentiometer as pin an0 for analog input 20 print potentiometer is potentiometer run potentiometer is 2026 _ Note that analog inputs and outputs are represented by integers in units of millivolts mV N...

Страница 64: ...l I O or analog input The following BASIC program generates a 1kHz square wave on a frequency output pin dtin0 for 1 second new 10 dim audio as pin dtin0 for frequency output 20 let audio 1000 30 slee...

Страница 65: ...ms us Note that the minimum sleep resolution is the clock tick which is 0 25 milliseconds Note also that in general it would be a bad idea to use a sleep statement in the on handler for a timer You c...

Страница 66: ...l Software Installation 1 Go to http www dataq com 159 in your web browser 2 Click on the DI 159 PLC Terminal Emulator download link and download the setup exe file The following prerequisites are req...

Страница 67: ...text file Save As Ctrl S Opens the Windows Save File dialog box Allows you to save the cur rently loaded program to a text file on your computer Exit Exits the program DI 159 Menu Run F5 Runs the curr...

Страница 68: ...ides the Version number of the software and copyright infor mation Icons Load Opens the Windows File Selection dialog box allowing you to load a BASIC program saved as a text file Save Program to Text...

Страница 69: ...ow can hold about 1000 rows The Output Display provides 4 program icons View in Excel Enabled after you are streaming data to a csv file Click this button to open Microsoft Excel and view data recorde...

Страница 70: ...ll not be identified until the code is saved into memory It is recommended to load example one in Help Sample PLC Code Ex 1 Boilerplate code before creating a program from scratch There are 3 program...

Страница 71: ...as pin Do3 for digital output 170 dim push_button as pin Pb for digital input 180 dim led0 as pin Led0 for digital output 190 dim led1 as pin Led1 for digital output 200 rem your program starts here...

Страница 72: ...heral Led0 in this case Timer intervals can be configured in seconds s mil liseconds ms or microseconds us and can range from milliseconds to hours Example 3 Flash Both LEDs Object Flash both LEDs at...

Страница 73: ...ur timers for 1 sec interval configure second of four timers for 1 4 sec interval execute subroutine flash_led0 when timer0 fires execute subroutine flash_led1 when timer1 fires do nothing while waiti...

Страница 74: ...down return from the subroutine Comment Note that the pushbutton and LEDs are low true A 0 written to either LED lights it and the pushbutton transitions from 1 to 0 when pressed Example 6 Create a Sq...

Страница 75: ...260 sub readin 270 cr cr 1 280 let o0 cr 1 290 let o1 cr 2 300 let o2 cr 4 310 let o3 cr 8 320 print cr c0 c1 c2 c3 c4 c5 c6 c7 i0 i1 i2 i3 o0 o1 o2 o3 330 led0 o0 340 led1 o1 350 endsub dimension cr...

Страница 76: ...DI 159 Block Diagram 76...

Страница 77: ...Dimensional Drawing 77...

Страница 78: ...59 product page for support issues and documentation at http www dataq com plc data acquisition di 159 plc html If you cannot find an answer to your question there please fill out a support ticket at...

Страница 79: ...241 Springside Drive Akron Ohio 44333 Telephone 330 668 1444 Fax 330 666 5434 Submit a support ticket to www dataq com ticket Direct Product Links click on text to jump to page Data Acquisition Data...

Отзывы: