Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Use MAX521 DAC with i2c


  • Please log in to reply
7 replies to this topic

#1 gnomathibus

gnomathibus

    Member

  • Members
  • PipPip
  • 13 posts
  • LocationFrance

Posted 16 April 2012 - 02:02 PM

Hello,

I want to use a MAX521 in I2C with my Netduino, I use the class I2CBus found on the forum, but I am having dificulties with the function to write a value to a regitre

here is my code:
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
using NetduinoPlusTesting;


namespace Maxdac
{
    public class Maxdacdevice 
    {

        
        private I2CDevice.Configuration _slaveConfig;
        private const int TransactionTimeout = 3000; // ms
        private const byte ClockRateKHz = 100;
        public byte Address { get; private set; }
        
        
         

        public Maxdacdevice(byte address)
        {
            Address = address;
            _slaveConfig = new I2CDevice.Configuration(address, ClockRateKHz);
         
        }
        
       
        public void write()
        {

         //Send command write Full Power DAC0

            I2CBus.GetInstance().Write(_slaveConfig, new byte[] { 0x07 , 0x00 }, TransactionTimeout);
            I2CBus.GetInstance().Write(_slaveConfig, new byte[] { 0xFF }, TransactionTimeout);

        }

       


    }
}


in fact I try to do a translation of an existing library for Arduino, but since I'm a beginner it was not easy for me
The link from the site of the library MAX520 / 1 for Arduino
and the to linkthe documentation of the IC

Sorry for my poor English I use Google to translate if someone could help me it would be super cool!

#2 gbreder

gbreder

    Advanced Member

  • Members
  • PipPipPip
  • 53 posts
  • LocationGermany

Posted 16 April 2012 - 06:16 PM

Hi, the first common mistake of I2C devices is to take all eight bits of the address. Please take only the most significant 7 and shift them one place to the right. The datasheet says that the address is someting like: 0, 1, 0, 1, ADR2, ADR1, ADR0, R/W This results in addresses between: 0x28 and 0x2F (these address values are hexadecimal) Is your address in this range? The first write statement writes zero to DAC7. Is this right? What should the second write statement do? Regards Guido

#3 gnomathibus

gnomathibus

    Member

  • Members
  • PipPip
  • 13 posts
  • LocationFrance

Posted 16 April 2012 - 07:10 PM

thank you very much for the interest in my problem Guido, effectively addresses are possible for the MAX521 0x28, 0x29, 0x2A, 0x2B


I in my promgram instantiates as:
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
using Maxdac;
using NetduinoPlusTesting;

namespace NetduinoPlusApplication5
{
    public class Program
    {
       
        public static void Main()
        {
            // write your code here
            OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);

            Maxdacdevice Max = new Maxdacdevice(0x2A);
                    
            
            Max.write();

            while (true)
            {
                led.Write(true);
                Thread.Sleep(250);
                led.Write(false);
                Thread.Sleep(250);
            }


        }

    }
}

The first write statement writes zero to DAC7. Is this right?
What should the second write statement do?

I2CBus.GetInstance().Write(_slaveConfig, new byte[] { 0x07 , 0x00 }, TransactionTimeout);
I2CBus.GetInstance().Write(_slaveConfig, new byte[] { 0xFF }, TransactionTimeout);



As I said in my topic I used the existing Arduino library that works fine as an example for me is a 0x07 B00000111 "Bitwise"? , 0x00 DAC0 and 0xFF has a full power

here the example of the library in Arduino Cpp:

maxdac::maxdac(byte address) //address should be 7 bits
{
  Wire.begin();
  _address = address;
}

void maxdac::write(int port, int value) //port is 0-7 for MAX521 and 0-3 for MAX520
{
  Wire.beginTransmission(_address); //tell the device we are ready to begin transmitting
  Wire.send((B00000111 & port)); //use some BitWise "&" to combine the command to write to a port with the port you wish to write to
  Wire.send(value); //send the actual value to the port
  Wire.endTransmission(); //stop communication
}


thank you again !

#4 gbreder

gbreder

    Advanced Member

  • Members
  • PipPipPip
  • 53 posts
  • LocationGermany

Posted 16 April 2012 - 07:34 PM

Hi,
the Arduino code
Wire.send((B00000111 & port)); //use some BitWise "&" to combine the command to write to a port with the port you wish to write to
Wire.send(value); //send the actual value to the port
would translate to
I2CBus.GetInstance().Write(_slaveConfig, new byte[] { (byte)(0x07 & port) }, TransactionTimeout);
I2CBus.GetInstance().Write(_slaveConfig, new byte[] { 0xFF }, TransactionTimeout);
where "port" is 0x00 for DAC0.

