PIC32MX: I2C DAC

From Mech
Jump to navigationJump to search

Original Assignment

Do not erase this section!

Your assignment is to interface to the MAX518 I2C digital to analog converter (DAC).

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 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). I2C Schematic.jpg

Code

Our code is separated into sections, each with sufficient comments to explain all functions Our code:

 
#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 
 
// DEFINES 
 
#define SYSCLK	(80000000) // set system clock to 80MHz 
#define PBCLK  (SYSCLK/1000) // set PBCLK to 80 KHz 
 
#define Fsck	50000 
#define BRG_VAL 	((PBCLK/2/Fsck)-2) 
 
#define Nop() asm( "nop" )  //allows for a pause command 
 
// a simple puase command 
void i2c_wait(unsigned int cnt) 
{ 
	while(--cnt) 
	{ 
		Nop(); 
		Nop(); 
	} 
} 
 
///////////////////////////////////////////// 
///////////////////////////////////////////// 
 
int main(void) 
 
{	 
	ODCA = 0xC000; 
	unsigned char SlaveAddress; 
	char i2cData[3]; 
	//Enable channel using default values 
	OpenI2C1( I2C_EN, BRG_VAL ); 
	SlaveAddress = 0x58; 
	i2cData[0] = ((SlaveAddress )); 
	i2cData[1] = 0x05; //command line 
	i2cData[2] = 0xFF; //end command 
 
while(1) // run the code over and over again 
{ 
 
 
 
	StartI2C1();	//Send the Start Bit (begin of data send) 
	IdleI2C1();		//Wait to complete 
	MasterWriteI2C1 (i2cData[0]); //address 
	IdleI2C1(); 
	AckI2C1(); //ask for acknowledge bit before continuing 
	MasterWriteI2C1 (i2cData[1]); //command line 
	IdleI2C1(); 
	AckI2C1(); 
	MasterWriteI2C1(i2cData[2]); //output 
	IdleI2C1(); 
	AckI2C1(); 
	StopI2C1();  //end of data send 
 
} 
}