coding the dac

hi guys!

i am a 20 year old student and this is one of my first posts, so forgive me if i ask some stupid questions.

i am trying to code the dac MCP 4822 but i dont want to use complicated code like:

 byte lowByte = val & 0xff;
  byte highByte = ((val >> 8) & 0xff) | channel << 7 | gain << 5 | shutdown << 4;

  digitalWrite(latchPin, 1);
  digitalWrite(csPin, 0);

  SPI.transfer(highByte);
  SPI.transfer(lowByte);

  digitalWrite(csPin, 1);
  digitalWrite(latchPin, 0);

basically i dont want to control the CS pin manually or use bit wise operators.

i figured out that one way of doing so will be to use the MCP 4822 header file available in the arduino library.
unfortunately i dont have the code that uses this header file. so if any one can help me with the code to op 0v and 5v from the DAC using this header file i would be very pleased.

thanks in advance,
cheers,
Marrc

Does the library not come with examples ?

If you have a library with a .h file the first thing to do is read the .h file to see what functions it provides, read any comments and then look for any examples in that library.

If its still mysterious the chances are the library is poorly documented - find another one if your can - or
write your own. This is a great exercise, all the messy low-level code in encapsulated in the library
for any other sketches to use.

still looking for answers on how to code MCP 4822. i just want some basic stuff to get started like code to output 1v and 5v from the dac. there is little provided in terms of examples in the IDE so any inputs will be appreciated.
cheers,
marrc

There's an MCP48xx DAC Library available in the IDE's library manager. Might be a good place to start?

marrc:
still looking for answers on how to code MCP 4822.

Why is that? I just read reply #1 and looked up the example sketch:

Have you tried the MCP48xx DAC library and the example program that comes with that? It's available in the Library Manager and looks easy enough.

Steve

1. Check that you have connected the MCP4822 DAC as per following diagram (Fig-1).
mcp48xx.png
Figure-1:

2. Connect a DVM in "DC Voltage Mode" between VOUTA and GND.

3. Upload the following sketch (taken from the example of IDE after including the MCP48xx.h Library in the IDE). The sketch has been compiled in my system but could not run as I don't own the chip.

#include <MCP48xx.h>
MCP4822 dac(10);

// We define an int variable to store the voltage in mV so 100mV = 0.1V
int voltage = 100;

void setup() 
{
    // We call the init() method to initialize the instance
    dac.init();

    // The channels are turned off at startup so we need to turn the channel we need on
    dac.turnOnChannelA();
    dac.turnOnChannelB();

    // We configure the channels in High gain
    // It is also the default value so it is not really needed
    dac.setGainA(MCP4822::High);
    dac.setGainB(MCP4822::High);
}

// We loop from 100mV to 2000mV for channel A and 4000mV for channel B
void loop() 
{
    // We set channel A to output 500mV
    dac.setVoltageA(voltage);

    // We set channel B to output 1000mV
    dac.setVoltageB(voltage * 2);

    // We send the command to the MCP4822
    // This is needed every time we make any change
    dac.updateDAC();

    if (voltage * 2 > 4000) {
        voltage = 100;
    }

    voltage = voltage + 100;

    delay(1000);
}

4. Check that value of the DVM reading changes by 100 mV (0.1 V) in every second. At around 4V, the DVM will preset to 100 mV.

5. If Step-4 works, then connect the DVM across VOUTB and GND to check that Ch-B is working with an increase of 200 mV in every second. At around 4V, the DVM will preset to 100 mV.

mcp48xx.png

marrc:
i am trying to code the dac MCP 4822 but i dont want to use complicated code like:

 byte lowByte = val & 0xff;

byte highByte = ((val >> 8) & 0xff) | channel << 7 | gain << 5 | shutdown << 4;

digitalWrite(latchPin, 1);
 digitalWrite(csPin, 0);

SPI.transfer(highByte);
 SPI.transfer(lowByte);

digitalWrite(csPin, 1);
 digitalWrite(latchPin, 0);

What’s so “complicated” about that?

basically i dont want to control the CS pin manually or use bit wise operators.

Again, that’s just basic Arduino programming. I still don’t see what the problem is.

[quote
5. If Step-4 works, then connect the DVM across VOUTB and GND to check that Ch-B is working with an increase of 200 mV in every second. At around 4V, the DVM will preset to 100 mV.
[/quote]
GolamMustafa! you are delighting as ever. one more thing can i use A6/A7 pins as CS? i know they r i/p pins but i have used up all other pins for PWM.
cheers,
marrc

marrc:
One more thing can I use A6/A7 pins as CS? I know they are input pins, but I have used up all other pins for PWM.

No.

marrc:
one more thing can i use A6/A7 pins as CS? i know they r i/p pins but i have used up all other pins for PWM.

NANO does not allow the DIO operation of A6/A7; but, MEGA allows. You can use 74LS259 (serial latch) to expand IO lines.

GolamMostafa:
NANO does not allow the DIO operation of A6/A7; but, MEGA allows. You can use 74LS259 (serial latch) to expand IO lines.

74LS series logic is obsolete, LS I/O has different logic levels and input/output electrical characteristics that make it a bad choice for hobby use (or almost any use, really). The only reason we used it back in 1975, was that that CMOS wasn’t fast enough yet.

We have these 74LS series in our local market to support the design of SAP computers in the college labs. I have suggested the OP to use the said non-programmable IO expander (just to get 3 extra IO lines) thinking that he might face difficulties to initialize the programmable expander like PCF8574 and similar.

I haven't read the data sheet, but if the SPI lines are not shared, the CS line on the peripheral can often just be permanently grounded or connected to Vdd (whichever makes it active). That would save a pin.

Electronics hobby shops here also carry a little bit of 74LS, but mostly they carry 74HC. HC parts are much more beginner friendly.

Okay, I'll bite. What is a SAP computer?

aarg:
I haven't read the data sheet, but if the SPI lines are not shared, the CS line on the peripheral can often just be permanently grounded or connected to Vdd (whichever makes it active). That would save a pin.

The CS/-pin of the DAC (MCP4822) can not be tied down to LOW as the method dac.updateDAC(); of the Library makes a transition (HIGH-LOW-HIGH) of the SS/-line of the Master to latch the data from input buffer into the actual register of the DAC.

Okay, I'll bite. What is a SAP computer?

SAP-1, 2, 3 (Simple As Possible) are three CPUs that are developed using "discrete TTL ICs" as course works in Digital Electronics Lab based on some American Text/Cook Books.

GolamMostafa:
The CS/-pin of the DAC (MCP4822) can not be tied down to LOW as the method dac.updateDAC(); of the Library makes a transition (HIGH-LOW-HIGH) of the SS/-line of the Master to latch the data from input buffer into the actual register of the DAC.

I don't think it can be connected to a pin expansion device either, for the same reason.

aarg:
I don’t think it can be connected to a pin expansion device either, for the same reason.

You are right; let the CS/-pin of the DAC be remain connected with the SS/-pin of the SPI Port of the Master.

I think, to give good advice about finding more I/O ports, we should see a good diagram of the whole system.

Even we don't know explicitly what Arduino he (the OP) is using. (probably NANO?)

The OP is a 20 yrs old student. He is lucky, by virtue of this Forum, to have communications/discussions with veterans about his projects.