PIC18 LaurTec Library  3.3.1
Open Source C Library for PIC18 Microcontrollers based on C18 - XC8 Compilers
 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.1
5 
6 Created on Date : 08/09/2014
7 Last update : 15/12/2014
8 
9 CopyRight 2006-2014 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 
54 
57 
58 //************************************************************
59 // MCP4822_initialize Implementation
60 //************************************************************
61 void MCP4822_initialize (unsigned char clock){
62 
63  unsigned char dummy_read = 0;
64 
65  // Pin initialization
66  MCP4822_SDO_TRIS &= ~(0x01 << MCP4822_SDO_BIT);
67  MCP4822_SDO_PORT &= ~(0x01 << MCP4822_SDO_BIT);
68 
69  MCP4822_SCK_TRIS &= ~(0x01 << MCP4822_SCK_BIT);
70  MCP4822_SCK_PORT &= ~(0x01 << MCP4822_SCK_BIT);
71 
72  MCP4822_CS_TRIS &= ~(0x01 << MCP4822_CS_BIT);
74 
75  MCP4822_LDAC_TRIS &= ~(0x01 << MCP4822_LDAC_BIT);
77 
78  OpenSPI(clock, MODE_00, SMPMID);
79 
80  dummy_read = ReadSPI ();
81 
84 
87 
88 }
89 
90 
91 //************************************************************
92 // MCP4822_set_value Implementation
93 //************************************************************
94 void MCP4822_set_value (unsigned int value, unsigned char channel){
95 
96  unsigned char byte_high = 0;
97  unsigned char byte_low = 0;
98 
99  byte_high = (unsigned char)((value & 0xFF00) >> 8);
100  byte_low = (unsigned char) (value & 0x00FF);
101 
102  //Clean the control bits
103  byte_high &= 0x0F;
104 
105  // Set configurations
106  if (channel == MCP4822_DAC_A){
107  byte_high |= channel | gain_value_DAC_A_MCP4822 | power_mode_DAC_A_MCP4822;
108  } else {
109  byte_high |= channel | gain_value_DAC_B_MCP4822 | power_mode_DAC_B_MCP4822;
110  }
111 
112 
113  MCP4822_CS_PORT &= ~(0x01 << MCP4822_CS_BIT);
114 
115  while(WriteSPI (byte_high));
116  while(WriteSPI (byte_low));
117 
118  MCP4822_CS_PORT |= 0x01 << MCP4822_CS_BIT;
119 }
120 
121 
122 //************************************************************
123 // MCP4822_set_amplitude Implementation
124 //************************************************************
125 void MCP4822_set_amplitude (unsigned int value, unsigned char channel){
126 
127  MCP4822_set_value (value, channel);
128 
130 }
131 
132 
133 //************************************************************
134 // MCP4822_latch_pulse Implementation
135 //************************************************************
137 
138  MCP4822_LDAC_PORT &= ~(0x01 << MCP4822_LDAC_BIT);
141 
142 
143 }
144 
145 
146 //************************************************************
147 // MCP4822_set_options Implementation
148 //************************************************************
149 void MCP4822_set_options (unsigned char gain, unsigned char power, unsigned char channel){
150 
151  if (channel == MCP4822_DAC_A) {
153  power_mode_DAC_A_MCP4822 = power;
154  } else
155 
156  if (channel == MCP4822_DAC_B) {
158  power_mode_DAC_B_MCP4822 = power;
159  }
160 
161 }