Tuesday, December 11, 2012

CGKEYCHIP1 Custom Keypad Example



The Maximite design uses a PS/2 connection as a keyboard interface. You can use a full keyboard connected to either a CGCOLORMAX1 or the CGMMSTICK1.
If you want to embed either of these two setups into a project, you might choose to replace the full keyboard with something either more compact or custom for your application. The CGKEYCHIP1 wired into a “ColorMax” or “stick” lets you create your own keyboard by adding your own buttons.
The CGKEYCHIP1 is a 24 pin chip in a DIP (through-hole) package. Four wires connect to the Maximite that include ground, power, and the two PS/2 communication signals (data/clock). You can wire in twelve distinct buttons for the keyboard keys plus three “modifier” buttons.
The CGKEYCHIP1 has two sets of keys that it can transmit:

Set1
Set2
F1
Up Arrow
F2
Down Arrow
F3
Left Arrow
F4
Right Arrow
F5
Space Bar
F6
Page Up
F7
Page Down
F8
Enter
F9
End
F10
Home
F11
Tab
F12
Backspace


An input pin on the chip selects between these sets of keys.
The CGKEYCHIP1 runs off of the 5V that the PS/2 connection provides. Both chip grounds should be connected to ground. The pins marked “NoConnect” should not be connected to anything.
Supplied with the CGKEYCHIP1 is a 1.3 kohm resistor that has to be connected from pin 11 to pin 15 for the chip to operate correctly
The PS/2 data signals are open-collector signals. When you connect the GCKEYCHIP1 to a CGCOLORMAX1 there are pull up resistors already on the ColorMax. When you connect to a CGMMSTICK1 and CGVGAKBD1 combination, the CGVGAKBD1 board already has pull up resistors in place for the two PS/2 lines. No additional resistors are needed in this case.

When you connect the CGKEYCHIP1 to just a CGMMSTICK1, you will need to attach two pull-up resistors in order to have the chip function correctly. Each of the two PS/2 lines should be pulled to 5V through a 10 kohm resistor.
The SetSelect input to the CGKEYCHIP1 selects the key set that you wish to use, either set 1 or set 2. Connecting this input directly to 5V will select set 1, while connecting this input to ground selects set 2.
The key inputs are activated by momentary contact to ground. Simple SPST buttons can be used.


You can write a very simple MMBasic program to check the codes that the keys transmit against the list of codes found in this document:
do
a$ = inkey$
if a$ <> "" then
print asc(a$)
endif
loop




When run, set 1 will return these codes:
Key
Plain
Control
Shift
Shift+Control
F1
145
209
177
241
F2
146
210
178
242
F3
147
211
179
243
F4
148
212
180
244
F5
149
213
181
245
F6
150
214
182
246
F7
151
215
183
247
F8
152
216
184
248
F9
153
217
185
249
F10
154
218
186
250
F11
155
219
187
251
F12
156
220
188
252






Set 2 returns these codes :
Key
Plain
Control
Up Arrow
128
192
Down Arrow
129
193
Left Arrow
130
194
Right Arrow
131
195
Space Bar
32
0
Page Up
136
200
Page Down
137
201
Enter
13
13
End
135
199
Home
134
198
Tab
9
9
Backspace
8
8


MMBasic interprets the Alt as a key by itself, not a modifier. The keycode for the Alt key is 139, Control-Alt is 203, Shift-Alt is 171, Control-Shift-Alt is 235. This program will return two key codes when Alt is used, first the Alt code, then the key code. Control-Shift-Alt-F1, for example, returns 235 241.












Purchase a CGMMSTICK1 Download of Maximite Integrated Development Environment: MMIDE


CGMMSTICK 1-Wire Interface Example


