AVRly - AVR Development Resources
mcp48x2_dac.h
Go to the documentation of this file.
1/******************************************************************************
2 @copyright Copyright © 2022 by Jason Duffy.
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21******************************************************************************/
22
23/**
24 * @file mcp48x2_dac.h
25 * @ingroup mcp48x2
26 * @author Jason Duffy
27 * @date 4th March 2022
28 * @brief Driver for the MCP4812 10 bit DAC (digital to analog converter) chip.
29 * @bug No known bugs.
30 * @details This file provides the basic low-level functionality for the
31 * MCP4812 10 bit DAC from Microchip. This driver currently only allows for a
32 * single DAC to be used, but this might be improved on at a later date. This
33 * driver was written using the datasheet for the Microchip MCP4812 chip, which
34 * can be found at the link below.
35 * @see https://ww1.microchip.com/downloads/en/DeviceDoc/20002249B.pdf
36 */
37
38#ifndef MCP_FOUR_EIGHT_X_TWO_DOT_H
39#define MCP_FOUR_EIGHT_X_TWO_DOT_H
40
41#include <stdbool.h>
42#include <stdint.h>
43
44#define DAC_CHANNEL_A true
45#define DAC_CHANNEL_B false
46
47
48/**
49 * Enumerated constants to select the DAC model number.
50 */
51typedef enum
52{
53 mcp4802,
54 mcp4812,
55 mcp4822
57
58
59/**
60 * Struct for config values for each channel - this is only to be used as a
61 * nested struct within the dac_config_t struct.
62 */
63typedef struct
64{
65 bool gain_low; // true = LOW (0 - 2048mV), false = HIGH (0 - 4096mV).
66 bool active; // true = Channel active, false = channel shutdown.
67 uint16_t level; // Value to be output on channel A.
69
70
71/**
72 * Config struct for DAC. Instantiate this object and assign values to it's
73 * members before passing it's address intp the init_dac() function.
74 */
75typedef struct
76{
77 model_num_t model; // mcp4802, mcp4812 or mcp4822.
78 bool sync_manually; // true = manual sync, false = sync automatically.
79 dac_channel_t channel_a; // Config data members for channel A.
80 dac_channel_t channel_b; // Config data members for channel B.
82
83
84/**
85 * Initialisation routine (run once at startup).
86 * This function is to be called before using any other DAC functions.
87 * Instantiate the dac_config_t object first then pass it's address into and
88 * call init_lcd() before using any other lcd functions.
89 * @param p_config is a pointer to the dac_config_t object.
90 */
91void init_dac(dac_config_t *p_config);
92
93
94/**
95 * Sends a new millivolts value to be output on DAC (Along with config
96 * settings). For 8 or 10 bit models only.
97 * @param channel_a: true = Channel A, false = Channel B.
98 * @param millivolts: mV value to be output by DAC. Ensure that this value
99 * doesn't exceed the maxixum for the DAC model and gain setting.
100 */
101void dac_set_voltage(bool channel_a, uint16_t millivolts);
102
103
104/**
105 * Sends a new millivolts value to be output on DAC (Along with config
106 * settings). For 12 bit models only.
107 * @param channel_a: true = Channel A, false = Channel B.
108 * @param millivolts: mV value to be output by DAC. Ensure that this value
109 * doesn't exceed the maxixum for the DAC model and gain setting.
110 * @param fractional: true = millivolts value has 0.5mV added to it. Only to be
111 * used when gain_low is true.
112 */
113void dac_set_voltage_12_bit(bool channel_a,
114 uint16_t millivolts,
115 bool fractional);
116
117
118/**
119 * Applies new config settings. This function takes updated config settings for
120 * both channels of DAC, then re-sends data so that the new settings take
121 * effect.
122 */
123void dac_reconfigure(void);
124
125
126/**
127 * Pulses LDAC pin low for a brief time (1uS). When sync_manually is true, call
128 * this function to latch both config settings and new voltage value into both
129 * channels of DAC simultaneously.
130 */
131void pulse_latch(void);
132
133#endif // MCP_FOUR_EIGHT_X_TWO_DOT_H
134
135
136/*** end of file ***/
model_num_t
Enumerated constants to select the DAC model number.
Definition: mcp48x2_dac.h:52
void dac_set_voltage(bool channel_a, uint16_t millivolts)
Sends a new millivolts value to be output on DAC (Along with config settings).
Definition: mcp48x2_dac.c:145
void dac_set_voltage_12_bit(bool channel_a, uint16_t millivolts, bool fractional)
Sends a new millivolts value to be output on DAC (Along with config settings).
Definition: mcp48x2_dac.c:189
void init_dac(dac_config_t *p_config)
Initialisation routine (run once at startup).
Definition: mcp48x2_dac.c:89
void dac_reconfigure(void)
Applies new config settings.
Definition: mcp48x2_dac.c:240
void pulse_latch(void)
Pulses LDAC pin low for a brief time (1uS).
Definition: mcp48x2_dac.c:286
Struct for config values for each channel - this is only to be used as a nested struct within the dac...
Definition: mcp48x2_dac.h:64
Config struct for DAC.
Definition: mcp48x2_dac.h:76