• No se han encontrado resultados

Capítulo 5 Propuesta de solución

5.2 Procedimientos del modelo de gestión de proyectos en el DIM

5.2.11 Grupo de procesos para la fase de planificación

The program shown in Listing 3-3 will write data to the port at address BASE+2. Note that this port only controls bits 0, 1, 2 and 3; bits 4, 5, 6 and 7 are not dedicated for internal use by the port at address BASE+2. Some of these bits that can be controlled are inverted internally by the parallel port electronics when output; bits 0, 1, and 3. Therefore, to nullify this inversion by hardware we must

invert bits 0, 1 and 3 in software. Bit 2 is not inverted internally by the parallel port hardware, and so we do not not need to invert it in software.

The that need to be made on the interface board are shown in Table 3-3.

Table 3-3 Connections to the LED Circuit. BASE+2 Address† (Driver IC, U3)ULN2803A

/D0 D0 (1)

/D1 D1 (2)

D2 D2 (3)

/D3 D3 (4)

The signals preceded with a slash ( / ) are internally inverted by the parallel port hardware.

Listing 3-3 Writing to the port at BASE+2 with compensation for internal inversions.

/***************************************************** WRITING TO PORT @ BASE+2, INTERNAL INVERSIONS COMPENSATED This program outputs 4 bits of data to the port at address BASE+2, compensating for the inverted bits 0, 1 and 3. You can change the value of the actual bit pattern you want to see output to the interface board.

*****************************************************/ #include <dos.h>

#define BASE 0x378 void main()

{

// BASE+2 bits 0,1 and 3 are internally inverted by // the parallel port hardware before being output. This // can be compensated in software by carrying out an // exclusive OR operation with the output data and 0x0B // (0000 1011). Bits 4-7 do not matter as they are not // connected.

outportb(BASE+2,0x0B ^ 0x0F); // NOTE: In binary 0x0F = 0000 1111

// The number being output (0x0F) can be changed to any // value between 0x00 and 0x0F. The four output signals // will correspond to the binary bit pattern represented by // the number.

// Examples:

// Bit No: 7 6 5 4 3 2 1 0 // 0x0F 0 0 0 0 1 1 1 1 // 0x05 0 0 0 0 0 1 0 1 }

In this program, the only line that requires explanation is:

outportb(BASE+2,0x0B ^ 0x0F);

The outportb()function writes data to the port in a manner similar to its use before. In this case, the address of the port is BASE+2. The define statement defines BASE to be a placeholder for 0x378. Therefore, the value of the first parameter is 0x378+2, which is 0x37A. The value of the second parameter is the data we want to send out the port. This data is obtained, by evaluating:

0x0B ^ 0x0F

The operator ‘^’ used in the above expression is known as the Exclusive-OR (XOR) operator. It is one of the many bit-wise operators available in C and C++ that is used to operate at bit level. You will have a better understanding of how bit- wise operators work once the operation shown in Table 3-5 has been explained. The operation of the exclusive OR operator will be described with the aid of Table 3-4. This operator requires two operands when used.

Table 3-4 Exclusive OR operation.

Operand A 0 0 1 1

Operand B 0 1 0 1

Result 0 1 1 0

NOTE

In the simple arithmetic operation: 3 + 5

the operator is ‘+’ and the two operands are 3 and 5.

For a bit-wise operator, the operands must be bits. In Table 3-4 the two operands are given the names Operand A and Operand B. The results produced by the XOR operation for all four possible combinations of the two operands are listed in the ‘Result’ row. As can be seen, the result is 1, only when just one of the two operands in a column is 1. When both operands in a column are identical, the result

is zero. So when the operands differ, the result is 1. As shown by columns 2 and 4 of Table 3-4, if we hold the Operand B at 1, the result will be the inversion of operand A. Operand B acts as a ‘filter’ for inverting specific bits of Operand A. We use this operation to perform software inversions to counteract the internal inversions generated by the parallel port hardware. Table 3-5 explains the result of evaluating:

0x0B ^ 0x0F

Here Operand A contains the data to be sent out and Operand B is the “filter” used to invert the bits already inverted by the parallel port.

Table 3-5 Evaluation of 0x0B ^ 0x0F. Bit No. Æ 7 6 5 4 3 2 1 0 Operand A (0x0F) 0 0 0 0 1 1 1 1 Operation XOR Operand B (0x0B) 0 0 0 0 1 0 1 1 Result 0 0 0 0 0 1 0 0

As explained earlier, bit-wise operators operate on a bit-by-bit basis. In other words, bit 0 of Operand A and bit 0 of Operand B are put through an exclusive OR operation. Likewise, another exclusive OR operation takes place between bit 1 of Operand A and bit 1 of Operand B, and so forth.

The filter comprises data bits that we want inverted set to 1, and bits to be left as is set to 0. Thus, to invert bits 0, 1 and 3 of data to be sent out, the bits 0, 1 and 3 of the filter are set to 1. As can be seen, in the ‘Result’ row of Table 3-5, bits 0, 1 and 3 are the opposite values of bits 0, 1 and 3 of Operand A. Therefore, when we write the exact bit pattern we want as Operand A, the affected bits will be inverted by software to become the ‘Result’. When those affected bits of the ‘Result’ are then sent to the parallel port hardware and internally inverted, the data arriving at the interface board will correspond to Operand A that we originally want to send out. To verify operation of the program, you can change the data (i.e. 0x0F) to any value between 0x00 and 0x0F. The LEDs that light up will correspond to the binary bit pattern of the data specified in the program. Note that the filter value 0x0B must not be changed – otherwise not all those specific bits we want to invert (0, 1, and 3) will actually be inverted in software.