Sie sind auf Seite 1von 19

INPUT/OUTP UT/OUTPUT PC INPUT/OUTPUT MODULE

his device transforms your PC into a powerful controller that you can use to program and automate machines, circuits, or just about anything you can imagine. Its flexible 29 I/O ports that you can configure to work ( bit by bit or in groups) as inputs or outputs makes it adaptable to a wide array of applications . Control of stepping motor system is unbelievably easy, thanks to its built-in Pulse Generator function. It even has encoder switch input functions, counting inputs, and port capture register.

29 PORT SERIAL I/O

Feature and Specifications -Number of I/O (TTL Logic): 24 user definable 3 fixed inputs 2 fixed outputs -Special Functions: Programmable Pulse generator Incremental encoder switch input 3 counter registers I/O port memory registers - All I/Os are LED monitored. - Connects to PC serial port. - Visual Basic Friendly. - Two way connector connection - 8 to 12V 0.5A power input - 28.8kbaud and 9600baud speed - z8 microcontroller engine

Designed by:
Henry Chua

Article written by:


Henry Chua

Comments?
hlc@e-gizmo.com

All ports are wired with buffered LED, so that you will know what is going on in your system in just one glance. It is wired to your PC via RS-232C (COM) link. It is Visual Basic friendly, or you can use other programming language of your choice with almost equal ease. Serial vs. Parallel Todays desktop PC packs computational prowess once seen only in supercomputers built just past a couple of decades ago. For an old timer like me, it seems like waste to use a powerful PC to do something which a humble (and much cheaper) microcontroller can do as well. So why use use a PC?

Copyright 2006 by e-Gizmo Mechatronix Central All rights reserved. No part of this publication may be reproduced in any form without the written consent of e-Gizmo Mechatronix. Content subject to change without prior notice. All informations contained herein are believed to be correct and reliable.

Before using this document, you must agree with the following terms and conditions:
1. e-Gizmo Mechatronix and the author cannot be held liable for any damage that may occur with the use or misuse of any information contained in this document. 2. You are allowed to reproduce this publication and the product it describes for personal use only. Commercial reproduction is prohibited!

copyright 2006 by e-Gizmo Mechatronix Central

Because it is there! In no time at all, we, the experimenters, quickly discovered that theres a lot of wonderful things we can do with the printer port - besides attaching a printer. LPT cult is born, and believers rapidly multiplied like bacterias in a culture. But almost as quickly as we jump into this fad, we are confronted with the limitations of the parallel port. Some of which are listed as follows: Limited I/O. As your application grows, you find the I/O offered by the parallel port limited and lacking. Limited wire length. Going beyond 1 meter from the parallel port may start an avalanche of problems. No isolation. No second chance. You make one small mistake in your experiment, the parallel port is gone. Now for an even more bad news, the motherboard usually goes with it. O/S compatibility. Not all operating system will surrender full control of the parallel port to your program without a fight. I can add some more, but the above list may already covered the most bothersome. We will keep the list short for now, and discuss how our serial I/O remedies these. The serial I/O offers 29 ports that you can configure as inputs or outputs, unlike the parallel port which offers 8 fixed outputs and 4 or 5 fixed inputs only (totaling 12). You can easily integrate another 29 ports serial I/O unit in case you need more. The serial I/O will work with cable link to your PC with length in excess of 8 meters. The RS-232 port is robust and wont be damaged easily. There is very little chance you will damage the PC motherboard while working on low voltage circuits. All known PC O/S supports COM functions. Your programs do not need backdoor access to serial port, assuring compatibility.

To be fair, the serial I/O has its shortcomings too. The most serious is, speed. Because the port is manipulated through serial transfer, (communication is exchanged one bit at a time) response becomes sluggish. As an example, serial I/O may take 13ms (at 9600 baud rate, or 4mS at 28.8kbaud high speed mode) to read a port group. Speed is one area where the parallel port reigns supreme. But, as you will learn later from this article, this limitation will be hardly noticable when you use the serial I/O to control a electromechanical system (e.g. A simple robot) . The mechanical response time is generally much slower than the serial I/O itself. Furthermore, useful functions are embedded in the serial I/O design so that even fast occuring events will not be missed by the serial I/O.

