Difference between revisions of "PIC32MX: I2C DAC"

From Mech
Jump to navigationJump to search
Line 12: Line 12:
== Circuit ==
== Circuit ==


The circuit was fairly simple after consulting the DAC online data sheets under the practical application wiring diagrams section. The Chip is a MAX 518 Digital to Analog Converter Chip. The two PIC32 NU32 outputs are shown as Serial Data Clock (A14) and Serial Data Output (A15).
The DAC used is a MAX518 Digital to Analog Converter Chip. The following circuit was used after consulting the MAX518 data sheet under the practical application wiring diagrams section. The two PIC32 NU32 outputs are shown as Serial Data Clock (A14) and Serial Data Output (A15).


A couple thing to keep in mind; the DAC requires 3.5V to read 'high' at any of its inputs, and the PIC can only put out 3.3V as a theoretical maximum, realistically around 2.7V. Therefore, a technique known as open drain was used to give the PIC outputs 5V. The PIC has built-in transistors, so setting the code to open drain will essentially make the output the base current, so that when it goes high, it will saturate the transistor (connected to 5V) sending 5V instead of 3.3V to the emitter, which is connected to the DAC input. The DAC inputs should be high when not in use, and although the manual says external pull-up resistors are not necessary (we used 10 kohms), one of their example circuits had them, and our circuit only functioned when they were in place.
One thing to keep in mind: the MAX518 is a 5V chip and requires 3.5V to read 'high' at any of its inputs, and the PIC can only put out 3.3V. Pull up resistors R1 and R2, 1 kOhm, allow the MAX518 to receive data from the PIC. The MAX518 can change its outputs (0 and 1) from 0 to 5V with 8 bit resolution.


[[Image:I2C Schematic.jpg]]
[[Image:I2C Schematic.jpg]]

Revision as of 12:31, 6 March 2010

Overview

Note: PROJECT NOT FULLY FUNCTIONAL!

On this page we will present code and a wiring schematic that allows for the PIC32MX to speak to an 8-bit Digital to Analog Converter. In particular, our project used the MAX518 DAC.

While we were able to send data to the DAC, we were NOT able to elicit output from said DAC.

This particular DAC, the MAX518 requires three bytes (note: 1 byte = 8 bits) of information every time it changes its output. The first byte defines the DAC as a slave, and contains the address. This byte WILL NOT change when repeatedly sending data to the DAC. The next byte is the actual data the IC should convert.

The third byte is named the command byte. The bits are, in order of most significant to least significant: R2, R1, R0, RST, PD, X, X, A0. R2 = R1 = R0 =0. This is always true. The next bit, RST, will reset all registers if set to 1. Setting PD to 1 will tell the device to hibernate after it outputs the desired data. The next two bits do not matter (X = don't care). Finally, the address bit, instructing the DAC which output to send the data too.

Circuit

The DAC used is a MAX518 Digital to Analog Converter Chip. The following circuit was used after consulting the MAX518 data sheet under the practical application wiring diagrams section. The two PIC32 NU32 outputs are shown as Serial Data Clock (A14) and Serial Data Output (A15).

One thing to keep in mind: the MAX518 is a 5V chip and requires 3.5V to read 'high' at any of its inputs, and the PIC can only put out 3.3V. Pull up resistors R1 and R2, 1 kOhm, allow the MAX518 to receive data from the PIC. The MAX518 can change its outputs (0 and 1) from 0 to 5V with 8 bit resolution.

I2C Schematic.jpg

Code

This code sets up the I2C to 100kHz and changes output 0 of the MAX518 every 100ms

 
#include <HardwareProfile.h> 
#include <HardwareProfile_NU32.h> 
#include <plib.h> 
 
// Configuration Bit settings 
// SYSCLK = 80 MHz (8MHz Crystal/ FPLLIDIV * FPLLMUL / FPLLODIV) 
// PBCLK = 50 KHz 
// Primary Osc w/PLL (XT+,HS+,EC+PLL) 
// WDT OFF 
// Other options are don't care 
// 
//#pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF 
//#pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_2 
 
//#define SYSCLK (80000000) // set system clock to 80MHz 
//#define PBCLK (SYSCLK/1000) // set PBCLK to 80 KHz 
#define FPB SYS_FREQ // Result of bootloader
 
#define I2CBAUD 100000 // 100 kHz clock
#define BRG_VAL ((SYS_FREQ/2/I2CBAUD)-2)

void Delayms( unsigned t)
// This uses Timer 1, can be changed to another timer. Assumes FPB = SYS_FREQ
{
    OpenTimer1(T1_ON | T1_PS_1_256, 0xFFFF);
    while (t--)
    {  // t x 1ms loop
        WriteTimer1(0);
        while (ReadTimer1() < FPB/256/1000);
	}
	CloseTimer1();
} // Delayms 
 
int main(void) 
{ 
    
    ODCA = 0xC000; 
    unsigned char addr = 0x58; 

    //Enable channel using default values 
    OpenI2C1( I2C_EN, BRG_VAL ); 
    unsigned char cmd = 0; //command line 
    unsigned char data = 0; //output data, digital value to be converted

    SYSTEMConfigPerformance(SYS_FREQ); // tell PIC its operating frequency
    mInitAllLEDs(); // initialize LEDs on NU32 board, defined in HardwareProfile_NU32.h
    mInitAllSwitches(); // initialize switches on NU32 board
 
    while(1) // run the code over and over again
    { 

        // start the I2C communication
        StartI2C1(); // Send the Start Bit (begin of data send) 
        IdleI2C1(); // Wait to complete 

        // write the address of the chip, defined by pins AD0 and AD1 on the MAX518
        MasterWriteI2C1 (addr); // address 
        IdleI2C1(); 
        while( !I2C1STATbits.ACKSTAT==0 ){}
        
        // write the command to tell the MAX518 to change its output on output 0
        MasterWriteI2C1 (cmd); // command line 
        IdleI2C1(); 
        while( !I2C1STATbits.ACKSTAT==0 ){}

        // wite the value to put on the output
        MasterWriteI2C1(data); // output 
        IdleI2C1(); 
        while( !I2C1STATbits.ACKSTAT==0 ){}

        // end the I2C communication
        StopI2C1(); // end of data send 
        IdleI2C1(); // Wait to complete 

        // the total write time is ~285us with an I2C clock of 100 kHz

        data++; // increase the data, make a sawtooth wave as an example
        Delayms(100);

        //if(data>255) { // don't have to worry about this, data is 8 bit so it will roll over automatically
        //    data=0;
        //}

    } // end while(1)
} // end main