spi 12bit digital to analog chip

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

brandonb
Posts: 438
Joined: Mon Aug 29, 2011 12:26 am
Location: arizona
Has thanked: 175 times
Been thanked: 173 times
Contact:

spi 12bit digital to analog chip

Postby brandonb » Tue Aug 06, 2013 2:07 am

i'm using a microchip mcp4921, these chips are easy to use with the spi protocol,
since this is a 12bit dac, you can get 4096 different values, using a 5v referance

Code: Select all

5volts/4096 = .001220703125 mv_per_step

to figure out which number to send to the spi port for desired output voltage is easy to

Code: Select all

chip_send_number = desired_voltage / mv_per_step

there is a couple things left to do, you have to set bits 12 and 13 in the spi 16 bit message to do this

Code: Select all

chip_send_number=chip_send_number | 3 << 12

then convert the 16 bit variable into two 8 bit variables to send

Code: Select all

high=(chip_send_number>>8) & 255
low=chip_send_number & 255

pull chip select pin low, send high variable char, send low variable char set chip select pin hit
this chips is very quick to make a triangle waveform by looping 0-4095-0
SPI DAC TEST.fcf
(11 KiB) Downloaded 497 times


in this flowchart i created a quick draw up as an example of manually picking out a voltage, using rc5 and a remote as a keypad, you can type in a voltage value then press the blue button to set it, top lcd line shows the commanded voltage, bottom line shows the commanded 12bit value
spi remote commanding dac.fcf
(18.16 KiB) Downloaded 407 times

here is a cheetsheet for hook up and operation
Attachments
CHEETSHEET SPI DAC.jpg
(74.72 KiB) Downloaded 4055 times
These users thanked the author brandonb for the post (total 8):
Kenrix2 (Tue Aug 06, 2013 3:24 am) • medelec35 (Tue Aug 06, 2013 10:48 am) • JohnCrow (Tue Aug 06, 2013 12:31 pm) • jgu1 (Tue Aug 06, 2013 2:16 pm) • dazz (Tue Aug 06, 2013 4:20 pm) • Spanish_dude (Wed Aug 07, 2013 3:59 am) • DannyBerry (Fri Aug 09, 2013 3:28 am) • headhuntergr (Sun Dec 28, 2014 10:54 am)
Rating: 42.11%
 

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8440
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2412 times
Been thanked: 3450 times
Contact:

Re: spi 12bit digital to analog chip

Postby medelec35 » Tue Aug 06, 2013 10:50 am

Thanks Brandon,
What a great tip.
I'm going to have a play with these devices :)

Thank you for sharing.
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

User avatar
JohnCrow
Valued Contributor
Valued Contributor
Posts: 1367
Joined: Wed Sep 19, 2007 1:21 pm
Location: Lincolnshire
Has thanked: 381 times
Been thanked: 715 times
Contact:

Re: spi 12bit digital to analog chip

Postby JohnCrow » Tue Aug 06, 2013 1:09 pm

Thanks Brandon

You've planted the seed for a new project. :)
1 in 10 people understand binary, the other one doesn't !

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: spi 12bit digital to analog chip

Postby Spanish_dude » Wed Aug 07, 2013 3:58 am

Same here!
I think you can easily make a crappy function generator with it.
Have a PC program send a data pakket of a sine/triangle/sawtooth waveform to a microcontroller.
Then use that microcontroller to send the data, through SPI, to the DAC.

I just ordered some MCP4821, 5V, 12 bit, int ref (2.048V or 4.096V), single output DAC.

brandonb
Posts: 438
Joined: Mon Aug 29, 2011 12:26 am
Location: arizona
Has thanked: 175 times
Been thanked: 173 times
Contact:

Re: spi 12bit digital to analog chip

Postby brandonb » Wed Aug 07, 2013 6:11 am

I think you can easily make a crappy function generator with it.
:lol:
i wanted to try something simular with auto sensor simulation... this might sound a bit weird but it would be self amusing to be able to datalog auto sensors to sram chips then play the data back with dac to veiw with a scope while having the value displayed on lcd as well

User avatar
JohnCrow
Valued Contributor
Valued Contributor
Posts: 1367
Joined: Wed Sep 19, 2007 1:21 pm
Location: Lincolnshire
Has thanked: 381 times
Been thanked: 715 times
Contact:

Simple DAC Power Supply

Postby JohnCrow » Thu Aug 08, 2013 7:24 pm

Digital To Analogue Converter Power Supply.

Introduction:

This project is a simple DC voltage generator with a 0 to 5V variable output.

This is based on the initial post by Brandon a few days ago.
All credit is to him for planting the seeds for a new set of experiments.


The device is a Microchip MCP4921.
This is a 12-Bit DAC which means it can use 4096 different values with a 5V reference.
The device is controlled using the SPI bus, which means it is very straightforward to use.

