Difference between revisions of "Analog Output"

From Mech
Jump to navigationJump to search
Line 33: Line 33:


#use i2c(Master, Fast, sda=PIN_C4,scl=PIN_C3, force_hw)
#use i2c(Master, Fast, sda=PIN_C4,scl=PIN_C3, force_hw)

i2c_start();
i2c_start();
i2c_write(0x58); // address. 0101 1000. Last 2 bits are set by 2 pins high or low on MAX517
i2c_write(0x58); // address. 0101 1000. Last 2 bits are set by 2 pins high or low on MAX517
i2c_write(0); // command. just zero
i2c_write(0); // command. just zero
i2c_write(i); // data. Translated to analog output voltage
i2c_write(i); // data. Translated to analog output voltage
i2c_stop();
i2c_stop();

Revision as of 19:56, 29 December 2007

The PIC doesn't have any analog outputs, even though it has 13 analog inputs. If you need analog output there are at least three ways to get it:

  • Use one of the two PWM outputs. The duty cycle ranging from 0% to 100% implies an average output voltage between 0V and 5V. You can use an RC low-pass filter to smooth the output. The smoother you want it, the slower it is to reflect changes in the duty cycle.
  • Connect 8 digital output lines to a simple DAC chip (digital to analog converter) such as the ADC0820. That's a lot of wires, but if you only need one analog output it may be ok.
  • Use a serial DAC chip such as the MAX517, as described next. This requires only 2 communication wires, and many DAC chips (and other chips) can be addressed.


2-wire serial communication: the i2c bus

There are quite a lot of chips available that communicate via a serial protocol called i2c ("i squared c"). This requires only two communication lines: SCL and SDA (plus a common ground of course). SCL refers to a CLock signal and SDA to a stream of DAta bits. A number of chips can share this 2-wire bus. The PIC would ordinarily be Bus Master, and select whichever chip it wishes to talk to by sending a one-byte address code. PIC-to-PIC communication can be set up using i2c also.

The 4520 PIC can control the i2c bus lines using a hardware i2c controller, or a software controller. Hardware is preferable because the timing of software-controlled bus signals can be disrupted by interrupts. The software controller can use any pins for SCL and SDA. The hardware controller can only use RC3 and RC4. Sadly RC3 is about the only pin that is NOT available at the 4520 board's edge connector, but you can connect to it on the PCB itself near pin 18 of the PIC.


I2c-dac.gif

Wiring an 12c bus

We'll illustrate an i2c bus with two MAX517 DAC chips. The SCL and SDA lines need pull-up resistors as shown. (Without them, i2c instructions cause the PIC to wait forever for the bus to clear.)

The i2c bus lines are wired in common to all i2c slave devices; here we have just two MAX517 DAC chips. Each slave must have a different one-byte address. In the case of the MAX517, six of the bits of this address are fixed by the manufacturer and two are selected by you when wiring the chip. In binary the address is 0101 10--, or in hex 0x58, 0x59, 0x5A, or 0x5B. The last two bits are chosen by wiring the AD1 and AD0 bits high or low. (If they are left unconnected, they default high.) So in the circuit shown we can address the two DACs as 0x58 and 0x59.

The PIC is then responsible to send a series of bytes to control a DAC. It sends an address byte to make the proper chip pay attention, then a command byte which for these simple chips is just zero, and then a data byte containing the value to convert to analog: 0 becomes 0V and 255 becomes 5V.


Using the DAC

The DAC has a Vref input, which scales the output voltage. If you tie Vref to +5V, the output voltage ranges 0V to 5V, but also any noise on the 5V rail will become noise on the DAC's output. You can use a cleaner Vref if you wish. See the MAX517 documentation.

To command the DAC, see the code MAX517.c in AnalogOutput. The keys lines are the one that sets up the i2c controller, and then lines to take control of the bus, send the address byte (0x58), the command byte (0), and the desired data byte (i in this case)

#use i2c(Master, Fast, sda=PIN_C4,scl=PIN_C3, force_hw) 

i2c_start();
i2c_write(0x58);    // address. 0101 1000.  Last 2 bits are set by 2 pins high or low on MAX517
i2c_write(0);       // command.  just zero
i2c_write(i);       // data.  Translated to analog output voltage
i2c_stop();