Matrix Keypad Interfacing

Discussion specific to the 24-pin ZX microcontrollers, e.g. ZX-24r, ZX-24s and ZX-24t.
sselver
Posts: 14
Joined: 26 April 2010, 19:41 PM

Matrix Keypad Interfacing

Post by sselver »

Hello,

I've been working on getting a matrix keypad working with the zx24 for a few weeks now, but still haven't succeeded. One of the problems is my lack of understanding of IC part numbers. As an example, it seems like there are several versions of IC that may fall under the umbrella of "PCF8574A", such as the "PCF8574ADW" or the "PCF8574AP". Can anyone possibly point me toward a good reference for a newbie to understand the IC names? Or better yet, would anyone be wiling to provide any detailed part numbers for the AN-205 ICs? Any help would be greatly appreciated!
Thanks,

Steve
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: Matrix Keypad Interfacing

Post by dkinzer »

sselver wrote:Can anyone possibly point me toward a good reference for a newbie to understand the IC names?
As far as I know, there is no industry standard for part numbering. Each manufacturer devises their own system to incorporate device type, speed grade, packaging and other parameters as necessary. Generally speaking, you'll find information to understand a particular part number in the device's datasheet or on the manufacturer's website.

For the PCF8574, for example, let's examine this NXP/Philips datasheet:
http://www.nxp.com/documents/data_sheet/PCF8574.pdf

In the "General Description" section, we find this useful information:
The PCF8574 and PCF8574A versions differ only in their slave address as shown in Fig.10.
This tells us that there is an "A" and a "non-A" part that are identical except for the I2C address to which they respond.

Moving on the the "Ordering Information" section, we find a chart that tells us that the P suffix indicates a PDIP-16 package, the T suffix indicates an SO-16 package and the TS suffix indicates an SSOP-20 package. Apparently, these are the only packaging options that NXP/Philips provides.

Since this datasheet does not describe all of the suffixes that you mentioned, we can search further for datasheets from other manufacturers. Consider this one from TI:
http://focus.ti.com/lit/ds/symlink/pcf8574.pdf

On the second page of this datasheet we find an "Ordering Information" chart that indicates TI's mapping of suffixes to package types. For example, the N suffix indicates PDIP-16 while the DW suffix indicates an SOIC-16 package.

Since the A suffix was not addressed in this datasheet, we search further at the TI site and find this one covering the PCF8574A:
http://focus.ti.com/lit/ds/symlink/pcf8574a.pdf
sselver wrote:I've been working on getting a matrix keypad working with the zx24 for a few weeks now, but still haven't succeeded.
Which part are you using and what kinds of problems are you encountering?
- Don Kinzer
sselver
Posts: 14
Joined: 26 April 2010, 19:41 PM

Post by sselver »

Hi Don,

Thanks very much for the thoughtful reply. I have tried several combinations of parts/wiring/compiling procedures without much luck so far, but I intend to keep at it until it works. Don't get me wrong- I'm not trying to imply that there's anything wrong with the application notes. On the contrary, I am sure I'm making some mistakes. That said, my problem is simply that I'm not seeing any response to the keys being pushed. If I understand correctly, I should see the values showing up in the debug area of the IDE. I do see the "ZBasic 3.0" message, but that's it. I'm pretty confident with the wiring, and the compiling procedure seems to be working smoothly.

On the AN205 Diagram for the I2C keypad wiring, are there any implied connections for pins 12 and 13 on the PCF8574? I'm currently using the PCF8574AP with a Devantech 4 x 3 keypad.

If I can't get it working, can you please consider assembling one for me for a reasonable fee (providing I sent all the required parts)?

THis is the compile message that the IDE spits out:

>"C:\Program Files\ZBasic\zbasic.exe" --target-device=ZX24p --directory="C:\Documents and Settings\Steve\Desktop\zbasic practice/" --project="i2c_int_keyboard.pjt"

No errors. Target device: ZX24p, Code: 590 bytes, RAM: 80 bytes, Persistent memory: 0 bytes
Thanks
STeve
sselver
Posts: 14
Joined: 26 April 2010, 19:41 PM

Post by sselver »

ALso..

The closest I've gotten so far is with the setup that uses the 74HC164/74HC165. In that case, the output is a continous stream of 31, 34, and 37. The output stops when a key is pressed, and then continues with the 31, 34, and 37s after the key is released. For this one, I'm currently using the following:

SN74HC05N
SN74HC165N
MC74HC164N

Any ideas? Thanks.

Steve
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

sselver wrote:I have tried several combinations of parts/wiring/compiling procedures without much luck so far.
Debugging an embedded application is a combination of art and science. An incremental approach usually works the best because it reduces the number of "unknown" compared to trying to test/debug an entire system at the same time. Divide and conquer, so to speak. You first make sure that the lowest level elements are working and then add more layers, testing each one, until you have the entire system up and running.