As an example of the operation of the 1-wire function of the CGMMSTICK1, this example interfaces to a DS1822 temperature device.
From the DS1822 datasheet:
The DS1822 digital thermometer provides 9- to 12-bit centigrade temperature measurements and has an alarm function with NV user-programmable upper and lower trigger points. The DS1822 communicates over a 1-Wire bus that by definition requires only one data line (and ground) for communication with a central microprocessor. It has an operating temperature range of –55°C to +125°C and is accurate to ±2.0°C over the range of –10°C to +85°C.
Unique 1-Wire® interface requires only one port pin for communication.
Each device has a unique 64-bit serial code stored in an on-board ROM.
The hardware couldn't be simpler.

The DS1822 pin 1 was connected to ground, pin 3 connected to 5V, and the center pin, pin 2, connected to the CGMMSTICK I/O pin 20. The center pin is the data pin of the 1-wire device. I/O pin 20 has a 5k ohm (two 10k in parallel) pullup.
From the software perspective reading from a 1-wire device isn't as simple as just sending a read command. You have to write to the device first to tell it you want to read.
Furthermore it takes a moment for the device to start and complete the temperature measurement process.
First the 'bus' should be reset. In this case the 1-wire bus is I/O Pin 20 and only contains the single DS1822 device.
' Reset the 1w bus, DS1822 device
OWRESET 20


The reset command selects the pin (bus) to reset and optionally can receive an indication that there are 1 or more devices present on the bus. This puts a reset pulse on the bus.
All devices have a serial number, so that if there are multiple devices on a single I/O Pin (bus), any individual device can be addressed for access.
In this example there is only a single device, so rather than talking to it by addressing it specifically, a command is used that skips the need for a serial number. This is called “Skip ROM” in the 1-wire terminology.
From the data sheet:
SKIP ROM [CCh]
The master can use this command to address all devices on the bus simultaneously without sending out any ROM code information. For example, the master can make all DS1822s on the bus perform simultaneous temperature conversions by issuing a Skip ROM command followed by a Convert T [44h] command.
Note that the Read Scratchpad [BEh] command can follow the Skip ROM command only if there is a single slave device on the bus. In this case time is saved by allowing the master to read from the slave without sending the device’s 64-bit ROM code. A Skip ROM command followed by a Read Scratchpad command will cause a data collision on the bus if there is more than one slave since multiple devices will attempt to transmit data simultaneously.
The DS1822 will be told to start a temperature conversion.
' Start temperature conversion
' Pin 20, send reset first, send two bytes
' &hCC - Skip ROM
' &h44 - Start conversion
OWWRITE 20, 1, 2, &hCC, &h44


The OWWRITE command selects a pin/bus to write to. A reset pulse again is sent to that bus and then two bytes are sent. &hCC is the command byte to skip the serial number, and &h44 tells the DS1822 to start the temperature conversion process.
The conversion process takes a little while to complete, so the program pauses to allow that conversion to finish.
' Wait a second for conversion process to finish
PAUSE 100


After that pause, a command is sent that tells the DS1822 that the next communication will be a read.
' Read data/temperature command
' Pin 20, send reset first, send two bytes
' &hCC - Skip ROM
' &hBE - read data command
OWWRITE 20, 1, 2, &hCC, &hBE


This again resets the bus and uses “Skip ROM” since this example uses only a single 1-wire device. &hBE is the command to read.
The read command is next to read the temperature value.
' Read data/temperature from DS1822
' Pin 20, send reset after, read two bytes
OWREAD 20, 2, 2, LowTemp, HighTemp


Note that the bus is NOT reset at the start of the read command. The DS1822 expects the read to follow the command to read without a reset between the commands.
OWREAD reads the selects bus line. The options to the command allow for a selected number of bytes to be read. In this case we need to read only the first two bytes of the multi-byte register, the registers that contain the temperature data. A bus reset happens after the OWREAD command.
Math is performed to combine the two bytes that were read from the temperature device according to the DS1822 data sheet.
' Combine and adjust according to data sheet
Cel = ((HighTemp And &b111) * 256 + LowTemp) / 16


The resulting Celsius temperature is printed, then converted to Fahrenheit and that is also printed.
' Print Celsius and convert/print Fahrenheit
Print "Celsius" Cel
Print "Fahrenheit" ( 9 / 5) * Cel + 32;











