Download Constructional Project
Transcript
Fig.2. Typical connections for Now to the crucial point – what features do we get in these chips? There are 10 registers per port – eight more than most processors provide. The datasheet for the MCP23S17 is surprisingly complicated, running to 48 pages. This is simply because Microchip want to pack as many features into the device as possible for the price. With such a simple chip, much of the cost is determined by the package. The silicon die also has to be a certain size to allow all the I/O pin wires to connect to it – so there is no point trying to save on device functionality when you have the silicon space anyway! Consequently, Microchip have thought of as many features and options as will physically fit on the silicon. Of course, you don’t need to use all the features, and probably wont. It’s nice to have the options, however. Configuration options The configuration registers break down into two groups of ten, duplicated between the two 8-bit ports, plus an overall control register. The common registers are suffixed with the letter A or B to indicate which port it is referring to. We will discuss the registers first, and then how to actually access them – which is more complicated than you might expect. IODIR: This determines whether a pin is an input or an output, just as the TRIS register does on a PIC processor. IPOL: Input polarity. An unusual register, this one; when a bit is set it causes the corresponding input port bit to be inverted in the input port register GPIO. It’s difficult to think of a use for this, but the default setting of the register is all zeros, having no effect, so you can safely ignore this register. GPINTEN: Interrupt-on-change enable. This register allows you to enable one or more pins to act as interrupt sources. When an interrupt occurs it simply causes one of the interrupt pins to change state. You can wire this pin to an interrupt pin on your microcontroller to receive immediate notification of a change of state on an input pin on the MCP23S17, useful for keyboard interfaces. DEFVAL: This register works in conjunction with the GPINTEN register. When the interrupton-change feature is enabled, this specifies the ‘default’ expected value on the corresponding input pin. When the input pin takes on a value different to the value in DEFVAL, an interrupt is generated. INTCON: This register also works in conjunction with the GPINTEN regMCP23S17 ister. It specifies whether an interrupt is generated when an input pin changes state (toggles) or when it changes to a value that is different to the default value specified in DEFVAL. GPPU: On a per pin basis, this allows an internal pull-up resistor to be enabled for the specified pins. This is a great feature for reducing external components, but bear in mind that the pull-up value is quite weak – 100kΩ – and is not accurately defined, so it could vary considerably between different ICs. By default, the pull-ups are disabled, so make sure you enable them for any unused I/O pin. INTF: Interrupt status flag register; this register will indicate which bit, or bits, caused an interrupt. The bit will stay set in the register until the GPIO or INTCAP register has been read. INTCAP: Interrupt capture register; this records the status on the input pins at the instant at which the interrupt occurred. This can be very useful for recording data at the precise instant that the interrupt occurred. After all, it could take several milliseconds from detecting an interrupt to actually reading the status of the input ports. Handy for interfacing to analogueto-digital ICs perhaps. GPIO: Port register; reading this register will return the status of the port pins. A write will cause any output pins to take on the value specified in the write. OLAT: Output latch register; this register holds the value of data written to a port, either through this register or to the GPIO register. It’s equivalent to the LAT register on the PIC18F devices. Everyday Practical Electronics, July 2008 Overall control register Finally, there is an overall control register called IOCON. There are two copies of this register in the memory map of the device. The extra one is there for convenience. Seven bits within this register control overall operation of the chip. These are: BANK: Selects whether the port A and port B registers are grouped separately, or accessed one after the other. This can help reduce the time it takes to read data from the two ports (if BANK = 0). There is little in the difference, so it is probably best leaving this at its default value of 0. MIRROR: determines whether the chip provides a single interrupt output pin (MIRROR = 1) or one pin per 8-bit port. SEQOP: Determines whether the internal address pointer of the device increments by one when an access is made to the device. Turning off sequential operation (SEQOP = 1) increases the speed at which a single port can be read (or polled) since it is not necessary to constantly resend the device address and command. For non-speed-critical applications it is probably better to turn off sequential addressing to help make your software more readable. DISSLW: Slew rate control; this is the one and only configuration option for the SPI interface. Enabling this feature improves the reliability of SPI communication at high speeds or when there are many devices on the SPI bus. HAEN: Hardware address pins enable; a strange option, this one. You can disable the use of the three address selection pins. If you disable the option you must still wire the pins to one of the supply rails, and you must still specify a value of 000 for the address bits in the SPI message. Best to leave this feature enabled. ODR: Determines whether the interrupt output pins are push-pull or open drain. Open drain means that the pin can only drive the signal low, which means you can parallel up several open drain signals to the same pin. If you use open drain, you must provide a pull-up resistor, since the device can only pull a signal low. INTPOL: When the ODR register is set to push-pull, this register determines whether the ‘an interrupt has occurred’ state is a high or low level. Next month In next month’s article we are going to concentrate on using this chip as an output port expander, and so will ignore the interrupt facilities of the device. How you use the interrupt features is very specific to the particular application, but if there is interest in this subject then we will cover it in another article. Each I/O pin can source up to 25mA, more than enough to drive an LED. Bear in mind, however, that the maximum current the device can manage overall is 150mA, so don’t drive 16 LEDs at 25mA! The author finds that most LEDs work quite acceptably at 6mA to 8mA, so with care you could drive 16 LEDs without buffering. Another great feature of the MCP23S17 is that it can operate down to 1.8V, which fits nicely with the operating voltage range of some of the small lowpowered microcontrollers. You can easily drive a circuit from two AAA cell batteries, or a lithium ‘coin’ cell battery if the overall power consumption is low enough. When operating below 3V, however, note that the maximum SPI bus speed is reduced to 5MHz. 67