In this particular case, I would recommend first verifying that you can successfully communicate with the PCF8574 or PFC8574A (whichever you have). If you cannot communicate with the IO Expander, then the keypad interface is clearly not going to work.

The test program below can be used to confirm that the communication is working correctly. (For this test, you should not have the matrix keypad connected to the I/O expander.) It is a simple program that does nothing more than write the value &HFF to the I/O expander and then read back the result. If the I/O expander is connected correctly, the Debug window should display this:

Code: Select all

ival=1, dataIn=&B11111111
ival=1, dataIn=&B11111111
ival=1, dataIn=&B11111111
If this works, then move on to the next step which is to connect a grounding wire to each output of the IO expander, one at a time. and observe that the dataIn value reflects the grounded output. For example, of you ground bit 0, the output should be &B11111110 and if you ground bit 4 it should be &B11101111. Ground each output in turn and confirm that the zero "walks" across the eight bits.

If this test produces the expected result, then the problem lies with the way that the matrix keypad is being connected. Try this first and let us know the result.

The test program below is written for the "A" version. If you're using the "non-A" part, change the value of i2cAddrBase from &H70 to &H40. Also, be sure to connect the A2, A1 and A0 inputs of the I/O expander to ground or modify the i2cAddrOfst to match the state of those inputs. Another thing to confirm is that you have pullup resistors on the SDA and SCL lines. A value of 2.2K to 4.7K should work fine.

Code: Select all

' define the address components of the PCF8574A
Private const i2cAddrBase as Byte = &H70  ' this part is fixed
Private const i2cAddrOfst as Byte = &H00  ' this part is selectable by A2-A0
Private const i2cAddr as Byte = i2cAddrBase + i2cAddrOfst

' define the I2C channel to use
Private const i2cChan as Byte = 0

Sub Main()
  Dim dataOut as Byte
  Dim dataIn as Byte
  Dim ival as Integer

  Call OpenI2C(i2cChan, 0, 0)
  dataOut = &HFF
  Do
    ival = I2CCmd(i2cChan, i2cAddr, 1, dataOut, 1, dataIn)
    Debug.Print "ival="; ival; ", dataIn=&B";
    Call dspBinaryValue(dataIn)
    Debug.Print
    Call Sleep(1.0)
  Loop
End Sub

'
'' dspBinaryValue
'
' Display a value in binary format.
'
Sub dspBinaryValue(ByVal val as Byte)
  Dim mask as Byte = &H80
  Do
    Debug.Print IIF(CBool(val And Mask), 1, 0);
    mask = Shr(mask, 1)
  Loop While &#40;mask <> 0&#41;
End Sub
- Don Kinzer
sselver
Posts: 14
Joined: 26 April 2010, 19:41 PM

Post by sselver »

Hi Don

I tried running that code with all of my PCF8574s but nothing showed up in the debug window. I have reasonably good programming skills, but no where near the electronics background it sounds like I'd need to make this happen.

The application notes make this look quite simple, so I figured I'd give it a shot but perhaps I'm in over my head. I've successfully programmed microcontrollers to do a handful of simple tasks, so reading a keypad didn't seem like it should be this hard.

Clearly you have succeeded with this application- can you please simply list the detailed part numbers of the ICs you used? If you can do that then my problem will be instantly reduced to wiring and I won't have to do all this complicated troubleshooting to figure out if I'm using the right IC.

Steve
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

sselver wrote:can you please simply list the detailed part numbers of the ICs you used?
I used PCF8574AP, Digi-Key p/n 568-1073-5-ND, with the following connections:
  • pins 1, 2, 3 and 8 to ground
  • pin 16 to +5V
  • pin 14 to pin 12 of the ZX-24 with a 2.2K resistor to +5V
  • pin 15 to pin 11 of the ZX-24 with a 2.2K resistor to +5V
- Don Kinzer
sselver
Posts: 14
Joined: 26 April 2010, 19:41 PM

Post by sselver »

THanks! I'll give this a shot and report back later

Steve
sselver
Posts: 14
Joined: 26 April 2010, 19:41 PM

MATRIX KEYPAD FOLLOW-UP

Post by sselver »

Hi Don,

Still have any interest in helping me figure this out? I bought the identical pcf8574 that you used and wired it up exactly as specified by you and the application notes. Unfortunately it still isn't working. I tried loading the project various ways, but nothing is helping.

In the debug window, all I see is the initial "ZBasic v3.0" message. Pressing keys on the keypad has no effect. I have all three 5V connections on a 5VDC bus, and all ground connections to a ground bus. I have carefully checked the row and column wiring for the keypad.