29 port Serial I/O module

FUNCTION AND FEATURES


Physical Ports All ports are 5 volt logic TTL compatible. Each can drive up to 5 TTL loads. I/O Group 0, P0 to P07. This 8 bit port group is configurable 4 bit (nibble) at a time. Port 0 to 3 forms the lower nibble, and Port 4 to 7 makes the upper nibble. If you configure port 1 as an input, the other three in the lower nibble group also configures as input. In the same way, configuring port 7 as output will configure port 6,5 and 4 as outputs as well.

P18 as input and the rest in the group as outputs. You can set each port alternately as input and output, and so on. There are 256 possible combinations you can play with. I/O Group 3, P24 to P28. This group is occupied by ports hardwired as fixed input and output. As such, no configuration function is available for these ports. P24, P25 and P26 are input only, whereas P27 and P28 are output only. This lack of programmability does not in any way makes them less useful because these ports perform other functions not available from the other groups.

PC COM PORT

PC Link RS-232C/RS-485 SWITCH ENCODER COUNTER 25

Z86E21-IO29 MICROCONTROLLER

COUNTER 26 COUNTER 27

PULSE GEN + Port Memory Reg 4 - Port Memory Reg 7 + Port Memory Reg 5 - Port Memory Reg 8 + Port Memory Reg 6 - Port Memory Reg 9

PORT 0 7

PORT 8-15

PORT 16-23

PORT 24-29

GROUP 0

GROUP 1

GROUP 2

GROUP 3

Figure 1. The Serial I/O functional block diagram.

I/O Group 1, P8 to P15. In this 8 bit port group, all eight are configured as input or output at the same time. You configure one, you configure all. To illustrate further, if you configure P8 as an output, P9 to P15 follow the configuration as outputs. I/O Group 2, P16 to P23. All ports in this group can be configured individually, a feature that sets it apart from the other I/O groups. For example, you can set
copyright 2006 by e-Gizmo Mechatronix Central

These functions are: Counter registers. A falling edge clock input on P25,P26,P27 causes counters counter 25,26,and 27, respectively, to increment its corresponding value. P25 and P26 also functions as encoder switch input. With an incremental encoder switch wired on these inputs, rotating the switch clockwise increments the value of encoder switch register 3

99. Conversely, the value decrements if switch is rotated in counterclockwise direction. Stepping Motor pulse generator P28. A train of pulses with user specified frequency (rate) and number of pulses goes out of this port. This can be used to drive your stepping motor circuitry. You can use P28 as ordinary output if this feature is not used. High Speed Functions As I mentioned to you early in this article, the main thing going against the serial I/O is it is quite slow compared to the parallel port. Generally, this limitation poses minor hindrance only, because the electromechanical components it is supposed to control are much slower by comparison. Let me use this example again to illustrate this further : The time it takes for the serial I/O to write all 8 bits in a group is about 13ms (at 9600 baud). A pneumatic solenoid valve switched through an electromechanical relay may take as much as 100ms to switch from one state to another. (Note: Relay and Solenoid valve are the most common component found in automated machines.) Some input devices, however, may flip-flop states at duration much shorter than 13mS. This event may occur at a time when the serial I/O is not looking, i.e., while busy talking with a PC. Under this condition, the event will not be detected and is lost. To remedy this, port memory registers are added.

Each group (except group 3) has its own pair of memory register that keeps checking its corresponding port at more than 40,000 times per second. At this rate, these registers can capture and record input change having duration as short as 25us. The +Port memory captures logic 0 to 1 events, while Port memory captures logic 1 to 0 events. + Port Memory Register 4. This register continuously monitor port group 0, and will capture and record a logic 0 to 1 event. + Port Memory Register 5. This register continuously monitor port group 1, and will capture and record a logic 0 to 1 event. + Port Memory Register 6. This register continuously monitor port group 2, and will capture and record a logic 0 to 1 event. - Port Memory Register 7. This register continuously monitor port group 0, and will capture and record a logic 1 to 0 event. - Port Memory Register 8. This register continuously monitor port group 1, and will capture and record a logic 1 to 0 event. - Port Memory Register 9. This register continuously monitor port group 2, and will capture and record a logic 1 to 0 event.

