Capítulo 4 Análisis de resultados
4.3 Análisis de brecha en la gestión de proyectos en el HLCH
As described in Chapter 2, there are three addresses associated with the parallel port (typically being 0x378, 0x379 and 0x37A). Although we use the term parallel port, this ‘port’ is really three ports combined together. The simplest of the three ports is the one at address 0x378. In general, this port is only used for output, however, more recent computers have the capability to input data using this port
Ground (0V) i 2V 3V 330R Driver VCC (+5V)
address. Nevertheless, to maintain compatibility, the software we have developed only uses data output to port 0x378. We will write a program that outputs a byte of data via port 0x378 to light up the respective LEDs on the interface board.
To verify the proper operation of our program, we need to connect the interface board to the parallel port of the PC. This is done using the interface board cable described in Chapter 2. The remainder of the connections to be carried out on the interface board must be made according to Table 3-1 below. Note that pin 1 of an IC on the interface board can be recognised by its rectangular shaped pcb pad. Make these connections using the interconnecting leads assembled earlier. When the connections are complete, the signal lines of the port at BASE address (0x378) will be connected to the Driver circuit that lights the eight LEDs via the Buffer IC on the interface board.
Table 3-1 Connections for basic output. BASE Address
(Buffer IC, U13)
ULN2803A Pin No. (Driver IC, U3)
D0 1 D1 2 D2 3 D3 4 D4 5 D5 6 D6 7 D7 8
The program shown in Listing 3-1 has several lines of program statements followed by comments. It uses a library function outportb() to output a byte of data to the port at BASE address. The port address and the data can be specified as actual arguments to outportb() at the time of calling. Since outportb() is a library function, we do not have to provide the body of the function. It comes from the library and will be searched for when linking takes place.
Listing 3-1 Writing to the port at Base address.
/***************************************************** WRITING TO A PORT (output operation)
This program outputs a certain bit pattern to the port at BASE address to light the respective LEDs on the interface board.
#include <dos.h> #define BASE 0x378 void main()
{
outportb(BASE,255); // in binary, 255 = 1111 1111 // The number 255 can be changed to any value betwen 0 // and 255, causing the eight output signals to
// correspond to the binary value represented by the // number. For example 65 = 0100 0001.
}
As mentioned in Section 1.2.2, the two lines starting with the hash sign (#) are compiler directives. The first compiler directive will include the header file dos.h (which is a source file). This file contains the prototype of the outportb() function. The header file dos.h also contains information about many other functions. However, as far as this program is concerned, only the information regarding the outportb()function is needed. The compiler would not be able to process the outportb() function, and the program could not use it if we did not include this header file. Note that the prototype does not specify how the function is to be executed. In other words, until linking takes place, the program will not have access to the actual instructions contained in the function.
The second compiler directive is a define statement. It simply instructs the preprocessor to replace any occurrences of BASE in the program by the hexadecimal number 0x378. Using the word BASE instead of 0x378 makes the code more readable since it is easier to relate to the word BASE than a number. In addition, if we ever wanted to change the base address, we only need to modify the define statement and the preprocessor will automatically implement that change in address throughout the program. The define statement can be used when writing larger programs to simplify the task of coding and improve readability. Next we encounter the main() function (the only function in this particular program) where all C++ programs start their operation. Usually a typical C++ program will have many functions coded (defined). The keyword void indicates that the main() function will not return any value. The body of the main() function starts with the open brace ({) just after the line void main() followed by its instructions. The only executable statement in this program is outportb(BASE,255). This statement is used to output a byte of data from the PC. After a few comments, the body of the main function ends with a close brace (}).
The function outportb() takes in two parameters; a port address and the data to be written to that address. In this program the port address is BASE, which will be replaced with 0x378 by the preprocessor. Therefore, the address of the port
where the data is to be sent is 0x378. The value of the data is 255 (decimal). It must be noted that the size of the data passed to a port is, most of the time, a byte. A byte can take 256 different values. When the value is 0, all eight bits of the byte will have their values as 0. When the value is 255, all eight bits of the byte will have their values set to 1. Other values between 0 and 255 will correspond to different bit patterns. After the interfacing connections given in Table 3-1 are made and the parallel port cable is connected to the interface board, this program can be compiled and executed. It will set all eight bits equal to 1 and as a result you should see all eight LEDs light up. If, for example, you change the outportb() line to outportb(BASE,65), then only the LEDs corresponding to bits 0 and 6 will be lit and all other LEDs will be off.
If the program fails to work and light the LEDs, make sure your base address is correct by using the program titled ‘base_adr.exe’ included with the accompanying CD-ROM.
Edit the program a number of times replacing the value of 255 with different numbers (less than 255) and observe how the LEDs light up on the board. This exercise will also help you understand how bit patterns relate to decimal data. as explained in Chapter 2. Alternatively, you may replace the number 255 with hexadecimal numbers; for example:
outportb(BASE, 0xF0);