Is there anything else you might recommend I do, considering that I don't have the skills to troubleshoot the PCF8574? As always, your advice would be greatly appreciated. As a reminder, I'm trying to replicate the i2c_int_keyboard.pjt project.


Steve
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: MATRIX KEYPAD FOLLOW-UP

Post by dkinzer »

sselver wrote:I bought the identical pcf8574 that you used and wired it up exactly as specified by you and the application notes.
I still think you're trying to do too much at once. Rather than attempting the entire keypad circuit, start by verifying that you can communicate with the PCF8574 (or PCF8574A - which do you have?). One of my posts a few above this one gives a simple program for verifying that the I2C communication is working and that you can read the I/O pins of the I/O expander.

If this program doesn't produce the anticipated result then there is something wrong with the wiring. I recommend that you sketch out a schematic for the connections, scan it and upload the image. Then, take a clear, well illuminated photo of the wiring and post that, too. Someone might be able to confirm that it is correct or spot an error.

Do you have a multi-meter, logic probe, oscilloscope or logic analyzer? A multi-meter is a basic tool that everyone should have and one suitable for digital work is quite inexpensive.
- Don Kinzer
sselver
Posts: 14
Joined: 26 April 2010, 19:41 PM

Post by sselver »

Don,

I'm using the PCF8574AP you specified by Digi-Key part number in a previous post. As for the equipment, I'm sporting a RadioShack digital multimeter, and none of the other tools (are they really necessary or only optional?). Frankly, I've never even heard of a logic probe or logic analyzer, although I'd be interested to learn more about them. I'll post back later with photos and results. Thanks again for your time

Steve
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

sselver wrote:I've never even heard of a logic probe or logic analyzer, although I'd be interested to learn more about them.
See the wikipedia entry Logic Probe. You can find commercially made logic probes from about $15 and up; kits are also available as are plans to make your own. Several listings can be found on eBay. I have a Tektronix P6401 logic probe that I keep connected to my prototyping board making it quick and easy to verify a logic level, pulse or oscillating signal.
- Don Kinzer
spamiam
Posts: 739
Joined: 13 November 2005, 6:39 AM

Re: MATRIX KEYPAD FOLLOW-UP

Post by spamiam »

dkinzer wrote:I still think you're trying to do too much at once. Rather than attempting the entire keypad circuit, start by verifying that you can communicate with the PCF8574 (or PCF8574A - which do you have?).
I agree with Don.

Divide and conquer!

Use just the ZX and the I/O expander.

You need a 4.7K pull-up resistor on the SCL and SDA ZX pins. Connect the SCL and SDA ZX pins to the matching pins on the PCF8574. THis is to allow communication with the I/O expander.

Then give the expander some inputs to report. Ground most of the I/O pins through a 2.2K resistor to ground. Ground the A0, A1, A2 pins (these are used to set the addesss of the chip). On three of the I/O pins, (e.g. the top of bottom 3) connect these through a 4.7K resistor to VCC.

Then communicate with the PCF8574. Do you always get the same reading when you ask it for the incoming signals? You should always get the same value because the pins are always the same setting.

If you always get the same value, then change the ground and vcc connections on the I/O pins. Does the reading change?

If you get consistent results and the values vary depending of the i/o pin settings. then try to determine if you are getting a reading that matches what you expect to read based on the PCF8574 inputs.

If you can't get consistent results, then there is a basic problem with communication or interpretation of the I2C readings.

I have found getting I2C communication to work properly to be a pain in the neck. It is not as simple as you think to figure out what data to send and what data to expect back, and when. Once you get the basic protocol required for the individual devie established, then I2C works really well.

Also use a slow I2C communication speed at first just in case that there is a speed-related problem with the wiring.

-Tony
sselver
Posts: 14
Joined: 26 April 2010, 19:41 PM

Thanks!

Post by sselver »

Don and Tony,

Thanks for the insight. I got my keypad working!

Now I'm stuck on another (smaller) issue and figured I don't have much to lose asking for help on that too.

Basically I'm using the keypad to control a PWM output pin, and the PWM works beautifully except for one thing: there is always a about 0.25 V measured from the PWM output pin, even when there are no pulses. Just noise?

I'd like to be able to stretch the whole range from 0.0 V to 5.0 V. Do I just need a pull-down resistor? Any tips or insight would be greatly appreciated.

Sincerely,

Steve Selverston
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: Thanks!

Post by dkinzer »

sselver wrote: the PWM works beautifully except for one thing: there is always a about 0.25 V measured from the PWM output pin, even when there are no pulses.
Which PWM mode are you using? The zxFastPWM mode might not be able to get to zero volts - I'd need to check the code but it is either 0% or 100% that is unobtainable in that mode. Note, however, that zxCorrectPWM mode is capable of realizing both 0% and 100%.
- Don Kinzer
Post Reply