PIC18 LaurTec Library  3.2.0
Open Source C Library for PIC18 Microcontrollers based on C18 - XC8 Compilers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
MCP2300x.c
Go to the documentation of this file.
1 /*******************************************************************************
2 
3 Author : Mauro Laurenti
4 Version : 1.0
5 Created on Date : 07/02/2014
6 Last update : 07/02/2014
7 
8 CopyRight 2006-2014 all rights are reserved
9 
10 
11 
12 CopyRight 2006-2013 all rights are reserved
13 
14 ********************************************************
15 SOFTWARE LICENSE AGREEMENT
16 ********************************************************
17 
18 The usage of the supplied software imply the acceptance of the following license.
19 
20 The software supplied herewith by Mauro Laurenti (the Author) is intended for
21 use solely and exclusively on Microchip PIC Microcontroller (registered mark).
22 The software is owned by the Author, and is protected under applicable
23 copyright laws. All rights are reserved.
24 Any use in violation of the foregoing restrictions may subject the
25 user to criminal sanctions under applicable laws, as well as to civil liability
26 for the breach of the terms and conditions of this license.
27 Commercial use is forbidden without a written acknowledgement with the Author.
28 Personal or educational use is allowed if the application containing the
29 following software doesn't aim to commercial use or monetary earning of any kind.
30 
31 THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES,
32 WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
33 TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
34 PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE AUTHOR SHALL NOT,
35 IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
36 CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
37 
38 *******************************************************************************/
39 
40 #ifdef __XC8
41  #include <xc.h>
42  #ifndef _PIC18
43  #error The MCP2300x Library supports only PIC18 devices
44  #endif
45 #endif
46 
47 #include "MCP2300x.h"
48 
49 
50 //************************************************************
51 // initialize_MC2300x function implementation
52 //************************************************************
53 void initialize_MC2300x (unsigned char crystal_frequency_MHz, unsigned int baud_rate_KHz){
54 
55  OpenI2C(MASTER, SLEW_ON);
56 
57  SSPADD = (((crystal_frequency_MHz *1000)/4)/baud_rate_KHz)-1;
58 }
59 
60 //************************************************************
61 // set_register_MCP2300x function implementation
62 //************************************************************
63 
64 signed char set_register_MCP2300x (unsigned char device_address, unsigned char register_add, unsigned char data ) {
65 
66  unsigned char control_byte = 0;
67 
68  control_byte = INTERNAL_ADDRESS | (device_address <<1);
69 
70  return (EEByteWrite (control_byte,register_add,data));
71 }
72 
73 
74 
75 //************************************************************
76 // get_register_MCP2300x function implementation
77 //************************************************************
78 
79 unsigned char get_register_MCP2300x (unsigned char device_address, unsigned char register_add){
80 
81  unsigned char control_byte = 0;
82  control_byte = INTERNAL_ADDRESS | (device_address <<1);
83 
84  return (EERandomRead (control_byte,register_add));
85 }
86 
87 
88 //************************************************************
89 // set_port_direction_MCP2300x function implementation
90 //************************************************************
91 signed char set_port_direction_MCP2300x (unsigned char device_address, unsigned char direction) {
92 
93  return (set_register_MCP2300x (device_address, IODIR, direction));
94 }
95 
96 
97 //************************************************************
98 // set_port_value_MCP2300x function implementation
99 //************************************************************
100 signed char set_port_value_MCP2300x (unsigned char device_address, unsigned char value){
101 
102  return (set_register_MCP2300x (device_address, GPIO, value));
103 }
104 
105 
106 //************************************************************
107 // set_port_polarity_MCP2300x function implementation
108 //************************************************************
109 signed char set_port_polarity_MCP2300x (unsigned char device_address, unsigned char value){
110 
111  return (set_register_MCP2300x (device_address, IPOL, value));
112 }
113 
114 
115 //************************************************************
116 // set_port_pull_up_resistor_MCP2300x function implementation
117 //************************************************************
118 signed char set_port_pull_up_resistor_MCP2300x (unsigned char device_address, unsigned char value){
119 
120  return (set_register_MCP2300x (device_address, GPPU, value));
121 }
122 
123 
124 //************************************************************
125 // get_port_value_MCP2300x function implementation
126 //************************************************************
127 unsigned char get_port_value_MCP2300x (unsigned char device_address){
128 
129  return (get_register_MCP2300x (device_address, GPIO));
130 }
131 
132 
133 
134 //************************************************************
135 // set_port_interrupt_MCP2300x function implementation
136 //************************************************************
137 signed char set_port_interrupt_MCP2300x (unsigned char device_address, unsigned char value){
138 
139  signed char return_value = 0;
140 
141  return_value = set_register_MCP2300x (device_address, GPINTEN, value);
142 
143  //This dummy read of the GPIO register is used to clean the interupt flags
144  get_register_MCP2300x (device_address, GPIO);
145 
146  return (return_value);
147 
148 }
149 
150 //************************************************************
151 // set_port_interrupt_configuration_MCP2300x function implementation
152 //************************************************************
153 signed char set_port_configuration_MCP2300x (unsigned char device_address, unsigned char value){
154  return (set_register_MCP2300x (device_address, IOCON, value));
155 }
156 
157 //************************************************************
158 // set_interrupt_compare_value_MCP2300x function implementation
159 //************************************************************
160 signed char set_interrupt_compare_value_MCP2300x (unsigned char device_address, unsigned char value){
161 
162  return (set_register_MCP2300x (device_address, DEFVAL, value));
163 }
164 
165 
166 //************************************************************
167 // set_interrupt_compare_enable_MCP2300x function implementation
168 //************************************************************
169 signed char set_interrupt_compare_enable_MCP2300x (unsigned char device_address, unsigned char value){
170 
171  return (set_register_MCP2300x (device_address, INTCON, value));
172 }
173 
174 
175 //************************************************************
176 // get_port_interrupt_flag_MCP2300x function implementation
177 //************************************************************
178 unsigned char get_port_interrupt_flag_MCP2300x (unsigned char device_address){
179 
180  return (get_register_MCP2300x (device_address, INTF));
181 }
182 
183 
184 //************************************************************
185 // get_port_interrupt_capture_MCP2300x function implementation
186 //************************************************************
187 unsigned char get_port_interrupt_capture_MCP2300x (unsigned char device_address){
188  return (get_register_MCP2300x (device_address, INTCAP));
189 }
190 
191