Industrial Training




Hardware Interaction Through 'C'



‘C’ as we know is a middle level programming language. It offers extreme flexibility in interacting with the hardware. Therefore it is not surprising to find that maximum number of people involved in programming the hardware, use it as their preferred language. We will also use ‘C’ for examples demonstrated in this article. ‘C’ offers us the ability to interact freely with the hardware with little overheads and very compact code. Before actually jumping into this realm of hardware interaction we will first see what is it that we call hardware, and how do we interact with it.

Hardware is nothing but some external circuitry/device that receives commands from a computer that is running a program. And how exactly do we communicate with this so-called device? The answer is through something called ports. But for some of us it is still a mystery as to what exactly is a port? If we hear the word port the first thing that comes to our mind is a large place by the ocean, with deep waters and numerous ships loading and unloading goods (a harbor sometimes), generally not something that could fit into the backside of our computer. In computers a port does pretty much the same job as the seaside one. The computer places data on them to be loaded and carried by some cable to its destination, most of the times some device (sometimes other computers). But there is a small difference here, not all ports are designed to receive as well as send data, some support only one-way traffic, while others support bi-directional traffic. A port thus is a place in the I/O space of the computer that is directly connected to the connectors that we see at the back of our computers. These are I/O's mapped devices, i.e. whatever we write on these I/O's locations it will be reflected on the pins of the sockets. There are also some ports that are not connected to any external pins. There are numerous types of sockets that can be attached to our computer. Some of the more commonly used ones are the COMM port, the LPT, the PS/2 etc. A computer is capable of writing as well as reading data from a port. Most popular libraries contain functions like outportb( ), inportb( ), outport( ), inport( ) to read/write byte/word from/to a specified port.

In this article we will read the actual time stored in CMOS memory. CMOS uses two ports—a control port (70h) and a data port (71h). On sending a pre-defined offset value to the control port a value representing the offset is made available at the data port. The following ‘C’ program shows how this can be achieved:

#include <stdio.h>
#include <dos.h>
main( )
{

unsigned char sec, min, hrs;

clrscr( ) ;

while ( !kbhit( ) )
{

outportb ( 0x70, 0x95 ) ;
outportb ( 0x70, 0x00 ) ;
sec = inportb ( 0x71 ) ;
outportb ( 0x70, 0x02 ) ;
min = inportb ( 0x71 ) ;
outportb ( 0x70, 0x04 ) ;
hrs = inportb ( 0x71 ) ;

printf ( "Time: %x:%x:%x", hrs, min, sec ) ;
delay ( 500 ) ;
}

}

In the above example we have placed the value 0x000h at port no 0x70h using the outportb( ) function. The CMOS in return places the seconds part of the system time at port number 71h, which we have read using the call to inportb(). On similar lines we have obtained the values of hours and minutes of system time by sending the offset values 02h and 04h to the control port and reading the values from the data port. Finally we have printed the time using printf( ) function. Similarly we can use all other items of information that CMOS memory contains.



Hi I am Pluto.