LTlib LaurTec Library  4.0.1
Open Source C Library for Microchip Microcontrollers based on XC8 Compiler
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
MCP4822.h
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 #ifndef LTLIB_MCP4822_H
45 #define LTLIB_MCP4822_H
46 
47 #ifdef __XC8
48  #include <xc.h>
49 #endif
50 
51 //*****************************************
52 // Library Test
53 //*****************************************
54 #include "LTlib.h"
55 
56 #ifndef LTLIB_SPI_MODULE_H
57  #error The MCP4822 Library requires the "module_SPI" library to be included.
58 #endif
59 
60 
61 //*****************************************
62 // SPI Module Switch
63 //*****************************************
64 #define MCP4822_SPI_MODULE_1
65 //#define MCP4822_SPI_MODULE_2
66 
67 #ifdef MCP4822_SPI_MODULE_1
68  #define MCP4822_SPI_open SPI1_open
69  #define MCP4822_SPI_baudrate SPI1_baudrate
70  #define MCP4822_SPI_mode SPI1_mode
71  #define MCP4822_SPI_write_byte SPI1_write_byte
72  #define MCP4822_SPI_read_byte SPI1_read_byte
73 #endif
74 
75 #ifdef MCP4822_SPI_MODULE_2
76  #define MCP4822_SPI_open SPI2_open
77  #define MCP4822_SPI_baudrate SPI2_baudrate
78  #define MCP4822_SPI_mode SPI2_mode
79  #define MCP4822_SPI_write_byte SPI2_write_byte
80  #define MCP4822_SPI_read_byte SPI2_read_byte
81 #endif
82 
83 //**************************************************
84 // Device Settings
85 //**************************************************
86 
87 #ifdef _PIC18
88  //CS pin
89  #define MCP4822_CS_TRIS TRISAbits.RA5
90  #define MCP4822_CS_PORT LATAbits.LATA5
91 
92  //LAT pin
93  #define MCP4822_LDAC_TRIS TRISEbits.RE0
94  #define MCP4822_LDAC_PORT LATEbits.LATE0
95 #endif
96 
97 #ifndef _PIC18
98  //CS pin
99  #define MCP4822_CS_TRIS TRISAbits.TRISA5
100  #define MCP4822_CS_PORT PORTAbits.RA5
101 
102  //LAT pin
103  #define MCP4822_LDAC_TRIS TRISEbits.TRISE0
104  #define MCP4822_LDAC_PORT PORTEbits.RE0
105 #endif
106 
107 //**************************************************
108 // Constants
109 //**************************************************
110 
111 #define MCP4822_PULSE_DELAY _delay(1)
112 
113 #define MCP4822_DAC_A 0x00
114 #define MCP4822_DAC_B 0x80
115 
116 #define MCP4822_GAIN_1 0x20
117 #define MCP4822_GAIN_2 0x00
118 
119 #define MCP4822_OUT_DEFAULT_VALUE 0x00
120 
121 #define MCP4822_SHUTDOWN_ON 0x00
122 #define MCP4822_SHUTDOWN_OFF 0x10
123 
124 #define MCP4822_CLOCK_FOSC_64 SPI_CLK_OSC_64
125 #define MCP4822_CLOCK_FOSC_16 SPI_CLK_OSC_16
126 #define MCP4822_CLOCK_FOSC_4 SPI_CLK_OSC_4
127 #define MCP4822_CLOCK_FOSC_TMR2 SPI_TIMER_OUT_CLK
128 
129 #define MCP4822_CS_DISABLED 0x01
130 #define MCP4822_CS_ENABLED 0x00
131 
132 #define MCP4822_LDAC_DISABLED 0x01
133 #define MCP4822_LDAC_ENABLED 0x00
134 
148 void MCP4822_initialize (unsigned char clock);
149 #define initialize_MCP4822 MCP4822_initialize
150 
151 
167 void MCP4822_set_value (unsigned int value, unsigned char channel);
168 #define set_value_MCP4822 MCP4822_set_value
169 
170 
185 void MCP4822_set_amplitude (unsigned int value, unsigned char channel);
186 #define set_amplitude_MCP4822 MCP4822_set_amplitude
187 
188 
199 void MCP4822_latch_pulse (void);
200 #define latch_pulse_MCP4822 MCP4822_latch_pulse
201 
202 
219 void MCP4822_set_options (unsigned char gain, unsigned char power, unsigned char channel);
220 #define set_options_MCP4822 MCP4822_set_options
221 
222 #endif