Diploma Practice C Online People
Certificate Contact Us

Understanding IO and Interfacing Digital IO chip with the CPU

So far we have connected memory chips to the CPU. These two chips are sufficient for the CPU to start working. First we need to put (store) valid instructions (instruction codes) in the ROM. How to store instructions in the ROM is an important topic, we discuss it in a separate article. For the time being, we assume that there exist a valid instructions in the ROM. When power is applied to the CPU, it fetches an instruction from the address present in the program counter. On power on, the default address in the program counter is zero. So CPU reads its first instruction from address 0. Address zero belongs to the ROM.

Note that address zero must belong to ROM only. If address zero belongs to RAM, on power on, no valid things will present in RAM. All the contents of RAM are lost when power is down. So on power-on RAM does not contain any valid data or instructions. So execution starting address must belong to ROM chip only.

Now CPU fetches instruction from address 0, decodes and executes it. Next it fetches from address 1 and execute. This goes on forever. Now let us think, what these instructions can do. Note that along with instructions, some numbers (data) also can be stored in the ROM. The program can move zero to register 1. Next it loads these numbers one by one into register 2, and add register 2 to register 1. At the end register 1 will contain the sum of all the numbers present in ROM. Finally program can store this register to a location in RAM.

So the program present in the ROM is adding the numbers present in the data area of ROM and storing the result (sum) in to the RAM. Program can also calculate the average, largest and smallest numbers. And store them in the RAM. But main drawback is that, we can not see anything from outside.

Importance of IO chips and IO devices

CPU with ROM and RAM is like a human brain. Our brain works like a combination of CPU and ROM/RAM. Our brain also can remember (store) two numbers and it can calculate sum or differense. It can also remember(store) the result. But if brain needs to read these numbers from external world, it needs help of eyes, or ears, or hands (to read braille letters). Similarly for the brain to communicate the result to the external world, it needs help of mouth or hands (to write on paper).

Similar to brain, our CPU also needs IO devices for reading the numbers (data) and for displaying the results. So keyboard and display monitor are such IO devices. But CPU can not directly connect to the keyboard or display monintor. CPU can connect to others only through its bus lines. But all most all IO devices can not be connected to the CPU's bus lines. So we need IO interface chips. These IO interface chips are connected to the CPU's bus. And IO devices are connected to these interface chips. In this way IO chips are acting as bridge between CPU and IO devices. IO chips are also refered as IO controllers or peripheral chips.

There exist is so many types of IO chips and each IO chip is meant for connecting to some kind of IO devices. Every IO chip will have a set of IO locations in it. CPU can read or write to these IO locations, just like memory locations. Similar to memory locations, every IO location in IO chip will have unique address. By using this IO location address, CPU will read or write to this locations. These IO locations in IO chips are refered as IO registers or Special Function Registers (SFR). Now on we will refer them as IO registers. These IO registers acts as medium for communication between CPU and IO device. When CPU wishes to send some data to IO device it writes data to this IO register. Then IO chip will send this data to the IO device. Similarly when IO device sends data, IO chip will receive and put it in IO register. Now CPU can read this data of IO device, from IO register.

CPU Bus interface pins and IO device interface pins

IO chips are also connected to the CPU's bus, just like memory chips. So like memory chip, every IO chip will have chip select pin, address pins, data pins, read pin and write pin. These are the CPU bus interface pins present in every IO chip. Memory chips will have only bus interface pins. Memory chips does not need any other pins. But every IO chip, besides bus interface pins, it will have IO device interface pins. These pins are for connecting to the IO device.

There exist different types of IO chips, for connecting to differnt types of IO devices. Each such IO chip will have different set of IO interface pins for connecting to a particular type of IO device. Think and look at the display monitor, how it is connected. It is connected through VGA connector. The VGA connector pins are coming from the VGA IO chip called graphics controller. Look at the PS/2 keyboard or PS/2 mouse, how they are connected.

Interface and communication protocol between IO chip and IO device

As an embedded systems engineer, one should understand these interfaces between IO chips and IO devices. That is how data or information is flowing between PS/2 keyboard and PS/2 IO chip. How information is flowing between graphics controller and display monitor.

IO devices in Destop computer vs IO devices in Embedded systems

IO devices attached to embedded hardware is quite different from the IO devices attached to regular computers. IO devices attached to regular computer are keyboard, mouse, display monitor, hard disk, printer, speakers, microphone, web camera etc..

In the case of embedded devices, IO devices vary from embedded device to device. Typically they will have a set of buttons like in washing machine or microwave oven. It may also have small LCD or LED displays. Most of the devices are operated through relays. Relays are used to switch On and Off various things like motors, valves, heaters etc.. Other common devices in embedded devices are sensors. Temperature sensor, pressure sensor, humidity, gas, and smoke sensors. Infrared sensors are used for operating the device with a remote.