Maybe this will work :)

Regard
Guido

#5 gnomathibus

gnomathibus

    Member

  • Members
  • PipPip
  • 13 posts
  • LocationFrance

Posted 16 April 2012 - 07:48 PM

I had already tried but unfortunately it does not work, I have the following error message: throw new Exception ("Could not write to device."); :(

#6 gbreder

gbreder

    Advanced Member

  • Members
  • PipPipPip
  • 53 posts
  • LocationGermany

Posted 17 April 2012 - 07:27 PM

Hi gnomathibus,
after I read the datasheet of your DAC again I realized I've made a mistake:
The code on the Netduino should be something like this:
var slaveConfig = new I2CDevice.Configuration(0x28, 100);

byte port = 0x00;
var i2CAdapter = I2CAdapter.Instance;
i2CAdapter.WriteBytes(slaveConfig, new byte[] {(byte)(0x07 & port), 0xFF});
The "(0x07 & port)" ensures that only the 3 smallest bits can be set as port. The "(byte)" converts the resulting integer back to one byte and "0xFF" sets the DAC0 to full power.
As far as I understand the datasheet this should work.

Regards
Guido

#7 gnomathibus

gnomathibus

    Member

  • Members
  • PipPip
  • 13 posts
  • LocationFrance

Posted 17 April 2012 - 07:59 PM

Thank you very much for your help Guido, but I solved my problem in the afternoon if I had not put pull-up resistor to the SDA and SCL ports on my arduino while I did not need to Max521 one, here is the wiring diagram that works impeccably well now and for me and my code snippet :)

using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
using NetduinoPlusTesting;


namespace Maxdac
{
    public class Maxdacdevice 
    {

        
        private I2CDevice.Configuration _slaveConfig;
        private const int TransactionTimeout = 3000; // ms
        private const byte ClockRateKHz = 100;
        public byte Address { get; private set; }
       
       
         

        public Maxdacdevice(byte address)
        {
            Address = address;
            _slaveConfig = new I2CDevice.Configuration(address, ClockRateKHz);
        
        }

        public void write(byte port, byte value)
        {
        
            I2CBus.GetInstance().Write(_slaveConfig, new byte[] { (byte)(0x07 & port), value }, TransactionTimeout);
        
        }

        public void reset()
        {
        
            I2CBus.GetInstance().Write(_slaveConfig, new byte[] { (byte)(0x10) }, TransactionTimeout);
      
        }

        public void shutDown(Boolean shutdown)
        {
            if (shutdown == true)
            {

                I2CBus.GetInstance().Write(_slaveConfig, new byte[] { (byte)(0x08), (byte)(0x00) }, TransactionTimeout);
            }
            else
            {

                I2CBus.GetInstance().Write(_slaveConfig, new byte[] { (byte)(0x00), (byte)(0x00) }, TransactionTimeout);

            }
        }


    }
}

and the progam


using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
using Maxdac;
using NetduinoPlusTesting;

namespace NetduinoPlusApplication5
{
    public class Program
    {
       
        public static void Main()
        {
            // write your code here
            OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);

            Maxdacdevice Max1 = new Maxdacdevice(0x2A);
            Maxdacdevice Max2 = new Maxdacdevice(0x29);

            Max1.reset();
            Max2.reset();
            Thread.Sleep(500);

            //Set FULLPOWER to MAX521 Adress(0x2A) DAC04  
            Max1.write(4,255);
            //Set FULLPOWER to MAX521 Adress(0x29) DAC01
            Max2.write(1, 255);
            Thread.Sleep(5000);

            //resets all registers to off
            Max1.reset();
            Max2.reset();
            Thread.Sleep(5000);


            //shuts down the DAC into power save mode
            Max1.shutDown(true);
            Max2.shutDown(true);
            Thread.Sleep(5000);

                       
            //wakes the DAC. Any write command will also wake the DAC from power save mode
            Max1.shutDown(false);
            Max2.shutDown(false);
            Thread.Sleep(5000);

           //Set HALFPOWER to MAX521 Adress(0x2A) DAC04  
            Max1.write(4, 127);
           //Set HALFPOWER to MAX521 Adress(0x2A) DAC01  
            Max2.write(1, 127);
            Thread.Sleep(5000);

            

            while (true)
            {
                led.Write(true);
                Thread.Sleep(250);
                led.Write(false);
                Thread.Sleep(250);
            }


        }

    }
}

Attached Files



#8 gbreder

gbreder

    Advanced Member

  • Members
  • PipPipPip
  • 53 posts
  • LocationGermany

Posted 18 April 2012 - 07:52 AM

Excellent! Thanks for your feedback. Guido




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.