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