Purchase a CGMMSTICK1 Download of Maximite Integrated Development Environment: MMIDE


CGMMSTICK I2C Interface Example


As an example of the operation if the I2C function of the CGMMSTICK1, this example interfaces to a PCF8574A I/O expansion device.

I/O Pins 12 and 13 are used for I2C, as described in the documentation.
The code used in this example assumes that the address pins (A0-A2) are all grounded, giving this chip an address (offset) of 0.
The example code runs a 'cylon' pattern on the LEDs.


' Enable I2C
' speed = 100 kHz
' timeout = 100 milliseconds
I2CEN 100, 100


DO


FOR loop1 = 0 TO 7


cylon = 2^loop1
cylon = (cylon XOR &hFF)


' Send I2C command to PCF8574A
' 8574A address = &h38
' send 1 byte
I2CSEND &h38, 0, 1, cylon


PAUSE 100


NEXT loop1


FOR loop2 = 6 TO 1 STEP -1


cylon = 2^loop2
cylon = (cylon XOR &hFF)


' Send I2C command to PCF8574A
' 8574A address = &h38
' send 1 byte
I2CSEND &h38, 0, 1, cylon


PAUSE 100


NEXT loop2


LOOP


' Disable I2C
I2CDIS









Purchase a CGMMSTICK1 Download of Maximite Integrated Development Environment: MMIDE

CGMMSTICK SPI Interface Example


As an example of the operation of the SPI function of the CGMMSTICK1, this example interfaces to a SPI potentiometer.



An MCP41100 SPI potentiometer was selected from Microchip Technology. This is a 100k potentiometer that can be adjusted in 256 steps from one end of the dial to another.
PB0 and PA0 were connected to 5V and ground respectively. PW0 is the 'wiper' and will move in position between 0V and 5V under control of the CGMMSTICK1 via the commands sent through MMIDE.
The SPI serial lines are all set as OC (open collector) with a pull up resistor of 10k ohms.
The CS (chip select) line of the digital pot is connected to I/O Pin 14, in open collector mode (with 10k ohm pull up). It is active low. The data transfer starts with this line high. The line is brought low, serial data is transmitted, and the line returned high.



The SPI applet is used to transmit two bytes to the digital pot.
Pins 11, 12, and 13 are used to communicate with the digital pot. Note that this hardware uses the 8-pin MCP41100, so there actually is no data read from the digital pot.
The clock and data pins are set to OC. The rate is L – low. The polarity for clock/data 1s set to 3 - clock is active low, data is captured on the rising edge and output on the falling edge.
The chip is selected (CS low) and 11 is clocked to the chip. This is the command byte for this chip. Then a value from 0 (0v) to 255 (5V) is clocked to the chip before returning high. When the CS is high the pot switches to the commanded setting.

 





Purchase a CGMMSTICK1 Download of Maximite Integrated Development Environment: MMIDE


CGCOLORMAX1 Serial I/O Example


Two serial ports are available on the CGMMSTICK1 and CGCOLORMAX1 for asynchronous serial communications.
MMIDE can again be used to demonstrate serial communication.
This example uses the CGCOLORMAX1, since it has on-board RS232 circuitry.