This is not a power supply in the sense of a bench supply, it is only to demonstrate the principles, and is only capable of supplying a few mA

Calculation:

12 Bits = 4096 steps

5V / 4096 =1.220703125 mV- Step

The data to send to the device is calculated as follows.

Data To Send = Desired Output/mV-Step

To program 2.5V

Data To Send = 2.5/0.001220703125
Data To Send = 2048

Before the data is sent, bits 12 & 13 need to be set.

Data To Send = Data To Send |3 << 12
In other words, OR the Data with 3, then left shift 12.

This 16 bit value then needs splitting to 2 x 8 bit bytes to send to the SPI Bus.

High Byte =( Data To Send >>8) & 255
In other words Right Shift 8 then AND with 255

Low Byte = Data To Send & 255
In other words AND with 255

Using The SPI Bus:

The data is sent to the SPI bus as follows.

Pull C0 High
INIT the SPI bus in Flowcode
Pull C0 Low
Send SPI High Byte
Send SPI Low Byte
Pull C0 High.

The DAC output will then be 2.5V dc for an input as in the above examples.

Hardware:

EB 006 Programmer
EB 014 Keypad
EB 017 Patch Board
EB 076 Touch Screen Display (Used to make showing the data easier)
PIC 16F877A Microcontroller
MCP4921 DAC Device.


MCP4921 Pin Outs:

1 Vdd 5V
2 CS C0
3 SCK C3
4 SDI C5
5 LDAC GND
6 Vref 5V
7 Vss GND
8 Vout Output


Usage:

Using the touch screen display allows more data to be displayed than using a standard 2 x 16 LCD.

On first power up or pressing the reset button, a simple user guide is displayed.
To pass this point, the screen is swiped.

The actual voltage is entered as a 4 digit variable without the decimal point. This is then added by dividing the 4 digit value entered by 1000.
This is then converted to a string to display on the LCD.

The system will output this level until a new value is entered.
Pressing # will set the output to zero.

Experimental data shows the accuracy to be about 0.5 measured on my Fluke 87/5 3.5 Digit Meter
The meter has a basic DC accuracy of 0.05% + 1 digit.

Currently, I have not implemented any checking for entries over 5 V.
If a value is entered, the output will be Input-5, though the display will show the entered value.

Example

Entered 6.5V

Display = 6.500 V
Output = 1.500 V

System.JPG
System.JPG (99.84 KiB) Viewed 9093 times


DAC Voltage TouchScreen V1.fcf
(30.28 KiB) Downloaded 380 times

Display.JPG
Display.JPG (79.3 KiB) Viewed 9093 times
These users thanked the author JohnCrow for the post (total 3):
dazz (Thu Aug 08, 2013 7:32 pm) • medelec35 (Thu Aug 08, 2013 7:36 pm) • brandonb (Fri Aug 09, 2013 2:40 am)
Rating: 15.79%
 
1 in 10 people understand binary, the other one doesn't !

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8440
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2412 times
Been thanked: 3450 times
Contact:

Re: spi 12bit digital to analog chip

Postby medelec35 » Thu Aug 08, 2013 8:15 pm

Thanks guys, this is a really informative and useful thread. :)

Martin
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: spi 12bit digital to analog chip

Postby Spanish_dude » Fri Aug 09, 2013 2:30 pm

Probably best to use a buffer, power transistor or some sort of FET to not draw to much current from the DAC.

- Nicolas

User avatar
JohnCrow
Valued Contributor
Valued Contributor
Posts: 1367
Joined: Wed Sep 19, 2007 1:21 pm
Location: Lincolnshire
Has thanked: 381 times
Been thanked: 715 times
Contact:

Re: spi 12bit digital to analog chip

Postby JohnCrow » Fri Aug 09, 2013 5:16 pm

Hi Nicolas

Probably best to use a buffer, power transistor or some sort of FET to not draw to much current from the DAC.


Yes thats true. This was just a simple idea to test the device.
Thats the next step, to see if I can make a useable psu from it. Maybe implement Brandons idea of using an RC5 remote control as well.
1 in 10 people understand binary, the other one doesn't !

Kenrix2
Flowcode v5 User
Posts: 211
Joined: Tue Feb 19, 2013 9:51 pm
Has thanked: 72 times
Been thanked: 177 times
Contact:

Re: spi 12bit digital to analog chip

Postby Kenrix2 » Fri Aug 09, 2013 10:07 pm