Digital IO or General Purpose IO controller

The most common IO chip found in every embedded hardware is digital IO also called general purpose IO (GPIO). The digital IO lines provided by the GPIO can be used for interfacing to the various IO devices. These digital IO lines are very simple to understand and use. At the same we can also use these digital IO lines to perform various communication protocols. These communication protocols could be simple to complex.

Look at the following figure. Here we have connected digital IO chip to the CPU's bus. This IO chip has got two IO registers in it. One is called Data register and other is called Direction register. Like memory locations, each of these registers are having unique address. The address of Direction register is 0xC0 and Data register address is 0xC1. As this chip has got only two registers, it will have only one address pin. CPU's address A0 pin is connected to this address pin.

Depending on the need, the digital IO pins of digital controller can be configured as input or output. If IO pin is configured as output, then we can set that line as low or high. When set as low, voltage on this pin will be at ground level, that is zero. When set to high, voltage is at Vcc level, that is 5 volts or 3.3 volts.

When IO pin is configured as input, we can not set the voltage on that pin. Instead we can read the voltage present on that pin. When a pin is configured as input pin, and no voltage is applied to that pin externally, then what voltage will we get ? To avoid this confusion, when pin is configured as input pin, internally a pull-up resister connected to that pin. Because of this pull-up resister, every input configured pin will read as high, when nothing is connected or high level is connected. If this pin is connected to ground ( 0 voltage), then it will read as low.

In the figure above, the D0 to D3 IO pins of digital IO controller are connected to the four LEDs. The D4 to D7 IO pins are connected to the push buttons. These push buttons, when pressed, will connect the IO pin to ground. When not pressed, it is open (not connected to ground).

Direction Register

The direction of pins can be configured by writing a value into direction register. This direction register is having 8 bits, each bit controls the direction of one IO pin. If bit 0 is 1, then D0 pin is configured as output. If bit 0 is 0, then D0 pin is configured as input pin.

Now look at the direction register in the figure above. The lower four bits of direction register are having ones, so IO pins D0 to D3 are output pins. LEDs are connected to these output pins. The upper 4 bits of direction register are zeros, so D4 to D7 pins are input pins. Push buttons are connected to these IO pins.

Data Register

The output pins can be controlled by writing to the data register. In the figure above, the lower four bits of data register is written with binary 0011. It makes D0, D1 outputs to high and D2,D3 pins to low. The LEDs connected to D0 and D1 are shown as glowing with green color. The LEDs connected to D2 and D3 output pins are shown in off condition with grey color.

The status of input pins can be can be found by reading the data register and checking the bits corresponding to input pins. In the data register the upper four bits gives the status of input pins. The upper four bits are having the binary 1101. It means D4,D6,D7 are in high state and D5 in low state. The push button connected to D5 is in pressed state. So D5 pins is connected to ground and in low state. Remaining pins are not connected to ground, so they are in high state, because of pull-up resister.

Flashing LED Program

It became a custom to start teaching C programming with 'Hello World' program. Similarly in teaching or learning embedded programming, one should start with flashing LED program. Every embedded hardware will have GPIO controller with so many IO pins. At least one of them will be connected an LED. So first embedded program we write will configure that IO pin as output. Next it writes 0 and 1 to that IO pin alternatively with delay in between. This delay also implemented very easily with an empty for loop of one million iterations. Now when we run this program, the LED will start flashing on and off with a delay of around 1 second. Now we can see our embedded programming working on the board.


Answer the following questions to check the understanding of the subject

1.  Is it possible for the CPU to work, only when ROM and RAM are connected?
2.  Is it possible for the CPU to work, only when ROM is connected?
3.  Is it possible for the CPU to work, only when RAM is connected?
4.  Explain the difference between IO devices and IO interface chips?
5.  What is the purpose of bus interface pins and IO interface pins present
    on the IO interface chip?
6.  Why memory (ROM/RAM) chips will have only bus interface pins?
7.  Give list of different types of IO interfaces you aware. Also find out
    each of these IO interfaces uses how many pins.
8.  List the type of IO devices typicall found in embedded devices.
9.  Explain how CPU can send and receive data to an IO device, through IO
    interface chip.
10. What is the purpose of digital IO or GPIO interface chip?
11. What are the two registers present in the GPIO interface chip and what are
    the addresses of each of these registers?
12. What is the purpose of direction register?
13. What is the purpose of data regisger?
14. What is the 'flashing LED' program?

#401, Sai Sushma Homes, 23/A, S.R. Nagar Main Road, Hyderabad-500038, India.
Ph: +91-40-48508764, 7702028989, depik.help@gmail.com