LTlib LaurTec Library
4.0.0 Beta
Open Source C Library for Microchip Microcontrollers based on XC8 Compiler
Main Page
Data Structures
Files
File List
Globals
All
Data Structures
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Macros
MCP4822.c
Go to the documentation of this file.
1
/*******************************************************************************
2
3
Author : Mauro Laurenti
4
Version : 1.2
5
6
Created on Date : 08/09/2014
7
Last update : 28/11/2015
8
9
CopyRight 2006-2015 all rights are reserved
10
11
********************************************************
12
SOFTWARE LICENSE AGREEMENT
13
********************************************************
14
15
The usage of the supplied software imply the acceptance of the following license.
16
17
The software supplied herewith by Mauro Laurenti (the Author) is intended for
18
use solely and exclusively on Microchip PIC Microcontroller (registered mark).
19
The software is owned by the Author, and is protected under applicable
20
copyright laws. All rights are reserved.
21
Any use in violation of the foregoing restrictions may subject the
22
user to criminal sanctions under applicable laws, as well as to civil liability
23
for the breach of the terms and conditions of this license.
24
Commercial use is forbidden without a written acknowledgement with the Author.
25
Personal or educational use is allowed if the application containing the
26
following software doesn't aim to commercial use or monetary earning of any kind.
27
28
THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES,
29
WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
30
TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
31
PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE AUTHOR SHALL NOT,
32
IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
33
CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
34
35
36
********************************************************
37
PURPOSES
38
********************************************************
39
40
This library allows the usage of the MCP4822 DAC.
41
42
*******************************************************************************/
43
44
45
#ifdef __XC8
46
#include <xc.h>
47
#endif
48
49
#include "
MCP4822.h
"
50
51
unsigned
char
gain_value_DAC_A_MCP4822
=
MCP4822_GAIN_2
;
52
unsigned
char
power_mode_DAC_A_MCP4822
=
MCP4822_SHUTDOWN_OFF
;
53
54
unsigned
char
gain_value_DAC_B_MCP4822
=
MCP4822_GAIN_2
;
55
unsigned
char
power_mode_DAC_B_MCP4822
=
MCP4822_SHUTDOWN_OFF
;
56
57
//************************************************************
58
// MCP4822_initialize Implementation
59
//************************************************************
60
void
MCP4822_initialize
(
unsigned
char
clock){
61
62
unsigned
char
dummy_read = 0;
63
64
// Pin initialization
65
MCP4822_CS_TRIS
=
PIN_AS_OUTPUT
;
66
MCP4822_CS_PORT
=
MCP4822_CS_DISABLED
;
67
68
MCP4822_LDAC_TRIS
=
PIN_AS_OUTPUT
;
69
MCP4822_LDAC_PORT
=
MCP4822_LDAC_DISABLED
;
70
71
MCP4822_SPI_mode
(
SPI_MODE_0
);
72
MCP4822_SPI_baudrate
(clock);
73
MCP4822_SPI_open
(
SPI_MASTER_DEVICE
);
74
75
dummy_read =
MCP4822_SPI_read_byte
();
76
77
MCP4822_set_options
(
MCP4822_GAIN_2
,
MCP4822_SHUTDOWN_ON
,
MCP4822_DAC_A
);
78
MCP4822_set_options
(
MCP4822_GAIN_2
,
MCP4822_SHUTDOWN_ON
,
MCP4822_DAC_B
);
79
80
MCP4822_set_amplitude
(
MCP4822_OUT_DEFAULT_VALUE
,
MCP4822_DAC_A
);
81
MCP4822_set_amplitude
(
MCP4822_OUT_DEFAULT_VALUE
,
MCP4822_DAC_B
);
82
83
}
84
85
86
//************************************************************
87
// MCP4822_set_value Implementation
88
//************************************************************
89
void
MCP4822_set_value
(
unsigned
int
value,
unsigned
char
channel){
90
91
unsigned
char
byte_high = 0;
92
unsigned
char
byte_low = 0;
93
94
byte_high = (
unsigned
char)((value & 0xFF00) >> 8);
95
byte_low = (
unsigned
char) (value & 0x00FF);
96
97
//Clean the control bits
98
byte_high &= 0x0F;
99
100
// Set configurations
101
if
(channel ==
MCP4822_DAC_A
){
102
byte_high |= channel |
gain_value_DAC_A_MCP4822
|
power_mode_DAC_A_MCP4822
;
103
}
else
{
104
byte_high |= channel |
gain_value_DAC_B_MCP4822
|
power_mode_DAC_B_MCP4822
;
105
}
106
107
108
MCP4822_CS_PORT
=
MCP4822_CS_ENABLED
;
109
110
while
(
MCP4822_SPI_write_byte
(byte_high));
111
while
(
MCP4822_SPI_write_byte
(byte_low));
112
113
MCP4822_CS_PORT
=
MCP4822_CS_DISABLED
;
114
}
115
116
117
//************************************************************
118
// MCP4822_set_amplitude Implementation
119
//************************************************************
120
void
MCP4822_set_amplitude
(
unsigned
int
value,
unsigned
char
channel){
121
122
MCP4822_set_value
(value, channel);
123
124
MCP4822_latch_pulse
();
125
}
126
127
128
//************************************************************
129
// MCP4822_latch_pulse Implementation
130
//************************************************************
131
void
MCP4822_latch_pulse
(
void
){
132
133
MCP4822_LDAC_PORT
=
MCP4822_LDAC_ENABLED
;
134
MCP4822_PULSE_DELAY
;
135
MCP4822_LDAC_PORT
=
MCP4822_LDAC_DISABLED
;
136
137
}
138
139
140
//************************************************************
141
// MCP4822_set_options Implementation
142
//************************************************************
143
void
MCP4822_set_options
(
unsigned
char
gain,
unsigned
char
power,
unsigned
char
channel){
144
145
if
(channel ==
MCP4822_DAC_A
) {
146
gain_value_DAC_A_MCP4822
= gain;
147
power_mode_DAC_A_MCP4822
= power;
148
}
else
149
150
if
(channel ==
MCP4822_DAC_B
) {
151
gain_value_DAC_B_MCP4822
= gain;
152
power_mode_DAC_B_MCP4822
= power;
153
}
154
155
}
LTlib_v_4.0.0
src
MCP4822.c
Generated on Sun Feb 21 2016 13:52:59 for LTlib LaurTec Library by
1.8.3.1