OK bear with me here. There are a lot of pin numbers that need to be kept straight.
From the Maximite manual:
COM1: uses pin 15 for receive data (data in) and pin 16 for transmit data (data out). If flow control is specified pin 17 will be used for RTS (receive flow control – it is an output) and pin 18 will be CTS (transmit flow control – it is an input).
COM2: uses pin 19 for receive data (data in) and pin 20 for transmit data (data out) in the monochrome Maximite and D0 for receive data and pin D1 for transmit data on the Colour Maximite.
We will use COM1 on the CGCOLORMAX1for this example.
The MMBasic firmware transmits data out of the Maximite on what the software refers to as I/O Pin 16. I/O Pin 16 is located on the CGCOLORMAX1 on J9 (and J9a). If you count pins from the square pin (1) on J9 and zig zag back and forth on that connector then I/O Pin 16 is located on that connector's 14th pin. On the hardware picture above I connected to J9.14 with a blue wire.
This TX signal needs to go to the RS232 circuit connector on the CGCOLORMAX1. This would be J10, along the edge of the printed circuit board. The blue wire needs to connect to pin 7 on J10. The circuit converts this TX signal to RS232 voltage levels and the TX signal then appears on J12, pin 6. This is connected to a PC serial port RS232 input via the 2nd blue wire.
The PC receives this signal from the CGCOLORMAX1 and transmits a reply to the receive circuit via a white wire connected to J12 pin 5. The RS232 circuit on the CGCOLORMAX1 converts the voltage level to something safe for the CGCOLORMAX1 and that received signal appears on J10, pin 6.
A white wire connects the J10 pin 6 signal to J9 (or J9a) Pin 12. That pin is associated with software I/O Pin 15 to receive the date.
Whew! Did you follow all of that?
A serial port application on the PC is set to the right baud (in this case 1200) to match that of the MMIDE applet.

The MMIDE applet communicates with the PC through the hardware described above.
The “Open 1” button sends the command:
OPEN “COM1:1200, 256” AS #1
to the CGCOLORMAX1 to open the serial port.
The text string that you type in the send text area is sent to the serial port when the “Send” button is pushed.
PRINT #1, “This is a test”
This is a test” is then sent out the serial port of the CGCOLORMAX1 to the PC.
A string typed in the serial port application on the PC is received by the CGCOLORMAX1 into the serial buffer. Pressing the “Get” button sends a command from MMIDE to the CGCOLORMAX1 to get stuff out of it's buffer and then MMIDE displays it.


Purchase a CGMMSTICK1 Download of Maximite Integrated Development Environment: MMIDE

CGMMSTICK Vacuum Fluorescent Display Example


I've had a couple of these display modules setting around for a while. They have a 10-pin connector, not the 'normal' 14 that an LCD module has. I also don't have a data sheet for the display, but a couple of old data sheets for different displays. Every time I bump into the modules in my box of stuff I wonder what they look like when activated.


I decided to wire up the display to a CGMMSTICK1 to test them. It is a 5V display, so I used OC outputs pulled to 5V through a 10k resistor. The display has a reset line, data in, and clock. Data is most significant bit first.
Pins 2, 4, 6, 8, 10 are ground
Pin 1 +5V
Pin 3 clock
Pin 5 data
Pin 7 reset




The value that it takes to write "A" is 1, "B" is 2. This doesn't follow ASCII. Below is the code I used to test the display:
' VFD test code


' I/O init
' Open collector data
SETPIN 12, 9 : PIN(12) = 1

' Open collector clock
SETPIN 13, 9 : PIN(13) = 0

' Open collector reset
SETPIN 15, 9 : PIN(15) = 1


' Values for characters
_A = 1 : _B = 2 : _C = 3 : _D = 4 : _E = 5
_F = 6 : _G = 7 : _H = 8 : _I = 9 : _J = 10

_K = 11 : _L = 12 : _M = 13 : _N = 14 : _O = 15
_P = 16 : _Q = 17 : _R = 18 : _S = 19 : _T = 20
_U = 21 : _V = 22 : _W = 23 : _X = 24 : _Y = 25 : _Z = 26


_AT = 0 : _SPACE = 32

_ZERO = 48 : _ONE = 49 : _TWO = 50 : _THREE = 51
_FOUR = 52 : _FIVE = 53 : _SIX = 54 : _SEVEN = 55
_EIGHT = 56 : _NINE = 57



' Initialize and display message
VFDRESET

VFDDATA _SPACE
VFDDATA _C
VFDDATA _I
VFDDATA _R
VFDDATA _C
VFDDATA _U
VFDDATA _I
VFDDATA _T
VFDDATA _SPACE
VFDDATA _G
VFDDATA _I
VFDDATA _Z
VFDDATA _M
VFDDATA _O
VFDDATA _S
VFDDATA _SPACE

END


' VFD display reset routine
Sub VFDRESET

