LTlib LaurTec Library  4.0.0 Beta
Open Source C Library for Microchip Microcontrollers based on XC8 Compiler
 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 
53 
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
67 
70 
72  MCP4822_SPI_baudrate(clock);
74 
75  dummy_read = MCP4822_SPI_read_byte ();
76 
79 
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 
109 
110  while(MCP4822_SPI_write_byte (byte_high));
111  while(MCP4822_SPI_write_byte (byte_low));
112 
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 
125 }
126 
127 
128 //************************************************************
129 // MCP4822_latch_pulse Implementation
130 //************************************************************
132 
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) {
147  power_mode_DAC_A_MCP4822 = power;
148  } else
149 
150  if (channel == MCP4822_DAC_B) {
152  power_mode_DAC_B_MCP4822 = power;
153  }
154 
155 }