Figure 2. Oscilloscope trace of one communication exchange between the Serial I/O and PC. Upper trace is the data stream coming from the PC sending the a Byte Write command. Lower trace is the Serial I/O response. The Byte Write, or GroupWrite() function, the slowest in the command set, takes about 13.8ms to complete (at 9600 baud).

29 port Serial I/O module

USING THE SERIAL I/O


A Communication Manual detailing the communication protocol between the PC and serial I/O is provided separate from this article. If you want to understand the operating details of this I/O, the communication manual becomes an essential reading. I did not include the communication detail in this article because it might come too heavy (or boring) to some readers. Most of the subroutines needed to perform these operations had already been written in Visual Basic code. A program template containing the source code of these functions is available for download at egizmo.com. (This file is also available in the CD-ROM that goes with your kit if you purchase this kit from authorized dealers.) We will discuss in greater details these functions in the following paragraphs. For most of us who just want to use the serial I/O with Visual Basic, the following section will serve as a quick start guide. Sending commands to serial I/O All functions described in the succeeding instruction must be send to the serial I/O module through the following function: Tx (function) Where: Function- can be any one of the functions described in the following sections.

I/O Groups
Several functions access 8 ports at a time for improved processing speed. This set of 8 ports are referred to as groups in this documentation. There are 4 I/O groups in this serial I/O project. Following illustrates the relationship of I/O ports to each bit in a group.

Group 0
P07 P06 P05 P04
Bit 7 Bit 6 Bit 5 Bit 4

P03 P02 P01 P00


Bit 3 Bit 2 Bit 1 Bit 0

Group 1
P15 P14 P13 P12
Bit 7 Bit 6 Bit 5 Bit 4

P11 P10 P09 P08


Bit 3 Bit 2 Bit 1 Bit 0

Group 2
P23 P22 P21 P20 P19 P18 P17 P16
Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0

Group 3
---Bit 7

P30 ----

P28 P27 P26 P25 ---Bit 3 Bit 2 Bit 1 Bit 0

Bit 6 Bit 5 Bit 4

Putting some example in in words, P21 belongs to group 2 and is on bit 5 of the group; P07 belongs to group 0 bit 7, etc..

Configuring ports
Visual Basic Function: IOconfig(cfg as byte, portgroup as byte) Where: cfg configuration data portgroupt port group to configure

Returned Value: Formatted command string I/O group 0

Cfg = 0 (Group 0)
P07 P06 P05 P04
Bit 7 Bit 6 Bit 5 Bit 4

P03 P02 P01 P00


Bit 3 Bit 2 Bit 1 Bit 0

There are four possible configurations for this group. 1. cfg= &H00, all Group 0 ports are configured as outputs

copyright 2006 by e-Gizmo Mechatronix Central

Group 3 P24,P29, and P31 are used by the system. Do not write on these ports!
5

Figure 3. Upper photo shows the Serial I/O connectors pin-out. The serial I/O appears bare as viewed from the component side, because most of the components used are SMD types (directly soldered on the copper side, lower photo). 6
29 port Serial I/O module

2. 3. 4.

cfg = &H0F, P07 to P04 are configured as outputs, P03 to P00 configured as inputs. cfg = &HF0, P07 to P04 are configures as inputs, P03 to P00 configured as outputs. cfg = &HFF, all Group 0 ports are configured as inputs.

as all outputs. I/O group 2

Cfg = 2 (Group 2)
P23 P22 P21 P20
Bit 7 Bit 6 Bit 5 Bit 4

Example: Tx (IOconfig(&H0F,0)) &H0F = 11110000 binary

