• No se han encontrado resultados

At the end of the previous chapter, we developed an object class named ParallelPort. This object has the capability to input and output data using all three addresses of the PC’s parallel port. It can easily be used to drive an 8-bit Digital-to-Analog Converter. Only partial functionality of the ParallelPort

class is needed to drive the DAC on the interface board. The DAC just needs to receive an 8-bit number from the PC. This can be done by sending an 8-bit number from the PC to the interface board using the port at address BASE. This is an output port with eight parallel signals we can connect to the interface board. Since we already have ParallelPort as a completely packaged object class, we can use it to drive the DAC.

The rest of the chapter will progress as follows. First, we will develop a program using the ParallelPort object to drive the DAC. When this program is executed, the DAC system will generate an analog voltage proportional to the 8-bit number sent to it. We will then proceed to learn about inheritance using an exercise in which a new class is derived to represent the DAC. This new object may require extra functionality that is specific to the DAC. We will then turn our attention to restrictions imposed by various access attributes. The proper use of access attributes will be described in detail. We will proceed through several different versions of the same program, each time strengthening code reuse. The final program presented will then be used in future chapters as the most appropriate object-oriented program to drive the DAC.

The first program to drive the DAC is given in Listing 6-1. The class definition and the member function definitions of the ParallelPort object class are exactly the same as given in the previous chapter. In the main() function, an object is instantiated using the ParallelPort class and it is given the name D_to_A. Then the WritePort0() function of the object is used repeatedly to send different data to the parallel port each time. Following each WritePort0() function is a getch() function. These getch() functions force the program to wait for a key press before executing the next statement. This will allow you time to carry out measurements on the interface board to verify whether or not the correct analog voltage has been generated by the DAC system.

Table 6-2 Connections for the DAC. BASE Address

(Buffer IC, U13)

DAC0800 (U8) D0 D0 (12) D1 D1 (11) D2 D2 (10) D3 D3 (9) D4 D4 (8) D5 D5 (7) D6 D6 (6) D7 D7 (5)

Before running your DAC programs, configure the interface board as follows. Ensure that an operational 9V battery is connected to the terminal block (J14). Fit

the jumper on the interface board across the two-pin header position marked LINK1, to select unipolar mode (0V to +5V). When the connections are completed according to Table 6-2, the 8 bits of the port at address BASE of the parallel port will be connected to the 8-bit input of the DAC.

Listing 6-1 Digital-to-Analog Conversion using the ParallelPort object class.

/***************************************************** This program uses the ParallelPort object developed in the previous chapter to write a byte of data to the Digital to Analog Convertor (DAC). The DAC generates an analog voltage proportional to the value of the data byte it receives. *****************************************************/ #include <iostream.h> #include <conio.h> class ParallelPort { private:

unsigned int BaseAddress; unsigned char InDataPort1; public:

ParallelPort(); // default constructor

ParallelPort(int baseaddress); // constructor void WritePort0(unsigned char data);

void WritePort2(unsigned char data); unsigned char ReadPort1();

}; ParallelPort::ParallelPort() { BaseAddress = 0x378; InDataPort1 = 0; } ParallelPort::ParallelPort(int baseaddress) { BaseAddress = baseaddress; InDataPort1 = 0; }

void ParallelPort::WritePort0(unsigned char data) {

}

void ParallelPort::WritePort2(unsigned char data) {

outportb(BaseAddress+2,data ^ 0x0B); }

unsigned char ParallelPort::ReadPort1() {

InDataPort1 = inportb(BaseAddress+1); // Invert most significant bit to compensate

// for internal inversion by printer port hardware. InDataPort1 ^= 0x80;

// Filter to clear unused data bits D0, D1 and D2 to zero. InDataPort1 &= 0xF8; return InDataPort1; } void main() { ParallelPort D_to_A;

cout << "Press a key ... " << endl; getch();

D_to_A.WritePort0(0);

cout << "Press a key ... " << endl; getch();

D_to_A.WritePort0(32);

cout << "Press a key ... " << endl; getch();

D_to_A.WritePort0(64);

cout << "Press a key ... " << endl; getch();

D_to_A.WritePort0(128);

cout << "Press a key ... " << endl; getch();

D_to_A.WritePort0(255); }

The analog output voltage can be measured at the pin on the interface board labelled VDAC, which is connected to pin 7 of the operational amplifier LM358 (U10B). Each time you press a key, the program will output a slightly higher 8-bit value and the DAC will produce a corresponding higher analog voltage. Read the analog voltage by connecting a voltmeter between the pin marked VDAC on the interface board and a ground pin.

As described earlier, additional circuitry has been provided on the interface board to facilitate both unipolar and bipolar output from the DAC. Move the jumper to the position marked LINK2 on the interface board and re-execute the program to check the bipolar operation of the DAC system.

Documento similar