how to get higher input/output ports

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Post Reply
saravana_3
Posts: 61
Joined: Thu Dec 20, 2007 4:23 pm
Location: singapore
Contact:

how to get higher input/output ports

Post by saravana_3 »

hi friends,
now i am using the 16F877A and using the HP 488 developmet board for my current project this chip has 33 I/O ports, for my next project I need 40 I/O ports, but if I go for high end chips i need to buy another development board that is capable of 64 pins, is there any way to use the same chip for the higher number of I/O ports, is it can use the multiplexer and de multiplexer, or any way by the source code.
please guide me
thanks
:roll:
saran

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

Re: how to get higher input/output ports

Post by Sean »

You will be able to greatly expand the 33 i/o of a 16F877A using combinations of multiplexers, latches, bi-lateral switches etc. available in the standard TTL/CMOS device ranges. A problem with this approach is that the resulting i/o has the (generally inferior) characteristics of the added components, not the microcontroller.
An alternative might be to use the i/o expander devices available for either SPI or I2C bus. These provide a great deal of flexibility with extremely simple wiring. Microchip produce two suitable devices, the MCP23S17 (SPI) and the MCP23017 (I2C). Each device provides 16 i/o, interrupt options, and multiple devices can be connected to the same bus to provide potentially massive expansion capabilities.

saravana_3
Posts: 61
Joined: Thu Dec 20, 2007 4:23 pm
Location: singapore
Contact:

Re: how to get higher input/output ports

Post by saravana_3 »

thanks Sean,
I think the MCP 23S17 is the right choice, if i use the chip I need to create the external hardware. i went through the data sheet, my under standing is the output of the MCU port (8 bit) will be connected to the MCP 23s17 and it will give the 16 output and it can be connected to the external devices. so for the 16F877A chip we can have double of the original size will able to get from the adaptor. but how to configure the MCU output and the expander output, let us say if PORTB's all the 8 bit give the output at the same time how it is going to be 16 individual outputs from the expander. are we need to download any programs into the expander, is it the same HPP-488 kit can be use to program the chip.
please explain me
thanks
:lol:
saran

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

Re: how to get higher input/output ports

Post by Sean »

Communication with the MCP23s17 is via the SPI bus, which is one of the configurations of the MSSP module in the PIC16F877A. The connections are available on the PORTC connector of the HP488 board. Only 3 SPI connections (SDO, SDI, SCK) are required, with an additional chip select signal. Several devices can be connected to the same SPI bus, using the addressing and chip select options, adding 16 I/O lines each.

Each MCP23s17 contains several registers that are used to configure the I/O. These will need to be programmed each time the system powers up.

Unfortunately we can not offer much support for this device as it is not part of our current device range. We do have access to the MCP23017 (the I2C version) which uses the same MSSP mdule in the PIC16F877A, has a slightly more complicated communication protocol, but only requires 2 bus connections and no chip select. We do not currently have any example software available, but it should be possible to find some on the Microchip website or other sites.

saravana_3
Posts: 61
Joined: Thu Dec 20, 2007 4:23 pm
Location: singapore
Contact:

Re: how to get higher input/output ports

Post by saravana_3 »

thanks Sean,
let me find in the microchip website, if you are able to give any examples that's fine,
let me keep on try to achieve it.
thanks
Saran
saran

saravana_3
Posts: 61
Joined: Thu Dec 20, 2007 4:23 pm
Location: singapore
Contact:

Re: how to get higher input/output ports

Post by saravana_3 »

Hi Sean,
i manage to get some examples from the web, those examples shows, how to set the expander's (MCP 23S17) ports as Input, output and how to scan the expander ports and transfer to the MCU. but I am using the latch operation that is if I press the swithch the MCU's RA0 then the output is latched with high at the MCu's RB0, when again pressing theswitch of RA0 then the RB0 output is low. I can do it easily with the MCU but if using the expander I/O I dont know how to do.
any help please.

you are mention can add multiple epanders in the port C how to give address to the individual devises, since we are not down loading any program into the expander how to assign the address to it.


as per the data sheet there were a lot of registers in the expander since we are not downloading the programs into it how to acess the register. if we need to control the registers from the MCU's 'C' code how to control when we using the multiple expanders.

please clear my doubts
thanks
Saran
saran

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

Re: how to get higher input/output ports

Post by Sean »

Each simple data transfer with the i/o expander chip requires the chip address and the register address within the chip to be transmitted before the data. The chip address byte also includes the read/write bit that will determine the direction of the data transfer. The control registers in the chip must be set up over the SPI bus each time the system is powered up. The port data can then be read/written over the SPI bus as required.

The output latching program you are currently using with the microcontroller ports should be able to work in a similar way with the i/o expander, using the data received and sent over the SPI bus.

The SPI versions of the i/o expander chips can be addressed in two seperate ways. Firstly there is the chip select (/CS) pin that can be used to select one, or more, devices. Secondly there are the external address inputs (A0, A1, A2) that can be used to individually address up to 8 devices sharing a common chip select line.

saravana_3
Posts: 61
Joined: Thu Dec 20, 2007 4:23 pm
Location: singapore
Contact:

Re: how to get higher input/output ports

Post by saravana_3 »

hi Sean,
i have assembled the MCP23S17 in pcb and connect it with the pic 16F877A, I wrote the C codes for the communication as per the examples found in the websites, but I cannot get any out puts in the PortA, PortB of the expander (when used as the output) there is no output at the MCU's PORTA or PORTB,when the expander is used as the input, I worried about my schematic connection, can you help me to get the schematic diagram that connects the PIC16F877A and the MCP23S17. it will help me to clear the hardware issue.
thanks
Saran
saran

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

Re: how to get higher input/output ports

Post by Sean »

