Comfile Cubloc User Manual
Comfile Cubloc User Manual
TM
User Manual
1
Blank Page
2
3
Warranty
Comfile Technology provides a one year warranty on its products against defects in
materials and workmanship. If you discover a defect, Comfile Technology will, at its
option, repair the product, replace the product, or refund the purchase price. Simply
return the product with a description of the problem and a copy of your invoice (if you
do not have your invoice, please include your name and telephone number). This
warranty does not apply if the product has been modified or damaged by accident,
abuse, or misuse.
If, within 30 days of having received your product, you find that it does not suit your
needs, you may return it for a refund. Comfile Technology will refund the purchase
price of the product, excluding shipping/handling costs. This does not apply if the
product has been altered or damaged.
Notice
This manual may be changed or updated without notice. Comfile Technology Inc. is not
responsible for any actions taken outside the explanation of this manual. This product
is protected by patents across the world. You may not change, copy, reproduce, or
translate it without the consent of Comfile Technology Inc.
Disclaimer of Liability
4
Preface
Comfile Technology has been developing PLC and BASIC controllers since
1997. Leveraging previous experience, we are providing a unique product
that is powerful, flexible, and has the best features of both BASIC
controllers and PLCs (Programmable Logic Controllers).
Cubloc was created for beginners and advanced users alike. Its simplified
commands and programming tools are an easy way to get started with
microcontrollers, yet the device is powerful enough to handle serious
automation applications with minimal time spent in the programming phase.
With our Plug-N-Play displays, development boards, and relay boards, you
will be able to put an application together in matter or hours, instead of
months.
5
Notice
The Start Kit or Industrial Kit comes with the latest version of Cubloc Studio
at the time the CD was created.
6
Table of Contents
CHAPTER 1: GETTING STARTED...........................................................................................17
What is Cubloc?.........................................................................18
Cubloc Specifications..................................................................19
Ladder Logic and BASIC..............................................................23
Multi-tasking of Ladder Logic and BASIC.......................................25
Advantages of an On-Chip PLC/Embedded Computer......................27
Development Environment..........................................................29
Hints for Traditional PLC Users.....................................................30
Hints for Microcontroller Users.....................................................31
Cubloc’s Internal Structure..........................................................32
Cubloc Peripherals......................................................................33
CHAPTER 2: HARDWARE........................................................................................................37
Hardware Features.....................................................................38
CB210......................................................................................39
CB220 / CB320..........................................................................41
Supplying power to the CB220 / CB320.........................................43
CB280 / CB380..........................................................................44
How to supply power to the CB280 and CB380...............................46
CB290......................................................................................47
CB400......................................................................................51
CB405......................................................................................55
CB405RT...................................................................................58
How to connect a battery to CB290 / CB405..................................61
Dimensions...............................................................................62
Math Functions.........................................................................116
Type Conversion.......................................................................118
String Functions.......................................................................121
AdIn( )....................................................................................130
Alias.......................................................................................132
Bcd2Bin..................................................................................133
BClr........................................................................................134
Beep.......................................................................................135
Bfree( )...................................................................................136
Bin2Bcd..................................................................................137
BLen( )...................................................................................138
ByteIn( ).................................................................................139
ByteOut..................................................................................140
CheckBf( )...............................................................................141
Compare.................................................................................142
Count( )..................................................................................143
CountReset..............................................................................145
Dcd........................................................................................146
Debug.....................................................................................147
Decr.......................................................................................150
Delay......................................................................................151
Do...Loop................................................................................152
DTZero...................................................................................154
EERead( )................................................................................155
EEWrite...................................................................................156
EKeyPad()...............................................................................157
8
For...Next................................................................................158
FreePin...................................................................................160
FreqOut..................................................................................161
Get( ).....................................................................................163
GetA.......................................................................................164
GetA2.....................................................................................165
GetCrc....................................................................................166
GetPad( )................................................................................167
GetStr( ).................................................................................168
GetStr2( )...............................................................................169
GoSub...Return........................................................................170
GoTo.......................................................................................171
HRead( ).................................................................................173
HWrite....................................................................................173
HeapClear...............................................................................174
Heap( )...................................................................................174
HeapW....................................................................................174
High.......................................................................................176
I2CStart..................................................................................177
I2CStop..................................................................................177
I2CRead( )..............................................................................178
I2CReadNA( )..........................................................................179
I2CWrite( )..............................................................................180
If...Then...ElseIf…EndIf..............................................................181
In( ).......................................................................................182
Incr........................................................................................183
Input......................................................................................184
KeyIn......................................................................................185
KeyInH...................................................................................186
Keypad...................................................................................187
Low........................................................................................188
MemAdr( )...............................................................................189
Ncd........................................................................................190
Nop........................................................................................191
On Int.....................................................................................192
On LadderInt GoSub.................................................................193
On Pad....................................................................................195
On Recv..................................................................................196
On Timer()..............................................................................197
OpenCom................................................................................198
Out.........................................................................................201
Output....................................................................................202
OutStat( )...............................................................................203
9
Pause.....................................................................................203
Peek( )....................................................................................204
Poke.......................................................................................204
PulsOut...................................................................................205
Put.........................................................................................206
PutA.......................................................................................207
PutA2.....................................................................................208
PutStr.....................................................................................209
Pwm.......................................................................................210
PwmOff...................................................................................211
RamClear................................................................................212
Reset......................................................................................213
Reverse...................................................................................214
Rnd( ).....................................................................................215
Select...Case...........................................................................216
Set Debug...............................................................................217
Debug Command How-to...........................................................217
Set I2C...................................................................................220
Set Int....................................................................................221
Set Ladder...............................................................................222
Set Modbus.............................................................................223
Set OnGlobal...........................................................................224
Set OnInt................................................................................225
Set OnLadderInt.......................................................................226
Set OnPad...............................................................................227
Set OnRecv.............................................................................228
Set OnTimer............................................................................229
Set OutOnly.............................................................................230
Set Pad...................................................................................231
Set RS232...............................................................................234
Set RS485...............................................................................235
Set Spi....................................................................................237
Set Until..................................................................................238
ShiftIn( ).................................................................................239
ShiftOut..................................................................................240
Spi.........................................................................................241
StepAccel................................................................................242
StepPulse................................................................................243
StepStat( )..............................................................................244
StepStop.................................................................................244
Sys( ).....................................................................................247
TADIn()...................................................................................248
Time( )...................................................................................249
10
TimeSet..................................................................................251
UDelay....................................................................................253
UsePin....................................................................................254
UTMax....................................................................................255
Wait.......................................................................................256
WaitTx....................................................................................257
CLCD Module...........................................................................263
GHLCD Graphic LCD : GHB3224C...............................................266
Cls.........................................................................................269
Clear......................................................................................269
CsrOn.....................................................................................269
CsrOff.....................................................................................269
Locate.....................................................................................269
Print.......................................................................................270
CLCDOut.................................................................................271
Layer......................................................................................272
GLayer....................................................................................273
Overlay...................................................................................273
Contrast..................................................................................274
Light.......................................................................................274
WMode...................................................................................274
Font........................................................................................275
Style.......................................................................................276
CMode....................................................................................276
Line........................................................................................277
LineTo.....................................................................................277
Box........................................................................................278
BoxClear.................................................................................278
BoxFill.....................................................................................278
Circle......................................................................................279
CircleFill..................................................................................279
Ellipse.....................................................................................280
ElFill.......................................................................................280
GLocate..................................................................................281
GPrint.....................................................................................281
DPrint.....................................................................................282
Offset.....................................................................................283
Pset........................................................................................284
Color......................................................................................284
LineStyle.................................................................................284
DotSize...................................................................................284
11
Paint.......................................................................................285
Arc.........................................................................................285
DefChr....................................................................................286
Bmp.......................................................................................286
GPush.....................................................................................288
GPop......................................................................................288
GPaste....................................................................................289
HPush.....................................................................................290
HPop......................................................................................290
HPaste....................................................................................290
Seven Segment Display: CSG....................................................292
CSGDec..................................................................................293
CSGNPut.................................................................................294
CSGXPut.................................................................................295
CSGDec..................................................................................295
CSGHex..................................................................................295
CHAPTER 8: INTERFACING...................................................................................................297
Input/Output Circuits................................................................298
RS-232 HOWTO.......................................................................302
CUNET....................................................................................304
Cubloc Study Board Circuit Diagram............................................306
About I²C…..............................................................................308
More About I²C… (Advanced).....................................................312
CHAPTER 9: MODBUS............................................................................................................315
About Modbus…........................................................................316
Modbus ASCII Master................................................................327
Modbus ASCII Slave.................................................................329
13
WADD, DWADD........................................................................410
WSUB, DWSUB........................................................................410
WMUL.....................................................................................411
WDIV, DWDIV..........................................................................412
WOR, DWOR............................................................................413
WXOR, DWXOR........................................................................414
WAND, DWAND........................................................................415
WROL, DWROL.........................................................................416
WROR, DWROR........................................................................417
WRCL, DWRCL.........................................................................418
WRCR, DWRCR........................................................................419
WINV, DWINV..........................................................................420
WNEG, DWNEG........................................................................421
GOTO, LABEL...........................................................................422
CALLS, SBRT, RET.....................................................................423
INTON....................................................................................424
TND........................................................................................425
Special Registers......................................................................426
What is Cutouch?.....................................................................431
Cutouch Specifications..............................................................432
Hardware Requirements............................................................433
Cutouch Dimensions ................................................................434
Cutouch I/O Ports ....................................................................435
Backup Battery........................................................................437
Cutouch Output ports ...............................................................438
Cutouch Input ports .................................................................439
Cutouch A/D and TTL I/O Ports ..................................................440
Cutouch jumper & connector .....................................................441
Menu System Library................................................................442
MENU Commands.....................................................................442
MenuSet.................................................................................443
MenuTitle................................................................................443
MenuCheck( )..........................................................................444
MenuReverse...........................................................................444
Menu( )...................................................................................445
WaitDraw................................................................................445
Touch Pad Input Example..........................................................446
Cutouch Sample Programs.........................................................448
CB405RT.................................................................................464
14
CB405RT-Related Commands....................................................465
RTCRead( ) .............................................................................465
RTCWrite ................................................................................468
HADIn( ) ................................................................................469
HADIn2( ) ..............................................................................470
Appendix A: ASCII CODE ..........................................................471
Appendix B: Note for BASIC STAMP users....................................472
Ladder Logic Special Registers...................................................473
15
MEMO
16
Chapter 1:
Getting
Started
17
What is Cubloc?
Cubloc is different from the traditional PLCs that you may have
encountered. Traditional PLCs are built into cases and have hardwired
connections, but Cubloc is an "On-Chip" PLC/Industrial Controller, meaning
you have more freedom and flexibility in the final product size and design.
Cubloc Modules are similar to traditional PLCs in that Ladder Logic can be
used…but the small size allows developers to design custom PCBs for any
application.
There are different models, each with a unique program memory size and
number of I/O ports. Please make a selection based on your product’s
requirement.
18
Cubloc Specifications
Processor CB210 CB220 CB280 CB290
Picture
Program
80KB 80KB 80KB 80KB
Memory
Data 2KB(BASIC)+ 2KB(BASIC)+ 2KB(BASIC)+ 24KB(BASIC)+
Memory 1KB(Ladder Logic) 1KB(Ladder Logic) 1KB(Ladder Logic) 4KB(Ladder Logic)
RTC No No No Yes
Data
Memory None None None Optional
Backup
Operating
-40 °C to 120 °C -40 °C to 120 °C -40 °C to 120 °C -40 °C to 120 °C
Temp.
Package I/O Board 24-pin DIP 600mil 64-pin Module 108-pin Module
19
Processor CB320 CB380 CB405 CB400
Picture
Program
200KB 200KB 200KB 200KB
Memory
6KB(BASIC)+
Data 6KB(BASIC)+ 51KB(BASIC)+4KB(Lad- 6KB(BASIC)+
1KB(Ladder
Memory Logic)
1KB(Ladder Logic) der Logic)+55KB(Heap) 1KB(Ladder Logic)
RTC No No No No
Data
Memory None None Optional None
Backup
Operating -40 °C to 120
-40 °C to 120 °C -40 °C to 120 °C -40 °C to 120 °C
Temp. °C
24-pin DIP
Package 600mil
64-pin Module 80-pin Module 108-pin Module
1.2"L x 0.6"W x
1.4"L x 1"W x
0.4"H 2.4"L x 1.9"W x 0.5"H 2.4"L x 1.9"W x 0.5"H
Size (30 x 15.3 x 11
0.4"H
(59.4 x 47.8 x 13 mm) (59.4 x 47.8 x 13 mm)
(35 x 25.4 x 11 mm)
mm)
The main advantage of Cubloc is that it fills Ladder Logic’s weaknesses with
the BASIC language. Ladder Logic is good enough to replace sequence
20
diagrams, but to collect data, print graphics, and process complex tasks is
asking a little bit too much. That is why we added the BASIC language.
You can now run both Ladder Logic and/or BASIC!
DIM A AS IN TE G E R
IF IN (0) = 0 T HE N
O UT 2,A
E N D IF
G O TO L AB1
21
There are other PLCs on the current market that support both Ladder Logic
and BASIC. However, these PLCs do not multi-task. Because BASIC is part
of their Ladder Logic, it does not run independently like Cubloc or Cutouch.
This can prove to be costly since BASIC is not real-time oriented and can
delay the Ladder Logic scans, possible causing missed inputs or other
undesired behavior. Cubloc, on the other hand, doesn't suffer from these
weaknesses because it multitasks; guaranteeing accuracy and precise
timing.
LADDER
BASIC
LADDER BASIC
LADDER
BASIC
With 32-bit IEEE floating point math support and MODBUS ASCII/RTU
support, the user will find that Cubloc and Cutouch are among the most
versatile BASIC/PLC hybrid chips on the market today.
22
Ladder Logic and BASIC
Ladder Logic's greatest advantage is that all circuits are laid out in parallel;
they are all processed as fast as the ladder scan time will allow. This allows
for a more parallel execution path for unrelated functions.
P0 P2 P9
A
P3
P5 P6 P8
B
As you can see above, both A and B circuits are in a waiting state, ready to
turn the output On as soon as input is turned On. For example, if input P3
turned On, P9 would turn On.
Dim A As Integer
Dim B As Integer
A=0
Jump
Again:
For B=0 to 10
Debug DEC A,CR
A = A + 10
Loop
Next
Goto Again
Whether you are an experienced MCU or PLC user, you will be able to
benefit by integrating both BASIC and Ladder Logic in your designs.
23
Another advantage of Ladder Logic is the ability to process input within a
guaranteed slot of time. No matter how complex the circuit becomes,
Ladder Logic is always ready to output when it receives input. This is the
primary reason why it is used for machine control and other automation
fields.
But these things are rarely a problem for programming languages such as
BASIC. BASIC is able to process floating point numbers, data
communications, and other things beyond the scope of what Ladder Logic
can do alone. Another advantage is that its syntax is very similar to the
English language (IF, GOTO, etc…), allowing both beginners and experienced
developers to learn in matter of hours instead of months. BASIC is a very
common programming language, and many developers may be able to start
programming a Cubloc with only a few glances at hardware-specific
commands.
That is why we created "Cubloc," where the user is free to use both L adder
Logic and/or BASIC based on the application being created. After
understanding the advantages of both Ladder Logic and BASIC, the user will
be able to create more efficient final products while saving development
time and reducing cost.
24
Multi-tasking of Ladder Logic and
BASIC
There are many ways to implement both BASIC and Ladder Logic in one
processor. The current products on the market use BASIC as part of Ladder
Logic. These products support BASIC and Ladder Logic but these products
have a few disadvantages.
FUNC #1
The first disadvantage is that when BASIC is executing, the execution time
of Ladder Logic also gets affected. For example, if BASIC creates in an
infinite loop, Ladder Logic will stop. Ladder Logic’s main advantage is that it
can process input in a guaranteed scan time. If Ladder Logic cannot
process within this guaranteed scan time because of BASIC, it might be
better to not use BASIC at all.
The second disadvantage is that BASIC routines can only be started from
Ladder Logic. BASIC is a powerful language and is able to process complex
algorithms in a sequential manner. But if we can only use BASIC as part of
Ladder Logic, we are not utilizing all of its capabilities.
The third disadvantage involves I/O. BASIC's execution of I/O can create
unwanted collisions with Ladder Logic. The reason is that Ladder Logic I/O
is updated once per scan, while BASIC I/O is accessed immediately.
25
Even if you only use BASIC, you will be able to build innumerable
applications. In comparison to many other BASIC processors on the market
today, Cubloc’s BASIC has a faster processing speed and the upper hand on
its main features.
In the case of I/O, the user can specify the I/O used by BASIC and Ladder
Logic, thereby eliminating I/O collision problems.
For example, there is a Master Control feature in Ladder Logic, allowing the
user to set Control Zones. Control Zones are sections within the Ladder
Logic containing portions of the control circuit. With the Master Control
feature, the user can enable/disable Ladder Logic’s Control Zones easily.
M1 MCS 0
P0 P2 P9
In BASIC, the user may read or write to Ladder Logic’s data memory. In the
above example, you can access Register M1 as _M(1) and write to it from
BASIC.
26
Advantages of an On-Chip
PLC/Embedded Computer
Cubloc's greatest advantages is that it is an "On-Chip" PLC. Normally, we
think of a PLC as a block type case with input and output lines. These
modules are usually mounted within yet another case, with external power
supplies, additional output modules, and other wiring requirements.
This is usually fine for one or two applications, but doesn’t lend itself easily
to larger scale production. Cubloc modules can be easily integrated into a
custom product, providing all the features of a PLC yet the professional
appearance and lower manufacturing cost of a custom design.
CUBLOC
CORE MODULE
CUBLOC
CORE MODULE
27
Cubloc is an On-Chip PLC, allowing an easy fit on a PCB. You may use the
PLC almost like an MCU. You can design a customized PCB for your desired
product, reducing its cost and size, and most importantly, making your
product one-of-a-kind.
The following table shows a few differences between a traditional PLC and
"On-Chip" PLC/Micro-computer, Cubloc.
28
Development Environment
Cubloc Studio can be installed on a Windows 7, Vista, XP, 2000, or 98
operating system equipped computer. If you would like to use it in a
Linux/Unix/Macintosh environment, you will need to install a virtual machine
(such as VMware) that allows your computer to run the Windows operating
system. An RS-232 port is also required, or you may use a USB-to-RS232C
converter.
RS232
1 SOUT 1 24 VIN
6 SIN 2 23 VSS
2 Rx ATN 3 22 RES
7 VSS 21 VDD
4
3 Tx P0 5 20 P15
8 P1 6 19 P14
4 DTR P2 7 18 P13
9 P3 8 17 P12
5 GND P4 9 16 P11
P5 10 15 P10
P6 11 14 P9
P7 12 13 P8
29
Hints for Traditional PLC Users
For users with much experience in traditional PLCs, they will find BASIC to
be a completely new language. Cubloc is a PLC with BASIC language
capabilities added. If uncomfortable with BASIC, however, the user may
program using only the Ladder Logic.
Even a Ladder Logic user may be able to incorporate new features into the
final product by making use of BASIC, as BASIC offer additional capabilities
and flexibility for communicating with other devices besides PLCs.
To use Cubloc, the user does not have to know BASIC. If the user does not
require LCD display or keypad usage, he or she does not need to use BASIC
at all, and can resort to using only Ladder Logic.
DISPLAY
KEYPAD
PC INTERFACE
MACHINE CONTROL
We provide many BASIC user interface libraries which you can simply copy
and paste to achieve the user interface structure desired.
30
Hints for Microcontroller Users
Microcontrollers (MCU), such as the PIC, AVR, and the 8051, are self-
contained programmable computers. For mass-production, MCUs can cut
costs and reduce the overall product size. But one disadvantage is that it
can be difficult to learn everything necessary to program an unfamiliar
controller. The hardware, commands, and even programming tools vary
widely between controller families. This can be a drawback for low quantity
or frequently-modified projects.
PC compiler
PC
socket
flash programmer
mcu cubloc
31
Cubloc’s Internal Structure
4) 1) 3) 2)
SRAM FLASH SRAM FLASH
2KB~51KB 200KB 1KB~4KB 200KB
BASIC LADDER
Data Memory Data Memory
5) I/O Ports
The BASIC Interpreter controls a Flash storage area for the user’s BASIC
programs. The Ladder Logic processor also has a Flash storage area for the
user’s Ladder Logic program. I/O ports are shared between BASIC and
Ladder Logic, allowing free access to both.
BASIC data memory can only be accessed by the BASIC Interpreter while
Ladder Logic data memory can be accessed by both the BASIC Interpreter
and the Ladder Logic Processor.
BASIC (1) and Ladder Logic (2) share the same Flash memory. The total
available memory space is 80KB for some models and 200KB for others.
BASIC and Ladder Logic can both use up to the entire memory area, if
needed.
I/O ports (5) can be used both by BASIC and Ladder Logic. The user must
specify I/O ports to use in both languages. All I/O ports can be used in
Ladder Logic or BASIC.
32
Cubloc Peripherals
PROTO BOARD Series
Proto-boards for Cubloc can be used for testing and debugging your future
products before starting PCB artwork or production. These proto-boards all
include basic power and interface circuits.
33
STUDY BOARD
The Study Board is geared for Cubloc first-timers. Connections for simple
experiments including switches, LEDs, RS232 communication, I2C, piezo,
ADC, toggle switches, and LCDs are included. We recommend the Start Kits,
which include a study board, a Cubloc module, all necessary cables, and a
manual.
34
Seven Segment Display Modules (CSG Series)
Seven segment display modules can be easily implemented using Cubloc’s
I2C protocol and native commands.
Cutouch Series
Cutouch is an integration of our graphic LCD, touch panel, and Cubloc core
module. With BASIC, you can control the LCD and touch panel. With
Ladder Logic, I/O ports can be controlled in real-time.
35
MEMO
36
Chapter 2:
Hardware
37
Hardware Features
Cubloc has the following features:
(BASIC and/or Ladder Logic) 80KB or 200KB Flash Memory
BASIC Execution Speed : 36,000 instructions per second
LADDER Execution Speed : 10 millisecond scan time
Data Memory for BASIC: 2KB to 51KB
Data Memory for LADDER: 1KB to 4KB
EEPROM Memory: 4KB
16 to 91 I/O pins (Ports)
8 to 16 10-bit ADC channels
3 to 12 PWM channels (DAC) from 8 to 16bit.
UART (H/W RS232C ports) 2 to 4 channels
RTC chip included (CB290)
38
CB210
The CB210 has 20 digital I/O ports, 6 of which can be used for A/D input.
It can be connected to a PC via USB for downloading and debugging and
serial communication. It can be powered either by a 9V power supply, or
through the USB port.
39
Please be aware of the following when using the CB210.
Use Cubloc Studio V3.3.0 and above. Cubloc Studio can be
downloaded from Comfile Technology’s website at
http://www.ComfileTech.com/. A USB Driver is included in the
download. You must install the USB Driver to use the CB210.
Only PWMs 3,4,and 5 are available. You cannot use PWM 0,1, and
2.
You can use ports 16 to 21 for digital I/O.
You can use ports 16 to 21 for A/D input, but you must first
configure the ports as input ports. For A/D input, you should
supply 3.3V-5V power to AVREF port.
The CB210 cannot handle currents over 300mA.
An LED is connected to port 30.
40
CB220 / CB320
The CB220 and CB320 are a 24 pin wide DIP style packages. They have 16
I/O ports and an internal 5V power regulator. The CB220 and CB320 have 6
spare I/O ports.
41
SIN, SOUT, ATN are RS-232 communication pins used to interface with a PC
for downloading, debugging, monitoring, and serial communication. All
Cubloc models have SOUT, SIN, ATN pins and are connected to a DE-9
connector (PC serial port) as shown below.
1 SOUT 1 24 VIN
6 SIN 23 VS S
2
2 Rx ATN 3 22 RES
7 VSS 21 VDD
4
3 Tx P0 5 20 P1 5
8 P1 19 P1 4
6
4 DTR P2 7 18 P1 3
9 P3 8 17 P1 2
5 GND P4 9 16 P11
P5 10 15 P1 0
P6 11 14 P9
P7 12 13 P8
Other pins are mostly I/O ports. The user may select which ports (pins) to
use as INPUT or OUTPUT. When set to INPUT, the pin enters a high-
impedance state; when set to OUTPUT, the pin either outputs logic-low or
logic-high. The maximum current (source/sink) available from the output
ports is 25mA. The user is free to choose which I/O ports he/she will use
for which purpose (such as ADC, PWM, etc…).
42
Supplying power to the CB220 /
CB320
The CB220 and CB320 have an internal 5V power regulator that accepts a
DC input between 5.5V and 12V.
Method 1
DC5.5~12V
1 SOUT 1 24 VIN
6 SIN 23 VSS
2
2 Rx ATN 3 22 RES
7 VSS 4 21 VDD
3 Tx P0 5 20 P15
8 P1 6 19 P14
4 DTR P2 18 P13
7
9 P3 17 P12
8
5 GND P4 9 16 P11
P5 10 15 P10
P6 11 14 P9
P7 12 13 P8
Method 2
43
CB280 / CB380
The CB280 and CB380 are packages of 64 pins of which 49 can be used for
I/O. The CB280 and CB380 do not have an 5V regulator internal; you must
supply a 5V regulated power source.
44
P24 37 I/O ADC0 : AD Channel 0
P25 38 I/O ADC1 : AD Channel 1
P26 39 I/O ADC2 : AD Channel 2
P27 40 I/O Block 3 ADC3 : AD Channel 3
P28 56 I/O ADC4 : AD Channel 4
P29 55 I/O ADC5 : AD Channel 5
P30 54 I/O ADC6 : AD Channel 6
P31 53 I/O ADC7 : AD Channel 7
P32 57 I/O
P33 58 I/O
P34 59 I/O
P35 60 I/O Block 4
P36 61 I/O
P37 62 I/O
P38 63 I/O
P39 64 I/O
P40 48 I/O
P41 47 I/O
P42 46 I/O
P43 45 I/O Block 5
P44 44 I/O
P45 43 I/O
P46 42 I/O
P47 41 I/O
P48 52 I/O
VDD 17 IN Power, 4.5V to 5.5V
VSS 18 IN GROUND
RES 19 IN RESET Input (LOW signal resets!),
Normally HIGH or OPEN
TX1 33 RS232 Channel 1, +/- 12V Data Output
RX1 34 RS232 Channel 1, +/- 12V Data Input
AVDD 35 ADC Power
TTLTX1 49 RS232 Channel 1, 5V (TTL level) Data
Output
TTLRX1 50 RS232 Channel 1, 5V (TTL level) Data
Input
AVREF 51 ADC Reference Voltage
45
How to supply power to the
CB280 and CB380
The CB280 and CB380 do not have an internal 5V regulator; you must
provide your own 5V power as shown below.
DC5V
1 S OU T 1 17 V DD TX1 33 49 TTLTX 1
6
S IN 2 18 V SS RX 1 34 50 TTLRX 1
2 Rx
7 ATN 3 19 RE S AV DD 35 51 AV RE F
3 Tx N/C 36 52 P 48
V SS 4 20 N/C
8
4 P0 5 21 P 16 P 24 37 53 P 31
DTR
9 P1 6 22 P 17 P 25 38 54 P 30
5 GND P2 7 23 P 18 P 26 39 55 P 29
P3 8 24 P 19 P 27 40 56 P 28
P4 9 25 P 20 P 47 41 57 P 32
P5 10 26 P 21 P 46 42 58 P 33
P6 11 27 P 22 P 45 43 59 P 34
P7 12 28 P 23 P 44 44 60 P 35
P8 13 29 P 15 P 43 45 61 P 36
P9 14 30 P 14 P 42 46 62 P 37
P 10 15 31 P 13 P 41 47 63 P 38
P 11 16 32 P 12 P 40 48 64 P 39
* Pin 20 and 36 are not used, please DO NOT CONNECT anything to these
pins.
46
CB290
The CB290 is a package of 108 pins of which 91 can be used as I/O ports.
It has a battery-backup-capable 28KB of memory and an RTC. The CB290
does not have an internal 5V regulator. Of the 91 I/O ports, 32 ports are
output only, 32 ports are input only, and rest can be set as output or input
as desired by the user program.
47
P16 83 I/O CUNET SCL
P17 84 I/O CUNET SDA
P18 85 I/O INT Channel 2
P19 86 I/O Block 2 INT Channel 3
P20 97 I/O
P21 98 I/O
P22 99 I/O High-speed Counter Channel 0
P23 100 I/O High-speed Counter Channel 1
P24 45 Output
P25 46 Output
P26 47 Output
P27 48 Output Block 3
P28 49 Output
P29 50 Output
P30 51 Output
P31 52 Output
P32 65 Output
P33 66 Output
P34 67 Output
P35 68 Output Block 4
P36 69 Output
P37 70 Output
P38 71 Output
P39 72 Output
P40 53 Output
P41 54 Output
P42 55 Output
P43 56 Output Block 5
P44 57 Output
P45 58 Output
P46 59 Output
P47 60 Output
P48 73 Output
P49 74 Output
P50 75 Output
P51 76 Output Block 6
P52 77 Output
P53 78 Output
P54 79 Output
P55 80 Output
P56 13 Input
P57 14 Input
P58 15 Input
P59 16 Input Block 7
P60 17 Input
P61 18 Input
P62 19 Input
P63 20 Input
48
P64 33 Input
P65 34 Input
P66 35 Input
P67 36 Input Block 8
P68 37 Input
P69 38 Input
P70 39 Input
P71 40 Input
P72 87 Input
P73 88 Input
P74 89 Input
P75 90 Input Block 9
P76 101 Input
P77 102 Input
P78 103 Input
P79 104 Input
P80 91 Input
P81 92 Input
P82 93 Input
P83 94 Input Block 10
P84 105 Input
P85 106 Input
P86 107 Input
P87 108 Input
P88 81 N/C N/C (Do not use this I/O
P89 82 I/O PWM Channel 3
P90 95 I/O Block 11 PWM Channel 4 / INT Channel 0
P91 96 I/O PWM Channel 5 / INT Channel 1
VDD 21,44 IN Power, 4.5V to 5.5V
VSS 22,64 IN GROUND
RES 23 IN RESET Input (LOW signal
resets!), Normally HIGH or OPEN
VBB 24 IN Battery Backup
TX1 41 RS232 Channel 1, +/- 12V Data
Output
RX1 42 RS232 Channel 1, +/- 12V Data
Input
AVDD 43 ADC Power
TTLTX1 61 RS232 Channel 1, 5V (TTL level)
Data Output
TTLRX1 62 RS232 Channel 1, 5V (TTL level)
Data Input
AVREF 63 ADC Reference Voltage
49
The CB290's output-only pins P24 to P55 are in a high impedance
state(High-Z) at power ON. You must use Set OutOnly On to enable the
pins if you wish to use them.
Set Outonly On
This command only works with CB290 Rev. B. The revision number can be
found on the bottom side of the CB290 module.
The Set Outonly command actually toggles virtual Port 88 to enable the
output-only pins. If your program accidentally uses P88, you will see
strange behavior on the output-only pins. Please do not access P88 in your
Basic or Ladder Logic programs.
CB290
S in 2 22 V ss R X1 42 62 T tlR X1
A tn 3 23 R ES AV d d 43 63 AVr ef
V ss 4 24 V BB V dd 44 64 V ss
S S_ P0 5 25 P 8 _A D C0 P 24 45 65 P 32
(Inp ut only) SC K _P 1 6 26 P 9 _A D C1 P 25 46 66 P 33
M O SI _P 2 7 27 P 10 _ AD C 2 P 26 47 67 P 34
M IS O _P 3 8 28 P 11_AD C3 P 27 48 68 P 35
P 16 _ SC L (C U NE T)
P 17 _ SD A(C U NE T)
P4
P W M 0_ P5
9
10
29
30
P 12_ ADC 4
P 13_ ADC 5
P 28
P 29
49
50
69
70
P 36
P 37
Output Only
P W M 1_ P6 11 31 P 14_ ADC 6 P 30 51 71 P 38
P 89 _ PW M 3
P W M 2_ P7 12 32 P 15_ ADC 7 P 31 52 72 P 39
P 19 _ IN T4
P 18 _ IN T3
P 56 13 33 P 64 P 40 53 73 P 48
P 57 14 34 P 65 P 41 54 74 P 49
P 58 15 35 P 66 P 42 55 75 P 50
P 72
P 73
P 74
P 75
P 80
P 81
P 82
P 83
N /C
P 59 16 36 P 67 P 43 56 76 P 51
P 60 17 37 P 68 P 44 57 77 P 52
81
82
83
84
85
86
87
88
89
90
91
92
93
94
P 61 18 38 P 69 P 45 58 78 P 53
P 62 19 39 P 70 P 46 59 79 P 54
P 63 20 40 P 71 P 47 60 80 P 55
95
96
97
98
99
1 00
1 01
1 02
1 03
1 04
1 05
1 06
1 07
1 08
PW M4 _P 90
PW M 5 _ P 91
P 20
P 21
H C N T0 _ P 22
H C N T1 _ P 23
P 76
P 77
P 78
P 79
P 84
P 85
P 86
P 87
0 1 3 4
CB290
7 8 5 6
11 2 9 10
Port Blocks
50
CB400
The CB400 is a package of 108 pins of which 80 can be used as I/O ports. It
has more I/O ports than the CB405, but less memory.
CB400 CB405
I/O 83 64
RAM 7K 110K
(BASIC 6K, LADDER 1K) (BASIC 51K, LADDER 4K)
(HEAP 55K)
FLASH 200KB
RS232 4 CH
A/D 16 CH
PWM 12 CH
51
Block Name Pin I/O Function Explanation
#
P0 5 I/O SPI SS
P1 6 Input SPI SCK Input Only
P2 7 Input SPI MOSI Input Only
0 P3 8 Input SPI MISO Input Only
P4 9 I/O
P5 10 I/O PWM CHANNEL 0
P6 11 I/O PWM CHANNEL 1
P7 12 I/O PWM CHANNEL 2
52
P32 45 I/O AD CHANNEL 8
P33 46 I/O AD CHANNEL 9
P34 47 I/O AD CHANNEL 10
4 P35 48 I/O AD CHANNEL 11
P36 49 I/O AD CHANNEL 12
P37 50 I/O AD CHANNEL 13
P38 51 I/O AD CHANNEL 14
P39 52 I/O AD CHANNEL 15
P48 65 I/O
P49 66 I/O
P50 67 I/O
6 P51 68 I/O PWM Channel 9
P52 69 I/O PWM Channel 10
P53 70 I/O PWM Channel 11
P54 71 I/O
P55 72 I/O
P64 85 I/O
P65 86 I/O
P66 87 I/O
8 P67 88 I/O
P68 89 I/O
P69 90 I/O
P70 91 I/O
P71 92 I/O
53
P72 99 I/O
P73 100 I/O
P74 101 I/O
9 P75 102 I/O
P76 103 I/O
P77 104 I/O
P78 105 I/O
P79 106 I/O
P80 93 I/O
P81 94 I/O
P82 104 I/O
54
CB405
The CB405 is a package of 80 pins of which 64 can be used as I/O ports. It
has a battery-backup-capable 55KB of memory. The CB405 does not have
an internal 5V regulator.
55
The following I/O ports are described in blocks.
56
Block Name Pin I/O Function Explanation
#
P32 45 I/O AD CHANNEL 8
P33 46 I/O AD CHANNEL 9
P34 47 I/O AD CHANNEL 10
4 P35 48 I/O AD CHANNEL 11
P36 49 I/O AD CHANNEL 12
P37 50 I/O AD CHANNEL 13
P38 51 I/O AD CHANNEL 14
P39 52 I/O AD CHANNEL 15
P48 65 I/O
P49 66 I/O
P50 67 I/O
6 P51 68 I/O PWM CHANNEL 9
P52 69 I/O PWM CHANNEL 10
P53 70 I/O PWM CHANNEL 11
P54 71 I/O
P55 72 I/O
57
CB405RT
- CB405 core module
- equipped with a built-in accurate RTC (DS3231)
- equipped with a built-in 16-bit A/D converter (8 channels)
The CB405RT is a product, which adds an RTC and a 16-bit ADC to the
original CB405. The external dimensions and pinout are the same as the
CB405 with the exception that ports P32 to P37 are used for the 16-bit ADC
and therefore cannot be used.
58
The following describes the I/O ports, listed by their port blocks.
P24 33 I/O
P25 34 I/O
P26 35 I/O
3 P27 36 I/O PWM3
P28 37 I/O PWM4 / INT0
P29 38 I/O PWM5 / INT1
P30 39 I/O INT2
P31 40 I/O INT3
59
This part has been changed from the original CB405.
P48 65 I/O
P49 66 I/O
P50 67 I/O
6 P51 68 I/O PWM CHANNEL 9
P52 69 I/O PWM CHANNEL 10
P53 70 I/O PWM CHANNEL 11
P54 71 I/O
P55 72 I/O
60
How to connect a battery to
CB290 / CB405
When a supercapacitor is connected to VBB, the memory can be maintained
anywhere from a couple days to a couple weeks once powered off. The
CB290/CB405 consumes about 15-20mA of current when idling. For a
longer backup period, a battery pack can be used. A protection diode, as
shown below, is necessary when using a battery, as the device normally
attempts to charge a capacitor through VBB. Due to the relatively high
standby current for battery backup, it is recommended to keep the device
powered if possible and only maintain battery backup for short periods in
the case of an emergency.
VBB
CB290
Power Features
Operating Voltage : 4.5V to 5.5V
Operating Clock : 18.432MHz
I/O Port Source Current : 20mA
I/O Port Sink Current : 25mA
Operating Temperature : -40 to 125 Degrees(Celcius)
Maintenance Temperature: -60 to 140 Degrees(Celcius)
Operating Humidity : 5 to 95% RH
(Keep the board's surface dry when in use)
Additional Information
If a Cubloc module is supplied with power above the recommended voltage,
the device can be destroyed. Also please be careful to avoid electrostatic
discharge, as it too can destroy the device. Please be aware that P1 is an
input-only pin. To reduce accidental power drain, please set unused pins to
input. All I/O ports are set to input as default at power on. When not using
SIN, SOUT, and ATN, please do not connect them to anything.
61
Dimensions
CB220
30mm (1181mil)
62
54mm (2126 mil)
6mm
(236m il ) CB290/400/405 14mm
(551m il )
8mm
(315m il)
63
Please refer to the diagram below for PCB design. The numbers are offsets
based on location 0, 0 (from the top left corner of the module’s internal
PCB; not the external plastic case).
X:150 X:2100
Y:1600 Y:1600
CB290
CB400
X:575
Y:150
X:0
Y:0
Unit : 1/1000 Inch (Mil)
X:150 X:2100
Y:1600 Y:1600
CB405
X:0
Y:0
Unit : 1/1000 Inch (Mil)
64
CB210
65
MEMO
66
Chapter 3:
Cubloc Studio
67
About Cubloc Studio
After installing Cubloc Studio and executing it, you will see the following
screen.
You will see that, at first, Cubloc Studio will default to the BASIC editor.
If you press F2, the screen will change to the Ladder Logic editor and if you
press F1, it will switch back to the BASIC editor.
68
Source files are saved as two files under the file extensions .CUL and .CUB.
If you need to backup or move source files, you must keep BOTH of these
files together.
When opening a file, you will only see .CUL files. (.CUB files are not
displayed, but they are in the same folder). When you open a .CUL file,
Cubloc Studio automatically opens CUB file.
The source code can only be saved on the PC. Source code downloaded to
the Cubloc module can not be uploaded back to the PC.
69
Creating BASIC Code
You can create BASIC code as shown below. Cubloc Studio's text editor is
similar to most text editors, and performs syntax highlighting of certain
commands.
Shortcut Explanation
CTRL-Z UNDO
CTRL-O OPEN
CTRL-S SAVE
CTRL-C COPY
CTRL-X CUT
CTRL-V PASTE
CTRL-F FIND
CTRL-HOME Go to the very beginning
CTRL-END Go to the very end
CTRL-Y REDO
70
Debugging
71
Menus
File Menu
Menu Explanation
New Create new file.
Open Open file.
Ladder Import Import Ladder Logic part of a Cubloc program.
Save Save current file.
Save As Save current file under a different name.
Save Object Save current program as an object file. Use this to
protect your source code. An object file is a strictly
binary format file so others cannot easily reverse
engineer it. You can use "Download from Object File" to
download an object file to Cubloc.
Print Ladder Print Ladder Logic section only.
Print BASIC Print BASIC section only.
Print Setup Setup printer for printing Ladder Logic editor.
Download from Object Download an object file to the Cubloc module.
file
BMP download for Download Bitmap to CT1721C
72
CT1721C
Touch calibration for Calibrate CT1721C's Touch-screen
CT1721C
BMP download for Download Bitmap to CT1820
CT1820
Touch calibration for Calibrate CT1820's Touch-screen
CT1721C
Store Current Time to Synchronize this PC's current time with the CT1820's
CT1820 RTC RTC
Last 4 Files Edited View last 4 files edited.
Exit Exit Cubloc Studio
Device Menu
If a Const Device statement does not exist in your source code, you can
use the "Device" menu can create a Const Device statement at the very
beginning of your source code. If a Const Device statement already
exists, using the "Device" menu will simply replace the Const Device
statement.
Run Menu
Menu Explanation
Download & Run Compile BASIC and Ladder Logic, download to
Cubloc module if there are no errors, and restart
the program automatically. To disable automatic
restart, please go to Setup->Studio Option to
change.
Ladder Logic Run Continue execution of Ladder Logic while monitoring
Ladder Logic Stop Stop execution of Ladder Logic while monitoring
Reset Reset the Cubloc module.
Ladder Monitor on Start Ladder Logic monitoring
BASIC Debug Terminal Open BASIC Debug Terminal Window.
This window opens automatically when there’s a
Debug statement in the source code.
Clear Cubloc’s Flash Clear Cubloc’s Flash Memory.
73
Memory
Write enable fuse off This will turn off the download function for a Cubloc
Core module to protect against noisy environments
where the flash memory can be affected. Once you
choose this menu, you will be unable to download
new programs to your Cubloc module. You will be
able to download again after a Firmware Download.
Run with Firmware View relays in use by Ladder Logic after
download compilation.
View Relay Usage See, at a glance, the status of relays in a separate
window.
View Watch window See, at a glance, the status of relays in a separate
window.
Check Syntax Check the source code's syntax
Setup Menu
Menu Explanation
PLC Setup Wizard Wizard to automatically generate BASIC code.
PC Interface Setup Setup the RS232 COM PORT for Download/Monitor.
Select COM1 through COM4.
Editor Environment Configuration options for the BASIC editor.
Setup
Ladder Logic Configuration options for Ladder Logic.
Environment Options
Use English menu Change the language of the menus to English
Firmware Download Download Firmware to Cubloc. Please use this to
download firmware to Cubloc manually.
74
Chapter 4:
Cubloc BASIC
Language
IMPORTANT
The device being used must be declared before using BASIC or Ladder
Logic. Below is an example of declaring the Cubloc CB220 module.
This should be the first line of a program. When this command is not
used, the CB220 model will be assumed.
75
Cubloc BASIC Features
Cubloc BASIC supports functions and subroutines.
The user is able to create subroutines and functions to organize their
programs. Using subroutines and functions allows the user to reuse code,
and organize programs for better readability.
76
Simple BASIC program
Below is an example of a simple BASIC program with a Do…Loop statement.
Dim A As Byte
Do
ByteOut 0, A
A=A+1
Loop
This program outputs the increasing binary value of A to Ports P0-P7. The
next program uses a function to accomplish the same task:
Dim A As Byte
Do
ByteOut 0, A
A=ADD_VALUE(A)
Loop
End
By placing A=A+1 in a function, the user will be able to separate one big
program into small manageable pieces. As you can see here, the main
program ends at the End command, and functions are added afterwards.
MAIN P ROGR A M
E nd
S UB
S ub Program
FUN C TION
S UB
77
Sub and Function
For subroutines, you can either use Sub or Function. Function can return
a value, but Sub can't.
To return values using Function, simply store the final value as the name of
the function as shown below:
DEMO PROGRAM
78
Global and Local Variables
Main Program
Global Variable
Sub DELAYTIME()
Dim K As Integer ' Declare K as Local Variable
For K=0 To 10
Next
End Sub
79
Calling Subroutines
Once the subroutine is created, they can be used like any other statement.
For a subroutine, you do not need parenthesis around the parameters. Use
commas to separate multiple parameters.
Dim K As Integer
K = SUMAB(100,200) ' Call subroutine and store return value in K
Debug Dec K,cr
End
80
Subroutine Position
Subroutines must be created after the main program. To do this, simply put
End at the end of your main program as shown below. End is only required
if you have subroutines
Dim A As Integer
LOOP1:
A = A + 1
Debug DP(A),CR
DELAYTIME
Goto Loop1
Sub DELAYTIME()
Dim K As Integer
For K=0 To 10
Next
End Sub
Subroutines and functions are created after the End statement. Gosub
subroutines must be within the main program like shown below:
Dim A As Integer
:
:
Gosub ABC
:
ABC:
:
End
81
Subroutine Parameters and Return Values
Functions may use any data type, except arrays, as parameters and return
values:
Function ABC(A AS String * 12) as String *12 ' Return String value
End Function
Dim A As Integer
Dim K As Integer
A = 100
K = ADDATEN(A)
Debug Dec? A, Dec? K,CR ' A is 100 and K is 110
End
82
In contrast, some languages allow values to be passed by reference in
which the actual memory address is passed to the subroutine. Cubloc
BASIC only supports passing by value.
ST = "COMFILE TECHNOLOGY"
ST = "COMFILE _
TECHNOLOGY"
Comments
Use an apostrophe/single-quote (') to add comments. Comments are
discarded during at compile time, and will not consume any program
memory.
Nested Subroutines
Nested subroutines are supported in Cubloc.
Colons
Colons cannot be used to append commands in Cubloc BASIC, as is possible
in some other languages.
83
Variables
There are 5 types of variables in Cubloc BASIC.
BYTE
WORD
LONG
84
DEMO PROGRAM
85
String
A String can be a maximum of 127 bytes in size. When the size is not
declared, a default size of 64 bytes will be assumed.
Dim ST As String * 14
ST = "COMFILE TECHNOLOGY" ' "LOGY" is not stored
ST = "COMFILE " TECHNOLOGY" ' (") cannot be used inside the String.
ST = "COMFILE ' TECHNOLOGY" ' (') cannot be used inside the String.
ST = "COMFILE , TECHNOLOGY" ' (,) cannot be used inside the String.
86
Example for printing to an LCD:
DEMO PROGRAM
87
Concatenate Multiple Strings
Dim a1 As String * 30
Dim a2 As String * 30
a1 = "Comfile "
a2 = "Technology "
a1 = a1 + a2 + ",Inc"
Debug a1,cr
The above program will show "Comfile Technology, Inc" on the debug
screen.
DEMO PROGRAM
88
How to Access Individual Characters within a String
You can treat strings as a Byte array. Simply append "_A" after the name
of your string variable as shown below:
Dim ST1 As String * 12 ' ST1_A Array is created at the same time.
ST1 = "123"
ST1_A(0) = ASC("A") ' Store A in the first character of ST1.
When you declare Dim St1 as String * 12, St1_A(12) is also declared
automatically by the Real-Time Operating System (RTOS). The string and
the array use the same memory space. Whether you use the string or the
array, you are still accessing same memory location.
89
About Variable Memory Space
In the CB220 and CB280, 2KB (2048 bytes) of data memory is available.
However, the memory is not dedicated entirely to variables. Some data
memory is reserved for use by peripherals like the DISPLAY and the RS232
buffers. An additional 80 bytes are used for the Debug statement.
When the user creates buffers with SET DISPLAY or OPENCOM, data memory
will be consumed to allocate buffers.
Initializing Memory
In Cubloc BASIC, data memory is not cleared when powered on. The user
should initialize variables or use the RamClear statement to initialize all data
memory to 0. If data memory is not cleared, the values in memory could
potentially be anything. If the contents of memory are to be predictable,
you must either initialize each variable, or use the Ramclear statement.
In the case of battery backed-up modules, the variables will remember their
values after a power cycle (powering off and on).
90
Arrays
Cubloc BASIC supports arrays of up to 8 dimensions. Each dimension can
contain up to 65,535 items.
A(6)
A(3,6)
A(3,3,6)
Be sure to make note of how much memory is used when using multi-
dimensional arrays.
91
Bit and Byte Modifiers
A variable’s bits and bytes can be accessed individually by using the
commands shown below:
Dim A As Integer
A.LowByte = &H12 ' Store &H12 at A’s lowest byte
Bit
LOWBIT Variable’s bit 0
BIT0 to 31 Variable’s bit 0 through 31
T7
T6
T5
4
T3
T2
T1
T0
T3
T3
T2
T2
T2
T2
T2
T2
IT
BI
BI
BI
BI
BI
BI
BI
BI
LONG
LOWBIT
Nibble
A nibble is 4 bits. The user can access individual nibbles to make
processing certain data formats easier.
LONG
92
Byte
To access specific bytes of a variable, the following can be used.
LOWBYTE
Word
To specify a certain Word of a variable, the following can be used:
(A Word is 16 bits)
LOWWORD
93
DEMO PROGRAM
94
Constants
Constants can be used to declare a constant value (a value that cannot be
changed) within the program. This essentially allows a number to be
assigned a name, often improving readability and debugging of the source
code.
When the constant is not given a type, the compiler will find an appropriate
type for it as shown below:
95
Constant Arrays...
In constant arrays, the user is able to store a list of values before the
program begins. A program requiring a large number of constant values
can be simplified as shown below:
Const Byte DATA1 = (31, 25, 102, 34, 1, 0, 0, 0, 0, 0, 65, 64, 34)
I = 0
A = DATA1(I) ' Store 31 in A.
I = I + 1
A = DATA1(I) ' Store 25 in A.
Const Byte DATA1 = ("CUBLOC SYSTEMS")
String data can be stored in Byte constant arrays. Each Byte contains the
ASCII code of each character in the String. In the example above, If
DATA1(0) is read, the ASCII code of 'C’ is returned. Likewise if DATA1(1) is
read, ASCII code of 'U’ is returned.
Integer and floating point (Single) numbers can also be stored in arrays
as shown below:
Const Byte DATA1 = (31, 25, 102, 34, 1, 0, 0, 0, 0, 0, 65, 64, 34,
12, 123, 94, 200, 0, 123, 44, 39, 120, 239,
132, 13, 34, 20, 101, 123, 44, 39, 12, 39)
2)
Const Byte DATA2 = (31, 25, 102, 34, 1, 0, 65, 64, 34_
, 101, 123, 44, 39, 12, 39)
96
Please make note of the following differences between arrays and constant
arrays.
DEMO PROGRAM
97
Operators
When evaluating expressions in a programming language, it is important to
understand the order of operations, also know as operator precedence. The
following table describes Cubloc BASIC's operator precedence.
Within each row in the table above, the operators are evaluated from left to
right. Operators can be used in conditional statement as show below.
98
When multiple operators are used, the expression will be evaluated in the
following order:
99
When numbers of different types are mixed in an expression, the final
result is cast to the type of the assigned variable.
Dim F1 As Single
Dim A As Long
F1 = 1.1234
A = F1 * 3.14 ' A gets 3 even though result is 3.525456.
The And, Xor, and Or operators are used for both logical and bit-wise
operations.
If A=1 And B=1 Then C=1 ' if A=1 and B=1 …(Logical Operation)
If A=1 Or B=1 Then C=1 ' if A=1 or B=1…(Logical Operation)
100
Strings can be compared with the = operator.
When comparing Strings, Cubloc BASIC compares the ASCII value of each
character in the String. Therefore, String comparisons are case-
sensitive; "COMFILE" does not equal "comfile".
DEMO PROGRAM
101
Expressing Numbers
There are three possible ways to represent numbers in Cubloc BASIC:
binary, decimal and hexadecimal. The binary and hexadecimal
representations are useful when interfacing to digital devices. The decimal
representation is the standard human readable format.
Examples:
102
The BASIC Preprocessor
The BASIC preprocessor is a macro processor that is used automatically by
the compiler to transform your program before compilation. It is called a
macro processor because it allows you to define macros, which are brief
abbreviations for longer constructs.
#include "filename"
Reuse code by including source files. For files in the same directory as the
current source file, you can write the following:
#include "MYLIB.cub"
For files in other directories, you will need to include the full path name as
shown here:
#include "c:\mysource\CUBLOC\lib\mylib.cub"
Using include files, you can store all of your common subroutines in a
separate file. In this case, please make sure to #include the subroutine
file at the very end of your program, after the End statement.
#define motorport 4
Low motorport
In the example above, motorport will be compiled as 4. You can also use
CONST for similar tasks. However, CONST will use data memory; #define will
only use program memory.
CONST motorport = 4
Low motorport
NOTE
The "name" argument in a #define statement is not case-sensitive. For
example, #define ALPHA 0 and #define alpha 0 do not define different
constants. They both define the same constant.
DEMO PROGRAM
104
Conditional Directives
A conditional directive is a directive that instructs the preprocessor to select
whether or not to include a part of code before compilation. Preprocessor
conditional directives can test arithmetic expressions, or whether a name is
defined as a macro.
#if constant
#endif
The preprocessor directive #if will compare a constant declared with CONST
to another constant. If the #if statement is true, the statements inside
the #if…#endif block will be compiled, otherwise the statements will be
discarded.
Delay 500
105
Using the preprocessor directive #elseif or #else, you can create more
complex #if…#endif expressions.
Delay 500
#else may only be used ONCE in an #if expression. Also, you can only
compare constants declared with the CONST statement in a #if directive.
#ifdef name
#endif
You can use #ifdef to check if a constant was previously defined with a
#define directive or CONST statment. If the constant has been previously
defined, the statements inside the #if…#endif block will be compiled,
otherwise they will be discarded.
#define LOWMODEL 0
#ifdef LOWMODEL
Low 0
#endif
#ifdef LOWMODEL
Low 0
#elseifdef HIGHMODEL
High 0
#else
Low 1
#endif
106
#ifndef name
#endif
#ifndef is the opposite of the #ifdef directive. If a constant has not been
defined, the statements inside a #ifndef…#endif block will be compiled,
otherwise the statements will be discarded.
#define LOWMODEL 0
#ifndef LOWMODEL
Low 0
#endif
#ifndef LOWMODEL
Low 0
#elseifndef HIGHMODEL
High 0
#else
Low 1
#endif
#if MODELNO = 0
Low 0
#elseifdef HIGHMODEL
High 0
#else
Low 1
#endif
Nested #if directives are not supported; #if may not be used inside
another #if.
107
To Use Only Ladder Logic
If you do not need to use BASIC, you can program in Ladder Logic alone.
But you will need a few lines of BASIC to get started, as shown below:
Device model, aliases, and pin I/O mode must be set in BASIC. Ladder
Logic must be started in BASIC with the Set Ladder On statement.
Set Ladder On ' If using only BASIC, don't use this statement
Ladderscan ' Or this one
108
Interrupts
Interrupts are external events that stop a program's normal flow
(interrupting the program) and immediately execute some other subroutine,
called an interrupt service routine (ISR). The On…GoSub statement can be
used to tell a program to listen for an interrupt and execute an ISR. When
the interrupt occurs, the main program stops execution and jumps to the
ISR designated by the ON…GOSUB statement. Once the ISR is finished, the
Return statement is used to return execution back to the main program.
INTERRUPT
ROU TINE
MAIN PR OGRAM
External key input can occur at any time and data can be received via RS-
232 at any time. Since the main program cannot wait forever to receive
these inputs, we need interrupts. If a key is pressed or serial data is
received while the main program is running, an interrupt occurs and the
main program jumps to the appropriate ISR.
109
More about Interrupts…
The Cubloc and Cutouch have a Real-Time Operating System (RTOS) which
controls interrupt events. This is slightly different from the microcontroller’s
hardware interrupts.
2. When interrupt B occurs while executing interrupt A's ISR, interrupt B's
ISR will be executed immediately and execution will return to interrupt A's
ISR to finish.
5. In the case of On Recv, data received during an On Recv ISR will simply
be stored in the serial port's receive buffer. Therefore the data will not be
lost. After the current On Recv ISR is finished, if there’s new data in the
receive buffer, another On Recv interrupt will be immediately generated.
The BClr statement can be used if you do not want to process another On
Recv interrupt.
6. If you declare an interrupt more than once, only the last one declared will
be in effect.
IMPORTANT!!!!!
Please pay attention when creating the interrupt service routine (ISR). It
must require less time to execute than the interval itself. If interval is set
to 10ms, the ISR must execute within 10 ms. Otherwise, collisions can
occur.
The Cubloc does not handle hardware interrupts directly. When an interrupt
occurs, the handler jumps to the BASIC interrupter. Because the BASIC
interpreter is slower than directly processed machine code, extra latency
110
can be added to the interrupt service routine and prevent the main routine
from operating smoothly.
If you need a device that can handle interrupts with speed and
ease, please consider the Moacon. The Moacon is an ARM based
modular industrial controller from Comfile Technology designed for high
speed, interrupt intensive applications.
111
Pointers using Peek, Poke, and
MemAdr
The following is an example that uses the EEWrite statement and the
EERead statement to read and write floating point data:
When you run this program, the debug window will show 3.00000 instead of
3.14. The reason is that the EEWrite statement automatically converts
floating point values to whole numbers.
In order to store floating point values, we can use Peek and Poke to read
the data directly:
We use MemAdr(F1) to find the memory address of F1 and then use the
Peek statement to directly access the memory and write 4 bytes. We store
that value in the EEPROM. Next, we use MemAdr(F2) and Poke to read 4
bytes directly.
112
Sharing Data
The Cubloc has separate BASIC and Ladder Logic data memory areas.
Variable A P
Variable B
Variable C M
Variable D
Variable E
C
Variable F T
D
Ladder Logic data memory can be accessed from BASIC easily by using
system variables. Using these system variables, data can be easily read
from or written to Ladder Logic data memory.
_D(0) = 1234
_D(1) = 3456
_D(2) = 100
For I = 0 TO 99
_M(I) = 0
Next
IF _P(3) = 1 Then _M(127) = 1
Accessing BASIC variables from Ladder Logic is not possible, but you can
use Ladder interrupts to request a BASIC routine to change a Ladder Logic
variable.
113
Use Ladder Logic pins in BASIC using the Alias
Command
The Alias command can be used to set aliases for Ladder Logic registers
(except the D register). Aliases, although declared in BASIC, can only be
used in Ladder Logic.
UsePin 0,In,START
UsePin 1,Out,RELAY
Alias M0 = MOTORSTATE
Alias M1 = RELAY1STATE
Alias T1 = SUBTIMER
114
Chapter 5:
Cubloc BASIC
Functions
115
Math Functions
Sin, Cos, Tan
Return sine, cos ine, and tangent values. Cubloc uses radians as units.
Use a Single for most precise results.
A=Sin B ' Return the sine of B.
A=Cos B ' Return the cosine of B.
A=Tan B ' Return the tangent of B.
116
Abs Return the absolute value of a Long.
Dim A As Long, B As Long
B = -1234
A=Abs B ' Return |B|.
Debug Dec A ' Print 1234
117
Type Conversion
Type conversion can be used to convert the variable to the desired
representation.
Hex
Converts the variable to a string representation of a hexadecimal value (16
bit). Hex8 means to convert to 8 decimal places. (1 to 8 can be used for
decimal places)
118
Dec
Converts an integer variable to a string representation of a decimal (10 bit).
Dec8 means to convert to 8 decimal places. (1 to 11 can be used for
decimal places)
?
Include the name of the variable by using question mark (?). This question
mark can only be used with HEX or DEC.
119
Float
Use Float to convert floating point values to String.
Dim ST As String * 15
ST = Float F1 ' First store in a String.
ST = Left(ST,3) ' Convert to 3 decimal places
Debug ST ' Print "3.14".
You can also store a value in String before printing with Debug statements
or displaying to the LCD as shown below.
120
String Functions
String functions are provided to assist the user in accessing and modifying
data within a String.
Dim A as Integer
Debug DP(A,10,0) ' Convert A into decimal String representation.
' Set display decimalPlaces to 10.
' If A is 1234, " 1234" will be displayed.
' (notice the 6 blank spaces)
121
HP(value, numberOfDigits, zeroPrint)
HP converts a variable into a hexadecimal string representation. If
ZeroPrint is set to 1, zeroes are substituted for blank spaces.
122
FP (value, wholeNumberDigits,
fractionalNumberDigits)
FP converts floating point variables into a formatted string with user defined
whole and fractional number of digits.
Dim A as Single
A = 3.14
Debug Float A ' 3.1400000 Prints all digits.
Debug FP(A,3,2) ' 3.14 Print user defined digits.
With the FP function, the user can control the number of digits to be used
for string data when using Debug or displaying to an LCD.
123
Left(stringValue, numberOfCharacters)
Returns a specified number of characters from the left side of a String
Right(stringValue, numberOfCharacters)
Returns a specified number of characters from the right side of a String.
124
Len(stringValue)
Return the length of the given String.
String(asciiCode, length)
Creates a String of a specified length with the given ASCII code repeated
for the length of the String.
SPC(numberOfSpaces)
Create a specified amount of blank space
125
LTrim(stringValue)
Removes all blank spaces from the left side of the given String.
RTrim(stringValue)
Removes all blank spaces from the right side of the given String.
126
Val(stringValue)
Parses a given String into its equivalent decimal value.
ValSng(stringValue)
Parses a given String into its equivalent floating point value.
ValHex(String variable)
Parses a given String in hexadecimal format into its numeric equivalent.
127
Chr(asciiCode)
Return the character represented by the given ASCII code.
ASC(stringValue)
Return ASCII code of the first character in the given String.
Caution:
A variable must be used when using string functions.
128
Chapter 6:
Cubloc BASIC
Statements &
Library
129
AdIn( )
variable = AdIn (channel)
variable : Variable to store results (No String or Single)
channel : AD Channel Number (not I/O Pin Number)
Cubloc has several 10bit Analog to Digital Converters (ADCs) and 16bit
Pulse Width Modulators (PWMs). The user can use an ADC to convert
analog signals to digital, or use a PWM to convert digital signals to analog.
The ADin command reads the analog signal's amplitude and stores the
result in a variable. Depending on the model, the number of ADC ports may
vary. For the CB280, there are 8 AD ports (P24 to P31). An ADC port must
be set to input before use.
Return value
1023
0
0V 5V Input voltage
Dim A As Integer
Input 24 ' Set port 24 to input.
A=AdIn(0) ' Do a A/D conversion on channel 0 and
' store result in A
130
The CB220 and CB280 ADC ports are shown below:
CB280
CB220 S OUT 1 17 V DD TX1 33 49 T TLTX 1
SOUT 1 24 VIN SIN 2 18 V SS RX1 34 50 T TLRX1
ATN 3 19 RES AVDD 35 51 AVREF
SIN 2 23 VSS
VSS 4 20 N/C N/C 36 52 P 48
ATN 3 22 RES
P0 5 21 P 16 P24 37 53 P 31
VSS 4 21 VDD P25 38 54 P 30 AD INPUT
P1 6 22 P 17
P0 5 20 P15 P2 7 23 P 18 P26 39 55 P 29
P27 40 56 P 28
PORT
P1 6 19 P14 P3 8 24 P 19
P47 41 57 P 32
P2 7 18 P13 P4 9 25 P 20
P5 10 26 P 21 P46 42 58 P 33
AD INPUT P3 8 17 P12
P45 43 59 P 34
P6 11 27 P 22
P4 9 16 P11
PORT P7 12 28 P 23 P44 44 60 P 35
P5 10 15 P10 P8 13 29 P 15 P43 45 61 P 36
P6 11 14 P9 P9 14 30 P 14 P42 46 62 P 37
P7 13 P8 P41 47 63 P 38
12 P 10 15 31 P 13
P 11 16 32 P 12 P40 48 64 P 39
The ADIn statement does a single conversion per call. TADIn is a macro
that returns the average of 10 conversions, giving the user more precise
results. If you need more precision rather than speed, we recommend the
using TADIn instead of ADIn. It is also possible to create your own
averaging or filtering code for better precision.
131
Alias
Alias registerName = aliasName
registerName : Register name such as P0, M0, T0 (Do not use D registers)
aliasName : An Alias for the register chosen (up to 32 characters)
Alias M0 = Rstate
Alias M0 = Kstate
Alias P0 = StartSw
132
Bcd2Bin
variable = Bcd2Bin( bcdValue)
variable : Variable to store results (Returns LONG)
bcdValue : BCD value to convert to binary
3451
0000 1101 0111 1011
0 D 7 B
The following is 3451 converted to BCD code. As you can see, each 4 bits
represent one of the digits.
3451
0011 0100 0101 0001
3 4 5 1
This command is useful when the user needs to convert a variable for a
device such as a 7 segment display, or a real-time clock.
Dim A As Integer
A=Bcd2Bin(&h1234)
Debug Dec A ' Print 1234
133
BClr
BClr channel, buffertype
channel : RS232 Channel (0 to 3)
buffertype : 0=Receive, 1=Send, 2=Both
Bclr clears the specified RS-232 channel’s input buffer, output buffer, or
both buffers. Use this statement if your code is about to receive data and
there may be unneeded data already in the buffer.
134
Beep
Beep port, length
port : Port number (0 to 255)
length : Pulse output period (1 to 65535)
P IE Z O
135
Bfree( )
variable = Bfree(channel, bufferType)
variable : Variable to store results (No String or Single)
channel : RS232 Channel number (0 to 3)
bufferType: 0=Receive Buffer, 1=Send Buffer
This function will return the number of free bytes in a receive or send buffer.
When sending data, this command can be used to avoid overflowing the
buffer. When receiving data, this command can help the program wait for a
specified amount of data to be received before taking action.
Dim A As Byte
OpenCom 1,19200,0, 100, 50
If BFree(1,1)>10 Then
Put "TECHNOLOGY"
End If
If the size of the buffer is set to 50, up to 49 bytes can be returned. When
the buffer is empty, BFree will return 1 less than the buffer size.
136
Bin2Bcd
variable = Bin2Bcd( binValue)
variable : Variable to store results (Returns Long)
binValue : Binary value to be converted
i = 123456
j = bin2Bcd(i)
Debug Hex j ' Print 123456
137
BLen( )
variable = BLen(channel, bufferType)
variable : Variable to store results (No String or Single)
channel : RS-232 channel number (0 to 3)
bufferType: 0=receive buffer, 1=send buffer
BLen returns the current number of bytes in the specified RS-232 channel’s
buffer. If the buffer is empty, 0 will be returned. When receiving data, this
function can be used to check how much data has been received before
using Get or GetStr to read the data received.
If the receive buffer is full, it will not be able to receive any more data. To
avoid this, use receive interrupts (See On Recv) or increase the buffer size.
Dim A As Byte
OpenCom 1,19200,0,100,50
On Recv DATARECV_RTN ' When data is received through
' RS232, jump to DATARECV_RTN
Do
Loop ' infinite loop
DATARECV_RTN:
If BLen(1,0) > 0 Then ' If there is at least 1 byte...
A = Get(1) ' Read 1 Byte
End If
Return ' End Interrupt routine
138
ByteIn( )
variable = ByteIn(portBlock)
variable : Variable to store results (No String or Single)
portBlock : I/O Port Block Number (0 to 15)
ByteIn reads from an I/O port block (a group of 8 I/O ports). Ports 0 to 7
are block 0 and ports 8 to 15 are block 1. The port block numbers vary for
the different models of Cubloc. When using this command, all I/O ports
within the port block have their I/O mode set to input and their input is
stored in a variable.
Dim A As Byte
A = ByteIn(0) ' Read from Port Block 0 and store in variable A.
The CB220 and CB280 port block groupings are shown below. Please refer
to the pin/port tables for the specific Cubloc module you are using.
SOUT 1 24 VIN
SI N 2 23 VSS
ATN 3 22 RES
VSS 4 21 VDD
P0 5 20 P15
P1 6 19 P14
P2 7 18 P13
BLOCK 0 P3 8 17 P12 BLOCK 1
P4 9 16 P11
P5 10 15 P10
P6 11 14 P9
P7 12 13 P8
139
ByteOut
ByteOut portBlock, value
portBlock : I/O Port Block Number (0 to 15).
value : Value to be output (0 to 255).
NOTE: I/O Port 1 only supports input. Therefore, ByteOut 0 will not set
Port 1 to Output.
140
CheckBf( )
variable = CheckBf(channel)
variable : Variable to store results (No String or Single)
channel : RS232 Channel (0 to 3)
The command CheckBf() can be used to check the current data in the
receive buffer without modification. Unlike Get, it will not erase the data
after reading. Only 1 byte can be read at a time.
141
Compare
Compare channel, target#, port, targetState
channel : High-speed counter channel
target# : Target # of pulses (CH0: 0 to 65535, CH1: 0 to 255)
port : Output port (DO NOT USE input-only ports)
targetState : Target output port state
Dim i As Integer
Set Count0 On
Compare 0,10,61,1
Do
i = Count(0)
Debug Goxy,0,0,dec4 i,Cr
Delay 100
Loop
142
Count( )
variable = Count(channel)
variable : Variable to store results. (No String or Single)
channel : Counter channel number (0 to 1)
Return the counted value from the specified counter channel. Please set the
counter input ports to input before use of this command (refer to the
pin/port table for the appropriate Cubloc module). Data types up to 32 bits
in size can be counted (Byte, Integer, and Long). The maximum pulse
frequency is 2MHz.
The Cubloc has 2 counter inputs. Counter channel 0 uses the same
resources as PWM0 through PWM2, so you cannot use both at the same
time. However, counter channel 1 can be used while PWM channel 0 is
running. The Set Count0 On command must be executed before using
counter channel 1. Counter channel 1 requires no additional settings.
SOUT 1 24 VIN
SIN 2 23 VSS
ATN 3 22 RES
VSS 4 21 VDD
P0 5 20 P15 COUNT 1
P1 6 19 P14 COUNT 0
P2 7 18 P13
P3 8 17 P12
P4 9 16 P11
P5 10 15 P10
P6 11 14 P9
P7 12 13 P8
Dim R As Integer
Input 15 ' Set port 15 as input. (Counter Channel 1)
R = Count(1) ' Read current counter value.
143
As illustrated below, counter 0 uses the same resources as PWM0 through
PWM2, so please be careful not to use both at the same time.
COUNTER 0
PWM0
TIMER A PWM1
PWM2
PWM3
TIMER B PWM4
PWM5
'
' Measure frequency from pulse output PWM 0 channel
'
Const Device = CB280
Dim A as Integer
Input 15
Low 5
Freqout 0,2000
Low 0
On Timer(100) Gosub GetFreq
Do
Loop
GetFreq:
A = Count(1)
Debug goxy,10,2
Debug dec5 A
Countreset 1
Reverse 0
Return
144
CountReset
CountReset channel
channel : Counter channel (0 to 1)
145
Dcd
variable = Dcd source
variable : Variable to store results. (No String or Single)
source : source value
Dcd is the opposite of Ncd. It returns the bit position (counting from one) of
the most significant bit that is a 1.
146
Debug
Debug data
data : data to send to PC
Cubloc supports RS-232 debugging with the Debug command. The user can
insert Debug commands as desired within a program. The result of the
Debug command is displayed on the Debug Terminal, which will
automatically appear after the program is downloaded from Cubloc Studio.
Dim A AS Integer
A = 123
Debug Dec A
Use Dec or Hex to convert numbers to strings for the Debug command. If
you do not use Dec or Hex, numbers will be printed as raw ASCII, usually
providing no useful output.
If you insert a question mark (?) before Dec or Hex, the variable’s name will
be printed before the value.
147
You can also specify the number of characters to print.
Debug Hex8 A
The HEX command will accept 1 through 8. HEX8 will print as an 8 digit
hexadecimal number. The DEC command will accept 1 through 10.
The Debug command is useful for printing strings and numbers in a user-
friendly format. During the execution of a Cubloc BASIC program, when the
Debug command is encountered, the resulting values are immediately
displayed on the debug terminal.
148
If you insert a Debug statement into a program and the debug terminal
displays output, it verifies that the program has executed to that point. By
using Debug commands, you will be able to detect the location of bugs in
your program, and monitor variables changes in real time.
You can send data to the download port of Cubloc by entering text in the
upper text box of the debug terminal. This is useful for interactive
communication with the Cubloc.
Warning
The Debug command may not be used while monitoring in Ladder Logic.
Likewise, Ladder Logic monitoring can not be used while debugging using
Debug statements.
The following is a chart of commands that can be used with the Debug
command. You can control the debug terminal's output like an output
screen or LCD.
Debug Goxy,5,5,Dec I
Debug Clr,"TEST PROGRAM"
149
Decr
Decr variable
variable : Variable to decrement. (No String or Single)
Decrements variable by 1.
150
Delay
Delay time
time : Time interval (variable or constant up to Long type value)
Delay delays program execution for the specified time in milliseconds. The
Delay command is best used for small amounts of time. We recommend
not using it for time measurements and other time-critical applications, as
the actual delay time can vary depending on other running tasks.
151
Do...Loop
Do...Loop will infinitely loop the enclosed commands unless Do While or Do
Until is used to conditionally terminate the loop. An Exit Do statement can
also be used within the DO...LOOP to exit from the loop.
Do
Commands
Loop
Dim K As Integer
Do
K=Adin(0) ' Read AD input from channel 0
Debug Dec K,Cr
Delay 1000
Loop
In the above example, the program will loop indefinitely inside Do and Loop.
An Exit Do or GOTO statement must be used to get out of the infinite loop.
Do While [Condition]
Commands
[Exit Do]
Loop
Do
Commands
[Exit Do]
Loop While [Condition]
Do Until [Condition]
Commands
[Exit Do]
Loop
Do
Commands
[Exit Do]
Loop Until [Condition]
152
DEMO PROGRAM
153
DTZero
DTZero variable
variable : Variable for decrement. (No String or Single)
154
EERead( )
variable = EERead (Address, ByteLength)
variable : Variable to store result (No String or Single)
address : 0 to 4095
byteLength : Number of bytes to read (1 to 4)
Dim A As Integer
Dim B As Integer
A = 100
EEWrite 0,A,2 ' Store 2 bytes of A in Address 0.
B = EEReed(0,2) ' Read 2 bytes from Address 0 and store in B.
155
EEWrite
EEWrite address, data, byteLength
address : 0 to 4095
data : Data to write to EEPROM (up to Long type values)
byteLength : Number of Bytes to write (1 to 4)
Store data in the specified address in EEPROM. This is very useful for
storing configuration or calibration data.
Dim A As Integer
Dim B As Integer
A = 100
EEWrite 0,A,2 ' Store A in Address 0.
B = EERead(0,2) ' Read from Address 0 and store in B.
If you are using the EEPROM for data acquisition or data that requires a lot
of writes, we recommend using a module with battery-backup memory,
such as the CB290 or CB405, instead. One alternative is an RS-232
Compact Flash (CF) or Secure Digital (SD) memory interface module.
156
EKeyPad()
variable = EKeyPad( portblockIn, portblockOut)
variable : Variable to store results (Returns Byte)
portblockIn : Port Block to receive input (0 to 15)
portblockOut : Port Block to output (0 to 15)
EKeyPad extends KeyPad to read up to 64 key inputs. Two port blocks are
used to read a keypad matrix of up to 8x8 lines. The input port block and
the output port block must be selected separately.
Ports not used within the output port block can be left unconnected.
Unconnected ports may not be used for other purposes. The following is an
example showing port block 0 as the input port block and port block 1 as
the output port block:
10
11
12
13
14
15
If no keys are pressed, 255 will be returned. Otherwise, the pressed key’s
scan code will be returned.
157
For...Next
For...Next will loop the commands within itself for a set number of times.
For Variable = StartingValue To EndingValue [Incremental Step]
Commands
[Exit For]
Next
In the below example, an Incremental Step is not set. By default each loop
increments by 1.
Dim K As Long
For K=0 To 10
Debug Dp(K),CR
Next
An Exit For command can be used within the For...Next loop to exit at
any time.
For K=0 To 10
Debug Dp(K),CR
If K=8 Then Exit For ' If K equals 8 exit the For...Next loop.
Next
When choosing a variable to use for the For...Next loop, please make sure
the chosen variable is able to cover the desired range. Byte variables can
cover 0 to 255. For larger values, a variable with a larger range must be
chosen.
Dim K As Byte
For K=0 To 255
Debug Dp(K),CR
Next
When using a negative Step, please choose a Long variable type if the loop
will go below 0.
Dim LK As Long
For LK=255 To 0 Step –1 ' This will reach -1 as last step
Debug Dp(LK),CR
Next
158
DEMO PROGRAM
159
FreePin
FreePin I/O
I/O : I/O port number
This command will reassign an I/O port back to BASIC, if it had previously
been assigned to Ladder Logic with UsePin.
160
FreqOut
FreqOut channel, freqValue
channel : PWM channel (0 to 15)
freqValue : Frequency value between 1 and 65535
Before using this command, please set the specified PWM port to output
mode and set to a logic high or logic low state. To stop the output, use the
PWMOff command.
161
PWM channel 0 is used for a FreqOut command, PWM channels 0, 1, and 2
cannot be used for a PWM command.
162
Get( )
variable = Get(channel, length)
variable : Variable to store results (Cannot use String or Single)
channel : RS-232 Channel (0 to 3)
length : Length of data to receive in bytes (1 to 4)
Read data from the RS-232 port. The Get command actually reads from the
receive buffer. If there is no data in the receive buffer, it will quit without
waiting for data and return 0. The Blen command can be used to check if
there is any data in the receive buffer before trying to read data.
TIPS
Use Sys(1) after Get or GetStr to verify how much data was actually read.
If 5 bytes are received but only 4 bytes are verified, 1 byte is lost.
GOTDATA:
A=Get(1,1)
Debug A
Return
163
GetA
GetA channel, arrayName, byteLength
channel : RS-232 Channel (0 to 3)
arrayName : Array to store received data (Byte type only)
byteLength : Number of bytes to store (1 to 65535)
The command GetA can be used to store received RS-232 data in a Byte
array. Data will be stored starting from the first element of the array.
Check the receive buffer with BLen before reading to avoid overflowing the
buffer.
GOTDATA:
GetA 1,A,8
Debug A(0),A(1),A(2),A(3),A(4),A(5),A(6),A(7)
Return
164
GetA2
GetA2 channel, arrayName, byteLength, stopChar
channel : RS-232 Channel (0 to 3)
arrayName : Array to store received data (Byte type only)
byteLength : Number of Bytes to store (1 to 65535)
stopChar : Stop character ASCII code
GetA2 is the same as GetA except it will stop reading data at the when it
encounters stopChar, even if the data received is less than byteLength. If
stopChar is not found, then it will operate just like GetA.
You can use the Sys(1) command to check the number of bytes read:
165
GetCrc
GetCrc variable, arrayName, byteLength
variable : String variable to store results (Integer type)
arrayName : Array with data(Must be a Byte array)
byteLength : number of bytes to calculate CRC
Set Ladder On
A(0) = 9
A(1) = 2
A(2) = 3
A(3) = 0
A(4) = 10
A(5) = 23
GetCrc B,A,6 ' Store in variable B the CRC for 6 bytes of array A
Debug Hex B,Cr
166
GetPad( )
variable = GetPad(length)
variable: Variable to store results
length: Length of data to receive
Reads length bytes from the pad buffer allocated with Set Pad.
Dim X As Integer
Dim Y As Integer
Do 'Run forever
Loop
ABC:
X = GetPad(2) 'First 2 bytes is the x coordinate
Y = GetPad(2) 'Second 2 bytes is y coordinate
Debug Dec X, Cr 'Print x coordinate
Debug Dec Y, Cr 'Print y coordinate
Return
167
GetStr( )
variable = GetStr(channel, length)
variable : String Variable to store results
channel : RS-232 Channel
length : Length of data to receive
Same as Get, except the variable to store results can only be a String and
the length of data is not limited to 4 bytes.
GOTDATA:
A = GetStr(1,8)
Debug A
Return
168
GetStr2( )
variable = GetStr(channel, byteLength, stopChar)
variable : String variable to store results
channel : RS-232 Channel
byteLength : Length of data to receive
stopChar : Stop character ASCII code
Same as the GetStr command, except it will stop reading data when it
encounters stopChar, even if the data received is less than byteLength. If
stopChar is not found, then it will operate just like GetStr.
169
GoSub...Return
The GoSub command can call a subroutine. The Return command must be
used at the end of the subroutine.
GoSub ADD_VALUE
ADD_VALUE:
A = A + 1
Return
170
GoTo
The GoTo command will instruct the current program to jump to a specified
label. This is part of every BASIC language, but its use should be limited in
an effort to adopt a structural programming paradigm. Be especially careful
when using GoTo within a GoSub or subroutine since improperly terminating
a subroutine can have undesired effects.
If I = 2 Then
Goto LAB1
End If
LAB1:
I = 3
About Labels…
A label can be set with a colon (:) to specify a point for GoTo or GoSub to
begin execution.
ADD_VALUE:
LINKPOINT:
171
Heap Memory Access
Heap memory access is a special feature only available on the CB405
module. The user may use 55KB of heap memory from address 0 through
56831 (&H0000 through &HDDFF). The heap can be used to store large
data for graphics, temperature tables, etc. With a backup battery, the heap
can be used for data logging and other persistent uses.
55K bytes
0000
4K bytes
0000
&HDDFF &HFFF
172
HRead( )
variable = HRead (address, byteLength)
variable : Variable to store results
address : Heap address
byteLength : Number of bytes to read, constant or variable (1 to 4)
Read the designated number of bytes set by length from heap address,
address, and store into variable.
HWrite
HWrite address, data, byteLength
address : Heap address
data : Constant or variable with data (whole numbers only)
byteLength : number of bytes to write
Dim A As Integer
Dim B As Integer
A = 100
HWrite 0,A,2 ' Write integer A to address 0.
B = HRead(0,2) ' Read from address 0 and store in B.
NOTE
EERead and EEWrite have same syntax as HRead and HWrite.
173
HeapClear
HeapClear
Heap( )
variable = Heap (address)
variable : Variable to store results
address : Heap address
HeapW
HeapW address, data
address : Heap memory address
data : Constant or variable with data (Byte only)
The heap is divided into byte unit addresses. When a Long variable is
stored, 4 bytes are stored, and 4 memory addresses are used.
HWrite 0, &H1234ABCD, 4
0 &HCD
1 &HAB
2 &H34
3 &H12
As you can see in the above table, when a Long variable is stored in heap
address 0, four memory addresses are taken.
174
DEMO PROGRAM
Const Device = CB405
Dim A As Byte
Dim i As Long,J As Long
i = &HABCD1234
HeapClear
HWrite 0,i,4
Do
HeapW 56830,100
HeapW 56831,123
175
High
High port
port : I/O port number
When a port is set to high, the port is internally connected to VDD (5V). If
it’s set to Low, the port is internally connected to VSS (0V). This allows
either source or sink interfacing to external components (up to 25ma for
source or sink).
176
I2CStart
I2CStart
Sets I2C SDA and SCL to start mode. After this command, SDA and SCL go
low.
SDA
SCL
START
I2CStop
I2CStop
Set I2C SDA and SCL to stop mode. After this command, SDA and SCL go
high.
SDA
SCL
STOP
177
I2CRead( )
variable = I2CRead(dummy)
variable : Variable to store results. (No String or Single)
dummy : Dummy value.
Read a byte from the I2C ports set by SET I2C command. Use any value
for the dummy value.
A = I2CRead(0)
SCL
SDA
Ack
This command will send an ACK signal back to the slave I2C device. After
reading a byte, an SCL pulse will be sent while SDA is kept low.
178
I2CReadNA( )
Variable = I2CReadNA (dummy)
variable : Variable to store results. (No String or Single)
dummy : Dummy value. (Normally 0)
A = I2CReadNA(0)
SCL
SDA
NoAck
179
I2CWrite( )
variable = I2CWrite data
variable : Acknowledge (0=Acknowledgment, 1=No Acknowledgment)
data : data to send (Byte value : 0 to 255)
Sends one byte of data through I2C. This command creates an ACK pulse
and returns 0 if there is an acknowledgment and 1 if there isn’t. No
acknowledgment indicates that there was a communication error (possibly
due to incorrect wiring). The following is an example illustrating how this
can be used to trigger an error processing function:
If you don’t need to check for an ACK, you can just use any variable to
receive the ACK status as shown below:
A = I2CWrite(DATA)
180
If...Then...ElseIf…EndIf
You can use If…Then…ElseIf...Else…EndIf conditional statements to
control execution of your program.
Usage 1
If A<10 Then B=1
Usage 2
If A<10 Then B=1 Else C=1
Usage 3
If A<10 Then ' When using more than 1 line,
B=1 ' do not put any Expressions after "Then".
End If
Usage 4
If A<10 Then
B=1
Else
C=1
End If
Usage 5 Usage 6
If A<10 Then
B=1 If A<10 Then
ElseIf A<20 Then B=1
C=1 ElseIf A<20 Then
End If C=1
ElseIf A<40 Then
C=2
Else
D=1
End If
181
In( )
variable = In(port)
variable : The variable to store result (No String or Single)
port : I/O port number (0 to 255)
Read the current state of the specified port. This function reads the state of
the I/O port and stores it in the specified variable. When this command is
executed, Cubloc will automatically set the port to input and read from the
port. You do not need to use the Input command to set the port
beforehand when using this command.
Dim A As Byte
A = In(8) ' Read the current state of port 8
' and store in variable A(0 or 1)
TIP
By default, all I/O ports are set to a high-Z (high impedance) input state at
power ON. When a port is set to output, it will either output a high or low
signal; high is 5V and low is 0V or GND (ground).
182
Incr
Incr variable
variable : Variable to increment. (No String or Single)
Incr A 'Increment A by 1.
183
Input
Input port
port : I/O port number (0 to 255)
High impedance means that the value of resistance is so high that it’s
neither high nor low; it won’t affect a circuit attached to the port.
184
KeyIn
variable = KeyIn( ort, debouncingTime)
variable : Variable to store results (No String or Single)
port : Input port (0 to 255)
deboucingTime : Debouncing time (1 to 65535)
The command KeyIn removes the input signal's contact bounce before
reading an input. You can use KeyIn only with active low inputs as shown
below. For active high inputs, please use KeyInH. When a button press is
detected, KeyIn will return 0; otherwise, it will return 1.
If you use 10 for the deboucing time, the Cubloc will debounce for 10 ms.
Contact bounce usually stops after 10ms, so a 10ms debouncing time will
suffice for most applications
Bouncing effect
185
KeyInH
variable = KeyInH( port, debouncingTime)
variable : Variable to store results (No String or Single)
port : Input port (0 to 255)
deboucingTime : Debouncing time (0 to 65535)
KeyInH is for active high inputs. For active low inputs, the KeyIn command
should be used.
186
Keypad
variable = Keypad( portBlock)
variable : Variable to store results (Returns Byte, No String or Single)
portBlock : Port block (0 to 15)
Use this command to read input from a matrix keypad. One port block can
be used to read a 4 by 4 keypad input. The keypad rows should be
connected to the lower 4 bits of the Port Block, and the keypad columns
should be connected to upper 4 bits of the port block.
Pullup resistors (2.2K to 10K) should be connected to the lower 4 bits of the
port block. A resistor should be connected even if a row is not being used.
0 4 8 12
0
1 5 9 13
1
2 6 10 14
2
3 7 11 15
3
If no keys are pressed, 255 will be returned. Otherwise, the pressed key’s
scan code will be returned.
187
Low
Low port
port : I/O port number (0 to 255)
This command sets the port's I/O mode to output and outputs logic low or
0V (GND).
When a port is set to logic high, the port is internally connected to VDD
(5V). If it is set to a logic low, the port is internally connected to VSS (0V).
This allows either source or sink interfacing to external components (up to
25mA for source or sink).
188
MemAdr( )
variable = MemAdr (targetVariable)
variable : Variable to store results (No String or Single)
targetVariable : Variable whose physical memory address should be
returned
The MemAdr command will return the memory location of the specified
target variable. This can be useful when used with the Peek and Poke
commands; operations similar to C pointer manipulation can be performed.
Dim A as Single
Dim Adr as Integer
Adr = MemAdr(A) 'Return the physical address of A.
189
Ncd
variable = Ncd bitPosition
variable : Variable to store results. (No String or Single)
bitPosition : Position of bit (0 to 31)
The Ncd command is used to return a value with the specified bit set to 1.
190
Nop
Nop
This command does nothing, but consumes one command cycle. It is useful
for tuning small intervals.
Low 8
Nop
High 8 'Output very short pulse to port 8. (About 50 micro Sec)
Nop
Low 8
191
On Int
On Int0 GoSub label
On Int1 GoSub label
On Int2 GoSub label
On Int3 GoSub label
Set OnIntx command must be used with this command in order for the
interrupt to work.
Dim A As Integer
On Int0 GoSub GETINT0
Set Int0 0 'Falling Edge Input
Do
Loop
GETINT0:
A=A+1 'Record number of interrupts
Return
192
On LadderInt GoSub
On LadderInt GoSub label
This can be used when a Ladder Logic program needs to trigger a specific
procedure in BASIC.
Please use the SetOut and DIFU command to write 1 to the register F40.
When the BASIC interrupt routine is finished, register F40 can be cleared by
writing a zero to it.
UsePin 0,In
Set Ladder On
Set Display 0,0,16,77,50
On LadderInt GoSub msg1_rtn
Dim i As Integer
Low 1
Do
i=i+1
ByteOut 1,i
Delay 200
Loop
msg1_rtn:
Locate 0,0
Print "ON LadderInt",Dec i
Reverse 1
Return
When P0 turns on, register F40 turns on and the msg1_rtn interrupt routine
in BASIC is executed. In the ISR, a String is printed to the LCD.
193
Although there is only one register, F40, with which call an ISR in BASIC
from Ladder Logic, we can use data register D to process many different
types of interrupts.
Given the ladder above, when P0 turns on, D0 gets 3 and the interrupt
routine is executed. If P2 turns on, D0 gets 2 and the interrupt routine is
executed. In the ISR, the user can then process the type of interrupt based
on the value stored in D0.
msg1_rtn:
If _D(0)=3 Then
Locate 0,0
Print "ON Ladderint",Dec i
End If
If _D(0)=2 Then
Locate 0,0
Print "TEST PROGRAM",Dec i
End If
Return
For a short version of the ladder above the user can use an INTON
command, which accomplishes both WMOV and SETOUT in one command.
194
On Pad
On Pad GoSub label
The On Pad interrupt will jump to the specified label when the
keypad/touchpad port receives a packet (packets sizes are assigned by the
Set Pad command). Please be sure to use a Return command after the
label.
GETTOUCH:
TX1 = GetPad(2)
TY1 = GetPad(2)
CircleFill TX1,TY1,10
PulsOut 18,300
Return
195
On Recv
On Recv0 GoSub label
On Recv1 GoSub label
On Recv2 GoSub label
On Recv3 GoSub label
Do
Loop ' Infinite Loop
DATARECV_RTN:
If BLen(1,0) > 4 Then
A(0) = Get(1,1) ' Read 1 Byte.
A(1) = Get(1,1) ' Read 1 Byte.
A(2) = Get(1,1) ' Read 1 Byte.
A(3) = Get(1,1) ' Read 1 Byte.
A(4) = Get(1,1) ' Read 1 Byte.
End If
Return ' End of interrupt routine
IMPORTANT
When a Recv interrupt service routine (ISR) is being executed,
subsequent Recv interrupts will not be received. After the ISR is
finished executing, if there is still data being received, another
Recv interrupt will be generated, and the ISR will execute again.
196
On Timer()
On Timer( interval ) GoSub label
interval : Interrupt interval 1=10ms, 2=20ms……65535=655350ms
1 to 65535 can be used
I = 0
Do
Loop
TIMERTN:
Incr i ' i is incremented by 1 every second.
Return
IMPORTANT
Be aware that ithe interrupt service routine (ISR) must
require less time to execute than the interval itself. If the
interval is set to 10ms, the ISR must execute within 10 ms;
otherwise collisions can occur.
197
OpenCom
OpenCom channel, baudRate, protocol, recvSize, sendSize
channel : RS232 Channel (0 to 3)
baudRate : BaudRate (Do not use a variable)
protocol : Protocol (Do not use a variable)
recvSize : Receive buffer size (Max. 1024, Do not use a variable)
sendSize : Send buffer size (Max. 1024, Do not use a variable)
You may use any value between 2400 to 230400, but we are recommed one of
the following values.
2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 76800, 115200, 230400
The following table shows typical settings based on the previous table:
198
The send and receive buffer size can be set with the OpenCom command.
Each buffer can be as large as 1024 bytes, but be aware that the send and
receive buffers consume data memory. So the larger the buffers, the fewer
number of variables can be used in the program. Receive buffer sizes from
30 to 100 and send buffer sizes from 30 to 50 will suffice for most
programs.
For the CB220 module, pins 1 and 2 can be used for channel 0. Ports 10
and 11 can be used for channel 1.
CHANNEL 0
TX SOUT 1 24 VIN
RX SIN 2 23 VSS
ATN 3 22 RES
VSS 4 21 VDD
P0 5 20 P15
P1 6 19 P14
P2 7 18 P13
P3 8 17 P12 CHANNEL 1
P4 9 16 P11 TX
P5 10 15 P10 RX
P6 11 14 P9
P7 12 13 P8
For the CB280 module, there are dedicated RS-232 ports. For channel 1,
there are 2 types of outputs: +/-12V and TTL (+5/0V).
+12V
-12V
+12V
SOUT 1 17 VDD TX1 33 49 TT LTX1 +5V
SIN 2 18 VSS R X1 34 50 TT LRX1
ATN 3 19 R ES AVD D 35 51 AVREF GND
-12V V SS 4 20 N /C N/C 36 52 P48
P0 5 21 P16 P24 37 53 P31
P1 6 22 P17 P25 38 54 P30
P2 7 23 P18 P26 39 55 P29
P3 8 24 P19 P27 40 56 P28
P4 9 25 P20 P47 41 57 P32
P5 10 26 P21 P46 42 58 P33
P6 11 27 P22 P45 43 59 P34
P7 12 28 P23 P44 44 60 P35
P8 13 29 P15 P43 45 61 P36
P9 14 30 P14 P42 46 62 P37
P10 15 31 P13 P41 47 63 P38
P11 16 32 P12 P40 48 64 P39
*If necessary, the Set RS232 command can reset the RS-232 channel's
configuration during program execution.
199
CB400/CB405 RS232 How-to
The following is a table of the 5V TTL signal pins for the CB405:
The CB400/CB405 has an internal MAX232 that can be used to convert any
of the 5V TTL signals to +/- 12V level signals. The following is an example
for connecting channel 3:
This allows any +/- 12V RS232 device to connect to TXE and RXE.
200
Out
Out port, value
port : I/O port number (0 to 255)
value : Value to output to the specified I/O port (1 or 0)
201
Output
Output port
port : I/O port number (0 to 255)
Set the port's I/O mode to output. All Cubloc module's I/O ports are set to
High-Z input by default at power on.
You can also use the High or Low commands to set a port's I/O mode to
output. When using the Output command, the port's output (logic high or
logic low) is not clearly defined. Therefore, it recommended to use the High
or Low commands to set a port's I/O mode so the port's output is properly
initialized.
Low 8 'Set port 8's I/O mode to output and output a logic low.
202
OutStat( )
variable = OutState(port)
variable : Variable to store results. (No String or Single)
port : I/O port number (0 to 255)
Reads the current output value for the specified port. This command is
different from the In command; it reads a port's current output, not input.
Dim A As Byte
A = OutStat(0) 'Read from port 0 and store the output in A.
Pause
Pause value
Same as Delay
203
Peek( )
variable = Peek (address, length)
variable : Variable to store result.s (No String or Single)
address : RAM address.
length : Number of bytes to read (1 to 4)
Reads the specified length of data starting from the specified data memory
address.
Poke
Poke address, value, length
address : RAM address
value : The value to write
length : Number of bytes to write (1 to 4)
Write the specified length of data starting at the specified data memory
address.
204
PulsOut
PulsOut port, period
port : Output port (0 to 255)
period : Pulse period (1 to 65535)
This is a subroutine that outputs a pulse. To create a high pulse, the output
port must be set to logic low beforehand. To create a Low pulse, the output
Port must be set to logic high before hand.
If you set the pulse period to 10, you will create a pulse of about 2.6ms.
Likewise, a Pulse Period of 100 will be about 23ms.
Low 2 High 2
PulsOut 2, 100 '23mS high pulse PulsOut 2, 100 '23mS low pulse
205
Put
Put channel, data, byteLength
channel : RS-232 channel (0 to 3)
data : Data to send (up to Long type value)
byteLength : Length of data in bytes (1 to 4)
This command sends data on the specified RS-232 channel. For data,
variables and constants can be used. To send a String, please use the
PutStr command instead.
OpenCom 1,19200,0,50,10
IMPORTANT
Dim A As Byte
The OpenCom
A = &HA0
command must be
Put 1,A,1 ' Send &HA0 (0xA0)
used beforehand
' to RS232 Channel 1.
The data is first stored in the send buffer set by OpenCom. The Cubloc
BASIC Interpreter will automatically keep transmitting the data until the
send buffer is empty.
If the send buffer is full when the Put command is executed, Put will
overwrite the data currently in the buffer. The BFree command can be used
to check the send buffer beforehand to prevent overwriting existing data in
the send buffer.
If BFree(1,1) > 2 Then ' If send buffer has at least 2 bytes free
Put 1,A,2
End If
Bfree is used to check how much free space the send buffer has before
writing any data to it.
TIP
After using Put or PutStr, the function Sys(0) can be used to verify that
the data has been stored in the send buffer.
OpenCom 1,19200,0,50,10
PutStr 1,"COMFILE"
Debug Dec Sys(0) ' If output is 7, all data has been stored
' in the send buffer
*Please refer to the On Recv interrupt service routine for receiving data
using the hardware serial buffer.
206
PutA
PutA channel, array, byteLength
channel : RS-232 channel. (0 to 3)
array : The Byte array to send
byteLength : Number of bytes to send (1 to 65535)
The command PutA is used to send a Byte array on the specified RS-232
channel. The array data will be sent starting from the first element of the
array.
IMPORTANT
If byteLength is larger than the array, Cubloc will send random
values.
*Please refer to the On Recv interrupt routine for receiving data using the
hardware serial buffer.
207
PutA2
PutA2 channel, array, byteLength, stopChar
channel : RS-232 channel. (0 to 3)
array : The byte array to send
byteLength : Number of bytes to send (1 to 65535)
stopChar : Stop character ASCII code
Same as the PutA command, except it will stop transmission when the
specified stop character in encountered in the array (stopChar will be the
last character to be sent).
208
PutStr
PutStr channel, data…
channel : RS-232 channel. (0 to 3)
data : String data (String variable , String constant or Constant)
OpenCom 1,19200,0,50,10
PutStr 1,"COMFILE TECHNOLOGY", Dec I, Cr
Similar to the Put command, PutStr puts data to be sent in the RS-232
port's send buffer. Afterward, the Cubloc BASIC interpreter takes care of
the actual sending. Use the BFree command to prevent overloading the
send buffer.
209
Pwm
Pwm channel, duty, period
channel : PWM channel (0 to 15)
duty : Duty cycle (must be less than the period)
period : Maximum of 65535
duty must to be less than period. The pulse's output will remain active for
a fraction of the period given by duty/period.
Pwm is independently hardware driven within the Cubloc. Once the Pwm
command is executed, it will keep running until the PwmOff command is
called.
200
1024
IMPORTANT
PWM channels 0, 1, and 2 must use the same value for period
since they share the same resources, but their duty cycles can
be different. PWM channels 3, 4, and 5 also must use the same
value for period, but again, their duty cycles can be different.
210
PwmOff
PwmOff channel
channel : PWM channel. (0 to 15)
SOUT 1 24 VIN
SIN 2 23 VSS
ATN 3 22 RES
VSS 4 21 VDD
P0 5 20 P15
P1 6 19 P14
P2 7 18 P13
P3 8 17 P12
P4 9 16 P11
PWM0 P5 10 15 P10
PWM1 P6 11 14 P9
PWM2 P7 12 13 P8
For CB220, 3 PWM channels are provided on ports P5, P6, and P7.
S OU T 1 17 V DD TX 1 33 49 T TLTX 1
S IN 2 18 V SS RX 1 34 50 T TLRX 1
AT N 3 19 RE S AV D D 35 51 AVR EF
VSS 4 20 N/ C N/C 36 52 P 48
P0 5 21 P 16 P 24 37 53 P 31
P1 6 22 P 17 P 25 38 54 P 30
P2 7 23 P 18 P 26 39 55 P 29
P3 8 24 P 19 PWM3 P 27 40 56 P 28
P4 9 25 P 20 PWM4 P 47 41 57 P 32
PWM0 P5 10 26 P 21 PWM5 P 46 42 58 P 33
PWM1 P6 11 27 P 22 P 45 43 59 P 34
PWM2 P7 12 28 P 23 P 44 44 60 P 35
P8 13 29 P 15 P 43 45 61 P 36
P9 14 30 P 14 P 42 46 62 P 37
P10 15 31 P 13 P 41 47 63 P 38
P 11 16 32 P 12 P 40 48 64 P 39
Please refer to the table below for PWM channels and their corresponding
I/O ports.
211
RamClear
RamClear
Clear Cubloc BASIC’s RAM. BASIC’s data memory can hold garbage values
at power on. RamClear can be used to initialize all data memory to zero.
*There are Cubloc modules that support battery backup of the RAM. If you
don’t use the RamClear command in these modules, Cubloc will retain the
values in RAM between power cycles.
212
Reset
Reset
Restarts the Cubloc BASIC program from the beginning. It does not clear
the data memory, so any variables that have been declared will retain their
values. RamClear should be used if this behavior is not desirable.
213
Reverse
Reverse port
port : I/O port number. (0 to 255)
214
Rnd( )
variable = Rnd(0)
The command Rnd returns a random number from 0 through 32767. The
number passed to Rnd has no effect, but is required.
Dim A As Integer
A = Rnd(0)
215
Select...Case
Select Case variable
[Case value [,value],…
[Statement 1]]
[Case value [,value],…
[Statement 2]]
[Case Else
[Statement 3]]
End Select
Select Case A
Case 1
B = 0
Case 2
B = 2
Case 3,4,5,6 ' Use Comma(,) for more than 1 value.
B = 3
Case Is < 1 ' Use < for logical operations.
B = 3
Case Else ' Use Else for all other cases.
B = 4
End Select
Select Case K
Case Is < 10 ' If less than 10
R = 0
Case Is < 40 ' If less than 40
R = 1
Case Is < 80
R = 2
Case Is < 100
R = 3
Case Else
R = 4
End select
216
Set Debug
Set Debug On|Off
When you don’t need any debugging features, you can use this command to
turn off all Debug commands instead of modifying every instance of of the
Debug command. When this command is used, no Debug command will be
compiled; they are simply discarded from the program.
Do
High 0
Delay 200
Low 0
Delay 200
Loop
217
2. How to check if a particular point of the program is
being executed
Simply insert a Debug command at the point in question, as shown below:
(The debug statement above will never execute, as the program will
execute the Do…Loop indefinitely)
218
3. How to simulate an LCD
You can simulate an LCD using the Debug terminal. Simply use GoXY, XX,
YY to access a particular location on the terminal as shown below:
219
Set I2C
Set I2C dataPort, clockPort
dataPort : SDA, data send/receive port. (0 to 255)
clockPort : SCL, clock send/receive port. (0 to 255)
The Set I2C command sets the data port (SDA) and clock port (SCL), for
I2C communication. Once this command is executed, both ports' I/O
modes become output, and their outputs become logic high. For I2C
communication please use ports capable of both input and output and 4.7K
resistors as shown below.
SCL
SDA
I2C communication requires ports capable of both input and output, but
some ports are only capable of either input or output. Please check the
port specifications in the data sheet for the model you are using.
220
Set Int
Set Intx mode
x : 0 to 3, External interrupt channel
mode : 0=Falling edge, 1=Rising edge, 2=Changing edge
This command must be used with the On Int command in order to receive
external interrupts. The mode of interrupt can be set to trigger on either
the falling edge, rising edge, or changing edge.
Set Int0 0 ' Set external interrupt 0 to trigger on the falling edge.
P9 14 30 P 14 P42 46 62 P37
P 10 15 31 P 13 P41 47 63 P38
P11 16 32 P 12 P40 48 64 P39
221
Set Ladder
Set Ladder On|Off
Ladder is set to Off by default. Use this command to turn enable Ladder
Logic.
Do
Loop 'BASIC program will run in infinite loop.
222
Set Modbus
Set Modbus mode, slaveAddress, returnInterval
mode : 0=ASCII, 1=RTU
slaveAddress : Slave address (1 to 254)
returnInterval : Return interval (1 to 255)
To enable Modbus slave mode, use the Set Modbus command. This
command will enable the Modbus slave. It must come after an OpenCom
command to set up RS-232 communication on RS-232 channel 1. The baud
rate, stop bit, and parity settings can be set with OpenCom.
Please refer to Chapter 9 for detailed description of Modbus with ASCII and
RTU examples.
The return interval is Cubloc's delay time for responding to the master
Modbus device. If the return interval is set to be too fast, the master
device might not be able to receive all data. The default setting is 1, which
is about 200 micro-seconds. A value of 100 is about 4.5ms and a value of
255 is about 11ms.
223
Set OnGlobal
Set OnGlobal On|OFF
OnGlobal is On by default.
When OnGlobal is set to Off and then set to On, all interrupt settings in
effect before OnGlobal was set to Off will be still be in effect.
If you don’t use any interrupts, you can turn off all interrupts using Set
OnGlobal Off to increase Cubloc's execution speed.
224
Set OnInt
Set OnIntx On|Off
x : 0 to 3, External interrupt channel
When Set OnIntx is On for a specific interrupt, the interrupt service routine
(ISR) set using the On Intx command will be executed when the
corresponding interrupt occurs. If Set OnIntx is Off, then the ISR will not
be executed when the corresponding external interrupt occurs. See also the
Set Intx command which controls external interrupts.
Set OnInt0 On
Set OnInt1 On
Set OnInt1 Off
Set OnInt2 Off
Set OnInt3 On
225
Set OnLadderInt
Set OnLadderInt On|Off
This command turns on or off the ability to receive Ladder Logic interrupts.
When Set OnLadderInt is On, the interrupt service routine (ISR) set using
the On LadderInt command will be executed when the corresponding
interrupt occurs. If Set OnGlobal is Off, then the ISR will not be executed
when the Ladder Logic interrupt occurs. See also the On LadderInt
command.
226
Set OnPad
Set OnPad On|Off
When Set OnPad is On, the interrupt service routine (ISR) set using the On
Pad command will be executed when the corresponding interrupt occurs. If
Set OnPad is Off, then the ISR will not be executed when the interrupt
occurs. See also the Set Pad and On Pad commands.
227
Set OnRecv
Set OnRecv0 On|Off
Set OnRecv1 On|Off
Set OnRecv2 On|Off
Set OnRecv2 On|Off
When Set OnRecv is On, the interrupt service routine (ISR) set using the On
Recv command will be executed when the corresponding interrupt occurs.
If Set OnRecv is Off, then the ISR will not be executed when the interrupt
occurs. See also the On Recv command.
Set OnRecv1 On
Set OnRecv1 Off
228
Set OnTimer
Set OnTimer On|Off
When Set OnTimer is On, the interrupt service routine (ISR) set using the
On Timer command will be executed when the corresponding interrupt
occurs. If Set OnTimer is Off, then the ISR will not be executed when the
interrupt occurs. See also the On Timer command.
229
Set OutOnly
Set OutOnly On|Off
S ou t 1 21 Vdd T X1 41 61 T tlTX 1
42 62
CB290
Sin 2 22 Vss R X1 T tlR X 1
A tn 3 23 R ES AV d d 43 63 AVr ef
V ss 4 24 VBB V dd 44 64 V ss
S S_ P 0 5 25 P8 _A D C0 P 24 45 65 P 32
( In p ut on ly ) S C K_P 1 6 26 P9 _A D C1 P 25 46 66 P 33
M O SI_P 2 7 27 P10 _AD C 2 P 26 47 67 P 34
M ISO _P 3 8 28 P11 _A D C3 P 27 48 68 P 35
P 16 _ SC L (C U NET )
P 17 _ S D A (C U NE T )
P4
P W M 0_ P 5
9
10
29
30
P12 _ AD C 4
P13 _ AD C 5
P 28
P 29
49
50
69
70
P 36
P 37
Ou tput Only
P W M 1_ P 6 11 31 P14 _ AD C 6 P 30 51 71 P 38
P 89 _ PW M 3
P W M 2_ P 7 12 32 P15 _ AD C 7 P 31 52 72 P 39
P 19 _ IN T4
P 18 _ IN T3
P 56 13 33 P64 P 40 53 73 P 48
P 57 14 34 P65 P 41 54 74 P 49
P 58 15 35 P66 P 42 55 75 P 50
P 72
P 73
P 74
P 75
P 80
P 81
P 82
P 83
N /C
P 59 16 36 P67 P 43 56 76 P 51
P 60 17 37 P68 P 44 57 77 P 52
81
82
83
84
85
86
87
88
89
90
91
92
93
94
P 61 18 38 P69 P 45 58 78 P 53
P 62 19 39 P70 P 46 59 79 P 54
P 63 20 40 P71 P 47 60 80 P 55
95
96
97
98
99
1 00
1 01
1 02
1 03
1 04
1 05
1 06
1 07
1 08
PW M4 _ P 90
PW M 5 _ P 91
P 20
P 21
H C N T0 _ P 22
H C N T1 _ P 23
P 76
P 77
P 78
P 79
P 84
P 85
P 86
P 87
230
Set Pad
Set Pad mode, packetSize, bufferSize
mode : Bit mode (0 to 255)
packetSize : Packet size (1 to 255)
bufferSize : Receive buffer size (1 to 255)
The Cubloc has a dedicated port for keypad / touchpad inputs similar to a
PC’s keyboard or mouse port. This port can be used with the Set Pad
command to create interrupts when input is received on a keypad,
touchpad, etc.,. This port is as an SPI slave.
To use pad communication, you must use a Set Pad command at the
beginning of your program. Pad communication uses 4 wires. SCK is used
as the clock signal, SS as the slave select, MOSI as the master out/slave in,
and MISO as master in/slave out.
TOUCH PAD
SS SS
SCK SCK
MOSI MOSI
MISO MISO
SOUT 1 24 VIN
SIN 2 23 VSS
ATN 3 22 RES
VSS 4 21 VDD
SS P0 5 20 P15
SCK P1 6 19 P14
MOSI P2 7 18 P13
MISO P3 8 17 P12
P4 9 16 P11
P5 10 15 P10
P6 11 14 P9
P7 12 13 P8
231
SOUT 1 17 VDD TX1 33 49 TTLTX1
SIN 2 18 VSS RX1 34 50 TTLRX1
ATN 3 19 RES AVDD 35 51 AVREF
VSS 4 20 N/C N/C 36 52 P48
SS P0 5 21 P16 P24 37 53 P31
SCK P1 6 22 P17 P25 38 54 P30
MOSI P2 7 23 P18 P26 39 55 P29
MISO P3 8 24 P19 P27 40 56 P28
P4 9 25 P20 P47 41 57 P32
P5 10 26 P21 P46 42 58 P33
P6 11 27 P22 P45 43 59 P34
P7 12 28 P23 P44 44 60 P35
P8 13 29 P15 P43 45 61 P36
P9 14 30 P14 P42 46 62 P37
P10 15 31 P13 P41 47 63 P38
P11 16 32 P12 P40 48 64 P39
The packetSize parameter sets the packet size need to cause an interrupt.
For example, the Cutouch panel requires 4 bytes to be received before an
interrupt is generated.
The bufferSize parameter is the total size of the receive buffer. The buffer
size must be at least 1 more than the packet size. A larger buffer will
essentially give you more time to process the interrupt service routine. The
buffer size is usually set to 5 or 10 times the packet size.
The mode parameter will set the receiving mode of the received data.
Please refer to the following table:
232
The receiving modes can be added together. For example, for MSB first,
High-Edge Triggered SCK, and sampling after SCK:
SCK
&H00 Sample
MSB B it6 Bit5 Bit4 Bit3 Bit2 Bit1 LSB
SCK
&H04 Sample
MSB B it6 Bit5 Bit4 Bit3 Bit2 Bit1 LSB
SCK
&H08 Sample
MSB B it6 Bit5 Bit4 Bit3 Bit2 Bit1 LSB
SCK
&H0C Sample
MSB B it6 Bit5 Bit4 Bit3 Bit2 Bit1 LSB
For pad communications, you can use Comfile’s keypads and touch screens.
The Set Pad command will automatically set the I/O mode of ports P0
through P3; the user doesn’t have to set them.
233
Set RS232
Set RS232 channel, baudRate, protocol
channel : RS-232 Channel (0 to 3)
baudRate : baudRate (Do not use a variable)
protocol : Protocol (Do not use a variable)
You can only use the OpenCom command once to open a serial port. Set
RS232 is used to change the baud rate and/or protocol of a serial port at run
time.
The following table shows typical settings based on the previous table:
234
Set RS485
Set RS485 channel, portNumber
channel : RS-232 channel (0 to 3)
portNumber : Transmit enable port number
You can refer to the following circuit schematic for connecting TTL signals
from a CB280 to the RS485 chip, SN75176B:
560
CUBLOC CB280
SN75176B
TTLRX R
RE B 485-
P48
DE A 485 +
TTLTX D
120
560
Example:
When the PC is transmitting, all the slave devices can only receive data.
PC
235
The Set RS485 command allows the Cubloc or Cutouch to control the data
line whenever it wants to send or receive data. While the data is being
sent, the transmit enable pin will output active high. This will be done
automatically by the Cubloc RTOS.
Set RS485 1,48 ' Set P48 as the transmit enable pin
TTLTX
P48
When using the Set RS485 command, the port chosen may not be used for
other purposes.
R
RE B multiple Cublocs or Cutouchs
A
DE
D
using RS485.
120
560 Please use a 120 Ohm
terminating resistor for the
device at the end.
SN75176 B
R
RE B The two 560 Ohm pull-up and
A
DE
D
pull-down resistors are required
for proper communication.
SN7 5176B
R
B
RE
DE 120
D A
236
Set Spi
Set Spi clk, mosi, miso, mode
clk : port for clock output
mosi : port for data (Master output, Slave Input)
miso : port for data (Master input, Slave output)
mode: communication mode
bit 3: 0 = MSB start, 1 = LSB start
bit 2: 0 = wait at clock LOW, 1 = wait at clock HIGH
bit 1: Output sampe point; 0 = before rising edge, 1 = after rising edge
bit 0: Input sample point; 0 = before rising edge, 1 = after rising edge
237
Set Until
Set Until channel, packetLength[, stopChar]
channel : RS232 channel. (0 to 3)
packetLength : Length of packet (0 to 255)
stopChar : Character to catch
charCheck: (Optional) Whether to use stopChar
0 = Check Char (default)
1 = Don’t Check Char
This is a conditional statement you can put right after the On Recv
command. Normally, the On Recv interrupt is generated when a single
byte is received via the specified serial port. Set Until can be used to only
generate an interrupt when either packetLength bytes are received, or
stopChar is detected.
As you can see above, the packet size is 99 bytes. In other words, if
character "S" is not received within 99 bytes, an interrupt will occur.
The stop character may also be written in decimal form as shown below:
238
ShiftIn( )
variable = ShiftIn(clock, data, mode, bitLength)
variable : Variable to store results. (No String or Single)
clock : Clock port. (0 to 255)
data : Data port. (0 to 255)
mode : 0 = LSB first (Least Significant Bit first), after rising edge
1 = MSB first (Most Significant Bit first), after rising edge
2 = LSB first (Least Significant Bit first), after falling edge
3 = MSB first (Most Significant Bit first), after falling edge
4 = LSB first (Least Significant Bit first), before rising edge
5 = MSB first (Most Significant Bit first), before fising edge
bitLength : Length of bits (1 to 16)
The ShiftIn command receives a shift input. It uses 2 ports, CLOCK and
DATA, to communicate.
CLK
Dim A AS Byte
A = ShiftIn(3,4,0,8) ' Port 3 is CLOCK, port 4 is DATA,
' mode is 0, 8 bits to be received.
CLK
DATA
0 1 1 0 0 1 0 0 = 26H
LSB M SB
239
ShiftOut
ShiftOut clock, data, mode, variable, bitLength
clock : Clock port. (0 to 255)
data : Data port. (0 to 255)
mode : 0 = LSB first (Least Significant Bit first)
1 = MSB first (Most Significant Bit first)
2 = MSB first(Most Significant Bit first) , create ACK (For I2C)
variable : Variable to store data (up to 65535)
bitLength : Bit length (1 to 16)
This command performs a shift output. There are 3 modes. Mode 2 is for
the I2C protocol. In I2C communication, an ACK signal is required for every
8 bits.
CLK
DATA
1 0 1 0 1 0 1 0 = 55H
LSB MSB
240
Spi
InData = Spi(OutData, Bits)
InData: Input data
OutData: Input data
Bits: Number of bits (1 to 32)
The Set Spi command must be used prior to the Spi command to ensure
the I/O ports are defined prior to sending or receiving data.
241
StepAccel
StepAccel channel, port, freqBase, freqTop, FreqAccel, qty
channel : StepPulse channel (StepAccel supports only 0)
port : Output port
freqBase : The starting stepper frequency (Up to FreqTop)
freqTop : The frequency after acceleration is finished (Up to 3.3KHz)
freqAccel : The acceleration in steps per second
qty : # of pulses to output (up to 2147483647
You can use any of the available I/O ports on the Cubloc. When the
StepAccel command is executed, the specified port's I/O mode is
automatically set to output. Even after the command has finished
generating pulses, the port's I/O mode remains output.
242
StepPulse
StepPulse channel, port, freq, qty
channel : StepPulse channel(0 or 1)
port : Output port
freq : Output frequency (Up to 15kHz)
qty : # of pulses to output (up to 2147483647)
StepPulse uses the Cubloc processor’s PWM counters. When using this
command, PWM3, PWM4, and PWM5 cannot be used.
For the CB400/CB405, when using channel 1, PWM6, PWM7, and PWM8
cannot be used. With the CB2XX series, only channel 0 may be used. With
the the CB400/CB405, StepPulse can be used on 2 different channels
simultaneously.
You can use any of the available I/O ports on the Cubloc. When the
StepPulse command is executed, the specified port's I/O mode is
automatically set to output. Even after the command has finished
generating pulses, the port's I/O mode remains output.
243
StepStat( )
variable = StepStat (Channel)
variable : Variable to store results
channel : StepPulse channel(0 or 1)
StepStat allows you to monitor how many pulses have been generated
since the last StepPulse command.
You can also check the output status of pulses using _F(56) or F56 in
Ladder Logic. When channel 0 is generating pulses, _F(56) will be logic
high, 1. When channel 1 is generating pulses, _F(57) will be logic high, 1.
If no pulses are being generated the F registers will be logic low, 0.
STEPPULSE CH0
F56
STEPPULSE CH1
F57
StepStop
StepStop channel
channel : StepPulse channel (0 or 1)
244
DEMO PROGRAM
Do While In(0) = 0
Loop
Loop
When the port 0 switch is pressed, port 5 will output 300 pulses at the
speed of 5kHz. The following is a circuit diagram for the code above:
CB280 5V
P0
P5
245
A stepper motor controller can be created using a stepper motor and
stepper motor driver as shown below.
Connect 3 Cubloc I/O ports to the stepper motor driver. The DISABLE and
DIRECTION pins are only to enable and set the direction of the stepper
motor.
Please refer to your stepper motor specifications for how many pulses are
required to move the stepper motor one rotation.
246
Sys( )
variable = Sys(address)
variable : Variable to store results. (No String or Single)
address : Address. (0 to 255)
Use the Sys(0) and Sys(1) commands are used to read the status of the
RS-232 buffers for both channel 0 and channel 1.
Sys(0) returns the actual number of bytes written to the RS-232 transmit
buffer after executing commands Put or PutStr.
Sys(1) returns the actual number of bytes read from the RS-232 receive
buffer after executing commands Get or GetStr.
Sys(6) returns the address of the top of the stack in data memory. At
power on, this value is 0. As variables are declared or subroutines and
functions are called, this value will increase. This can be used provide an
indication of a program’s data memory usage at runtime.
247
TADIn()
variable = TADIn(Channel)
variable : Variable to store results. (No String or Single)
channel : AD channel number (Not Port number, 0 to 15)
This command is similar to ADIn, but returns the average of 10 values read
with the ADIn command. When working under noisy environments, using
TADIn could help in obtaining more precise results.
248
Time( )
variable = Time (address)
variable : Variable to store results. (No String or Single)
address : Address of time value (0 to 6)
The CT1721C, CB290 has an internal Real Time Clock (RTC) chip. You can
use the Time and TimeSet commands to write and read time values to and
from the RTC. Time information such as the current time of day, day of the
week, and year can be written to the RTC, and read from it in real-time.
If a backup battery is used, time is kept current even when the module
powers off.
The following is a chart showing the addresses of the RTC and its
corresponding values.
* You cannot use these commands for the CB220 nor CB280 since they do
not have an RTC.
Please refer to the chart below for the weekdays' corresponding numerical
values:
Sunday 0
Monday 1
Tuesday 2
Wednesday 3
Thursday 4
Friday 5
Saturday 6
249
System Real Time Clock RTC)
This feature will allow you to use the system timer of a Cubloc as an RTC.
You can use Time and TimeSet commands to access the following
addresses:
Address 10 will increment its value by 1 every second. When its value
becomes 60, address 11 will increment its value by 1. When address 11’s
value becomes 60, address 12 will increment its value by 1. When address
12’s value becomes 65535, it will reset back to 0. At power on, all
addresses are set to 0. The TimeSet command can be used to set the time
at the beginning of user’s program.
The system RTC (addresses 10 to 13)'s values are stored as raw binary
values, unlike the CB290's and CB405's on-chip RTC . There is no need to
convert the values using BCD2Bin and Bin2BCD.
The system RTC uses the processor’s system timer so there can be a slight
time difference ( < 1%) after a 24 hour period.
Address 13 will increment its value by 1 every second, just like address 10,
except when it reaches 65,535 it will reset to 0. Addresses 10 through 13
must be used with Cubloc Studio version 2.0.X and above.
250
TimeSet
TimeSet address, value
address : Address of time value (0 to 6)
value : Time value. (0 to 255)
The following is an example showing how to set the time, and output the
current time to the debug window:
Const Device=CB290
Dim i As Byte
TimeSet 0,0 'Sec
TimeSet 1,&H32 'Min
TimeSet 2,&H11 'Hour
TimeSet 3,&H1 'Date
TimeSet 4,&H5 'Day of the week
TimeSet 5,&H6 'Month
TimeSet 6,&H5 'Year
Do
i = Time(6)
Debug "Year ","200",Hex i, " "
i = Time(5)
Select Case i
Case 0
Debug "January"
Case 1
Debug "February"
Case 2
Debug "March"
Case 3
Debug "April"
Case 4
Debug "May"
Case 5
Debug "June"
Case 6
251
Debug "July"
Case 7
Debug "August"
Case 8
Debug "September"
Case 9
Debug "November"
Case 10
Debug "December"
End Select
i = Time(3)
Debug " ", Hex2 i 'Print date
Debug " "
i = Time(4)
Select Case i
Case 0
Debug "Sunday "
Case 1
Debug "Monday "
Case 2
Debug "Tuesday "
Case 3
Debug "Wednesday "
Case 4
Debug "Thursday "
Case 5
Debug "Friday "
Case 6
Debug "Saturday "
End Select
Debug Cr
i = Time(2)
Debug Hex2 i,":"
i = Time(1)
Debug Hex2 i,":"
i = Time(0)
Debug Hex i,Cr
Delay 1000
Loop
252
UDelay
UDelay time
time : Interval (1 to 65535)
253
UsePin
UsePin port, In/Out[, aliasName]
port : I/O port number. (0 to 255)
In/Out : "In" or "Out"
aliasName : Alias for the port (Optional)
This command is used to set a port's I/O mode and alias for Ladder Logic
programs. This is required before the ports can be used in Ladder Logic.
UsePin 0,In,START
UsePin 1,Out,RELAY
UsePin 2,In,BKEY
UsePin 3,Out,MOTOR
Use the FreePin command to return reassign the port back to BASIC.
254
UTMax
UTMax variable
variable : Variable to increment. (No String or Single)
255
Wait
Wait time
time : interval variable or constant (in milliseconds) 10 to 2,147,483,640
This command will generate a delay using the system clock. This delay
function is accurate to 10ms. It is much more precise than the Delay
command.
256
WaitTx
WaitTx channel
channel : RS-232 channel. (0 to 3)
This WaitTx command will wait until the send buffer is flushed.
Do While BFree(1,1)<49 ' Wait until all data has been sent
Loop
257
MEMO
258
Chapter 7:
Cubloc Display
Library
259
The Cubloc integrated display functions make it easy to control Comfile LCD
products such as the GHLCD or CLCD. Drawing lines, circles, boxes, and
printing strings can all be done with a single line of code.
The CLCD receives data via RS-232 or the CuNet I2C communication
protocol.
260
Set Display
Set Display type, method, baud, bufferSize
type : 0=RS-232LCD, 1= GHB3224, 2=CLCD
method : Communication method 0=CUNET, 1=RS-232 CH1
baud : Slave address when method = 0
baudRate when method = 1
bufferSize : Send buffer size (up to 128)
This command is used to initialize the display settings. It can only be used
once. All displays will communicate using the method set here.. This
command configures the type of LCD, the communication method, the baud
rate, and the buffer size. CLCDs will use method 0.
This method only supports the use of RS-232 channel 1 for display. For the
CB220, port 11(TX) is used.
CHANNEL 0
TX SOUT 1 24 VIN
RX SIN 2 23 VSS
ATN 3 22 RES
VSS 4 21 VDD
P0 5 20 P15
P1 6 19 P14
P2 7 18 P13
P3 8 17 P12 CHANNEL 1
P4 9 16 P11 TX
P5 10 15 P10 RX
P6 11 14 P9
P7 12 13 P8
For the CB280, pin 33 or pin 49 can be used. Pin 49 outputs a 12V level
signal and pin 33 outputs a 5V level signal.
CHANNEL 1
CHANNEL 0 CHANNEL 1
TX S OUT 1 17 VDD TX TX1 33 49 TTLTX1
RX RX1 34 50 TTLRX1
RX S IN 2 18 VSS
ATN 3 19 RE S AV DD 35 51 AVRE F
V SS 4 20 N/C N/C 36 52 P 48
P0 5 21 P16 P 24 37 53 P 31
P1 6 22 P17 P 25 38 54 P 30
P2 7 23 P18 P 26 39 55 P 29
P3 8 24 P19 PWM3 P 27 40 56 P 28
P4 9 25 P20 PWM4 P 47 41 57 P 32
P5 10 26 P21 PWM5 P 46 42 58 P 33
P6 11 27 P22 P 45 43 59 P 34
P7 12 28 P23 P 44 44 60 P 35
P8 13 29 P15 P 43 45 61 P 36
P9 14 30 P14 P 42 46 62 P 37
P 10 15 31 P13 P 41 47 63 P 38
P 11 16 32 P12 P 40 48 64 P 39
261
The following baud rates are supported:
The recommended buffer size is around 50 to 128. If the send buffer size
too small, data will not be displayed correctly. If the send buffer size is too
big, it will take up unnecessary memory.
The Set Display command can only be used once at the beginning of the
program.
For the CB220 , use I/O port 8 (Clock) and I/O port 9 (Data).
SOUT 1 24 VIN
SI N 2 23 VSS
ATN 3 22 RES
VSS 4 21 VDD
P0 5 20 P15
P1 6 19 P14
P2 7 18 P13
P3 8 17 P12
P4 9 16 P11
P5 10 15 P10
P6 11 14 P9 SDA
P7 12 13 P8 SCL
CUNET can be used with displays that support it. CUNET does not use baud
rate settings, it uses slave address settings instead.
Although multiple devices can be connected via I2C, for CUNET displays
only ONE device may be attached.
262
CLCD Module
On the back of the CLCD, a control board is attached. This control board
receives CuNET signals and prints to the CLCD.
DIP S/W
The CLCD can also communicate using RS-232. There are two RS-232
connectors, one for 3-pin 5V level signals and the other for 4-pin +/- 12V
level signals.
X
_R
5V D
D
D
C
A
L
GN
GN
5V
RX
5V
GN
N/
SC
SD
5V
Use the CLCD DIP switches to set the I2C slave address. The 4 th DIP switch
is not used.
DIP Switch RS232 Baud rate I2C Slave
Address
1 2 3
ON
2400 0
1 2 3
4800 1
ON
1 2 3
ON
9600 2
1 2 3
ON
19200 3
1 2 3
ON
28800 4
1 2 3
ON 38400 5
1 2 3
ON 57600 6
1 2 3
ON
115200 7
263
CuNET and RS-232 communication can both be used. If both are
connected, please make sure when one of them is processing, the other is
not.
data received is not a command, the CLCD will display it on the screen.
When using RS-232, the maximum baud rate settings for 12V(4-pin) levels
is 38400. For TTL 5V levels (3-pin), a baud rate up to 115200 can be used.
* The slave address specified in the Set Display command should match
that of the CLCD .
265
GHLCD Graphic LCD : GHB3224C
The GHLCD is an LCD that features the ability to display characters and
graphics on three different layers. Unlike the CLCD, the GHLCD supports
many different commands for easily drawing lines, circles, and boxes.
There are also commands to copy, cut, and paste graphics, and a BMP
downloader for downloading images to the GHLCD.
The GHB3224C model is a blue and white STN type LCD with a display area
of 320 by 240 pixels. There are 3 layers. The first layer is for text and the
other 2 layers can be used for graphics.
Layer1
Layer2
Layer3
266
The text layer is a 40x15 grid as illustrated below. Each character size is 8
by 16 pixels.
0 319
239
Please note that graphics or characters will appear randomly when trying to
print outside the specified range of pixels shown here.
267
GHB3224C supports CuNET.
The GHB3224C model supports CuNET. When using Cubloc with the
GHCLD, using CuNET instead of serial communications will free up the serial
port for other uses.
*Warning : The CuNET slave address and display slave address must match.
The display lave address can be set with the DIP switch.
268
Cls
Cls
Initialize the LCD and clear all layers.
(Set a little bit of delay to give the LCD time to initialize.)
Cls
Delay 200
Clear
Clear layer
Erase the specified layer(s).
CsrOn
CsrOn
Turn the cursor on. (Default is OFF).
CsrOff
CsrOff
Turn the cursor off.
Locate
Locate x,y
x : x coordinate of LCD
y : y coordinate of LCD
Set the position of the text cursor. After the CLS command, the LCD
defaults to position 0,0.
269
Print
Print text
text : String, String variable, or String constant containing text to be printed
This command prints characters on the text layer. To print characters to the
graphic layer, the GPrint command can be used.
270
CLCDOut
CLCDOut x, y, text
x: x coordinate of LCD
y: y coordinate of LCD
text : String, String variable, or String constant containing text to be printed
Using the Locate and Print command separately can occasionally result in
characters being printed to the wrong location. The CLCDOut command was
created to address this problem.
Do
CLCDOUT 1,1,"COMFILE",Dec I
Incr I
Delay 200
Loop
This command can only be used with CLCD modules manufactured after
December 2010. Users of existing CLCD modules manufactured before
December 2010 should contact Comfile Technology for an upgrade.
271
Layer
Layer layer1Mode, layer2Mode, layer3Mode
Layer1Mode : Set layer 1 mode (0=off, 1=on, 2=flash)
Layer2Mode : Set layer 2 mode (0=off, 1=on, 2=flash)
Layer3Mode : Set layer 3 mode (0=off, 1=on, 2=flash)
Set the mode of the specified layer. The flash mode will flash the layer at
16Hz. When the LCD is powered on, layer 1 and 2 defaults to on and Layer
3 defaults to off.
This command can be used to hide the process of drawing lines, circles,
etc.,. Set the layer to off when drawing, then set the layer on when
drawing is complete.
272
GLayer
GLayer layerNumber
LayerNumber : Set the graphic layer. (0,1,2)
There are 3 layers on the GHLCD GHB3224 series. Any of the layers may
be used as a graphic layer. Graphic commands such as Line, Circle, and
Box can be used to draw on a graphic layer. Normally, layer 1 is used for
text while layer 2 is used for graphics. Layers 2 and 3 have slightly
different characteristics. We recommend layer 2 for graphics that require a
lot of erasing.
Layer 1 can also be used as a graphic layer. In this case, you can even
erase text characters with graphic commands. To set Layer 3 to a graphic
layer, use the command Layer 3 On.
Overlay
Overlay overlayMode
overlayMode : Logical mode (0=OR, 1=AND, 2=XOR )
This command determines the drawing logic mode between layer 1 and
layer 2. Usually, layer 1 is for text and layer 2 is for graphics. By using this
command, the user can specify the overlay mode when layer 1 and layer 2
are displaying on the same position. The default is XOR, which will invert
when layer 1 and layer 2 print to the same positions. OR will allow graphics
on both layers to overlap. AND will display graphics only where they
overlap.
273
Contrast
Contrast value
value : Contrast value ( 1 to 1024 )
This command controls the contrast of the LCD. Use this command with
care, the contrast setting is sensitive. You will most likely need to adjust
the contrast wheel on the back of the LCD after using this command.
Contrast 450
Delay 500
Please add a 500ms delay after each call to this command to prevent an
overrun in the graphics buffer.
Light
Light value
value : Backlight 0=Off, 1=On
This command is used to turn the backlight on and off. The default is on.
WMode
WMode value
value : 0=Fast, 1=Slow
This command is used to select the method for refreshing the screen. To
draw graphics or text, data must be written to memory. Slow mode only
writes to memory between screen refreshes, so the screen redraws at a
slower rate, but produces no imperfections in the display. Fast mode writes
to memory at any time, so the screen refreshes at a faster rate, but can
cause imperfections in the display, such as flickering and noise.
274
Font
Font fontSize, eFontWidth
fontSize : 0 to 8 Font Selection
eFontWidth : 0 = fixed width, 1=variable width
This command sets the size and with of the font used to print characters.
The GHB3224 LCD has 4 different font sizes and 2 different widths.
275
Style
Style bold, inverse, underline
bold : 0=Normal, 2 or 3 =Bold
inverse : 0=Normal, 1=Inverse
underline : 0=Normal, 1=Underline
fonts. M AX M AX INVERSE
M AX UNDERLINE
CMode
CMode value
value : 0=Box type, 1=Underline type
276
Line
Line x1, y1, x2, y2
y2. 0
239
LineTo
LineTo x, y
to x, y. 0
LineTo 200,50
point
277
Box
Box x1, y1, x2, y2
0 319
BoxClear
BoxClear x1, y1, x2, y2
BoxFill
BoxFill x1, y1, x2, y2,logic
logicOperator : 0=OR, 1=AND, 2=XOR
278
Circle
Circle x, y, r
239
CircleFill
CircleFill x, y, r
Draw and fill a circle with x, y as the center
0 319
and with r as the radius. 0
CircleFill 200,100,50
279
Ellipse
Ellipse x, y, r1, r2
This command draws an ellipse with center x,
0 319
y, horizontal radius r1, and vertical radius r2. 0
239
ElFill
ElFill x, y, r1, r2
This command draws and fills an ellipse with
center x, y, horizontal radius r1, and vertical
radius r2.
ElFill 200,100,100,50
280
GLocate
GLocate x, y
This command specifies the graphical text
position on the current graphic layer.
GPrint
GPrint string
281
DPrint
DPrint string
DPRint "WE LOVE CUBLOC",CR ' Print String and go to next line
0 319
WE LOVE CUBLOC
239
This command prints faster than GPrint since it simply overwrites the
background. When trying to display animations or numbers that change
rapidly, such as a moving ball or the current time, DPrint will allow
smoother transitions.
DPrint can only be used with X-Axis values that are a multiple of 8. For
example, you can use GLocate 8,2 or GLocate 16,101 but not Glocate 10,
30.
282
Offset
Offset x, y
0 319
CUBLOC IS FUN
COMFILE TECHNOLOGY
239
0 319
CUBLOC IS FUN
COMFILE TECHNOLOGY
239
283
Pset
PSet x, y
Color
Color value
value : 0 = white, 1 = black
This command sets the current drawing color. The default value is 0
(white).
LineStyle
LineStyle value
This command sets the line style used when drawing lines and shapes. You
can make dotted lines by increasing value. The default value is 0, a solid
line.
DotSize
DotSize value, style
value : the size of the dot
style : the style of the dot: 0 = rectangular, 1 = circular
This command sets the dot size used when drawing lines and shapes. Value
sets the size of the dot, and style makes the dot either rectangular (0), or
circular (1).
DotSize 1,1 ' Set dot size to 1 and dot style to circular.
284
Paint
Paint x, y
Arc
Arc x, y, r, start, end
arc
' from 200 to 290 degrees.
285
DefChr
DefChr code, data
code : Custom character code (&hDB30 to &hDBFF)
data : 32byte bitmap data
DefChr &HDB30,&HAA,&HAA,&HAA,&HAA,&HAA,&HAA,&HAA,&HAA,_
&HAA,&HAA,&HAA,&H55,&HAA,&HAA,&HAA,&HAA,_
&HAA,&HAA,&HAA,&HAA,&HAA,&HAA,&HAA,&HAA,_
&HAA,&HAA,&HAA,&HAA,&HAA,&HAA,&HAA,&HAA
Print Chr(&HDB30)
Bmp
Bmp x, y, fileNumber, layer
x, y : Coordinates where bitmap should display
fileNumber : Bitmap File number
layer : Layer on which to display the bitmap.
The GHB3224 has flash memory for storing bitmap files. The BMP
Downloader is used to download bitmaps to the LCD. Once the bitmaps
files are stored in flash memory, this command can be used to display them
on the LCD.
*The GHB3224 has 102,400 bytes of flash memory for storing bitmap files
which can store approximately 10 320x240 full screen images.
286
Graphic Data Push and Pop Commands
The GHB3224 has a separate stack for storing graphic data. The current
screen can be pushed and popped on and off the stack. By saving and
restoring the screen to the stack, copy, cut, and paste features can be
implemented.
The GPush and GPop can be used for precise cutting of the current screen
while HPush and HPop can be used for higher speed operations with less
precision.
The stack is LIFO (last in first out), so it will pop the last screen that was
pushed onto the stack.
1 2 3 4 1
3 2 2
4 1 4 1
287
GPush
GPush x1, y1, x2, y2, layer
Push the rectangular area with upper-left coordinates x1, y1 and lower-right
coordinates x2, y2 onto the stack.
GPush 10,20,200,100,2
0 319
239
GPop
GPop x, y, layer, logic
logic =0 : OR
logic =1 : AND
logic =2 : XOR
logic =3 : Clear screen then pop
This command pops a screen from stack and displays it on the specified
layer at coordinates x,y. The logic parameter specifies how the popped
screen should be combined with the data currently displayed on the
specified layer.
GPop 120,20,2,0
0 319
23 9
0 319
23 9
288
GPaste
GPaste x, y, layer, logic
logic =0 : OR
logic =1 : AND
logic =2 : XOR
logic =3 : Clear screen then pop
This command pastes a screen from the stack and displays it on the
specified layer at coordinates x,y. The logic parameter specifies how the
popped screen should be combined with the data currently displayed on the
specified layer.
This command is identical to GPop except it will not remove the screen from
stack. Therefore, you can use this command if the current item in the stack
must be used again.
0 3 19
239
0 3 19
239
289
HPush
HPush x1, y1, x2, y2, layer
The HPush, HPop, and HPaste commands are similar to GPush, GPop, and
GPaste except the x coordinate in multiples of 8 as shown below:
HPush 6,20,12,100,2
HPop
HPop x, y, layer
This command is the same as GPop, except the x coordinate must be from 0
to 39.
HPop 10,20,2,0
HPaste
HPaste x, y, layer
This command is the same as GPaste except the x coordinate must be from
0 and 39.
290
GHB3224C DIP Switch Settings
On the back of the GHB3224B, there are DIP switches to set the RS232
baud rate and I2C slave address. GHB3224 DIP Switch number 4 is not
used.
1 2 3
ON
4800 1
1 2 3
ON
9600 2
1 2 3
ON
19200 3
1 2 3
ON
28800 4
1 2 3
ON
38400 5
1 2 3
ON
57600 6
1 2 3
ON
115200 7
291
Seven Segment Display: CSG
A seven segment display can be used to display numbers. Eight LEDs are
used for most seven segment displays as shown below, allowing decimal
points to be displayed as well.
As you can see above, the front has a 4 digit seven segment display and the
back has two I2C connections. After connecting the CSG to a Cubloc, you
can use the commands in the following table to easily and quickly display
the numbers you want.
292
CSGDec
The CSGDec command is used to print decimal values to the display.
To use CSG commands, the Set I2C command must be used beforehand.
Slave Address
The I2C slave address can be set using the DIP switches on the back of the
CSG module. A total of 4 addresses can be set per I2C line pair.
1 2 3
ON
1
1 2 3
ON
2
1 2 3
ON
3
293
To display more than 4 digits, use 2 CSG modules as shown below and set
different slave addresses for each.
CSGNPut
CSGNPut slaveAddress, digit, data
slaveAddress : CSG module I2C slave address
digit : Digit position (0 to 3)
data : Data (&H30 to &H39, &H41 to &H46)
This command displays the desired digit on the specified CSG module. The
most significant bit of the data parameter controls the decimal point.
You can use &H30 through 39 and &H41 through &H46 only.
294
CSGXPut
CSGXPut slaveAddress, digit, data
slaveAddress : CSG module I2C slave address
digit : Digit (0 through 3)
data : Bit array with each position corresponding to an LED segment
This command turns on the LED at the specified position. When displaying
anything other than numbers, this command can be used to control each
LED segment individually.
F B
G
E C
H
D
Bit 7 6 5 4 3 2 1 0
LED H G F E D C B A
CSGDec
CSGDec slaveAddress, data
slaveAddress : CSG I2C slave address
data : Decimal value
CSGHex
CSGHex slaveAddress, data
slaveAddress : CSG slave address
data : Hexadecimal value to display
295
MEMO
296
Chapter 8:
Interfacing
297
Input/Output Circuits
How to connect LEDs
Connect an LED as shown below, and output a logic high to the connected
I/O port to turn the LED ON.
330 ohm
CuBLOC I/O Port
C u B LO C I/ O P o rt
10Kohm
The Cubloc core module uses 5V power. When using a larger voltage,
please use an appropriate voltage converter or regulator.
298
How to Connect an Output Relay.
The following diagram shows how to connect an output relay to a Cubloc
I/O port. A photocoupler can be used to separate isolate 24V and 5V
circuits and protect against noise. Noise coming from 24V circuit will not
affect the 5V circuit and vice-versa.
24V
24V +
+
C u BLO C RELAY
I/ O P o rt 1N4148
24V
+
ZNR
PC-18T1
10K 222
LOAD
GND
24V -
24V +
C u BLO C
I/ O P o r t
10K
1N4148
LOAD
4 .7 K
P C - 1 8 T1
24V -
2.2K (1W)
CuBLOC 0.1uF 47K
I/O Port
680 270
+ 24V
24V +
22K KPC714 100
5V
299
How to connect an AD Input
To connect an AD input to the CB280, the AVDD and AVREF pins must be
connected to a 5V source. AVDD supplies power to the ADC of the Cubloc
and AVREF is the reference voltage that the ADC uses to do conversions. If
a 5V source is connected to the AVREF pin, input voltages from 0 to 5V will
be converted and if a 3V source is connected to the AVREF pin, input
voltages from 0 to 3V will be converted.
DC5V
DC5V
DC5V
5V
300
The following illustrates a 4-20mA signal connected to the ADC input port.
You can use a 230 Ohm and 20 Ohm resistor in series instead of a 250 Ohm
resistor.
For an input voltage from 0 to 10V, use 2 resistors as shown below. This is
called a voltage divider.
1Kohm
0~10V CUBLOC I/O Port
1Kohm
The Cubloc has 6 PWM ports. If you use a simple circuit like that shown
below, you can make a digital-to-analog converter.
301
RS-232 HOWTO
A PC's RS-232 interface typically use +/-12V signals, while the Cubloc's RS-
232 interface uses 5V signals. To avoid the need to make a separate circuit
to convert the +/-12V signals from the PC to the 5V signals of the Cubloc,
and vise-versa, the Cubloc was built with both a +/-12V RS-232 interface
(channel 0 – the download port) and a 5V RS-232 interface (channel 1).
+12V
SOUT 1 24 VIN
SIN 2 23 VSS
ATN 3 22 RES
-12V VSS 4 21 VDD
P0 5 20 P15
P1 6 19 P14
P2 7 18 P13
P3 8 17 P12
+5V
P4 9 16 P11
P5 10 15 P10
P6 11 14 P9
GND
P7 12 13 P8
For the CB280/380, RS-232 channel 1 can make use of either 5V or +/-12V
signals, but the pins are different. For a 5V interface, use pins 49 and 50.
For a +/12V interface use pins 33 and 34.
+12V
-12V
+12V
Downloading programs to the Cubloc is very easy, since the PC's RS-232
interface can connect directly to pins 1 and 2 of the Cubloc. For RS-422
and RS-485, 5V signals are provided on RS-232 channel 1.
302
For the CB280, a +/-12V RS-232 interface is provided in addition to the 5V
interface, but only one can be used at a time.
PC 2 RD
RS232C
3 TD
Port
5
5V
0.1uF
5V
0.1uF
1 16
0.1uF 2 15
3 14
0.1uF 4 13
5 12 CuBLOC RX
6 11 CuBLOC TX
0.1uF 7 10
8 9
The MAX232 is a very useful chip for converting between 5V and +/-12V
RS-232 signals.
1 MAX232 16
2 15
3 14 RS232C OUTPUT
4 13 RS232C INPUT
5 12 TTL OUTPUT
6 11 TTL INPUT
RS232C OUTPUT 7 10 TTL INPUT
RS232C INPUT 8 9 TTL OUTPUT
303
CUNET
CUNET is a communication protocol for Cubloc peripherals such as the
CLCD, GHLCD, CSG modules. With just 2 pins, SCL and SDA, up to 127
devices can be communicated with simultaneously. CUNET uses Cubloc’s
I2C protocol to perform the communication.
To use CUNET, please be sure to add pull up resistors(4.7K each) to the SCL
and SDA lines. SCL and SDA pins are in an open-collector configuration,
protecting against outside noise. It automatically removes pulses less than
50ns.
SOUT 1 24 VIN
SIN 2 23 VSS
ATN 3 22 R ES
VSS 4 21 VDD
P0 5 20 P15 1 GND
2
P1 6 19 P14 5V (RESET)
P2 7 18 P13 SCL
3
P3 8 17 P12 SDA
P4 9 16 P11 4
P5 10 15 P10
P6 11 14 P9
P7 12 13 P8
4.7Kohm x 2
When using CUNET, the connector’s pin 1 must be connected to ground, pin
2 to 5V or RESET, pin 3 to SCL, and pin 4 to SDA. This 4 pin configuration
is standard for all CUNET interfaces.
When using CUNET, the Cubloc core module will act as the master and the
connected device will act as as the slave. All CUNET devices will respond to
the Cubloc while in an idle state.
304
When the device is to be powered from the CUNET bus, pin 2 of the CUNET
device should be connected to 5V on the main module,:
Power
GND GND
5V 5V
SCL SCL
SDA SDA
CuNET Module
MAIN
Power Power
G ND GND
RESET RESET
SCL SCL
SDA SDA
CuNET Module
MAIN
305
Cubloc Study Board Circuit
Diagram
The Cubloc Study Board is useful for first timers and developers of the
Cubloc. Simple experiments can be created using switches, LED, RS-232
communication, I2C, piezo, ADC, toggle switches, and LCDs.
Download Port
Reset Switch Contac t
(LED, S/Ws)
(3)ALCD
Connector
I/O Ports
Contact
CuNET
(4)CuNET
Jumper
Bread Board
When 9V is supplied, the 5V voltage regulator inside the Study Board will
provide 5V to the main module and all peripherals. DC adapter polarity can
be used either way. For normal operation, please use a 9V adapter with at
least 200mA of current.
306
Cubloc Study Board Schematic
(1) To use RS-232 channel 1, please connect wires to the appropriate pins
on the interface labeled RS-232C in the upper right hand corner.
TX TX
RX RX
(3) When using CuNET, all jumpers must be shorted. If using pin 8 and 9
for general I/O, all jumpers should be left open.
307
About I²C…
The Cubloc provides an easy set of commands to communicate using the
I²C protocol. I²C communication is a widely used protocol, primarily for
communicating with ADC, EEPROM, DAC, and external I/O chips.
I²C uses two lines, SDA and SCL, and devices must operate in either master
or slave mode. The Cubloc can only be used as a master.
Be sure to configure I2C using the Set I2C command before using any
other I²C commands.
When SCL(Clock) and SDA(Data) are logic high, I²C is in an idle state. If a
START command is executed during the idle state, I²C begins.
When SCL and SDA are both logic low, I²C is in a busy state. If a STOP
command is executed during the busy state, I²C stops.
SDA
SCL
308
Using an EEPROM through I²C
R/W
S : Start
A : Acknowledge
P : Stop
The first bit is for the Start command. The 4 upper bits of CONTROL BYTE
must be 1010 and the 3 lower bits are for selecting the chip’s address. The
user may change the EEPROM chip’s address by configuring the chip.
For a read, 1 can be written into R/W and for a write, 0 can be written into
R/W. A is for acknowledgment of the 8 bits (1 byte) sent. Then HIGH
ADDRESS, LOW ADDRESS and DATA can be sent. When all data is sent, the
Stop command can be transmitted.
Next, we will look at how to read 1 byte from the EEPROM. Although it
might look more complex than writing 1 byte, we will soon find out that
they are very similar.
309
R/W Repeated Start R/W NoAck
S : Start
A : Acknowledge Read Point
P : Stop
Read Point is where the actual data will be read from the EERPOM. The first
part of the command is for setting the address to read data from.
And now, we will look at how to read multiple bytes from the EEPROM. If
we don’t send a STOP command, we can keep reading from the EEPROM
since it automatically increments its address. In this way, we can set the
starting address only once, and then read the rest of the data much faster.
310
I²C example
The following example shows a CB280 and 24LC32 EEPROM connected. A
value will be written to a specified address of the EEPROM and then read
back to display on the DEBUG window of Cubloc Studio.
24LC32 CB280
' Print Results
Debug Hex a,cr
Delay 500 A0
Loop A1 SCL P2
A2 SDA P3
err_proc:
Debug "Error !"
Do
Loop
311
More About I²C… (Advanced)
I²C is a common protocol used by many devices today. Cubloc uses I²C as
one of its primary communication protocols.
CuNET is built upon the I²C protocol. The main advantage of CuNET is that
it’s hardware controlled for LCDs. (Not CSG modules or I/O ports)
The Cubloc simulates a master I²C device. Therefore, all other connected
I²C devices must operate as slaves.
*Note: The I/O port used for I²C communication must be an Input/Output
port, not Input Only or Output Only.
5V
5V
SCL
SDA
Even though the maximum range for a typical I²C bus is around 12 feet, a
long distance extender chip such as the P82B715 can be used to extend the
bus to almost ¾ of a mile. A P82B96 can also be used as a buffer to
protect the I²C devices from electrical surges and interference.
312
N/C VCC
GND N/C
LDA LDA
SDA SDA
LCL LCL
SCL SCL
LONG CABLE
By using the P82B96, ground and power can be isolated on each end.
SDA VCC
SDA TX
RX SCL RX
P82B96
TX RY SCL TY
GND TY RY
P82B96
12V 12V
5V 5V
LONG CABLE
SDA SDA
SCL SCL
P82B96 P82B96
Please refer to Phillips website for more information on the specific chips
discussed here: http://www.standardics.nxp.com/.
313
If you are using I²C within 12 feet, we recommend using the following
protection circuit:
If the I²C devices are connected with no buffers, electrical interference can
cause damage either the Cubloc or the I²C slave device. By using diodes as
shown below, you can protect against most of the electrical interference. If
the devices are in a heavy, industrial environment, we recommend using
P82B96 chips as buffers.
A0 VCC
A1 WP
24LC32 CABLE
A2 SCL
SCL
GND SDA
SDA
CUBLOC
314
Chapter 9:
MODBUS
315
About Modbus…
Modbus is a protocol developed by Modicon as an interface to their PLCs.
It is usually used with devices like touchscreens, HMI devices, and SCADA
software; most of which now support Modbus.
In Modbus, there are master and slave devices. The master provides
command while the slave receives and responds to commands. The slave
can only send data to the master when requested; it cannot initiate
communication on its own.
Each slave has a unique address called a slave address. The master, using
those slave addresses, can talk to one slave at a time.
The master sends messages in units of "frames". Each frame contains the
slave address, command, data, and checksum codes. The slave receives a
trame, analyzes it, performs the requested function, and responds. When
responding to the master, slaves also respond in frames.
There are two Modbus transmission modes, ASCII and RTU. RTU is binary
and can be implemented in fewer bytes than ASCII, making it a little more
compact. ASCII uses a Longitudinal Redundancy Check (LRC) for error
checking while RTU uses a Cyclic Redundancy Check (CRC).
The table below shows a Modbus frame in both ASCII and RTU:
ASCII uses a colon (: ) to mark the start of a start of a frame and ends the
frame with a Carriage Return (CR) or a Line Feed(LF).
316
START SLAVE FUNCTION DATA LRC END
ADR
: (COLON) 2 Bytes 2 Bytes n Bytes 2 Bytes CR,LF
RTU requires no special characters to mark the start and end of a frame. It
uses 4 bytes of silence (a delay) to indicate the start and finish.
Cubloc support
NOTE: Starting with Cubloc Studio v3.3.1, the CB400, CB405 and
CB405RT no longer support Modbus ASCII. However, Modbus RTU is still
supported, so please use Modbus RTU instead.
317
Function Code 01: Read Coil Status
Function Code 02: Read Input Status
These functions are used to read the bit status of a PLC’s Register. The
following is an example that reads registers P20 through P56 from slave
address of 3.
Query:
Field RTU Bytes ASCII Bytes
Header : (colon) 1
Slave Address 0X03 1 03 2
Function Code 0X01 1 01 2
Start Address HI 0X00 1 00 2
Start Address LO 0X14 1 14 2
Length HI 0X00 1 00 2
Length LO 0X25 1 25 2
Error Check CRC 2 LRC 2
Ending Code CR LF 2
For Modbus ASCII, the LRC is the 2’s complement of the 8-bit sum of all
packet values except the colon, CR, and LF.
ASCII : 0 3 0 1 0 0 1 3 0 0 2 5 C 4 CR LF
Hex 3A 30 33 30 31 30 30 31 33 30 30 32 35 43 34 13 10
318
The response frame to the query above would be:
Response:
Field RTU Bytes ASCII Bytes
Header : (colon) 1
Slave Address 0X03 1 03 2
Function Code 0X01 1 01 2
Byte Count 0X05 1 05 2
Data 1 0X53 1 53 2
Data 2 0X6B 1 6B 2
Data 3 0X01 1 01 2
Data 4 0XF4 1 F4 2
Data 5 0X1B 1 1B 2
Error Check CRC 2 LRC 2
Ending Code CR LF 2
If you look at the response to the query, you can see that bits 20 through
27 make up one byte.
Data 1's least significant bit corresponds to P20 and Data 1's most
significant bit corresponds to P27. Likewise, Data 2 corresponds to P28
through P35, Data 3 to P36 through P43, Data 4 to P44 through 51, and
Data 5 to P52 through P59. We don't need P57 through P59, so the most
significant 3 bits of Data 5 can be disregarded.
319
Function Code 03: Read Holding Registers
Function Code 04: Read Input Registers
These functions can read 1 word (16 bits), and is usually used for counters,
timers, and data registers. The following shows an example that reads
slave address 3’s registers D0 through D2.
Query:
Field RTU Bytes ASCII Bytes
Header : (colon) 1
Slave Address 0X03 1 03 2
Function Code 0X03 1 03 2
Start Address HI 0X70 1 70 2
Start Address LO 0X00 1 00 2
Length HI 0X00 1 00 2
Length LO 0X03 1 03 2
Error Check CRC 2 LRC 2
Ending Code CR LF 2
The query above asked for a length of 3. And since 1 word is 2 bytes, we
will get 6 bytes total in the response.
Response:
Field RTU Bytes ASCII Bytes
Header : (colon) 1
Slave Address 0X03 1 03 2
Function Code 0X03 1 03 2
Byte Count 0X06 1 06 2
Data 1 LO 0X03 1 03 2
Data 1 HI 0XE8 1 E8 2
Data 2 LO 0X01 1 01 2
Data 2 HI 0XF4 1 F4 2
Data 3 LO 0X05 1 05 2
Data 3 HI 0X33 1 33 2
Length LO 0X03 1 03 2
Error Check CRC 2 LRC 2
Ending Code CR LF 2
320
Function Code 05: Force Single Coil
This function can be used to remotely set a register's value in units of bits.
To turn on a bit register, 0xFF00 must be set. To turn off a bit register,
0x0000 must be sent. Any other values will be ignored. The following is an
example showing slave address 3’s P1 register being turned on.
Query:
Field RTU Bytes ASCII Bytes
Header : (colon) 1
Slave Address 0X03 1 03 2
Function Code 0X05 1 05 2
Start Address HI 0X01 1 01 2
Start Address LO 0X00 1 00 2
Length HI 0XFF 1 FF 2
Length LO 0X00 1 00 2
Error Check CRC 2 LRC 2
Ending Code CR LF 2
Response:
Field RTU Bytes ASCII Bytes
Header : (colon) 1
Slave Address 0X03 1 03 2
Function Code 0X05 1 05 2
Start Address HI 0X01 1 01 2
Start Address LO 0X00 1 00 2
Length HI 0XFF 1 FF 2
Length LO 0X00 1 00 2
Error Check CRC 2 LRC 2
Ending Code CR LF 2
321
Function Code 06: Preset Single Registers
PLC’s can remotely control the status of its registers in word (2 bytes) units
through this function code.
Query:
Field RTU Bytes ASCII Bytes
Header 1 : (colon) 1
Slave Address 0X03 2 03 2
Function Code 0X06 2 06 2
Start Address HI 0X70 2 07 2
Start Address LO 0X01 2 01 2
Length HI 0X12 2 12 2
Length LO 0X34 2 34 2
Error Check CRC 2 LRC 2
Ending Code 2 CR LF 2
Response:
Field RTU Bytes ASCII Bytes
Header : (colon) 1
Slave Address 0X03 1 03 2
Function Code 0X06 1 06 2
Start Address HI 0X70 1 01 2
Start Address LO 0X01 1 70 2
Length HI 0X12 1 12 2
Length LO 0X34 1 34 2
Error Check CRC 2 LRC 2
Ending Code CR LF 2
322
Function Code 15: Force Multiple Coils
PLC’s can remotely control the status of its registers in units of multiple bits
through this function code. The following is an example showing slave
address 3’s P20 through P30 being turned on/off.
Query:
Field RTU Bytes ASCII Bytes
Header : (colon) 1
Slave Address 0X03 1 03 2
Function Code 0X0F 1 0F 2
Start Address HI 0X00 1 00 2
Start Address LO 0X14 1 14 2
Length HI 0X00 1 00 2
Length LO 0X0B 1 0B 2
Byte Count 0X02 1 02 2
Data 1 0XD1 1 D1 2
Data 2 0X05 1 05 2
Error Check CRC 2 LRC 2
Ending Code CR LF 2
The following table shows how Data in the above query is divided. P20
corresponds to the least significant bit of the first byte sent, and P27
corresponds to the most significant bit. There will be a total of 2 bytes sent
in this manner. Unused bits can be set to zero.
Bit 1 1 0 1 0 0 0 1 0 0 0 0 0 1 0 1
Reg P2 P2 P2 P2 P2 P2 P2 P2 P3 P2 P28
. 7 6 5 4 3 2 1 0 0 9
Response:
Field RTU Bytes ASCII Bytes
Header : (colon) 1
Slave Address 0X03 1 03 2
Function Code 0X0F 1 0F 2
Start Address HI 0X00 1 00 2
Start Address LO 0X14 1 14 2
Length HI 0X00 1 00 2
Length LO 0X0B 1 0B 2
Error Check CRC 2 LRC 2
Ending Code CR LF 2
323
Function Code 16: Preset Multiple Registers
PLC’s can remotely control the status of registers in units of multiple words
through this function code. The following is an example showing slave
address 3’s D0 through D2 being written.
Query:
Field RTU Bytes ASCII Bytes
Header : (colon) 1
Slave Address 0X03 1 03 2
Function Code 0X10 1 10 2
Start Address HI 0X70 1 70 2
Start Address LO 0X00 1 00 2
Length HI 0X00 1 00 2
Length LO 0X03 1 03 2
Byte Count 0X06 1 06 2
Data 1 HI 0XD1 1 D1 2
Data 1 LO 0X03 1 03 2
Data 2 HI 0X0A 1 0A 2
Data 2 LO 0X12 1 12 2
Data 3 HI 0X04 1 04 2
Data 3 LO 0X05 1 05 2
Error Check CRC 2 LRC 2
Ending Code CR LF 2
Response:
Field RTU Bytes ASCII Bytes
Header : (colon) 1
Slave Address 0X03 1 03 2
Function Code 0X10 1 10 2
Start Address HI 0X70 1 70 2
Start Address LO 0X00 1 00 2
Length HI 0X00 1 00 2
Length LO 0X03 1 03 2
Error Check CRC 2 LRC 2
Ending Code CR LF 2
324
Error Check
If there is an error in the data from the master, the slave will send back an
error code.
The error check is only for MODBUS ASCII; there is no error check in RTU.
Modbus RTU uses a CRC to check for errors in transmission.
325
Using Modbus with HMI / SCADA Addresses
The following describes the address mechanism to be used from HMI /
SCADA software.
326
Modbus ASCII Master
There are no special commands needed to set the Cubloc as a Modbus
master. One simply needs to use Cubloc's RS-232 commands like Get and
Put.
'Master Source
OpenCom 1,115200,3,80,80
On Recv1 Gosub GETMODBUS ' Data Receive Interrupt routine
Set Until 1,60,10 ' When Ending Code (10)
' on Channel 1 is discovered,
' create an interrupt
Do
For Port=2 To 4
BitWrite Port, 1 'Turn P0,P1,P2 ON!
Delay 100
Next
For Port=2 To 4
BitWrite Port, 0 'Turn P0,P1,P2 OFF!
Delay 100
Next
Loop
GETMODBUS:
If BLen(1,0) > 0 Then ' If buffer empty then
A=BLen(1,0) ' Store the buffer length in A!
Debug "GOT RESPONSE: "
B=GetStr(1,A) ' Store received data in B
Debug B
End If
Return
End
Sub BitWrite(K As Integer, D As Integer)
Dim LRC As Integer
PutStr 1,":0305"
PutStr 1,Hp(k,4,1)
If D=0 Then
PutStr 1,"0000"
327
LRC = -(3+5+K.Byte1+K.Byte0) 'Calculate LRC
Else
PutStr 1,"00FF"
LRC = -(3+5+K.Byte1+K.Byte0+0xFF) ' LRC
End If
PutStr 1,Hex2(LRC),13,10 'Send
End Sub
328
Modbus ASCII Slave
' Slave Source
Const Device = cb280
OpenCom 1,115200,3,80,80
Set Modbus 0,3
UsePin 2, Out
UsePin 3, Out
UsePin 4, Out
Set Ladder On
Master Slave
TX RX
RS232 RS232 P2
CH1 CH1 P3
RX TX
P4
GND GND
CB280 CB280
When the slave finishes processing the data sent by the master, the return
packet from the slave will cause a jump to the label GETMODBUS. The Set
Until command is used to check for the ending code LF (10).
The GetStr command is used to store all received data in RDATA. The data
in RDATA can be analyzed for any communication errors.
If the slave is not connected, the program will never jump to GETMODBUS.
329
MEMO
330
Chapter 10:
Application
Notes
331
NOTE 1. Switch Input
Let’s say you are developing some kind of machine controlled by a Cubloc.
The first thing you need is a user interface. Our task today is to build a
machine that will receive input from a switch and process it to perform a
task.
We will make a "start" and "stop" button that will turn a lamp on and off.
SOUT VIN
SIN VSS
ATN R ES
VSS VDD
START KEY P0 P15
P1 P14
P2 P13
330ohm.
P3 P12
STO P KEY P4 P11
P5 P10
10Kohm. P6 P9
P7 P8
CB220
<Filename: startstopkey.cul>
Dim a As Byte
Do
If In(0) = 1 Then a = 1
If In(4) = 1 Then a = 0
Out 14,a
Loop
When the switch is pressed, a "bouncing" effect occurs from the switch’s
mechanical spring.
332
The above picture shows how bouncing can confuse Cubloc controller with
alternating high and low signal levels. To get rid of this bouncing effect, a
capacitor and resistor can be added to filter it out.
Dim a As Byte
Do
If KeyInH(0,20) = 1 Then a = 1
If KeyInH(4,20) = 1 Then a = 0
Out 14,a
Loop
The 2nd parameter of KeyInH(0, 20) a delay before reading the input so
the signal bounce has time to settle. This delay is called the debouncing
time. The software will wait for 20ms before reading the input.
DC24V DC5V
2.2Kohm.
CUBLOC I/O
PC-18T1 10Kohm.
333
NOTE 2. Keypad Input
This application demonstrates interfacing to a 4x4 keypad and displaying
the results on a 4 digit seven segment module (CSG module)
CB280
P8 0 4 8 12
P9 P0
1 5 9 13
P1
2 6 10 14
P2
3 7 11 15
P3
P4 1 2 3 ST OP
P5 4 5 6 L
P6 7 8 9 R
P7 ESC 0 CLR GO
The CSG module is a 4 digit seven segment LED module that can be
connected via CUNET or the I2C interface to display numbers and custom
characters.
<Filename: csgprint.cul>
Const Device = CB280
Set I2c 9,8
Dim i As Byte
Do
CsgDec 0,i
i = i + 1
Loop
334
If you connect the CSG to the CuNet connector and execute the program
above, the CSG module will show incrementing numbers.
The key matrix can be easily read using the KeyPad command. If you look
carefully at the keypad, you will see that the scan code does not match the
actual key pressed. In order to read the correct key, we will use a Byte
array (KEYTABLE) to map the scan codes to the appropriate key.
And now we will make a simple program that receives input. When a
number key input is received, it is displayed to the CSG module as a 4 digit
number. The number is stored int the variable K, which is in BCD format.
We then use the BCD2Bin command to convert the BCD value back into
binary.
335
M = BCD2Bin(K)
Debug Dec M,CR
End If
Loop
When there is no input, the returned scan code is 255. Using the statment
Do While keypad(0) < 255, we wait until a key is released which will
return a scan code of 255. This is to allow the processor to stop reading
input while a key is being pressed. Otherwise, the processor might receive
multiple key inputs since the Cubloc's execution speed is very fast.
Using _D(0) = M, you can pass the scan code to Ladder Logic's D0 register.
If you need to use a keypad in Ladder Logic, you can make minor
modifications to this code to quickly get your results.
336
NOTE 3. Temperature Sensor
There are many uses for devices that sense temperature. Refrigerators,
heaters, air conditioners, automobiles, etc.. all make use of devices that
sense temperature.
We will take a look at the NTC thermistor and interface it with the Cubloc.
337
22 11061.0 11418.2 11776.4
23 10584.6 10921.6 11259.2
24 10131.3 10449.3 10767.5
25 9700.0 10000.0 10300.0
26 9281.3 9572.5 9864.0
For connecting the sensor to the Cubloc, please refer to the following circuit
diagram. To protect against voltage surges, a zener diode is recommended,
especially if the thermistor is attached to a long probe wire.
5.1V
1Kohm. ZEN ER
1% DIOD E 0.47uF CUBLO C
A/D CHA NNEL 0
NTC TH.
As you can see in the circuit diagram, we will be using an ADC (Analog-to-
Digital) converter to read the voltage across the sensor. The A/D converter
will convert the voltage into a value between 0 and 1024.
The most important part of this application note is the following table which
converts the temperature and voltage to an A/D value between 0 and 1024.
(Only some of the temperatures are shown.)
338
0 32610.0 4.851234752 994
1 30993.7 4.8437193 992
2 29466.8 4.835887094 990
3 28023.9 4.827728362 989
4 26660.0 4.819233234 987
5 25370.2 4.810391755 985
6 24150.1 4.801193902 983
7 22995.7 4.79162959 981
8 21903.1 4.781688696 979
9 20868.5 4.771361072 977
10 19888.7 4.760636561 975
11 18960.5 4.749505017 973
12 18080.8 4.737956327 970
13 17246.9 4.725980424 968
14 16456.1 4.713567319 965
15 15706.0 4.700707114 963
16 14994.4 4.68739003 960
17 14318.9 4.673606431 957
18 13677.7 4.659346849 954
19 13068.7 4.644602011 951
20 12490.3 4.629362861 948
21 11940.6 4.613620595 945
22 11418.2 4.597366683 942
23 10921.6 4.580592903 938
24 10449.3 4.563291365 935
25 10000.0 4.545454545 931
26 9572.5 4.527075313 927
27 9165.6 4.508146964 923
28 8778.3 4.488663246 919
29 8409.4 4.468618396 915
30 8058.1 4.448007162 911
31 7723.3 4.426824842 907
32 7404.3 4.405067304 902
33 7100.2 4.382731022 898
34 6810.2 4.359813102 893
35 6533.7 4.336311306 888
36 6269.8 4.312224084 883
37 6018.0 4.287550592 878
38 5777.7 4.262290722 873
39 5548.3 4.236445118 868
50 3606.1 3.914475937 802
51 3472.1 3.881948015 795
52 3343.7 3.848917708 788
53 3220.8 3.815397329 781
54 3103.1 3.781399998 774
55 2990.2 3.746939622 767
56 2882.1 3.712030877 760
57 2778.4 3.676689176 753
58 2679.0 3.640930651 746
59 2583.6 3.604772114 738
81 1220.4 2.748157207 563
82 1181.9 2.7084025 555
83 1144.8 2.668747011 547
84 1109.0 2.629210536 538
85 1074.5 2.589812422 530
86 1041.3 2.550571543 522
339
87 1009.2 2.511506263 514
88 978.3 2.472634416 506
89 948.5 2.433973277 498
90 919.8 2.395539544 491
91 892.0 2.357349316 483
92 865.3 2.319418079 475
93 839.4 2.281760687 467
94 814.5 2.244391354 460
95 790.4 2.207323646 452
96 767.1 2.170570465 445
97 744.7 2.134144055 437
98 723.0 2.098055989 430
99 702.0 2.062317177 422
100 681.8 2.026937858 415
101 662.2 1.99192761 408
102 643.3 1.957295352 401
103 625.0 1.92304935 394
104 607.3 1.889197225 387
105 590.2 1.855745964 380
106 573.7 1.822701928 373
107 557.7 1.790070865 367
108 542.2 1.757857926 360
109 527.2 1.726067674 353
239 33.5 0.162295782 33
240 33.0 0.159800146 33
241 32.5 0.157350769 32
242 32.0 0.154946682 32
243 31.5 0.152586936 31
244 31.0 0.150270604 31
245 30.5 0.147996779 30
246 30.0 0.145764577 30
247 29.6 0.143573131 29
248 29.1 0.141421596 29
249 28.7 0.139309144 29
250 28.2 0.137234968 28
'
' NTC THERMISTOR READ TABLE
' 10K DIODE TYPE
'
Const Device = cb280
340
Do
b = Tadin(0)
If b > 990 Or b < 400 Then
Debug "Out of Range" 'Check short or open th.
End If
For a=0 To 100
If b > TH_TABLE(a) Then Exit For
Next
Debug Dec a,cr
Delay 500
Loop
<Filename: ntcth.cul>
The formula for acquiring the A/D conversion value from the R-T table is as
follows:
5
V= x THR
(1000 + THR)
THR is the resistance value. 1000 is for a 1K Ohm resistor and 5 is for 5
volts. The 10 bit A/D converter of the Cubloc will return a value between 0
and 1024. Therefore, to get the A/D value, you must multiply the result, V,
by 204.8. A chart can be made by plotting this formula in a spreadsheet.
341
NOTE 4. Sound Bytes
This application note will demonstrate a few examples that generate sound
with the Cubloc. Sound can be generated using the Cubloc's I/O port or
PWM channel. With a PWM channel, sounds of varying frequency can be
generated.
SOUT VIN
SIN VSS
ATN RES
VSS VDD
P0 P15
P1 P14
P2 P13
Speaker
0.047uF P3 P12
P4 P11
P5 P10
P6 P9
P7 P8
CB220
The example above shows the CB220's PWM channel 0 of CB220 being used
with the FreqOut command to produce a sound.
With commands like FreqOut and Delay, simple sounds can be created.
342
Octave 4 Octave 5
A B C D E F G A B C D E F G
A B C D E F G H I J K L M N
To express one note, 2 characters are used: the first character is for the
frequency of the note and second character is for the length of the note.
Do
Loop
End
When using the PWM port for other purposes, the FreqOut command cannot
be used. To get around this limitation, any regular I/O port can be used to
create sound.
The following example shows how to make an alert sound with I/O port P4.
This program uses the Reverse and UDelay commands to alternate the I/O
port between logic-high and logic-low
343
Const Device = CB220
Low 4
Do
SOUND 4,110,60
SOUND 4,80,60
SOUND 4,40,160
Loop
End
344
NOTE 5. RC Servo Motor
RC servo motors (or "servos" for short) are used by many hobbyists to
make remote control cars, planes, etc.,. In recent years, they have been
used for robot arms, legs, and wheels.
With the Cubloc's PWM outputs, a servo can be easily added to any project.
A servo typically has three wires. The black wire is ground and red wire is
for power. The other yellow wire is for receiving a PWM signal. A typical
pulse rate is approximately 60 pulses per second.
SOUT VIN
SIN VSS
ATN RES
VSS VDD
P0 P15
Red : 5V
P1 P14
P2 P13
P3 P12
P4 P11
P5 P10
P6 P9
P7 P8
Black:GND CB220
The servo will move to a location set by the PWM's pulse and duty cycle,
and will hold its position.
o o o
-45 0 +45
345
Const Device = CB280
Low 5
Pwm 0,2500,32768
When the code above is executed, a 1ms pulse will be generated from I/O
port P5 and the RC servo motor will position itself to -45 degrees.
When the code above is executed, a 1.5ms pulse will be generated from I/O
port P5 and the RC servo will position itself to 0 degrees.
By simply changing the duty value of PWM command, the RC servo can
easily be controlled. For the CB220, 3 RC servos can be controlled
simultaneously while the CB280 and CB290 can control up to 6 RC servos.
The CB400/CB405 can control up to 12 servos.
346
NOTE 6. Digital Thermometer
The DS1620 is a digital thermometer. The chip has an internal temperature
conversion table so the user does not have to make a separate table.
Temperatures between -55 and 125 degrees Celsius can be measured by
the DS1620 in units of 0.5 degrees.
SOUT VIN
SIN VSS
ATN RES
VSS VDD
P0 P15
P1 P14
P2 P13
P3 P12
P4 P11
P5 P10
P6 P9
P7 P8
CB220
DQ VDD
CLK T (HI)
RST T(LO)
GND T(COM)
DS1620
ShiftOut ioclk,iodq,0,&hEE,8
Low iorst
Do
High iorst
ShiftOut ioclk,iodq,0,&haa,8
i = ShiftIn(ioclk,iodq,4,9)
i = i
debug dec i,cr
Low iorst
Delay 100
Loop
347
NOTE 7. DS1302 RTC
The DS1302 RTC (Real Time Clock) is a chip that acts as an electronic time
keeper. It has the ability to keep time and date in real-time. We will show
you how to implement this clock chip into your application.
SOUT VIN
SIN VS S
ATN RES
VSS VDD
P0 P 15
P1 P 14
P2 P 13
32768Hz P3 P 12
VCC2 VCC P4 P11
X1 SCLK P5 P 10
X2 I/O P6 P9
GND RST P7 P8
DS1302 CB220
Do
High iorst
adr = &h81
ShiftOut ioclk,iodio,0,adr,8
i = ShiftIn(ioclk,iodio,4,8)
Debug Hex i,cr
Low iorst
Delay 1000
Loop
348
The code above will read address 0, the seconds value, and display it in the
debug window. At the beginning of the program, writes are enabled to the
DS1302 chip and address 0 is set to 50 seconds. Within the Do Loop, data
is read from the DS1302. The DS1302 has 6 addresses as shown below:
10
ADDRESS 2 (hour) 0
A/ P HR HR
12/ 24
These addresses can be used to read and write to the DS1302. Please note
that the data is in BCD code format.
349
NOTE 8. MCP3202 12 Bit A/D
Conversion
The Cubloc has a 10 bit A/D converter. For greater resolution, meaning
greater precision, you can use a chip like the MCP3202. The MCP3202 is a
12 bit A/D converter that supports the SPI protocol. Here we will show you
how to implement this 12 bit A/D converter into your project.
SOUT VIN
SIN VSS
ATN RES
VSS VDD
P0 P15
P1 P14
P2 P13
P3 P12
CS VCC P4 P11
CH0 CLK P5 P10
CH1 DO P6 P9
GND DI P7 P8
MCS3202 CB220
350
The MCP3202 will convert voltage coming into CH0 and CH1 to a digital
value and retain it. SPI communication can then be used to read the value
from the MCP3202.
The voltage measured on the MCP320 CH0 and CH1 pins must not be
greater than the voltage supplied to the MCP3202. The result of the A/D
conversion is displayed in the debug window.
351
NOTE 9. Reading from and
Writing to an EEPROM
Typically EEPROMs store between 0.5 to 64KB of data. Data is retained
even after powering off. For example, if you wanted to retain a temperature
setting for a temperature controller, you could simply store the value of the
temperature in the EEPROM in case of a power outage.
The Cubloc has an internal EEPROM of 4KB. This EEPROM can be used to
store small amounts of data. If a larger EEPROM is needed, the 24LC512
can be used to store up to 64KB of data.
The following example will demonstrate how to access the 24LC32 4KB
EEPROM using the I2C protocol. Serial EEPROMs usually support either SPI
or I2C. I2C EEPROM part numbers begin with 24XXXX and SPI EEPROM
part numbers begin with 93XXX.
SOUT VIN
SIN VSS
ATN RES
VSS VDD
P0 P15
P1 P14
P2 P13
P3 P12
A0 VCC P4 P11
A1 NC P5 P10
A2 SC L P6 P9
GND SDA P7 P8
err_proc:
Debug "Error !"
Do
Loop
This example program will write a number to the EEPROM and read it back.
When this program runs correctly, numbers will increment in the debug
window. This program can be easily modified to support other EEPROMs.
Note: Typically, EEPROMs need a delay of about 5ms after a write operation.
353
MEMO
354
Chapter 11:
Ladder Logic
WARNING
If you do not use the SET LADDER ON command in BASIC, Ladder Logic will
not be executed.
355
Ladder Logic Basics
The following is an example of an electrical circuit with one switch and a
lamp.
If you express the above circuit using Ladder Logic, the following results:
P0 P9
As you can see, Ladder Logic is basically an easy way to model electrical
circuits in software. The switch corresponds to the input port P0 and the
lamp corresponds to the output port P9.
There are many ways to connect other devices such as timers, counters,
etc.,. The following is an example illustrating OR and AND logic using Ladder
Logic:
P0 P2 P9
P3
356
In this circuit diagram, P0 and P3 are ORed and that result is ANDed with P2
If you express the circuit diagram above in Ladder Logic, it will be as
follows:
In Cubloc Studio, the right side is not shown. In Cubloc Ladder Logic, P0,
P1, P2 are called "Registers".
357
Creating Ladder Logic Programs
The screen below shows you how Ladder Logic programs are created in
Cubloc Studio.
The red box shown above is the Ladder Logic cursor. You may use the
keyboard's up, down, left, and right keys or the mouse to control the red
box. After moving to the desired position, you can use the keys F3 through
F12 to place the desired symbol. You can also enter text for each symbol.
358
1. Press F3 to make a contact.
3. Press F5 couple times and you will see that it creates a line.
At the very end of the Ladder Logic program, you must always put an END
command.
359
Editing Ladder Logic Text
Editing Text
To edit existing text, place the cursor in the desired location and press
ENTER. A text box will appear for editing the text.
Erasing a Cell
To erase a cell, select it with the cursor, and...
You can press CTRL-D to erase a rung. This actually moves the rung to a
buffer.
360
Rung Recovery
To recover an erased rung (recall it from the buffer), press CTRL-U.
If you press the DEL key, the cell is erased and items on the right are pulled
one cell to the left.
If you press the INS key, a blank cell is inserted and items on the right are
moved one cell to the right.
Rung Copy
When the same style of rung is needed, you can press CTRL-A and it will
copy the rung above and pasted it one rung below.
361
Comments
You can enter comments by adding an apostrophe (').
362
Ladder Logic Block Copy and Paste
You can make a block of ladder logic and copy and paste to different parts
of the program.
Click and drag the mouse to select the block you wish to copy. Press CTRL-
C to copy and CTRL-V to paste. Similar to text editing, you can press CTRL-
X and CTRL-V to cut and paste also.
NOTE: Please be aware that the Ladder Logic editor does not support undo.
363
Monitoring
Cubloc Studio supports real-time monitoring of Ladder Logic.
C lic k H e re
364
Time Chart Monitoring
C lic k H e re
Relay select
Use/ Unuse
To use the Time Chart Monitor, you must turn Debug off in BASIC. To do
this, simply add the Set Debug Off command at the very beginning of the
BASIC program.
NOTE: While using the Time Chart Monitor, Ladder Monitoring may not be
used.
365
Watch Point
The Watch Point feature is useful when, in a long Ladder Logic program, two
registers cannot be viewed simultaneously in the same screen due to their
location in the ladder. Two apostrophes ('') are used to add a watch point.
Examples:
'’P0 '’P1 '’D0
NOTE: To set a watch point it’s two apostrophes(''), not a quotation mark(")
that must be used.
" "
' '
S H IF T + "
'
366
Options Window
If you select "Auto Run when download", the program will automatically
reset itself after downloading. This can become a problem for machines
that are sensitive to resets. To precisely control when to reset a program
turn this option off.
In the "Help" menu, Cubloc Studio upgrade and version information can be
found.
367
PLC Setup Wizard
To use Ladder Logic in Cubloc, you must create some minimal BASIC code.
Although very simple, this can be hard for first-timers. You can use the PLC
Setup Wizard and setup the I/Os you will be using and create the BASIC
source automatically.
As you can see in above screen, device name, I/O mode, alias, and other
features can be set simply by clicking.
You can set aliases for registers, turn Modbus on, and set the Modbus baud
rate. You can always review the current BASIC code generated in real-time
by navigating to the "Output BASIC code review" tab.
368
To get values for A/D, PWM, or COUNT, read from the D registers. For
ADC0, the AD value is stored in D(10). Reading from register D(10) will
return the AD value for ADC0.
For HIGH COUNT1, simply read from register D(39). If necessary, the
register for storing and writing values can be changed by modifying the
BASIC code.
When finished making changes, click the "Replace Basic Code" button to
produce the final BASIC code. Please be aware that any existing BASIC
code will be overwritten.
Changes can also be saved to a file by clicking the "Save As..." button.
Click the "Load..." button to restore from a saved file.
369
Usage of Ladder Register
With this feature, the aliases of all registers can be seen. A great deal of
time can be saved using this feature while debugging and developing the
final product. Go to Run->View Register Usage to open this window.
370
Registers
CB220, CB280 Registers
The following is a chart showing the registers for the CB220 and CB280.
P, M, and F registers are in bit units whereas T, C, and D are in word units.
To access P, M, and F registers in word units, use WP, WM, and WF
respectively.
371
CB290 and CB405 Registers
The following is a chart showing the registers for the CB290 and CB405.
The CB290 and CB405 have more M, C, T, and D registers than the CB220
and CB280.
P, M, and F Registers are in bit units whereas T, C, and D are in word units.
To access P, M, and F registers in word units, use WP, WM, and WF
respectively.
WP0 contains P0 through P15. P0 is the least significant bit of WP0 and P15
is the most significant bit. These registers are often used with commands
like WMOV.
WP0 P15 P0
372
Ladder Symbols
Contact A, Contact B
Input/Output registers are the most basic symbols among the registers in
Ladder Logic.
Contact A
Output R elay
Contact B
Function Registers
Function Relay
373
Internal Registers
Internal registers (M) only operate within the program. Unless connected to
an actual external port, they are only used internally. M registers can be
used as input or output symbols.
374
Using I/Os
Cubloc I/O ports can be used by both BASIC and Ladder Logic. Without
defined settings, all I/O ports are controlled in BASIC. The UsePin
command must be used to set the I/O ports for use in Ladder Logic.
UsePin 0,IN
UsePin 1,OUT
The above code sets P0 as input and P1 as output for use in Ladder Logic.
Those ports declared with the UsePin command will be re-flashed during a
ladder scan. Re-flashing means that, prior to a ladder scan, those ports
declared as IN with the UsePin command will have their input read and
copied to the port's corresponding P register. After the ladder scan, those
ports declared as OUT with the UsePin command will have the
corresponding P register's value written to the port's output.
INPUT REFLA SH
OUTPUT REFLASH
In BASIC, the commands In and Out an be used to control I/O ports. This
method directly accesses the I/O ports when reading and writing. In order
to avoid collisions between the two, I/O ports should be used in either
BASIC or Ladder Logic, but not both. Once a port is declared with the
UsePin command, it can only be used in Ladder Logic and cannot be
directly accessed in BASIC, except through the ladder registers.
375
Use of Aliases
When creating Ladder Logic using "Register numbers" such as P0, P1, and
M0, aliases can be used to help simplify programs.
Alias M0 = MAINMOTOR
Alias M2 = STATUS1
Alias M4 = MOTORSTOP
Aliases can be declared using either the UsePin or the Alias commands.
376
Starting Ladder Logic
Cubloc executes BASIC first. To enable Ladder Logic, use the command Set
Ladder On. After this command is executed, Ladder Logic will begin
running and perform ladder scans every 10 milliseconds.
If the Set Ladder On command is not used, Ladder Logic will not run and
no ladder scans will be performed
Set Ladder On
or
377
Using Ladder Logic Only
If only using Ladder Logic a minimal amount of BASIC code is required. The
BASIC code must contain, at a minimum, a device declaration, the Set
Ladder On command, and a Do...Loop to keep the program executing.
Port declarations, and aliases must also be declared in BASIC.
Do
Loop 'BASIC program will run in infinite loop.
378
Ladder Logic Commands
Low-Level Commands
Command Symbol Description
LOAD Contact A (Open by default)
OUT Output
379
High-Level Commands
Command Parameter Description
Data Commands
WMOV s,d Move Word
DWMOV s,d Move Double Word
WXCHG s,d Swap Word
DWXCHG s,d Swap Double Word
FMOV s,d,n Move Data Multiple Times (Fill Data)
GMOV s,d,n Move a Group of Data
Increment/Decrement Commands
WINC d Increment a Word by 1
DWINC d Increment a Double Word by 1
WDEC d Decrement a Word by 1
DWDEC d Decrement a Double Word by 1
Math Commands
WADD s1,s2,d Word Add
DWADD s1,s2,d Double Word Add
WSUB s1,s2,d Word Subtract
DWSUB s1,s2,d Double Word Subtract
WMUL s1,s2,d Word Multiplication
WDIV s1,s2,d Word Division
DWDIV s1,s2,d Double Word Division
Logical Commands
WAND s1,s2,d Word AND
DWAND s1,s2,d Double Word AND
WOR s1,s2,d Word OR
DWOR s1,s2,d Double Word OR
WXOR s1,s2,d Word XOR
DWXOR s1,s2,d Double Word XOR
Bit Shift Commands
WROL d Shift Word 1 bit to the Left
DWROL d Shift Double Word 1 bit to the Left
WROR d Shift Word 1 bit to the Right
DWROR d Shift Double Word 1 bit to the Right
380
LOAD, LOADN, OUT
LOAD is for contacts that are open by default and LOADN is for contacts
that are closed by default.
LOAD OUT
LOADN
Registers P M F S C T D Constants
that can be
used
LOAD O O O O O O
LOADN
OUT O O
P0
P2
P1
P3
381
NOT, AND, OR
NOT Symbol
AND
OR
NOT inverts the results. On the first rung in the example above, if P0 is ON
then P5 will be OFF.
AND is performed on two registers when they are placed horizontally beside
each other on the same rung. On the second rung in the example above,
both registers P0 and P1 must be ON in order for P5 to be ON.
BLOCK AND
BLOCK OR
382
SETOUT, RSTOUT
SETOUT turns a register ON, and holds it on even if its input condition
changes. Contrary to SETOUT, RSTOUT turns a register OFF and holds it
OFF even if its input condition changes.
On the first rung in the example below, SETOUT will turn P5 ON when P0
turns ON, and will hold P5 ON even if P0 turns OFF. In the second rung of
the example below, RSTOUT will turn P5 OFF when P1 is ON, and will hold
P5 OFF even when P1 turns OFF.
P0
P1
P5
383
DEFCON
Cubloc Studio 3.3.1 and later
DEFCON name, value
Please note you cannot use reserved words as constant names. For
example DEFCON WMOV 100 is not legal since WMOV is a reserved
command name.
384
DF, DFN
DF turns an output ON for the duration of one ladder scan when the input
changes from OFF to ON.
Conversely, DFN turns an output ON for the duration of one ladder scan
when the input changes from ON to OFF.
P0
P1
1 SC AN
P5
1 SC AN
P6
385
LOOP
Cubloc Studio 3.3.1 and later
LOOP label, register
386
MCS, MCSCLR
MCS and MCSCLR are used to conditionally execute rungs in a ladder.
Rungs between MCS x and MCSCLR x are executed only when the input to
MCS x is ON. If the input to MCS x is OFF, the rungs between MCS x and
MCSCLR x will not be executed. Using this command, an block of Ladder
Logic can be conditionally executed.
MCS # (0~7)
In the example above, when M0 turns ON, the rungs between MCS 0 and
MCSCLR are executed normally. If M0 is OFF, P5 and P6 will not be
processed.
When the input to an MCS is OFF, all outputs within that MCS block will turn
OFF, timers withing the MCS block will be reset, and counters within the
MCS block will stop counting.
387
The following example shows MCS 1 nested within MCS 0.
MCS numbers can be used repeatedly, but cannot be nested within one
another.
388
Step Control
S Registers are used for step control. Step control arguments must follow
the following format.
Relay ( 0~15 )
Step # ( 0~255 )
S7:126
Step control can be performed in two ways: "Normal Step" and "Reverse
Step". STEPSET is used for "Normal Step".
STEPSET
STEPSET turns ON the current step, but only if the previous step is ON.
STEPSET is processed one step at a time, in sequence, according to the step
number. For example, in the ladder above, when P1 turns ON, S0:2 is
turned ON, but only if S0:1 is ON. After S0:2 turns ON, S0:1 is turns OFF.
When P2 turns ON, S0:0 is turned ON and all other steps are turned OFF.
S0:0, or step 0, is used to reset. All other steps are processed in order.
P0
P1
P2
S0:0
S0:1
S0:2
389
STEPOUT
STEPOUT differs from STEPSET in two ways. First, it is not evaluated in
sequence, and second, when a step is turned ON, it remains in that state
regardless of its input condition. When using STEP OUT and a step is
turned ON, all other steps are turned OFF, so only one step is ON at any
given time.
In the ladder above, when P1 turns ON, S0:2 will turn ON. When P0 turns
on S0:1 turns ON, and S0:2 turns OFF and S0:1 turns ON. When PO turns
OFF, S0:1 is unchanged as each step, when turned ON, remains ON
regardless of its input condition. Finally when P2 turns ON, S0:0 turns ON
and S0:1 turns OFF.
P0
P1
P2
S0:0
S0:1
S0:2
390
TMON, TAMON
Cubloc Studio 3.3.1 and later
TMON arg, timeout
TAMON arg, timeout
Implements a timer that turns arg ON when the input turns ON, and
remains ON until timeout elapses. After timeout elapses, arg will remain on
regardless of the input.
P0
T0
1sec
If the input (P0) turns OFF, the output (T0) still remains ON.
391
P0
T0
1sec
After the timeout elapses, the output (T0) will turn OFF even if the input
(P0) remains ON.
P0
T0
1sec 1sec
If the input (P0) turns ON after the timeout has elapsed, the output (T0)
will turn ON again.
P0
T0
1sec
If the input (P0) turns OFF and then ON before the timeout has elapsed,
the output (T0) will still remain ON only for the specified timeout.
392
TON, TAON
TON and TAON provide the capability to delay the effect of an input
changing from OFF to ON for a specified amount of time. When the input
turns ON, a timer begins incrementing from its initial value to the specified
value. When the specified value is reached, the output is turned ON. TON
increments every 0.01 seconds and TAON increments every 0.1 seconds.
For TON and TAON there are 2 parameters. The first parameter can be any
value from T0 through T99, and the second parameter can be any numeric
constant or data register, such as D0.
Usable P M F S C T D Constants
Registers
TON, TAON O O O O
In the ladder above, when START turns ON, timer T0 increases from 0 to
100 in increments of 0.01 seconds. When 100 is reached, T0 will turn ON.
Here, 100 is equal to a time of 1 second for TON, and 10 seconds for TAON.
1sec
START
T0
When START turns OFF, the timer is reset to its initial value and T0 will turn
OFF. TON and TAON will reset their values upon powering OFF. Using KTON
and KTAON, the battery backup feature can be used to retain the timer's
values between power cycles. The example below illustrates how to reset
TAON.
393
TOFF, TAOFF
TOFF and TAOFF provide the capability to delay the effect of an input
changing from ON to OFF for a specified amount of time. When input turns
ON, the output turns ON immediately, but when the input turns OFF, the
output is kept ON for the set amount of time, and then turned OFF. TOFF
increases in increments of 0.01 seconds and TAOFF increases in increments
0.1 seconds.
For TON and TAON there are 2 parameters. The first parameter can be any
value from T0 through T99, and the second parameter can be any numeric
constant or data register, such as D0.
Usable P M F S C T D Constants
Registers
TOFF, TAOFF O O O O
In the ladder above, when START turns ON, the timer T0 immediately turns
ON. When START turns OFF, the timer will start decreasing from 100 to 0.
When 0 is reached, T0 will turn OFF. Here, 100 is equal to a time of 1
second for TOFF, and 10 seconds for TAOFF.
1sec
START
T0
394
CTU
This CTU command is an UP counter. When an input is received the counter
is incremented by one. When the counter reaches a specified value, the
specified register will turn ON There is also a reset input so the counter can
be reset as needed.
100 pulse
PULSE
RESET
C0
CTD
This CTD command is a DOWN counter. When an input is received the
counter is decremented by one. When the counter reaches 0, the specified
register will turn ON. There is a also a reset input so the counter can be
reset as needed.
100 pulse
PULSE
RESET
C1
395
UP/DOWN COUNTER
Below is a simple example of how an UP counter can be used to make an
UP/DOWN Counter.
P0 is for counting UP, P2 is for counting DOWN, and P1 is for resetting the
counter. When the counter reaches 100, C0 turns ON.
P0
P2
P1
C0
COUNT
C0
396
KCTU
This command is exactly same as the CTU command, except this command
will be able to remember its count when the module is powered off. By
comparison, the CTU command will lose its count when the module is
powered off. This command can only be used with modules that support
battery backup such as the CB290, CB405, and CuTouch.
100 pulse
P0
P1
C0
When using this command for the very first time, use the RESET signal to
initialize the counter. Otherwise the counter could start at some random
value.
KCTD
This command is exactly same as the CTD command, except this command
will be able to remember its count when the module is powered off. By
comparison, the CTD command will lose its count when the module is
powered off. This command can only be used with modules that support
battery backup such as the CB290, CB405, and CuTouch.
397
Comparison Logic
Compare 2 word(16 bit) or 2 double word(32 bit) values and turn on an
output when the conditions are satisfied.
When either D0=T1 or D1<100 and if C0>=99, M0 will turn ON. In other
words, either D0 has to equal to value of T1 or D1 has to be less than 100
while C0 must be greater than or equal to 99.
398
Storing Words and Double Words
A byte is 8 bits, a word is 16 bits, and a double word is 32 bits.
1 BYTE
1 WORD
DOUBLE WORD
Memory is divided into bytes, so words and double words must be split
across multiple bytes. Therefore, there are two ways to store words and
double words: Least significant byte first (Little Endian) or most significant
byte first (Big Endian). Cubloc stores its data in Little Endian.
D0 5678
D1 1234
D2
D3
D4
399
Binary, Decimal, Hexadecimal
To program well, we need to know binary, decimal, and hexadecimal
numbers. The following chart shows the relationships between these
notations.
Binary: 00101010B
Hexadecimal: 0ABCDH
We put a B at the end of the binary number and an H at the end for
hexadecimal numbers. To clearly identify that a hexadecimal value is a
number, we can put a 0 in front of the hexadecimal number.
(E.g. : 0ABH, 0A1H, 0BCDH )
*BASIC is slightly different from Ladder Logic in the way binary and
hexadecimal numbers are expressed. &B100010 is used for binary and
&HAB is used for hexadecimal.
400
WBCD
Cubloc Studio 3.3.1 and later
WBCD s, d
Converts the 16 bit binary value in s to a BCD (Binary Coded Decimal) value
ans stores the result in d.
D0 1234 D2 4660
04D2H 1234H
401
WBIN
Cubloc Studio 3.3.1 and later
WBIN s, d
Converts the 16 bit BCD value in s to binary and stores the result in d.
Values shown are in hexadecimal.
Usable P M F S C T D Constants
Register
s, d O O O
4660 1234
D2 D4
1234H 04D2H
402
WBCNT, DWBCNT
Cubloc Studio 3.3.1 and later
WBCNT s, d
DWBCNT s, d
Counts the number of active bits (1s) in s in stores the result in d. WBCNT
operates words (16 bits) and DWBCNT operates on double words (32 bits).
BIT 15 BIT 0
0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0
number of bits is 3
Usable P M F S C T D Constants
Register
s O O O
d O
403
WMOV, DWMOV
WMOV s, d
DWMOV s, d
The WMOV command moves 16 bit data from s to d. DWMOV is used for 32
bit data.
Usable P M F S C T D Constants
Register
s (Source) O O O O
d (Destination) O O O
When the input START turns ON, D0 will get 100. When IN0 turns ON, D2
will get 1234H.
D0 100
D1
D2 1234H
D3 0
D4
404
WXCHG, DWXCHG
WXCHG s, d
DWXCHG s, d
Usable P M F S C T D Constants
Registers
s O O O
d O O O
When START turns ON, D0 gets 100 and D1 gets 123. When IN0 turns ON,
D0 and D1 swap their data. The result is as shown below:
D0 123
D1 100
D2
D3
D4
405
FMOV
FMOV s, d, n
Usable P M F S C T D Constants
Registers
s O O O
d O O O
n O
D0 100
D1 100
D2 100
D3 100
D4 100
D5 100
406
GMOV
GMOV s, d, n
Usable P M F S C T D Constants
Registers
s O O O
d O O O
n O
D0 12
D1 34
D2 56
D3 78
D4 90
D5
D6
D7
D8
D9
D10 12
D11 34
D12 56
D13 78
D14 90
D15
D16
407
WCMP, DWCMP
Cubloc Studio 3.3.1 and later
WCMP arg1, arg2
DWCMP arg1, arg2
Compares arg1 and arg2. WCMP operates on words (16 bits) and DWCMP
operates on double words (32 bits).
Usable P M F S C T D Constants
Registers
arg1, arg2 O O O O
Flags:
F73 (zero flag) - ON if s1 and s2 are equal
F66 (less than flag) - ON if s1 is less than s2
F67 (greater than flag) - ON if s1 is greater than s2
408
WINC, DWINC, WDEC, DWDEC
WINC d
DWINC d
WDEC d
DWDEC d
Usable P M F S C T D Constants
Registers
d O O O
D0 99
D1
D2
D3
409
WADD, DWADD
WADD s1, s2, d
DWADD s1, s2, d
These commands adds s1 and s2 and store the result in d. WADD adds
word values and DWADD adds double word values.
Usable P M F S C T D Constants
Registers
s1 O O O O
s2 O O O O
d O O O
WSUB, DWSUB
WSUB s1, s2, d
DWSUB s1, s2, d
Usable P M F S C T D Constants
Registers
s1 O O O O
s2 O O O O
d O O O
410
WMUL
WMUL s1, s2, d
Usable P M F S C T D Constants
Registers
s1 O O O O
s2 O O O O
d O O O
In the ladder above, the result of 1234H * 1234H is stored as the double
word 14B5A90H in D1.
D0 1234H
D1 5A90H
D2 14BH
D0 3456H
D1 0012H
D2 0AD78H
D3 4B60H
D4 0
D5 0
411
WDIV, DWDIV
WDIV s1, s2, d
DWDIV s1, s2, d
These commands divide s1 and s2 and store the result in d and the
remainder in d+1. WDIV divides word values and DWDIV divides double
word values.
Usable P M F S C T D Constants
Registers
s1 O O O O
s2 O O O O
d O O O
D0 1234H
D1
D2 3
D3
D4 611H
D5 1
D0 5678H
D1 1234H
D2 7
D3 0
D4 0C335H
D5 299H
D6 5
D7 0
412
WOR, DWOR
WOR s1, s2, d
DWOR s1, s2, d
Usable P M F S C T D Constants
Registers
s1 O O O O
s2 O O O O
d O O O
D0 1200H
D1 34H
D2 1234H
413
WXOR, DWXOR
WXOR s1, s2, d
DWXOR s1, s2, d
These commands logically XOR s1 and s2 and store the result in d. WXOR
divides word values and DWXOR divides double word values.
Usable P M F S C T D Constants
Registers
s1 O O O O
s2 O O O O
d O O O
D0 1234H
D1 0FFH
D2 12CBH
When you want to invert specific bits, you can use XOR logical operation.
414
WAND, DWAND
WAND s1, s2, d
DWAND s1, s2, d
These commands logically AND s1 and s2 and store the result in d. WAND
divides word values and DWAND divides double word values.
D0 1234H
D1 0FFH
D2 34H
You can use AND operation when you want to use specific bits only.
415
WROL, DWROL
WROL d
DWROL d
This commands rotates the bits in d 1 bit to the left (least significant bit to
most significant bit). Bits on the far left are appended on the far right.
WROL operates on word values and DWROL operates on double word
values.
D0 0843H
D1
416
WROR, DWROR
WROR d
DWROR d
This commands rotates the bits in d 1 bit to the right (most significant bit to
least significant bit). Bits on the far right are appended on the far left.
WROR operates on word values and DWROR operates on double word
values.
D0
D1 0C210H
417
WRCL, DWRCL
Cubloc Studio 3.3.1 and later
WRCL arg
DWRCL arg
Shifts arg one bit to the left and fills the carry bit (F72). WRCL operates on
words (16 bits) and DWRCL operates on double words (32 bits).
Usable P M F S C T D Constants
Registers
arg O O O O
Example:
MSB LSB
BEFORE 1 0 1 0 1 1 0 0 0
CARRY (F72)
Shift to the left
MSB LSB
AFTER 1 0 1 0 1 1 0 0 0 0
CARRY (F72) Shift to the left
The most significant bit is moved to the carry flag, while the least significant
bit is populated with a 0.
418
WRCR, DWRCR
Cubloc Studio 3.3.1 and later
WRCR arg
DWRCR arg
Shifts arg one bit to the right and fills the carry bit (F72). WRCR operates
on words (16 bits) and DWRCR operates on double words (32 bits).
Usable P M F S C T D Constants
Registers
arg O O O O
Example:
MSB LSB
BEFORE 0 1 0 1 0 1 1 0 0
CARRY (F72)
Shift to the right
MSB LSB
AFTER 0 0 1 0 1 0 1 1 0 0
The least significant bit is moved to the carry flag, while the most significant
bit is populated with a 0.
419
WINV, DWINV
Cubloc Studio 3.3.1 and later
WINV arg
DWINV arg
Toggles, inverts, each bit in arg making each 1 a 0, and each 0 a 1. WINV
operates words (16 bits) and DWINV operates on double word (32 bits).
Usable P M F S C T D Constants
Registers
arg O O O O
420
WNEG, DWNEG
Cubloc Studio 3.3.1 and later
WNEG arg
DWNEG arg
Toggles the sign (+/-) of arg. WNEG operates words (16 bits) and DWNEG
operates on double word (32 bits).
Usable P M F S C T D Constants
Registers
arg O O O O
421
GOTO, LABEL
GOTO label
LABEL label
The GOTO command causes a jump to label. The LABEL command is used
to name a position in the program to jump to.
In the ladder above, when START turns ON, the program will jump to label
SK_1
In the ladder below, when D0 equals C0, the program will jump to SK_1.
422
CALLS, SBRT, RET
CALLS label
SBRT label
M a in P ro g ra m
R ET m u st b e u se d w h e n
t h e r e a r e s u b - ro u t in e s
S ta rt o f s u b - ro u t in e
E n d o f s u b - ro u t in e
E n d o f La d d e r
Please be aware that when adding sub-routines to your program, RET must
be appended to the end of main program, before the first subroutine. END
must be placed at the very end of the main program after the last
subroutine.
423
INTON
INTON s,d
Usually P M F S C T D Constants
egisters
s (Source) O O O O
d (Destination) O O O
424
TND
TND is a conditional exit command. TND is typically used to abort a ladder
scan.
In the ladder above, when P0 turns ON the ladder scan will be aborted.
It can also be used to exit from subroutines when a certain condition is met.
In the ladder above, when P1 turns ON, the subroutine will be aborted, but
the ladder scan will continue.
425
Special Registers
Cubloc contains a set of special registers that can be used to obtain the
Cubloc's current status or for timing functions and applications.
Special Explanation
Register
F0 Always OFF
F1 Always ON
F2 Turn on 1 SCAN time at POWER UP (Set Ladder On).
F3 Reserved
F4 Reserved
F5 Reserved
F6 Reserved
F7 Reserved
F8 1 SCAN every 10ms
F9 1 SCAN every 100ms
F10 Reserved
F11 Reserved
F12 Reserved
F13 Reserved
F14 Reserved
F15 Reserved
F16 Repeat ON/OFF every 1 Scan time. Period 2 Scan times.
F17 Repeat ON/OFF every 2 Scan times. Period 4 Scan times.
F18 Repeat ON/OFF every 4 Scan times. Period 8 Scan times.
F19 Repeat ON/OFF every 8 Scan times. Period 16 Scan times.
F20 Repeat ON/OFF every 16 Scan times. Period 32 Scan times.
F21 Repeat ON/OFF every 32 Scan times. Period 64 Scan times.
F22 Repeat ON/OFF every 64 Scan times. Period 128 Scan times.
F23 Repeat ON/OFF every 128 Scan times. Period 256 Scan times.
F24 Repeat ON/OFF every 10ms (20ms period)
F25 Repeat ON/OFF every 20ms (40ms period)
F26 Repeat ON/OFF every 40ms (80ms period)
F27 Repeat ON/OFF every 80ms (160ms period)
F28 Repeat ON/OFF every 160ms (320ms period)
F29 Repeat ON/OFF every 320ms (640ms period)
F30 Repeat ON/OFF every 640ms (1.280s period)
F31 Repeat ON/OFF every 2.56 seconds (5.12s period)
F32 Repeat ON/OFF every 5.12 seconds (10.24s period)
F33 Repeat ON/OFF every 10.24 seconds (20.48s period)
F34 Repeat ON/OFF every 20.48 seconds (40.96s period)
F35 Repeat ON/OFF every 40.96 seconds (81.92s period)
F36 Repeat ON/OFF every 81.92 seconds (163.84s period)
F37 Repeat ON/OFF every 163.84 seconds (327.68s period)
F38 Repeat ON/OFF every 327.68 seconds (655.36s period)
F39 Repeat ON/OFF every 655.36 seconds (1310.72s period)
F40 Call LADDERINT in BASIC
F41 Reserved
426
Special Explanation
Register
F42 Reserved
F56 StepPulse Channel 0 Status
F57 StepPulse Channel 1 Status
F65 (F_ERR) Error
F66 (F_<) < Result of WCMP, DWCMP
F67 (F_>) > Result of WCMP, DWCMP
F72 Carry Flag
(F_CARRY)
F73 Zero Flag
(F_ZERO)
* Turning F2 ON causes one ladder scan immediately when the Set Ladder
On command is executed in BASIC.
427
MEMO
428
Chapter 12:
Cutouch
429
Preface
The Cutouch is a fully integrated graphical touchscreen device containing a
Cubloc embedded computer. In recent years, touchscreens have found
increasing use in the field of industrial automation. However, most
touchscreen devices require connection to an external PLC, and require
learning yet another complex interface description method. In addition, the
cost of touchscreen interfaces has remained relatively high.
The embedded BASIC language can be used to draw graphics and print
characters to the LCD, and process touchscreen coordinates. BASIC makes
it easy to interface to various types of sensors, read analog values, process
text, perform mathematical operations, and perform custom RS-232
communication; tasks that are difficult to accomplish with a traditional PLC.
Ladder Logic is also available and can execute alongside BASIC to perform
sequential processing and real-time logic as done traditional PLCs.
The Cutouch has reprogrammable flash memory for BASIC and Ladder Logic
programs. An RS-232 serial port is used to download and debug code using
a Windows PC, but once disconnected from the PC, the Cutouch will operate
as a stand-alone device.
430
What is Cutouch?
The Cutouch is quite different from traditional touchscreens. Traditional
touchscreens are not a complete integrated product for solution
development. They are usually only capable of displaying graphics and
capturing touch input. Most touchscreens require an external controller in
order to affect the real world through I/O.
CUTOUCH
431
Cutouch Specifications
Processor Cutouch CT1721
Microprocessor Dual Core Atmega128 @ 18.432Mhz
Program Memory
80KB
(Flash)
Data Memory (RAM) 24KB(BASIC)+4KB(Ladder Logic)
EEPROM 4KB EEPROM
Program Speed 36,000/sec
General Purpose I/O - 82 I/O lines (TTL & 24V DC)
- 2 High-speed hardware-independent serial
Serial Ports for ports
Communication - Configurable Baud rates: 2400bps to
230,400 bps
8 channel 10-bit ADCs
Analog Inputs
Configurable Voltage: 0 to 5V OR 0 to 10V
- 6 Channel 16-bit PWMs (DACs)
Analog Outputs - Output Voltage Range: 0 to 5V
- Configurable Frequencies: 35hz to 1.5Mhz
External Interrupts 4 Channels
High Speed Counters 2 Channel 16-bit Counters
- Required Power: 24V DC
- Current Consumption w/ ports unloaded:
@ 24V w/ Backlight ON: 170mA
Power
@ 24V w/ Backlight OFF: 70mA
@ 12V w/ Backlight ON: 340mA
12V w/ Backlight OFF: 130mA
RTC (Real Time Clock) Yes
- 1 User Configurable Timer
Timers
- Configurable Interval Units = 10ms
*Yes, a 1 Farad rechargeable Super-
RTC Back-up
Capacitor is included.
Data Memory Back-up No
Operating Temperature 0 °C to 70 °C
Integrated Touch-screen Panel w/ 2mm
Package
Headers and 2.5mm RCABLE Headers
432
Hardware Requirements
The Cubloc Studio software used to develop programs for the Cutouch will
run on a computer with Windows 7, Vista, 2000, or 98 installed. If you
would like to use it in Linux/Unix/Macintosh environment, virtual machine
software, such as Vmware, will be needed to host the Windows operating
system.
433
Cutouch Dimensions
434
Cutouch I/O Ports
Name I/O Port Block Description
P0 I/O ADC0
P1 I/O ADC1
P2 I/O ADC2
P3 I/O Block 0 ADC3
P4 I/O ADC4
P5 I/O ADC5
P6 I/O ADC6
P7 I/O ADC7
P8 I/O PWM0
P9 I/O PWM1
P10 I/O PWM2
P11 I/O Block 1 PWM3
P12 I/O PWM4 / INT0
P13 I/O PWM5 / INT1
P14 I/O INT2
P15 I/O INT3 (RS485 Transmit enable)
P16 I/O High-speed Counter 0
P17 IN High-speed Counter 1
P18 OUTPUT BUZZER
P19~P23 N/C
P24~31 OUTPUT Block 3 8 Output only port –NPN TR
P32~39 OUTPUT Block 4 8 Output only port – NPN TR
P40~47 OUTPUT Block 5 8 Output only port – NPN TR
P48~55 OUTPUT Block 6 8 Output only port – NPN TR
P56~63 INPUT Block 7 8 Input only port- DC24V
P64~71 INPUT Block 8 8 Input only port - DC24V
P72~79 INPUT Block 9 8 Input only port - DC24V
P80~87 INPUT Block 10 8 Input only port - DC24V
N/C = No Connection
The Cutouch Add-On Board allows opto-isolated 24V DC inputs and 24V TR
outputs for J1 through J4.
* Please be careful to not input more than 5V into a Cutouch TTL ports as it
can damage the product.
435
There are extra RS232 headers as shown below:
GND
1 DTR Download / Monitoring
6
2 TD
RD
7
3 RD
8 TD
4
DTR
9
5
GND
GND TD RS232 Channel 1
RD
PC SIDE
436
Backup Battery
The Cutouch will maintain data in its volatile memory when powered off by
using a backup battery. If backup is not needed, the program should clear
the memory at the beginning of the program. Use RamClear to clear all
data memory at the start of your program.
*The Cutouch comes with a self-charging 1.0F super-capacitor that can last
about a day (up to 30hrs). You can replace it with a 10.0F super-capacitor
to extend the duration to about 300 hours (12.5 days). Adding a battery
can provide additional backup time depending on capacity. To add a backup
battery, please connect to the ports labeled “External Battery,” under the
super-capacitor (not visible when the back cover is in place).
437
+2
4V
P5
438
5
P5
4
P5
3
P
52
P5
1
P5
0
P
49
P4
8
+2
4V
P4
7
P4
6
P4
5
P
44
P4
3
LOAD
LOAD
LOAD
LOAD
P4
2
P
41
P4
0
+2
4V
P22
P23
P24
P25
P26
P27
+24V
P
39
P
38
P3
7
P3
6
P3
5
P3
4
Cutouch Output ports
P
33
P3
+24V
2
CT1721C
+
24
V
P
CT1721C has 32 NPN TR output ports. (Sink DC output)
31
P
30
P
29
G P
G N 28
N D
D 24 P2
V 7
G O P2
G N UT 6
D P
U
ND
5V T P2
5
O
U P
TP 24
U
T
Cutouch Input ports
CT1721C has 32 opto-isolated DC input.
87
86
85
4
3
82
81
C 80
9
78
77
6
5
4
3
C 72
1
0
9
8
67
6
5
C 64
3
2
1
60
59
58
7
C 6
4
`
P8
P8
P7
P7
P7
P7
P7
P7
P7
P6
P6
P6
P6
P6
P6
P6
P5
P5
M
M
P
P
P
P
P
P
P
P
P
P
P
O
O
DC24V INPUT
P56
P57
P58
P59
P60
P61
P62
P63
439
Cutouch A/D and TTL I/O Ports
CT1721C has 8 A/D ports.
A/ D INPUT
D
P7
P6
P5
4
3
2
P1
P0
N
P
P
P
G
OFF = 0 to 5V
ON = 0 to 10V
OFF
ON
14
13
0
D
P7
P6
P5
P4
3
2
P1
P0
T0
9
P8
S4 N D
TE
N
P1
P1
P1
P
P
P
N
G
P
P
N
N
G
85
G
C
C
H
H
R
/
5
P1
440
Cutouch jumper & connector
RS485 +
RS232 Batt. Backup
3V
Rs485 -
Rs485 +
Signal Ground
DC9 - 24V
- +
Download
RS232 CH1
-+
DC9 - 24V
441
Menu System Library
The Cutouch supports extra commands easily create and manipulate
menus. With the menu system library, a menu system as shown below can
be made in less than 5 minutes.
MENU Commands
The Cutouch has enough memory to support approximately 100 menu
buttons. The MenuSet command is used to set the x and y coordinates, as
well as the style of the menu. The MenuTitle command can be used to
name the menu. When touch input is received, the MenuCheck command
can be used to determine which menu button was pressed.
0 99
Each menu button's position and style can be changed using the MenuSet
command. Up to 100 buttons can be displayed on a single screen. Each
button can be assigned a different function on a different screen, resulting
in virtually unlimited menus and buttons.
442
MenuSet
MenuSet index, style, x1, y1, x2, y2
index : Menu index number
style : Button style; 0=none, 1=box, 2=box with shadow
x1,y1,x2,y2 : Menu button position
The index value must be a number from 0 through 99. Style is the shape
of the button, where 0 is for no box, 1 is for a box, and 2 is for a shadowed
box.
0 1 2
x1, y1, x2, y2 are the x and y axis positions of the left upper and lower
right corners. When this command is executed, the set part of the screen
becomes part of the button’s area.
MenuTitle
MenuTitle index, x, y, string
index :Menu index number
x,y : Title location based on left upper corner of button
string : Name of the menu
MenuSet only draws the box itself. Use the MenuTitle command to set the
name of the menu like shown here:
443
MenuCheck( )
variable = MenuCheck( index, touchX, touchX)
variable : Variable to store results (1 if selected, 0 if unselected)
index : Menu index number
touchX : x coordinate of the touch
touchY : y coordinate of the touch
Use this command to determine which menu button has been touched.
touchX and touchY are the x and y coordinates of where the user touched
the screen. If the coordinates of the touch match the coordinates of the
button, 1 is returned, otherwise 0 is returned.
If MenuCheck(0,TX1,TY1) = 1 Then
MenuReverse 0
Beep 18,180
End If
MenuReverse
MenuReverse index
index : Menu index number
This command causes the menu button identified by index to have it's
colors reversed. This is useful to provide visual feedback to a user,
indicating that a menu button has been touched.
444
Menu( )
Variable = Menu( index, position)
variable : Variable to store results (1 = selected, 0 = unselected)
index : Menu index
position : Position (0=x1, 1=y1, 2=x2, 3=y2)
To find the current coordinates of Menu buttons set using the MenuSet
command, use Menu() function to return the current status of the specified
menu. 0 will read x2, 1 will read y1, 2 will read x2, and 3 will read y2.
If Menu(0,1) < 100 THEN ' If Menu button 0’ s Y1 is less than 100
WaitDraw
WaitDraw
This command will wait for a drawing command to finish before resuming
execution.
The Cutouch has an internal buffer for receiving graphic commands from
the internal Cubloc controller. If this buffer fills up and data is sent to it, the
existing data in the buffer can be overwritten. In order to avoid these
situations, use the WaitDraw command to wait until the buffer has enough
space before sending graphic commands.
445
Touch Pad Input Example
Set Pad, On Pad, and GetPad can all be used to determine which menu
buttons were touched by the user.
All Pad commands are used for receiving and processing touch input. We
can use On Pad interrupts to capture touch events. The following is an
example program that uses the touch pad:
'
' DEMO FOR Cutouch
'
Const Device = CT1720
Dim TX1 As Integer, TY1 As Integer
Set Pad 0,4,5 ' (1) Activate Touch PAD Input
On Pad GoSub abc ' (2) Declare pad interrupts
Do
Loop
abc:
TX1 = GetPad(2) ' (3) Interrupt Service routine
TY1 = GetPad(2)
CircleFill TX1,TY1,10 ' (4) Draw a circle where it
' was touched
Return
(1) SET PAD 0, 4, 5 : This command will activate the pad inputs. (Syntax:
Set Pad mode, packetSize, bufferSize). The Cutouch has a separate
touch controller that will sense touch input and send it back to the CPU
using the SPI protocol. This touch controller will create a pad signal that is
equal to mode = 0 (Most significant bit, rising edge sampling). Input
packets are 4 bytes each ( x and y coordinates each get 2 bytes). Buffer
size is 5; one more than the actual packet size.
(2) On Pad GoSub abc: This command is for PAD interrupt declaration.
When a pad event occurs, the program will jump to label abc.
(3) This is the interrupt service routine. When a pad event occurs, this part
of the code will be executed, until Return. GetPad will read the data
received from touch pad, 2 bytes for the x coordinate and 2 bytes for the y
coordinate.
446
The following is an example combining menu and pad commands. When a
menu button is pressed, a beep will sound and the menu button will colors
reversed.
'
' DEMO FOR Cutouch
'
Const Device = CT1720
Dim TX1 As Integer, TY1 As Integer
Dim k As Long
Contrast 550
Set Pad 0,4,5
On Pad GoSub abc
MenuSet 0,2,8,16,87,63
MenuTitle 0,13,13,"Start"
MenuSet 1,2,96,16,176,63
MenuTitle 1,13,13,"End"
MenuSet 2,2,184,16,264,63
MenuTitle 2,13,13,"Restart"
Low 18
Do
Loop
abc:
TX1 = GetPad(2)
TY1 = GetPad(2)
CircleFill TX1,TY1,10
If MenuCheck(0,TX1,TY1) = 1 Then
MenuReverse 0
PulsUut 18,300 ' Send out beep to piezo
End If
If MenuCheck(1,TX1,TY1) = 1 Then
MenuReverse 1
PulsOut 18,300
End If
If MenuCheck(2,TX1,TY1) = 1 Then
MenuReverse 2
PulsOut 18,300
End If
Return
447
Cutouch Sample Programs
SAMPLE 1
Let’s make a simple counter that will print to the screen. The source files
used here are in the Cubloc Studio installation directory. (Usually
C:\Program Files\Comfile Tools\CublocStudio)
<Filename : CT001.CUL>
Const Device = Ct1720
Dim i As Integer
Contrast 550 ' LCD CONTRAST SETTING
Do
Locate 15,6
Print DEC5 i
Incr i
Delay 200
Loop
448
SAMPLE 2
The following sample program will display a "RESET" button and will
increment the number displayed on the screen every time the button is
pressed.
<Filename : CT002.CUL>
Const Device = Ct1720
Dim i As Integer
Dim TX1 As Integer, TY1 As Integer
Contrast 550
Set Pad 0,4,5
On Pad GoSub GETTOUCH
MenuSet 0,2,120,155,195,200
MenuTitle 0,20,14,"RESET"
Do
Locate 15,6
Print DEC5 i
Incr i
Delay 200
Loop
GETTOUCH:
TX1 = GetPad(2)
TY1 = GetPad(2)
If MenuCheck(0,TX1,TY1) = 1 Then
PulsOut 18,300
I = 0
End If
Return
The SET PAD command activates touch input. The ON PAD command
jumps to a label when touch input is received. The MENUSET command
sets the desired touch input area and the MENUTITLE command sets the
name of the button itself. PulsOut outputs a BEEP sound to the piezo
speaker.
449
SAMPLE 3
This sample draws a circle wherever the screen is touched.
<Filename : CT003.CUL>
Const Device = Ct1720
Dim TX1 As Integer, TY1 As Integer
Contrast 550
Set Pad 0,4,5
On Pad Gosub GETTOUCH
Do
Loop
GETTOUCH:
TX1 = Getpad(2)
TY1 = Getpad(2)
Circlefill TX1,TY1,10
Pulsout 18,300
Return
450
SAMPLE 4
This sample make a virtual keypad that accept numerical input.
1 2 3
1234 4 5 6
7 8 9
0 ENTER
<Filename : CT004.CUL>
Const Device = Ct1720
Dim TX1 As Integer, TY1 As Integer
Dim I As Integer
Contrast 550
Set Pad 0,4,5
MenuSet
MenuSet 0,2,165,50,195,75
MenuTitle 0,11,4,"1"
MenuSet 1,2,205,50,235,75
MenuTitle 1,11,4,"2"
MenuSet 2,2,245,50,275,75
MenuTitle 2,11,4,"3"
MenuSet 3,2,165,85,195,110
MenuTitle 3,11,4,"4"
MenuSet 4,2,205,85,235,110
MenuTitle 4,11,4,"5"
MenuSet 5,2,245,85,275,110
MenuTitle 5,11,4,"6"
MenuSet 6,2,165,120,195,145
MenuTitle 6,11,4,"7"
MenuSet 7,2,205,120,235,145
MenuTitle 7,11,4,"8"
MenuSet 8,2,245,120,275,145
MenuTitle 8,11,4,"9"
MenuSet 9,2,165,155,195,180
MenuTitle 9,11,4,"0"
MenuSet 10,2,205,155,275,180
MenuTitle 10,17,4,"ENTER" I =0
Do
Loop
GETTOUCH:
TX1 = GetPad(2)
TY1 = GetPad(2)
If MenuCheck(0,TX1,TY1) = 1 Then
451
I = I << 4
I = I + 1
PulsOut 18,300
Elseif MenuCheck(1,TX1,TY1) = 1 Then
I = I << 4
I = I + 2
PulsOut 18,300
Elseif MenuCheck(2,TX1,TY1) = 1 Then
I = I << 4
I = I + 3
PulsOut 18,300
Elseif MenuCheck(3,TX1,TY1) = 1 Then
I = I << 4
I = I + 4
PulsOut 18,300
Elseif MenuCheck(4,TX1,TY1) = 1 Then
I = I << 4
I = I + 5
PulsOut 18,300
Elseif MenuCheck(5,TX1,TY1) = 1 Then
I = I << 4
I = I + 6
PulsOut 18,300
Elseif MenuCheck(6,TX1,TY1) = 1 Then
I = I << 4
I = I + 7
PulsOut 18,300
Elseif MenuCheck(7,TX1,TY1) = 1 Then
I = I << 4
I = I + 8
PulsOut 18,300
Elseif MenuCheck(8,TX1,TY1) = 1 Then
I = I << 4
I = I + 9
PulsOut 18,300
Elseif MenuCheck(9,TX1,TY1) = 1 Then
I = I << 4
PulsOut 18,300
Elseif MenuCheck(10,TX1,TY1) = 1 Then
I = 0
PulsOut 18,300
End If
Locate 3,3
Print HEX4 I
Return
The final value "I" is stored in BCD format. The Bcd2Bin command can be
used to convert it back to a binary number.
452
SAMPLE 5
This sample uses CUCANVAS to make some menus. To create the virtual
keypad shown in the previous page, it would take a long time to manually
code it and place buttons. CUCANVAS can save time.
Run CUCANVAS and press the "Add Form" button in the upper right hand
corner. Enter a desired name for the new form ("NumKey" is used in this
example).
On the left side of CUCANVAS, there a toolbar with an arrow, box, filled box,
circle, filled circle, line, text, and menu box. Select the last button, menu
box, and draw a small box on the screen.
The 0 on the button means the menu number is 0. In the actual screen,
this number will not be displayed. Type "1" in the Title field on the top.
There is now a "1" button.
453
A keypad like the one shown below can be created in less than 5 minutes by
creating each button in the same way the "1" button was made.
454
Now for the fun part. Simply click "Generate" on the menu bar, and click
"View Basic Code." CUCANVAS will generate a subroutine that includes the
buttons just created. Simply copy (Ctrl+C) and paste (CTRL+V) this code
to Cubloc Studio. A complete menu has just been created in a matter of
minutes. The "To Clipboard" button will also copy the generated code to the
clipboard.
Instead of copying and pasting repetitive menu creations, include files can
be used. Click the "Save to File" button and save the code as an include
(*.inc) file.
455
Include files make it easy to change the interface of a program without a lot
of cut and paste operations within the main code.
<Filename : CT005.CUL>
Const Device = Ct1720
Dim TX1 As Integer, TY1 As Integer
Dim I As Integer
Contrast 550
Set Pad 0,4,5
On Pad GoSub GETTOUCH
NUMKEY ' Execute the Sub-routine in INCLUDE file
I =0
Do
Loop
GETTOUCH:
TX1 = GetPad(2)
TY1 = GetPad(2)
If MenuCheck(0,TX1,TY1) = 1 Then
I = I << 4
I = I + 1
PulsOut 18,300
Elseif MenuCheck(1,TX1,TY1) = 1 Then
I = I << 4
I = I + 2
PulsOut 18,300
Elseif MenuCheck(2,TX1,TY1) = 1 Then
I = I << 4
I = I + 3
PulsOut 18,300
Elseif MenuCheck(3,TX1,TY1) = 1 Then
I = I << 4
I = I + 4
PulsOut 18,300
Elseif MenuCheck(4,TX1,TY1) = 1 Then
I = I << 4
I = I + 5
PulsOut 18,300
Elseif MenuCheck(5,TX1,TY1) = 1 Then
I = I << 4
I = I + 6
PulsOut 18,300
Elseif MenuCheck(6,TX1,TY1) = 1 Then
I = I << 4
I = I + 7
PulsOut 18,300
Elseif MenuCheck(7,TX1,TY1) = 1 Then
I = I << 4
456
I = I + 8
PulsOut 18,300
Elseif MenuCheck(8,TX1,TY1) = 1 Then
I = I << 4
I = I + 9
PulsOut 18,300
Elseif MenuCheck(9,TX1,TY1) = 1 Then
I = I << 4
PulsOut 18,300
Elseif MenuCheck(10,TX1,TY1) = 1 Then
I = 0
PulsOut 18,300
End If
Locate 3,3
Print HEX4 I
Return
End
#INCLUDE "CT005.INC"
We must place the #include command at the end of the code, as the
generated code is in the form of a subroutine, which must come after the
End statement in the main program.
457
SAMPLE 6
This sample demonstrates how to set up a paging and menu system.
MAIN
CurrentScreen = _MAINMENU
Do
If CurrentScreen = _MAINMENU Then
Set Onpad Off
DisplayTime
Set Onpad On
Endif
Delay 250
Loop
ProcessTouch:
TX1 = GetpadX()
TY1 = GetpadY()
458
ProcessSubMenu2
Case _SUBMENU3
ProcessSubMenu3
Case _SUBMENU4
ProcessSubMenu4
End Select
Return
End
Sub ProcessMainMenu()
If Menucheck(0,TX1,TY1) = 1 Then
FlashMenu 0
CurrentScreen = _SUBMENU1
Cls
SUBMENU1
Sub ProcessSubMenu1()
If Menucheck(0,TX1,TY1) = 1 Then
FlashMenu 0
Beeper 1
Sub ProcessSubMenu2()
If Menucheck(0,TX1,TY1) = 1 Then
FlashMenu 0
459
Beeper 2
Sub ProcessSubMenu3()
If Menucheck(0,TX1,TY1) = 1 Then
FlashMenu 0
Beeper 3
Sub ProcessSubMenu4()
If Menucheck(0,TX1,TY1) = 1 Then
FlashMenu 0
Beeper 4
For i = 1 To Num
High 18
Delay 100
Low 18
Delay 200
Next
End Sub
#include "CT1721treemenu.inc"
SUB SUBMENU1()
FONT 6,1
STYLE 0,0,0
GLOCATE 96,24
GPRINT "Submenu 1"
LINESTYLE 0
DOTSIZE 0,0
COLOR 1
BOX 80,16,232,56
FONT 2,1
MENUSET 0,2,88,88,224,120
MENUTITLE 0,22,8,"Beep 1 time"
MENUSET 1,2,0,208,72,239
MENUTITLE 1,10,7,"<BACK"
FONT 4,0
END SUB
461
SUB SUBMENU2()
FONT 6,1
STYLE 0,0,0
GLOCATE 96,24
GPRINT "Submenu 2"
LINESTYLE 0
DOTSIZE 0,0
COLOR 1
BOX 80,16,232,56
FONT 2,1
MENUSET 0,2,88,88,224,120
MENUTITLE 0,16,8,"Beep 2 times"
MENUSET 1,2,0,208,72,239
MENUTITLE 1,10,7,"<BACK"
FONT 4,0
END SUB
SUB SUBMENU3()
FONT 6,1
STYLE 0,0,0
GLOCATE 96,24
GPRINT "Submenu 3"
LINESTYLE 0
DOTSIZE 0,0
COLOR 1
BOX 80,16,232,56
FONT 2,1
MENUSET 0,2,88,88,224,120
MENUTITLE 0,16,8,"Beep 3 times"
MENUSET 1,2,0,208,72,239
MENUTITLE 1,10,7,"<BACK"
FONT 4,0
END SUB
SUB SUBMENU4()
FONT 6,1
STYLE 0,0,0
GLOCATE 96,24
GPRINT "Submenu 4"
LINESTYLE 0
DOTSIZE 0,0
COLOR 1
BOX 80,16,232,56
FONT 2,1
MENUSET 0,2,88,88,224,120
MENUTITLE 0,16,8,"Beep 4 times"
MENUSET 1,2,0,208,72,239
MENUTITLE 1,10,7,"<BACK"
FONT 4,0
END SUB
462
Chapter 13:
CB405RT
463
CB405RT
- CB405 core module
- equipped with a built-in accurate RTC (DS3231)
- equipped with a built-in 16-bit A/D converter (8 channels)
The CB405RT is a product, which adds an RTC and a 16-bit ADC to the
original CB405. The external dimensions and pinout are the same as the
CB405 with the exception that ports P32 to P37 are used for the 16-bit ADC
and therefore cannot be used.
464
CB405RT-Related Commands
CB405RT-related commands can be used on in Cubloc Studio V2.6.B or in
later versions.
RTCRead( )
variable = RTCRead( address )
variable : variable to store the result (Byte type)
address : RTC’s address
If you see the address table of RTC chip (DS3231) below, you will notice
that the time data are stored from addresses 0 through 6. The rest of the
addresses are related to the alarm and RTC chipsetting; thus, we
recommend those interested to refer to the DS3231 databook.
465
CAUTION:
To use CB405RT, in the beginning of the program, one must replace Const
Device = CB405 with #Include "CB405RT".
Wait 100
RTCwrite 0,&h20 ' Sec
RTCwrite 1,&h59 ' Min
RTCwrite 2,&h23 ' Hour 24h
RTCwrite 3,&h7 ' day 1-7, 1=Sun, 2=Mon, 3=Tue, 4=Wed, 5=Thu,
' 6=FRI, 7=SAT
RTCwrite 4,&h31 ' Date
RTCwrite 5,&h12 ' Month
RTCwrite 6,&h08 ' Year
Do
i = RTCread(0)
Debug Goxy,1,1,Hex2 i, " Sec"
i = RTCread(1)
Debug Goxy,1,2,Hex2 i, " Min"
i = RTCread(2) And &h3f
Debug Goxy,1,3,Hex2 i, " Hour"
i = RTCread(3)
Debug Goxy,1,4,Hex2 i, " Day"
i = RTCread(4)
Debug Goxy,1,5,Hex2 i, " Date"
i = RTCread(5)
Debug Goxy,1,6,Hex2 i, " Month"
i = RTCread(6)
Debug Goxy,1,7,Hex2 i, " Year"
Wait 500
Loop
466
When the above example program is executed, the following debug window
will appear.
467
RTCWrite
RTCWrite address, data
address : RTC chip’s address
data : the variable or constant to store
This command writes a new data value to the specified address of the RTC
chip.
The DS3231 is an RTC chip that avoids such problems. It has a thermistor
(i.e., a temperature sensor) along with a 32 KHz crystal. The oscillation
frequency is adapted based on the temperature sensed by the thermistor.
As a result, the crystal oscillations don’t fluctuate and the clock keeps better
time.
But be aware that although the DS3231 keeps great time under varying
temperatures, it is still not 100% perfect and should be verified for time
critical applications.
468
HADIn( )
variable = HADIn( channel )
variable : Variable to store the result (Integer or Long type)
channel : AD channel
This function stores the result of 16-bit A/D conversion at the specified
variable.
#include "CB405RT"
Do
Debug Goxy,1,1,Dec5 HADIn(0)
Wait 500
Loop
The 16-bit ADC gives a result ranging from 0V to Vref expressed as one of
the 65536 equally divided intervals. It is capable of much more accurate
measurement compared to a 10-bit ADC.
469
HADIn2( )
variable = HADIn2(channel combination)
variable : Variable to store the result (Integer or Long type)
channel : Combination: AD channel combination code
This command is for a differential A/D input. Two channels are paired up,
and the A/D converter converts the difference between the two voltages.
#include "CB405RT"
Do
Debug Goxy,1,1,Dec5 Hadin2(0)
Wait 500
Loop
TIP
For a more accurate measurement, one should start with a reliable power
source. A Linear power source (e.g., using 7805, etc.) is better for A/D
input than a switching power source circuit (e.g., using LM2576). Also,
when the A/D input pins are extended and exposed outside the board, a
protection circuit (where additional chips such as the isolation circuit, etc.
are involved) should be added to protect the core module from external
noise. Keep in mind that, especially when a voltage over 5V could be
applied to the A/D input port, repairable damage can occur to the module
470
Appendix A: ASCII CODE
Code char. Code char. Code char. Code char.
00H NUL 20H SPACE 40H @ 60H `
01H SOH 21H ! 41H A 61H a
02H STX 22H " 42H B 62H b
03H ETX 23H # 43H C 63H c
04H EOT 24H $ 44H D 64H d
05H ENQ 25H % 45H E 65H e
06H ACK 26H & 46H F 66H f
07H BEL 27H ' 47H G 67H g
08H BS 28H ( 48H H 68H h
09H HT 29H ) 49H I 69H I
0AH LF 2AH * 4AH J 6AH j
0BH VT 2BH + 4BH K 6BH k
0CH FF 2CH , 4CH L 6CH l
0DH CR 2DH - 4DH M 6DH m
0EH SO 2EH . 4EH N 6EH n
0FH SI 2FH / 4FH O 6FH o
471
Appendix B: Note for BASIC
STAMP users
When using Parallax’s Basic Stamp compatible development board, please
be aware of the following:
1 SOUT 1 24 VIN
6 SIN 23 VSS
2
2 Rx ATN 3 22 RES
7 VSS 21 VDD
4
3 Tx P0 5 20 P15
8 P1 19 P14
6
4 DTR P2 7 18 P13
9 P3 8 17 P12
5 P4 9 16 P11
P5 10 15 P10
P6 11 14 P9
P7 12 13 P8
1 SOUT 1 24 VIN
6 SIN 23 VSS
2
2 Rx ATN 3 22 RES
7 VSS 21 VDD
4
3 Tx P0 20 P15
5
8 P1 19 P14
6
4 DTR P2 18 P13
7
9 P3 17 P12
8
5 P4 9 16 P11
P5 10 15 P10
P6 11 14 P9
P7 12 13 P8
Short here
472
Ladder Logic Special Registers
Special Explanation
Register
F0 Always OFF
F1 Always ON
F2 Turn on 1 SCAN time at POWER UP (Set Ladder On).
F3 Reserved
F4 Reserved
F5 Reserved
F6 Reserved
F7 Reserved
F8 1 SCAN every 10ms
F9 1 SCAN every 100ms
F10 Reserved
F11 Reserved
F12 Reserved
F13 Reserved
F14 Reserved
F15 Reserved
F16 Repeat ON/OFF every 1 Scan time.
F17 Repeat ON/OFF every 2 Scan times.
F18 Repeat ON/OFF every 4 Scan times.
F19 Repeat ON/OFF every 8 Scan times.
F20 Repeat ON/OFF every 16 Scan times.
F21 Repeat ON/OFF every 32 Scan times.
F22 Repeat ON/OFF every 64 Scan times.
F23 Repeat ON/OFF every 128 Scan times.
F24 Repeat ON/OFF every 10ms
F25 Repeat ON/OFF every 20ms
F26 Repeat ON/OFF every 40ms
F27 Repeat ON/OFF every 80ms
F28 Repeat ON/OFF every 160ms
F29 Repeat ON/OFF every 320ms
F30 Repeat ON/OFF every 640ms
F31 Repeat ON/OFF every 2.56 seconds
F32 Repeat ON/OFF every 5.12 seconds
F33 Repeat ON/OFF every 10.24 seconds
F34 Repeat ON/OFF every 20.48 seconds
F35 Repeat ON/OFF every 40.96 seconds
F36 Repeat ON/OFF every 81.92 seconds
F37 Repeat ON/OFF every 163.84 seconds
F38 Repeat ON/OFF every 327.68 seconds
F39 Repeat ON/OFF every 655.36 seconds
F40 Call LADDERINT in BASIC
F41 Reserved
F42 Reserved
F65 (F_ERR) Error
F66 (F_<) < Result of WCMP, DWCMP
F67 (F_>) > Result of WCMP, DWCMP
F72 (F_CARRY) Carry Flag
F73 (F_ZERO) Zero Flag
473
MEMO
474
Index
AdIn..............................................................................................130
Alias..............................................................................................132
AND...............................................................................................382
Arc................................................................................................285
ASC...............................................................................................128
Bcd2Bin..........................................................................................133
BClr...............................................................................................134
Beep..............................................................................................135
Bfree..............................................................................................136
Bin2Bcd..........................................................................................137
Bit...................................................................................................92
BLen..............................................................................................138
Bmp...............................................................................................286
Box................................................................................................278
BoxClear.........................................................................................278
BoxFill............................................................................................278
Byte................................................................................................93
ByteIn............................................................................................139
ByteOut..........................................................................................140
CALLS............................................................................................423
CheckBf..........................................................................................141
Chr................................................................................................128
Circle.............................................................................................279
CircleFill.........................................................................................279
CLCDOut........................................................................................271
Clear..............................................................................................269
Cls.................................................................................................269
CMode............................................................................................276
Color..............................................................................................284
Compare........................................................................................142
Con.................................................................................................95
Constant Arrays................................................................................96
Constants.........................................................................................95
Contrast.........................................................................................274
Count( ).........................................................................................143
CountReset.....................................................................................145
CSGDec..........................................................................................295
CSGHex..........................................................................................295
CSGNPut........................................................................................294
CSGXPut.........................................................................................295
475
CsrOff............................................................................................269
CsrOn............................................................................................269
CTD...............................................................................................395
CTU...............................................................................................394
Dcd................................................................................................146
Debug............................................................................................147
Dec................................................................................................119
Decr..............................................................................................150
DefChr...........................................................................................286
DEFCON.........................................................................................384
Delay.............................................................................................151
DF.................................................................................................385
DFN...............................................................................................385
Do...Loop.......................................................................................152
DotSize..........................................................................................284
DP.................................................................................................121
DPrint............................................................................................282
DTZero...........................................................................................154
DWADD..........................................................................................410
DWAND..........................................................................................415
DWBCNT........................................................................................403
DWCMP..........................................................................................408
DWDEC..........................................................................................409
DWDIV...........................................................................................412
DWINC...........................................................................................409
DWINV...........................................................................................420
DWMOV..........................................................................................404
DWNEG..........................................................................................421
DWOR............................................................................................413
DWRCL...........................................................................................418
DWRCR..........................................................................................419
DWROL..........................................................................................416
DWROR..........................................................................................417
DWSUB..........................................................................................410
DWXCHG........................................................................................405
DWXOR..........................................................................................414
EEPROM.........................................................................................156
EERead( ).......................................................................................155
EEWrite..........................................................................................156
EKeyPad().......................................................................................157
ElFill...............................................................................................280
Ellipse............................................................................................280
Float..............................................................................................120
476
FMOV.............................................................................................406
Font...............................................................................................275
For...Next.......................................................................................158
FP..................................................................................................123
FreePin...........................................................................................160
FreqOut..........................................................................................161
Get( ).............................................................................................163
GetA..............................................................................................164
GetA2............................................................................................165
GetCrc...........................................................................................166
GetPad( )........................................................................................167
GetStr( )........................................................................................168
GetStr2()........................................................................................169
GLayer...........................................................................................273
GLocate..........................................................................................281
GMOV............................................................................................407
GoSub...Return...............................................................................170
GOTO.............................................................................................422
GoTo .............................................................................................171
GPaste...........................................................................................289
GPop..............................................................................................288
GPrint............................................................................................281
GPush............................................................................................288
HADIn( ) .......................................................................................469
HADIn2( ) ......................................................................................470
Heap( )..........................................................................................174
HeapClear.......................................................................................174
HeapW...........................................................................................174
Hex................................................................................................118
High...............................................................................................176
HP.................................................................................................122
HPaste...........................................................................................290
HPop..............................................................................................290
HPush............................................................................................290
HRead............................................................................................173
HWrite...........................................................................................173
I2CRead.........................................................................................178
I2CReadNA.....................................................................................179
I2CStart.........................................................................................177
I2CStop..........................................................................................177
I2CWrite.........................................................................................180
If…Then…Elseif...Else…EndIf..............................................................181
In..................................................................................................182
477
Incr...............................................................................................183
Input.............................................................................................184
INTON............................................................................................424
KCTD.............................................................................................397
KCTU.............................................................................................397
KeyIn.............................................................................................185
KeyInH...........................................................................................186
Keypad...........................................................................................187
Label..............................................................................................171
LABEL............................................................................................422
Layer.............................................................................................272
Left................................................................................................124
Len................................................................................................125
Light..............................................................................................274
Line...............................................................................................277
LineStyle........................................................................................284
LineTo............................................................................................277
LOAD.............................................................................................381
LOADN...........................................................................................381
Locate............................................................................................269
LOOP.............................................................................................386
Low...............................................................................................188
LTrim..............................................................................................126
MCS...............................................................................................387
MCSCLR.........................................................................................387
MemAdr.........................................................................................189
menu buttons..................................................................................442
Menu( )..........................................................................................445
MenuCheck( )..................................................................................444
MenuReverse..................................................................................444
MenuSet.........................................................................................443
MenuTitle.......................................................................................443
Mid................................................................................................124
monitoring......................................................................................364
multi-tasking....................................................................................25
Ncd................................................................................................190
Nibble..............................................................................................92
Nop...............................................................................................191
NOT...............................................................................................382
Offset.............................................................................................283
On Int............................................................................................192
On LadderInt GoSub........................................................................193
On Pad...........................................................................................195
478
On Recv.........................................................................................196
On Timer()......................................................................................197
OpenCom.......................................................................................198
Operators.........................................................................................98
OR.................................................................................................382
Out................................................................................................201
OUT...............................................................................................381
Output...........................................................................................202
OutStat( ).......................................................................................203
Overlay..........................................................................................273
Paint..............................................................................................285
Pause.............................................................................................203
Peek.......................................................................................112, 204
PLC Setup Wizard............................................................................368
Poke.......................................................................................112, 204
Print............................................................................................270p.
Pset...............................................................................................284
PulsOut..........................................................................................205
Put................................................................................................206
PutA..............................................................................................207
PutA2.............................................................................................208
PutStr............................................................................................209
Pwm..............................................................................................210
PwmOff..........................................................................................211
RamClear........................................................................................212
Reset.............................................................................................213
RET................................................................................................423
Reverse..........................................................................................214
Right..............................................................................................124
Rnd( )............................................................................................215
RSTOUT..........................................................................................383
RTCRead( ) ....................................................................................465
RTCWrite .......................................................................................468
RTrim.............................................................................................126
SBRT..............................................................................................423
Select...Case...................................................................................216
Set Debug......................................................................................217
Set Display.....................................................................................261
Set I2C..........................................................................................220
Set Int...........................................................................................221
Set Ladder......................................................................................222
Set Modbus.....................................................................................223
Set OnGlobal...................................................................................224
479
Set OnInt.......................................................................................225
Set OnLadderInt..............................................................................226
Set OnPad......................................................................................227
Set OnRecv.....................................................................................228
Set OnTimer...................................................................................229
Set OutOnly....................................................................................230
Set Pad..........................................................................................231
Set RS232......................................................................................234
Set RS485......................................................................................235
Set Spi...........................................................................................237
Set Until.........................................................................................238
SETOUT..........................................................................................383
ShiftIn( )........................................................................................239
ShiftOut.........................................................................................240
SPC...............................................................................................125
Special Registers......................................................................426, 473
Spi.................................................................................................241
StepAccel.......................................................................................242
STEPOUT........................................................................................390
StepPulse.......................................................................................243
STEPSET.........................................................................................389
StepStat( ).....................................................................................244
StepStop........................................................................................244
String..............................................................................................86
STRING(.........................................................................................125
String()..........................................................................................125
Style..............................................................................................276
Sys( )............................................................................................247
TADIN............................................................................................131
TADIn()..........................................................................................248
TAMON...........................................................................................391
TAOFF............................................................................................394
TAON.............................................................................................393
Time( )...........................................................................................249
TimeSet.........................................................................................251
TMON.............................................................................................391
TND...............................................................................................425
TOFF..............................................................................................394
TON...............................................................................................393
UDelay...........................................................................................253
UsePin............................................................................................254
UTMax............................................................................................255
Val.................................................................................................127
480
ValHex...........................................................................................127
ValSng...........................................................................................127
WADD............................................................................................410
Wait...............................................................................................256
WaitDraw........................................................................................445
WaitTx...........................................................................................257
WAND.....................................................................................405, 415
WBCD............................................................................................401
WBCNT...........................................................................................403
WBIN.............................................................................................402
WCMP............................................................................................408
WDEC............................................................................................409
WDIV.............................................................................................412
WINC.............................................................................................409
WINV.............................................................................................420
WMode...........................................................................................274
WMOV............................................................................................404
WMUL............................................................................................411
WNEG............................................................................................421
WOR..............................................................................................413
Word...............................................................................................93
WRCL.............................................................................................418
WRCR............................................................................................419
WROL.............................................................................................416
WROR............................................................................................417
WSUB............................................................................................410
WXCHG..........................................................................................405
WXOR............................................................................................414
#define..........................................................................................103
#endif............................................................................................105
#if.................................................................................................105
#ifdef.............................................................................................106
#ifndef...........................................................................................106
#include.........................................................................................103
481