Giter Club home page Giter Club logo

mcp48x2's Introduction

MCP48x2 DAC SPI library

Quick start

1. Add submodule to your project

git submodule add [email protected]:anisyanka/mcp48x2.git /third/party/folder

2. Add definition for a DAC type and sources

# C defines
C_DEFS += -DDAC_TYPE_MCP4822
C_SOURCES += /third/party/folder/mcp48x2.c
C_INCLUDES += /third/party/folder

For now DAC_TYPE_MCP4822 (12 bit DAC), DAC_TYPE_MCP4812 (10 bit DAC) and DAC_TYPE_MCP4802 (8 bit DAC) are only supported.

3. Call default initialization function

mcp48x2_ret_t mcp48x2_default_init(mcp48x2_device_t *dev, mcp48x2_ll_t *ll);

Default setup is:

  • both channels are in active mode
  • gain factor equals 2 for both channels
  • initial output voltages are set to zero for both channels.

You can use other init function from the header file if the default setup doesn't fit you.

dev - describes one instance of your DACs on your board. The board can contain plenty of DACs. This param helps you to control each instance separately.

ll - describes low level functions to allow the library to control hardware. In this case to control GPIOs and SPI. It looks like this:

typedef struct
{
	/* Manage NSS pin */
	void (*nss_set)(void);
	void (*nss_reset)(void);

	/* Manage LDAC pin */
	void (*ldac_set)(void);
	void (*ldac_reset)(void);
	void (*delay_us)(uint32_t us);

	/* Transmit 16 bits with SPI */
	mcp48x2_ret_t (*spi_write_packet)(uint16_t data);
} mcp48x2_ll_t;

You have to implement all these function by yourself and pass it to the library. The lib is able to work on different hardwares with the help of this.

4. Enjoy using the DAC

Set output voltage with the help of:

mcp48x2_ret_t mcp48x2_set_channel_value(mcp48x2_device_t *dev,
                                        mcp48x2_ch_t ch, uint16_t val);

Remember that:

Vout = (2048 * val * G) / 2^(DAC resolution)

  • 2048 - internal reference voltage
  • G - gain factor
  • val = [0 - 4095] for MCP4922
  • val = [0 - 1023] for MCP4912
  • val = [0 - 255] for MCP4902

mcp48x2's People

Contributors

anisyanka avatar

Watchers

 avatar

Forkers

hyeong-yong

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.