Many Microchip Pic's have a built in DAC Module. Microchip's Product Selection Tool returns 41 different 8 bit Micro's with DAC's. The 8 bit micro's that are supported by Flowcode use a 5 bit DAC. There are some newer 8 bit micro's that have an 8 bit DAC but, these are not yet directly supported by Flowcode. Many 16 bit Pic micro's have even higher resolution. Attached is a simple experiment using the built in DAC Module. There are 4 variations. Rather than writing 4 different programs I just did one program with 4 seperate loops showing the different variations. Although at first glance 5 bits may not seem like much but, when using a lower voltage reference you can get get a fairly fine resolution. I mainly use the DAC as an internal offset for the comparators but, this flowchart outputs the DAC to the DAC Out Pin. This output is not buffered. When I need some current out on that pin, I use an opamp. Here is a schematic of what I have used. I am not an EE so use at your own risk.

OPAMP.png
OPAMP.png (3.78 KiB) Viewed 9030 times
Attachments
DAC_Reference_Experiment.fcf
(8.5 KiB) Downloaded 313 times
These users thanked the author Kenrix2 for the post (total 3):
medelec35 (Fri Aug 09, 2013 11:20 pm) • JohnCrow (Sat Aug 10, 2013 9:13 am) • brandonb (Mon Aug 12, 2013 2:34 am)
Rating: 15.79%
 

George_B
Posts: 88
Joined: Wed Jul 04, 2012 11:21 pm
Has thanked: 37 times
Been thanked: 12 times
Contact:

Re: spi 12bit digital to analog chip

Postby George_B » Mon May 28, 2018 3:52 pm

Hi, i am making some experiments with the MCP4821 DAC DEVICE.

At the moment i can only read 2.05 V at the output of the device(pin8).

Probably i am running the device using a gain of 1

I would like to use a full scale and a gain of 2 but i can't figure out how to send a command to the MCP4821.

Any idea ?

Regards
George
Attachments
DAC Voltagev7.fcfx
(13.12 KiB) Downloaded 55 times

George_B
Posts: 88
Joined: Wed Jul 04, 2012 11:21 pm
Has thanked: 37 times
Been thanked: 12 times
Contact:

Re: spi 12bit digital to analog chip

Postby George_B » Wed May 30, 2018 7:45 pm

Hi all,

I finally managed to read in full scale mode the MCP4821.

I would like to use 4 devices connected to the same micro controller.

Would it be possible to connect each device to separate pins ?

I tried to do a program with four DAC devices connected in separate pins and the result is not the expected. The reading is getting negative from all three devices except the first on which gets the actual reading value.

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1161
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 357 times
Been thanked: 572 times
Contact:

Re: spi 12bit digital to analog chip

Postby QMESAR » Fri Jun 01, 2018 8:25 am

George_B wrote:I tried to do a program with four DAC devices connected in separate pins and the result is not the expected.


Normal configuration for multiple SPI devices on the same bus is
SDO All connected in Parallel
SDI all connected in Parallel
SCK all connected in parallel

Then each chip(IC) has its own CS line connected to a mcu pin, your code then select (pull down ) the CS pin on the device you want to communicate
to all other devices which has their CS pins now high will not answer in any way,then you pull the CS pin high and select the next device CS pin (low) to which you want to communicate.Devices which the CS pin is high can never interfere with the device communication which the pin is low
These users thanked the author QMESAR for the post (total 3):
Benj (Fri Jun 01, 2018 9:49 am) • medelec35 (Fri Jun 01, 2018 9:55 am) • George_B (Sat Jun 02, 2018 8:58 pm)
Rating: 15.79%
 

George_B
Posts: 88
Joined: Wed Jul 04, 2012 11:21 pm
Has thanked: 37 times
Been thanked: 12 times
Contact:

Re: spi 12bit digital to analog chip

Postby George_B » Sat Jun 02, 2018 9:02 pm

Hi QMESAR,

Thank you for your reply. I understand what you mean and i will use it in case that i am going to make a parallel use of devices sharing the I2C lines.

The issue that i have is that the chip that i want to use, is the MSP3421 which has not got a CS pin and therefor this is not possible at this time.

I am wondering if i have a transistor powering each MCD3421 and control the power of the deivice each time.. would this be a solution ?

Regards
George

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1161
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 357 times
Been thanked: 572 times
Contact:

Re: spi 12bit digital to analog chip

Postby QMESAR » Sun Jun 03, 2018 5:34 am

George_B wrote:The issue that i have is that the chip that i want to use, is the MSP3421 which has not got a CS pin and therefor this is not possible at this time.

Well yes looking at the datasheet of the device it does not have an SPI Interface therefore it will also not have a CS pin. To communicate with the chip you send the address and the chip knows now the following data is intended for him.

Dont confuse SPI and I2C with one another, I would suggest you read bit how to work with I2C it will help you understand how to configure and use your MSP3421,
These users thanked the author QMESAR for the post:
George_B (Wed Jun 06, 2018 8:45 pm)
Rating: 5.26%