Close

Not a member yet? Register now and get started.

lock and key

Sign in to your account.

Account Login

Forgot your password?

Adding More Digital I/O Part 2

Part 2 -Adding More Digital I/O to your 16 Bit Microcontroller

 Experiments (The Software)

 

 In a previous increment of this feature we discussed how to use the MCP23S08 device to add digital I/O to the Experimenter (see figure below). In this increment we will discuss the associated software to make this work. Demo software is written in PIC24F C Complier and is available as a software downloadable project from the Nuts and Volts web site.  The demo uses the MCP23S08 device to emulate an eight bit output port. All of the outputs are configured to drive their own individual LED through an external current limiting 100 ohm resistor. The microcontroller turns each LED on and off one at a time in sequence; in addition the LCD on the Experimenter announces the demo. Let’s get started with the software overview.

 

Before using the MCP23S08 to drive eight LEDs it needs to be programmed over SPI. There are up to eight distinct registers to program in the device internally and the following register descriptions offer a snapshot of the device’s total capabilities. Keep in mind the device is built for general purpose applications and we are only programming IODIR and GPIO for our experiment.

  • Address 0   IODIR sets pin to be input or output
  • Address 1   IOPOL sets polarity on pin for positive or negative logic
  • Address 2   GPINTEN interrupt enable
  • Address 3   DEFVAL default configuration on each pin
  • Address 4   INTCON interrupt control for each pin
  • Address 5   IOCON device configurations like enabling external address pins
  • Address 6   GPPU enables internal pull up resistor for pin
  • Address 7   INTF interrupt flag condition for specific pin
  • Address 8   INTCAP pin value captured upon interrupt
  • Address 9   GPIO input/output ports for device
  • Address 10 OLAT provides access to output port latches of the device

 

The MCP23S08 follows a specific protocol for the microcontrollers to access its register set. (see diagram below). Once a register is addressed the final data read or write operation can be performed as the next byte transfer prior to CS going high.

 

Figure 4 Addressing Internal MCP23S08 registers

Our initializing of the MCP23S08 is very straightforward and appears in the Main function in demo project file Graphicstest.c (see initialization code). CSPORT is chip select for MCP23S08 (pin 10 on Expansion bus). Critical functions used in initialization are:

  • initSD() – initializes SPI for operation , library module referenced by Graphics.h
  • writeSPI (data) – writes eight bit data\command  out to MCP23S08 , library module referenced by Graphic.h

 

Initialization Code

 

Once the SPI is initialized, the SPI is then used to set up MCP23S08 operation. First, we activate chip select CSPORT to initiate transfer with MCP23S08 and then output opcode 0×40 to SPI to access  MCP23S08 device at address 0 for a write operation( reference hook up diagram in feature part 1 description). The MCP23S08 pins A1, A0 are tied to ground, making the device address 0. The next Opcode 0×00 specifies the write address as IODIR (port direction setting control) and the final 0×00 sets up IODIR register for all MCP23S08 port bits to outputs. CSPORT is then set high to complete the transaction.

Once this is done we just enter a continuous loop (see operating code) where each led pattern update is just a separate MCP23S08 transaction. Again CSPORT is activated, opcode 0×40 is again issued to indicate write operation to designated MCP23S08 at address 0, a new opcode 0×90 is then issued to address GPIO register  to access port contents, and then finally the LED pattern is issued to the port , before CSPORT is set high closing the transaction. This basic transaction scheme is repeated eight times within the loop continuously, between times delays, (to allow you to see the individual LEDs flash). The led pattern changes from 0×01, 0×02, 0×04, 0×08, 0×10, 0×20, 0×40 and 0×80 before repeating.

Operating Code

One thing to keep in mind with the PIC24F microcontroller smaller packages (like the one used in the Experimenter) is the Peripheral Pin select feature or PPS. With PPS you need to configure the PIC24F pins on startup to access its internal peripherals like the SPI. This is essential for configuring the SPI pins to map to the I/O expansion bus, and exists in our demo at the start of the Main function in demo project file Graphicstest.c.  The MCP23S08 device is very comprehensive in its capability –that leaves you a lot of room to explore. What is presented is only the beginning. Microchip offers an even more challenging application note that uses device to scan and report back key scan codes under interrupt for a 4X4 keypad. Hope this entices you to explore this part and make it part of your Experimenter applications.