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
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 #endif
39 
40 #include "MCP2300x.h"
41 
42 
43 //************************************************************
44 // initialize_MC2300x function implementation
45 //************************************************************
46 void MCP2300x_initialize (unsigned int baud_rate_KHz){
47 
49  MCP2300X_I2C_baud_rate (baud_rate_KHz);
50 }
51 
52 //************************************************************
53 // set_register_MCP2300x function implementation
54 //************************************************************
55 
56 signed char MCP2300x_set_register (unsigned char device_address, unsigned char register_add, unsigned char data ) {
57 
58  unsigned char control_byte = 0;
59 
60  control_byte = MCP2300X_INTERNAL_ADDRESS | (device_address <<1);
61 
62  return (MCP2300X_I2C_write_byte_to_external_device (control_byte,register_add,data));
63 }
64 
65 
66 
67 //************************************************************
68 // get_register_MCP2300x function implementation
69 //************************************************************
70 
71 unsigned char MCP2300x_get_register (unsigned char device_address, unsigned char register_add){
72 
73  unsigned char control_byte = 0;
74  control_byte = MCP2300X_INTERNAL_ADDRESS | (device_address <<1);
75 
76  return (MCP2300X_I2C_read_byte_from_external_device (control_byte, register_add));
77 }
78 
79 
80 //************************************************************
81 // set_port_direction_MCP2300x function implementation
82 //************************************************************
83 signed char MCP2300x_set_port_direction (unsigned char device_address, unsigned char direction) {
84 
85  return (set_register_MCP2300x (device_address, MCP2300x_IODIR, direction));
86 }
87 
88 
89 //************************************************************
90 // set_port_value_MCP2300x function implementation
91 //************************************************************
92 signed char MCP2300x_set_port_value (unsigned char device_address, unsigned char value){
93 
94  return (set_register_MCP2300x (device_address, MCP2300x_GPIO, value));
95 }
96 
97 
98 //************************************************************
99 // set_port_polarity_MCP2300x function implementation
100 //************************************************************
101 signed char MCP2300x_set_port_polarity (unsigned char device_address, unsigned char value){
102 
103  return (set_register_MCP2300x (device_address, MCP2300x_IPOL, value));
104 }
105 
106 
107 //************************************************************
108 // set_port_pull_up_resistor_MCP2300x function implementation
109 //************************************************************
110 signed char MCP2300x_set_port_pull_up_resistor (unsigned char device_address, unsigned char value){
111 
112  return (set_register_MCP2300x (device_address, MCP2300x_GPPU, value));
113 }
114 
115 
116 //************************************************************
117 // get_port_value_MCP2300x function implementation
118 //************************************************************
119 unsigned char MCP2300x_get_port_value (unsigned char device_address){
120 
121  return (get_register_MCP2300x (device_address, MCP2300x_GPIO));
122 }
123 
124 
125 
126 //************************************************************
127 // set_port_interrupt_MCP2300x function implementation
128 //************************************************************
129 signed char MCP2300x_set_port_interrupt (unsigned char device_address, unsigned char value){
130 
131  signed char return_value = 0;
132 
133  return_value = set_register_MCP2300x (device_address, MCP2300x_GPINTEN, value);
134 
135  //This dummy read of the GPIO register is used to clean the interupt flags
136  get_register_MCP2300x (device_address, MCP2300x_GPIO);
137 
138  return (return_value);
139 
140 }
141 
142 //************************************************************
143 // set_port_interrupt_configuration_MCP2300x function implementation
144 //************************************************************
145 signed char MCP2300x_set_port_configuration (unsigned char device_address, unsigned char value){
146  return (set_register_MCP2300x (device_address, MCP2300x_IOCON, value));
147 }
148 
149 //************************************************************
150 // set_interrupt_compare_value_MCP2300x function implementation
151 //************************************************************
152 signed char MCP2300x_set_interrupt_compare_value (unsigned char device_address, unsigned char value){
153 
154  return (set_register_MCP2300x (device_address, MCP2300x_DEFVAL, value));
155 }
156 
157 
158 //************************************************************
159 // set_interrupt_compare_enable_MCP2300x function implementation
160 //************************************************************
161 signed char MCP2300x_set_interrupt_compare_enable (unsigned char device_address, unsigned char value){
162 
163  return (set_register_MCP2300x (device_address, MCP2300x_INTCON, value));
164 }
165 
166 
167 //************************************************************
168 // get_port_interrupt_flag_MCP2300x function implementation
169 //************************************************************
170 unsigned char MCP2300x_get_port_interrupt_flag (unsigned char device_address){
171 
172  return (get_register_MCP2300x (device_address, MCP2300x_INTF));
173 }
174 
175 
176 //************************************************************
177 // get_port_interrupt_capture_MCP2300x function implementation
178 //************************************************************
179 unsigned char MCP2300x_get_port_interrupt_capture (unsigned char device_address){
180  return (get_register_MCP2300x (device_address, MCP2300x_INTCAP));
181 }
182 
183