' pulse the reset line
PIN(15) = 1 : PAUSE 1
PIN(15) = 0 : PAUSE 1
PIN(15) = 1 : PAUSE 1

' Clear the display
VFDDATA &h20
VFDDATA &h20
VFDDATA &h20
VFDDATA &h20
VFDDATA &h20
VFDDATA &h20
VFDDATA &h20
VFDDATA &h20
VFDDATA &h20
VFDDATA &h20
VFDDATA &h20
VFDDATA &h20
VFDDATA &h20
VFDDATA &h20
VFDDATA &h20
VFDDATA &h20
' Turn display on
VFDDATA &hFF

End Sub

' Write data to display, MSB first
Sub VFDDATA (valu)

PIN(13) = 1

PIN(12) = valu AND &B10000000
PIN(13) = 0 : PIN(13) = 1

PIN(12) = valu AND &B01000000
PIN(13) = 0 : PIN(13) = 1

PIN(12) = valu AND &B00100000
PIN(13) = 0 : PIN(13) = 1

PIN(12) = valu AND &B00010000
PIN(13) = 0 : PIN(13) = 1

PIN(12) = valu AND &B00001000
PIN(13) = 0 : PIN(13) = 1

PIN(12) = valu AND &B00000100
PIN(13) = 0 : PIN(13) = 1

PIN(12) = valu AND &B00000010
PIN(13) = 0 : PIN(13) = 1

PIN(12) = valu AND &B00000001
PIN(13) = 0 : PIN(13) = 1

End Sub




And the result:


Purchase a CGMMSTICK1 Download of Maximite Integrated Development Environment: MMIDE

CGMMSTICK LCD Interface Example