P19 P18 P17 P16


Bit 3 Bit 2 Bit 1 Bit 0

Cfg = 0 (Group 0) 1 1 1 1 0
Bit 3

Bit 7 Bit 6 Bit 5 Bit 4

Bit 2 Bit 1 Bit 0

P07 P06 P05 P04

P03 P02 P01 P00

Ports in this group are bit programmable, meaning; each port can be configured independently. Each bit in cfg corresponds to the configuration of each port. Port23 is configured by putting a 1 or 0 on bit 7 of cfg. Port 22 is configured by bit 6, so on. A bit programmed with a 0 configures a port as an output. A 1 configures it as an input. Example: Tx ( IOconfig(&H5A,0)

This call to the subroutine will configure port group 0 P07,P06,P05,P04 as inputs and P03,P02,P01,P00 as outputs. (Note: In Visual Basic, a hex number is prefixed with &H.) I/O group 1

&H5A 01011010 in binary

Cfg = 2 (Group 2) 0 1 0 1 1
Bit 3

Cfg = 1 (Group 1)
P15 P14 P13 P12
Bit 7 Bit 6 Bit 5 Bit 4

Bit 7 Bit 6 Bit 5 Bit 4

Bit 2 Bit 1 Bit 0

P11 P10 P09 P08


Bit 3 Bit 2 Bit 1 Bit 0

P23 P22 P21 P20

P19 P18 P17 P16

All 8 ports in this group are configured in unison. Group 1 can either be all output or all input only. Hence cfg for group 1 has only two valid values: 1. cfg= &H00, all Group 1 ports are configured as outputs 2. cfg = &HFF, all Gorup 1 ports are configured as inputs. Example: Tx (IOconfig(&H00,1))

This example call will configure P23,P21,P18,P16 as outputs. P22,P20,P19,P17 will function as inputs.

Writing to a single output:


Visual Basic Function: HI(portnum as byte) - Turn output portnum to Logic 1 LO(portnum as byte) - Turn output portnum to Logic 0 Where: portnum Port to switch logic

Cfg = 1 (Group 1) 0
Bit 7

0
Bit 3

Returned Value: Command String Note: This function will work only with output ports. Example:

Bit 6 Bit 5 Bit 4

Bit 2 Bit 1 Bit 0

P15 P14 P13 P12

P11 P10 P09 P08

This call to the subroutine will configure port group 1


copyright 2006 by e-Gizmo Mechatronix Central

P16,P17 of group 2 is configured as output 7

Tx (IOconfig(&HFC,2)) P16 is switched to logic 1 Tx (HI(16)) P17 is switched to logic 0 Tx (LO(17))

GroupWrite(group as byte) - Write a byte to a group of 8 ports. Where: group port group number 0 to 3 Group = 0 writes the present state of P00 to P07 at the same time. Group = 1 writes the present state of P08 to P15 at the same time. Group = 2 writes the present state of P16 to P23 at the same time. Group = 3 writes the present state of P28 and P29 at the same time. Specifying Variable: P(0) to P(29) as Boolean P(0) corresponds to P00, P(30) to P30 Returned Value: Command String Before calling the GroupWrite() function, you have to set the specifying variables that corresponds to the port you wish to change state. Example:

Reading a single input:


Visual Basic Function: Peek(portnum as byte) - Read state of input portnum - Results can be read from variable P(portnum) Where: portnum Port to read Returned Value: Command String Note: This function will work with both input and output ports. Example: P16,P17 of group 2 is configured as output Tx (IOconfig(&HFC,2)) P23 is input, read its logic state Tx (Peek(23)) Perform following functions if P23 is logic 1 If P(23)=True then . . End If

Objective set P4 and P27 to logic 1, P0 and P29 to Logic 0. Your routine should look something like this: P4 and P0 belongs to group 0 P(4) = True P4 Logic 1 P(0) = False P0 Logic 0 Tx (GroupWrite(0)) P4 and P0 now changed P27 and P29 belongs to group 3 P(27) = True P27 logic 1 P(29) = False P29 logic 0 Tx (GroupWrite(3) ) P27 and P29 now changed While the GroupWrite() function apparently requires more program lines compared to HI() and LO() functions doing the same task, it actually executes much faster if used to manipulate two or more port in the

Writing to a port group (Byte write):


If you want to modify the logic state of more than one port, the group write function offer the fastest and efficient way of doing it. Visual Basic Function:

5
8

If you want to write to a single port only, use the HI() or LO() function. Otherwise, use the GroupWrite(). This function executes multi port writes (on the same group of ports) much faster.

29 port Serial I/O module

same group. The important thing to remember is, if you want to write to a single port only, use the HI() or LO() function. Otherwise, use the GroupWrite(). This function executes multi port writes (on the same group of ports) much faster.

. End If If P(11)= True then . . End If

Your P11 routine

Reading a port group (Byte Read):


The ByteRead() function provides the fastest way of reading more than one port in a group. Visual Basic Function: GroupRead(group as byte) - Read a byte from a group of 8 ports. Where: group port group number 0 to 3 Group = 0 reads the present state of P00 to P07 at the same time. Group = 1 reads the present state of P08 to P15 at the same time. Group = 2 reads the present state of P16 to P23 at the same time. Group = 3 reads the present state of P27 to P29 at the same time. Results are stored in the specifying variable: P(0) to P(29) as Boolean P(0) corresponds to P00, P(29) to P29 Returned Value: Command String Example: Objective read the logic state of P09 and P11 P09 and P11 belongs to group 1 Tx (GroupRead(1) ) P(09) to P(11) are now read If P(9)= True then . Note that in this example, although you may be interested with the state of P09 and P11 only, all 8 ports in the group are actually read at the same time. If you want to read a single port only on, use the Peek() function. Otherwise, use the GroupRead(). This function executes multi port reads (on the same group of ports) much faster. Using the Port Memory registers The Port memory registers, as mentioned early in this article, detects logic changes in the inputs that are too fast for the Peek() or GroupRead() function to detect. Each port group are watched by a pair of registers, the +register looks for logic 0 to 1 occurrence, while the register looks for logic 1 to 0 transition. The +register bit corresponding to the port must be initialized with a 0. A low to high event on this port will change this value with a 1. In a similar manner, -register bit corresponding to a port is first loaded with a 1. a Logic 1 to 0 events on this port will change this value with a zero Confused? Not to worry. A visual basic function is also provided to take most of the pain out. Visual Basic Function: This function checks if fast input event occurred on an input: CheckEvent( group as byte, init as Boolean) Where: group port group number 4 to9

Your P9 routine

If you want to read a single port only on, use the Peek() function. Otherwise, use the GroupRead(). This function executes multi port reads (on the same group of ports) much faster.

copyright 2006 by e-Gizmo Mechatronix Central

Pplus initialized with 0

Pplus stays at 1

5V

0
P20

Pplus(20) 5V

1
P20

Pplus(20)

Pminus stays at 0

Pminus initialized with 1

0V

0
>= 20uS

0V Pminus(20)

1
>= 20uS

Pminus(20)

Pplus change to 1

Pplus stays at 1

5V 0V

1
P20

Pplus(20) 5V 0V Pminus(20)

1
P20

Pplus(20)

Pminus stays at 0

Pminus change to 0

Pminus(20

Figure 4. The port memory register lets you capture short events that will mostly pass unnoticed with normal port reading. Pplus registers are on constant lookout for a logic 0 to 1 change to its corresponding inputs, while Pminus looks and records logic 1 to logic 0 events.

Group = 4 record low to high change in P00 to P07. Group = 5 record low to high change in state of P08 to P15. Group = 6 record low to high change in state of P16 to P23. Group = 7 record high to low change in state of P00 to P07. Group = 8 record high to low change in state of P08 to P15. Group = 9 record high to low change in state of P16 to P23.

Returned Value: Command String Example: A normally low fast sensor is connected to P20. A short low to high event will be captured and detected by the following routine: Low to high sensing, use Pplus(20) Port20 Pplus(20) belongs to group=6 init=True to setup port memory before using Tx (CheckEvent(2,True)) Do

Init = True initialize Pplus() array in group to all False if group = 4 to 6 = True initialize Pminus() array in group to all False if group = 7 to 9 Results are stored in the specifying variable: Pplus(0) to Pplus(23) as Boolean for Logic=1 Pplus(0) corresponds to P00, Pplus(23) to P23 Pminus(0) to Pminus(23) as Boolean for Logic=0 Pminus(0) corresponds to P00, Pminus(23) to P23 10

check port, do not initialize this time Tx (CheckEvent(6,False)) If event is detected, execute If clause If Pplus(20) = True then Your routine here.. End If Loop Until ..

29 port Serial I/O module

Using the Counter Registers


Visual Basic Function: ReadCount (register as byte ) - Read the content of specified counter register.

5V

P25
0V


COUNTER 25

5V

ClearCount( register as byte ) Reset the content of specified counter register to 0. Where: Register counter register 25,26,27 or 99 COUNTER 25 increments on the falling edge of P25 input. COUNTER 26 increments on the falling edge of P26 input. COUNTER 27 increments on the falling edge of P27 input. COUNTER 99 Up/Down-incremental encoder counter Returned Value: Command String Specifying Varaibles: Results of the operations are stored in the corresponding variables listed as follows: Counter25 Counter26 Counter27 Counter99 Up/down or Incremental encoder COUNTER 99 works in conjunction with P25 and P26 inputs. If a falling edge input is detected by P25, COUNTER 99 counts up or down, depending upon the state of P26. If P26 is at logic 1, COUNTER 99 value increments by 1. Otherwise, COUNTER 99 decrements by 1. An incremental encoder switch is a component that can be viewed as a digital potentiometer. Used with COUNTER 99 of the serial I/O, it gives you a convenient means to adjust digital values, i.e., by just rotating a knob, just like an ordinary potentiometer. The application section gives you an example application using an incremental encoder switch.

P26
0V


COUNTER 26

5V

P27
0V


COUNTER 27

5V

P25
0V

P26


COUNTER 99

Figure 5. P25, P26 and P27 does some other job aside from ordinary input task. They function as counter inputs at the same time. Each input responds to logic 1 to 0 events by incrementing their corresponding counter. P25 and P26 does even more. P25 also counts up counter99 if P26 is high during the event, or counts down otherwise.

Figure 6. One very useful application of counter99 function is with the use of incremental encoder switch. With only a simple setup, you get the equivalent function of a digital potentiometer.
copyright 2006 by e-Gizmo Mechatronix Central

11

Using the Pulse Generator (Stepping Motor Function)


Visual Basic Functions: PulseFreq(freq As Integer) - Set pulse generator frequency PulseSteps(steps As Long) - Set number of pulses to generate PulseRun() - Start pulse generator PulseStop() - Stop pulse generator Where: Freq Pulse frequency, 43 to 10,000 Hz steps Number of pulses to generate, 0 to 65535 steps=0 for continuous pulse output Returned Value: Command String The Pulse Generator will generate output pulses via P30 at the specified frequency freq and number of pulses steps. If the number of pulses is set at zero, the pulse generator will generate pulses continuously until stopped by the StopPulse() function. You can use PulseStop() function at any time to stop the pulse generator. You can use PulseFreq() at any time, even while the pulse generator is running, to change frequency. This feature can be very useful if you like to ramp up or ramp down the speed of the stepping motor. While the number of steps can be changed at any time using the PulseSteps() function, the operation must only be done before running the Pulse Generator. Otherwise, you will loose track with the position of the motor. Although the pulse generator is primarily intended for stepping motor control, you can use it for other purposes as well. For example, you can use it to generate musical notes. Note: The pulse generator uses a 14 bit frequency divider to generate the frequencies. The divider limited resolution does not permit us to generate very precise frequencies, especially near the high frequency limit. 12

PulseSteps()

NUMBER OF STEPS

PulseFreq()

PULSE RATE

PulseStart() PulseStop()

PULSE GENERATOR

P30

Figure 7. Specify the number of steps, and then the pulse rate, and finally start the pulse generator. The Serial I/O will the do rest without further program intervention. This feature is very useful for stepping motor control applications. As an example, for Freq=5000, the actual frequency may run at 5009 Hz. Example: Set up and run the Pulse Generator for 5000 output pulses at 100 pulses per seconds Set the number of pulses Tx (PulseSteps(5000)) Set the pulse frequency Tx(PulseFreq(100)) Run the pulse generator Tx(PulseStart())

Setting the communication speed


Visual Basic Function: Set28k( Hi as Boolean) - set communication speed at 28.8 kbps Where: Hi = true for 28.8kbs Hi = False for 9600 baud Returned Value: Command String
29 port Serial I/O module

YOUR FIRST SERIAL I/O PROGRAM The most effective way to learn something is by doing. We will write a simple program that will, in the process, serve as a guided tour on how use the PC Serial I/O project with the provided Visual Basic program template. We will build an application that will light the monitor LEDs of the Serial I/O on a pre determined sequence. This is an easy and convenient exercise as the application will not require any external circuit connections To perform this exercise, you should have the following items and equipment ready: - Serial I/O project module - AC/DC adapter or power supply, 9-12VDC , current rating >= 500mA. - Serial Cable - PC with a serial port (Com port) and Visual Basic 6.0 installed.

Prepare the hardware by connecting the female side of the serial cable a to COM port in your PC. Plug in the male side to the Serial I/O module port.

copyright 2006 by e-Gizmo Mechatronix Central

13

Connect the power supply to apply power. You should see the Serial I/O power indicator ON. The power input is diode protected againts polarity reversal. If you connect the power input with reversed polarity, the Serial I/O will not power ON, but is not damaged. Be careful though, excessive supply voltage (> 24V) can kill the board in an instant.

On your PC, copy the program template folder Serialio ( from Serial I/O CD or e-gizmo.com) to your hard disk drive. We will refer to the copy of this folder hereon as the working folder. Rename this folder, if you like, closer to the title of the application you will be working on. For this exercise, you can simply rename this folder as LightShow. Double click the serialio.vbp file contained within the working folder. This will open the Visual Basic for this session. At this point, you may wish to explore the source code listing of the program template.

Click the main.frm from the Project Explorer window to display the blank form. Insert a timer object and name this timer as tmrLights. Set tmrLights.Interval to 20.

14

29 port Serial I/O module

Ok, now we are ready to put some codes in our program. Let us use P0 to P23 as outputs so that our program can turn ON or OFF the monitor LEDs. We will configure these ports to function as outputs using the IOconfig() function. The next concern is where to put the configuration routine in the code. Since this IOconfig() needs to be executed just once (for each port groups), the convenient location may be at the Form_Load() event function of the main.frm form. Double click the main form to display Form_Load() function. Add the configuration IOconfig() functions that will configure P0 to P23 as outputs.

In this example, we want the three port groups to put on a light show. This calls for a routine that basically put the pattern on the P() variables before calling the GroupWrite() function to transfer the pattern to the Serial I/ O module ports. Double click the tmrLights timer object and and put the codes shown next page in tmrLights_Timer() function:

copyright 2006 by e-Gizmo Mechatronix Central

15

Private Sub tmrLights_Timer() Dim i As Integer This part initialize the port variables P() to a known state at the start of the program Executed once only, when Initial=True You should set Initial=True at the Form_Load() function of this form If Initial = True Then Initial = False P(0) = True P(8) = False P(10) = False P(12) = False P(14) = False P(9) = True P(11) = True P(13) = True P(15) = True P(16) = True Exit Sub End If Group 0 show sequence This will light LED one at a time starting at P(0) to P(7) If P(7) = True Then P(7) = False Restart if P(7) already ON P(0) = True Else For i = 0 To 6 Else shift the ON LED If P(i) = True Then a position UP P(i) = False P(i + 1) = True Exit For End If Next i End If Tx (GroupWrite(0)) Write result one bit initialy ON Group 1 Initially LEDs are alternately turn ON

Group 1 P8 to P15 show This alternately lights the odd and even column of this group For i = 8 To 15 P(i) = Not P(i) reversing logic Next i Tx (GroupWrite(1))

effect achieve by simply

Write result

Group 2 P16 to P23 show Similar to group 0 except that the sequence is reversed everytime the last or first LED in the group is ON Reverse if last LED ON If P(23) = True Then Rev = True If P(16) = True Then Rev = False If Rev = False Then For i = 16 To 22 If P(i) = True Then P(i) = False P(i + 1) = True Exit For End If Next i Else For i = 23 To 17 Step -1 If P(i) = True Then P(i) = False P(i - 1) = True Exit For End If Next i End If Tx (GroupWrite(2)) End Sub

Group 2 starts with 1 ON LED

16

29 port Serial I/O module

Finally, add the variable declaration for Initial and Rev. Then setup Initial = True at the Form_Load() function.

We will make no attempt to describe the example program details to keep our focus set at the primary intention of this section a quick start guide on how to enter your Serial I/O programs. The example program is simple enough, even casual programmers should have no trouble understanding it. Run your first Serial I/O VB code and enjoy the show!

copyright 2006 by e-Gizmo Mechatronix Central

17

APPLICATION EXAMPLES
Connecting a stepping motor A typical stepping motor driver, such as the PWM stepping motor driver kit from e-Gizmo, would require a pulse input (CLK) that will cause the stepping motor it drives to rotate at a known speed and displacement, and a control input that will tell the direction of rotation, i.e. clockwise or counterclockwise. The Serial I/O has all these functions built-in. It has an internal pulse generator complemented by easy-touse software functions that you can use to supply the pulse. Any one of its general purpose output configurable port can be use as direction control. Figure 8 shows an easy way to connect e-Gizmo PWM stepping motor driver kit to Serial I/O. P30, the pulse generator output, connects to the CLK input of the driver. P28 is used to control the direction or rotation. An example demo program using this set-up can be downloaded from www.e-gizmo.com download section of this project.

POWER SUPPLY
+5V GND +VMOTOR

SERIAL I/O

+5V GND +VMOTOR

STEPPING MOTOR
P30 P28 GND /CLK /DIR GND

PWM STEPPING MOTOR CONTROLLER

PH A PH /A PH B PH /B

Figure 8. Example application with Serial I/O controlling a stepping motor system.

18

29 port Serial I/O module

Connecting an Incremental Encoder Switch and a 4-digit LED Display Kit You will never appreciate the usefulness of an incremental encoder switch unless you start using it. As mentioned early in the article, an incremental encoder switch is the digital equivalent of a potentiometer, or volume control as most of us locals would like to call it. Used with a digital controller, such as a microcontroller or the Serial I/O, this component provides a convenient way, and perhaps even more important, an intuitive way to adjust system settings and parameters. As an example, you can use it to adjust the timer settings in digital timer circuits, use it to adjust voltage output in digital power supply circuit, and so on. The example application given below shows you how to connect and use the incremental encoder with the Serial I/O project. Together with e-Gizmos 4-digit LED display kit, you can easily write an application that requires some input without the use of a keyboard, and display numeric data with the LED display kit. An example program using this set-up can be downloaded from www.e-gizmo.com download section of this project.

SERIAL I/O
CONNECT TO GND

4-DIGIT LED DISPLAY KIT


P26 GND P25 P27 P00 P01 P02 P03 P04 GND

GND



Figure 9. Digital potentiometer application using Serial I/O counter function. The example application also features the 4-digit LED Display kit module.

copyright 2006 by e-Gizmo Mechatronix Central

19

Das könnte Ihnen auch gefallen