PIC32MX: I2C DAC

From Mech
Revision as of 21:36, 15 February 2010 by MatthewLuther (talk | contribs) (→‎Code)
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 ____ DAC.

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

Circuit

Include a schematic and give any part numbers. A photo of your circuit is OK, but not as a replacement for a schematic.

Code

==

  1. include <HardwareProfile.h>
  2. include <HardwareProfile_NU32.h>
  3. 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 //

  1. pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF
  2. pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_2

// DEFINES

  1. define SYSCLK (80000000) // set system clock to 80MHz
  2. define PBCLK (SYSCLK/1000) // set PBCLK to 80 KHz
  1. define Fsck 50000
  2. define BRG_VAL ((PBCLK/2/Fsck)-2)
  1. 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

} }