The Direct I/O page of MMIDE can also be used to test out an LCD display. In this example an LCD display is interfaced to the first ten (Pins 1-10 from MMBasic's perspective) and clicking the “Write” button in the 8-bit LCD Applet will write the text string that is in the text box above the button.
The LCD is wired as follows:
Pin 1 (11 on the connector) connects to the LCD data/control line.
Pin 2 connects to the LCD enable line.
The LCS read/write line is connected to ground. Ground and 5V power the LCD.
Pins 3-10 connect to the LCD D0-D7 data lines.

Prior to writing the text to the LCD, the applet writes the command sequence of &h33, &h33, &h38, &h0C, &h01. The LCD data sheet will help you understand the commands.






Purchase a CGMMSTICK1 Download of Maximite Integrated Development Environment: MMIDE


Monday, January 30, 2012

Maximite Input Example

Simple input is pretty simple to test using MMIDE. All of the 20 pins of the CGMMSTICK1 can be set to be digital inputs. To test a simple button input, just use this testing facility.

All 20 of the CGMMSTICK1 lines can be controlled for testing.

CGMMSTICK1 button input.

A simple button input for the CGMMSTICK1 would use a 10 kilo ohm resistor to pull the input line (in this case MMBasic pin 20) high, then the button press would short the line low.

Testing the input.


With the right pin set to “Digital Input” both states of the button can be tested using MMIDE to do the work. In the picture above the red square and value of 1 indicate that the button is not pushed – the resistor is pulling the input line high.


Purchase a CGMMSTICK1

Download of Maximite Integrated Development Environment: MMIDE

Maximite Sound Example

The Maximite can make sound using the 'SOUND' command. The SOUND command accepts a frequency to play for the sound and a duration to play that sound.

MMIDE has an Applet that plays Ode to Joy.

On the CGMMSTICK1 the audio connection is NOT on J1 along with all of the port lines. Instead it is at the top of the board on an unpopulated header named J5.

On J5 the top connection is ground, the bottom (square) hole is the audio line.

If you have a small amplifier, you can connect the amp to these two lines and run the Ode to Joy Applet that is in MMIDE.

CGMMSTICK1 audio connection.
Using the Ode to Joy Applet in MMIDE to play a tune.


The Ode to Joy Applet downloads a MMBasic program to the CGMMSTICK1 and then runs that program. Below is the program that is sent. You might notice that there is a test for data where the frequency could be 0. If the frequency is 0 no note is played, just a delay.

I asked my 15 year old son to write this program. He used a frequency/note chart to get the frequencies right, but the notes for Ode to Joy were in his head – he didn't have to look them up.

His frequency table was a little bit different than the table that I reproduced after the program below.

10 FOR a=1 TO 65
20 READ fr,du
30 IF fr=0 THEN
40 PAUSE du
50 ELSE
60 SOUND fr,du
70 ENDIF
80 PAUSE du
90 PAUSE 30
100 NEXT a
110 DATA 493.88,300,493.88,300
120 DATA 523.25,300,587.33,300
130 DATA 587.33,300,523.25,300
140 DATA 493.88,300,440.00,300
150 DATA 392.00,300,392.00,300
160 DATA 440.00,300,493.88,300
170 DATA 493.88,450,440.00,200
180 DATA 440.00,300
190 DATA 0,200
200 DATA 493.88,300,493.88,300
210 DATA 523.25,300,587.33,300
220 DATA 587.33,300,523.25,300
230 DATA 493.88,300,440.00,300
240 DATA 392.00,300,392.00,300
250 DATA 440.00,300,493.88,300
260 DATA 440.00,450,392.00,200
270 DATA 392.00,300
280 DATA 0,200
290 DATA 440.00,300,440.00,300
300 DATA 493.88,300,392.00,300
310 DATA 440.00,300,493.88,150
320 DATA 523.25,150,493.88,300
330 DATA 392.00,300,440.00,300
340 DATA 493.88,150,523.25,150
350 DATA 493.88,300,440.00,300
360 DATA 392.00,300,440.00,300
370 DATA 293.66,550
380 DATA 0,200
390 DATA 493.88,300,493.88,300
400 DATA 523.25,300,587.33,300
410 DATA 587.33,300,523.25,300
420 DATA 493.88,300,440.00,300
430 DATA 392.00,300,392.00,300
440 DATA 440.00,300,493.88,300
450 DATA 440.00,450,392.00,200
460 DATA 392.00,300



Note/Frequency Table for SOUND command
Note Frequency
C 16.4
C sharp / D flat 17.3
D 18.4
D sharp / E flat 19.5
E 20.6
F 21.8
F sharp / G flat 23.1
G 24.5
G sharp / A flat 26
A 27.5
A sharp / B flat 29.1
B 30.9
C 32.7
C sharp / D flat 34.7
D 36.7
D sharp / E flat 38.9
E 41.2
F 43.7
F sharp / G flat 46.3
G 49
G sharp / A flat 51.9
A 55
A sharp / B flat 58.3
B 61.7
C 65.4
C sharp / D flat 69.3
D 73.4
D sharp / E flat 77.8
E 82.4
F 87.3
F sharp / G flat 92.5
G 98
G sharp / A flat 104
A 110
A sharp / B flat 117
B 123
C 131
C sharp / D flat 139
D 147
D sharp / E flat 156
E 165
F 175
F sharp / G flat 185
G 196
G sharp / A flat 208
A 220
A sharp / B flat 233
B 247
C 262
C sharp / D flat 277
D 294
D sharp / E flat 311
E 330
F 349
F sharp / G flat 370
G 392
G sharp / A flat 415
A 440
A sharp / B flat 466
B 494
C 523
C sharp / D flat 554
D 587
D sharp / E flat 622
E 659
F 698
F sharp / G flat 740
G 784
G sharp / A flat 831
A 880
A sharp / B flat 932
B 988
C 1047
C sharp / D flat 1109
D 1175
D sharp / E flat 1245
E 1319
F 1397
F sharp / G flat 1480
G 1568
G sharp / A flat 1661
A 1760
A sharp / B flat 1865
B 1976
C 2093
C sharp / D flat 2217
D 2349
D sharp / E flat 2489
E 2637
F 2794
F sharp / G flat 2960
G 3136
G sharp / A flat 3322
A 3520
A sharp / B flat 3729
B 3951





Purchase a CGMMSTICK1

Download of Maximite Integrated Development Environment: MMIDE