I have just received some samples of the MCP23S17 devices for another project and hope to have a working demonstration soon. I will be using the HP488 board and PIC16F877A for initial trials and send you the details when it is working (hopefully in the next 24/48 hours).

saravana_3
Posts: 61
Joined: Thu Dec 20, 2007 4:23 pm
Location: singapore
Contact:

Re: how to get higher input/output ports

Post by saravana_3 »

thank you sean,
i am waiting for your kind reply
thanks
Saran
saran

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

Re: how to get higher input/output ports

Post by Sean »

My prototype circuit worked successfully. Here are the connections I used:

MCP23S17 PIN
1 - 8 PORTB
9 5v
10 0v
11 /CS (I used RC0 on the 877A, any available port pin can be used)
12 SCK (RC3 on the 877A)
13 SI (RC5/SDO on the 877A)
14 SO (RC4/SDI on the 877A)
15 - 17 A0 - A2 (Initially connected to 0v. Can be used later to modify the opcodes recognised by the chip)
18 /RESET (10k pull-up resistor to 5v)
19 - 20 INTA-B (No connection. Can be used later in some more advanced configurations)
21 - 28 PORTA

In the default configuration each register write operation requires three bytes to be sent on the SPI bus:
Set the /CS output low
Send 0x40 ( Op code to write data to a device with address 0)
Send Register address
Send Data
Set the /CS line high

A register read operation requires two bytes to be sent and one to be read:
Set the /CS output low
Send 0x41 ( Op code to read data from a device with address 0)
Send Register address
Receive Data
Set the /CS line high

To initialise the chip send the required direction bits to the IODIR registers 0x00 and 0x01 (equivalent to TRISA and TRISB)
Read or write the GPIO registers 0x12 and 0x13 (equivalent to PORTA and PORTB).

This is the simplest configuration of the device. When communications have been established it is possible to reconfigure it for other modes of operation. All the necessary information is in the device data sheet.

saravana_3
Posts: 61
Joined: Thu Dec 20, 2007 4:23 pm
Location: singapore
Contact:

Re: how to get higher input/output ports

Post by saravana_3 »

Hi sean,
thank you verymuch for your information, i follow the connection as you said, but some of the terms you said about the data transfering are not very sure, (writing operation and reading operation) i got the examples from the website and try to run. but still the same i cannot get any input and output at the expander. let me crack my head to fix the problem
saran

saravana_3
Posts: 61
Joined: Thu Dec 20, 2007 4:23 pm
Location: singapore
Contact:

Re: how to get higher input/output ports

Post by saravana_3 »

Hi Sean,
after the strugling i can see the port expander is starts to work, i use the expander's portA as the input and connect the switches (5 v direct through switch) and Expander,s PortB as the output (LED through 470 ohm resister). if i give some input and then i can get some output at the LED but the MCP23s17 is getting heated up when i give the 5v input to the expander, but normaly our development board switch output is 5v with out the resister.
is it we need to give the voltage through the resisters. or any where else i am making the mistake
thanks
Saran
saran

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

Re: how to get higher input/output ports

Post by Sean »

We seem to be using identical test circuits.
The chip should not be heating up when you apply 5v to the inputs, unless PORTA has been accidentally configured as an output. Make sure that you are setting the value of register 0 to 0xff (255).
Use pull-down resistors (between 10k and 100k) on each input to ensure a good 0V signal when the the switches are open. We also usually fit 220 ohm resistors in series with each port pin to limit the current to a safe level when outputs are incorrectly configured or connected.

saravana_3
Posts: 61
Joined: Thu Dec 20, 2007 4:23 pm
Location: singapore
Contact:

Re: how to get higher input/output ports

Post by saravana_3 »

thanks Sean,
let me follow your instruction, and let you know the outcome
thanks
Saran
saran

saravana_3
Posts: 61
Joined: Thu Dec 20, 2007 4:23 pm
Location: singapore
Contact:

Re: how to get higher input/output ports

Post by saravana_3 »

Hi sean,
I have checked in the program the input and output is defined propperly,

i give the 5v input to the port expander from the switch through the 470ohm resister, but it is not working, if i give directly 5v then it is working some times but the chip heated up.
another think i notice when the program is running the output of the switch before resister is 5v after the resister is 250mv
and when i check the resistece between the ground and the port expander input is only 20 ohms (almost short). after i stop or reset the progran the resistence is infinity.
also the output is very inconsistant, if it off two switches, more than two outputs of off. basically it is not stable,
saran

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

Re: how to get higher input/output ports

Post by Sean »

Your results indicate that the port with the switches attached is being conofigured as an output by your program, with all output pins low. After reset all ports are automatically configured as inputs. If you power your circuit, but do not send any register informaion to the expander chip, you should find that your switches behave as you would expect. The voltage drops across the 470ohm resistors seems to confirmed that the port is being programmed to the wrong mode.

If you want to send me a list of your hardware connections and the values you are sending to each register in the expander chip, I will see if I can spot any problems. If you also want to send a demonstration program I will try to check it for you.

saravana_3
Posts: 61
Joined: Thu Dec 20, 2007 4:23 pm
Location: singapore
Contact:

Re: how to get higher input/output ports

Post by saravana_3 »

Hi Sean,
I found the problem,but it is strange, i am using the pic16f877a, PLCC type, since our kit dont have the facility of the PLCC socket, i use the DIP to PLCC socket adaptor,and use the PLCC type chip instead of DIP type chip, today i remove the adaptor and directly plug inthe DIP type PIC 16F877A chip and then every thing is working fine,it is not heating the MCp23S17
but the same adaptor is wroking fine in my past projects.
thanks for your kind support,
let me find out WHY?
thanks
Saran
saran

Post Reply