0% found this document useful (0 votes)
57 views

Hardware Programming

C is a mid-level programming language well-suited for interacting with hardware through ports. Ports allow communication between a computer program and external devices. A port is a place in the computer's I/O space that is directly connected to connectors on the back of the computer. The example program reads the current time from the CMOS memory by writing to the control port at address 70h and reading the data port at address 71h.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Hardware Programming

C is a mid-level programming language well-suited for interacting with hardware through ports. Ports allow communication between a computer program and external devices. A port is a place in the computer's I/O space that is directly connected to connectors on the back of the computer. The example program reads the current time from the CMOS memory by writing to the control port at address 70h and reading the data port at address 71h.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

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 ) ;
}

You might also like