LTlib LaurTec Library  4.0.0 Beta
Open Source C Library for Microchip Microcontrollers based on XC8 Compiler
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
module_IO.c
Go to the documentation of this file.
1 /*******************************************************************************
2 
3 Autore : Mauro Laurenti
4 Versione : 1.0
5 
6 Created on Date : 15/01/2016
7 Last update : 15/01/2016
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 #ifdef __XC8
38 #include <xc.h>
39 #endif
40 
41 #include "module_IO.h"
42 
43 
44 //************************************************************
45 // IO_set_all_port_as_inputs function implementation
46 //************************************************************
48 
49  #if (NUMBER_OF_IO_PORTS == 5)
50 
51  #ifdef _PIC18
52  LATA = 0x00;
53  LATB = 0x00;
54  LATC = 0x00;
55  LATD = 0x00;
56  LATE = 0x00;
57  #endif
58 
59  #ifndef _PIC18
60  PORTA = 0x00;
61  PORTB = 0x00;
62  PORTC = 0x00;
63  PORTD = 0x00;
64  PORTE = 0x00;
65  #endif
66 
67  TRISA = 0xFF;
68  TRISB = 0xFF;
69  TRISC = 0xFF;
70  TRISD = 0xFF;
71  TRISE = 0xFF;
72 
73  //Disable ADC buffer
74  #ifdef _PIC18
75 
76  #ifdef ADC_WITH_ANSEL_REG
77  ANSELA = 0x00;
78  ANSELB = 0x00;
79  ANSELC = 0x00;
80  ANSELD = 0x00;
81  ANSELE = 0x00;
82  #endif
83 
84  #ifndef ADC_WITH_ANSEL_REG
85  //ADCON1 = 0x0F;
86  #endif
87 
88  #endif
89 
90  #ifndef _PIC18
91 
92  #ifdef ADC_WITH_ANSEL_REG
93  ANSEL = 0x00;
94  ANSELH = 0x00;
95  #endif
96 
97  #ifndef ADC_WITH_ANSEL_REG
98  ADCON1 = 0x07;
99  #endif
100  #endif
101 
102  #endif
103 
104 }
105 
106 
107 //************************************************************
108 // IO_set_port_direction function implementation
109 //************************************************************
110 void IO_set_port_direction (unsigned char mcu_port, port_size port_direction){
111 
112  #if (NUMBER_OF_IO_PORTS == 5)
113 
114  if (mcu_port == IO_PORTA){
115  TRISA = port_direction;
116  }
117 
118  if (mcu_port == IO_PORTB){
119  TRISB = port_direction;
120  }
121 
122  if (mcu_port == IO_PORTC){
123  TRISC = port_direction;
124  }
125 
126  if (mcu_port == IO_PORTD){
127  TRISD = port_direction;
128  }
129 
130  if (mcu_port == IO_PORTE){
131  TRISE = port_direction;
132  }
133 
134  #endif
135 }
136 
137 
138 //************************************************************
139 // IO_set_port_direction function implementation
140 //************************************************************
141 void IO_write_port (unsigned char mcu_port, port_size port_data){
142 
143  #if (NUMBER_OF_IO_PORTS == 5)
144 
145  #ifdef _PIC18
146  if (mcu_port == IO_PORTA){
147  LATA = port_data;
148  }
149 
150  if (mcu_port == IO_PORTB){
151  LATB = port_data;
152  }
153 
154  if (mcu_port == IO_PORTC){
155  LATC = port_data;
156  }
157 
158  if (mcu_port == IO_PORTD){
159  LATD = port_data;
160  }
161 
162  if (mcu_port == IO_PORTE){
163  LATE = port_data;
164  }
165  #endif
166 
167  #ifndef _PIC18
168  if (mcu_port == IO_PORTA){
169  PORTA = port_data;
170  }
171 
172  if (mcu_port == IO_PORTB){
173  PORTB = port_data;
174  }
175 
176  if (mcu_port == IO_PORTC){
177  PORTC = port_data;
178  }
179 
180  if (mcu_port == IO_PORTD){
181  PORTD = port_data;
182  }
183 
184  if (mcu_port == IO_PORTE){
185  PORTE = port_data;
186  }
187  #endif
188 
189  #endif
190 }
191 
192 
193 //************************************************************
194 // IO_read_port function implementation
195 //************************************************************
196 port_size IO_read_port (unsigned char mcu_port){
197 
198  #if (NUMBER_OF_IO_PORTS == 5)
199 
200  if (mcu_port == IO_PORTA){
201  return (PORTA);
202  }
203 
204  if (mcu_port == IO_PORTB){
205  return (PORTB);
206  }
207 
208  if (mcu_port == IO_PORTC){
209  return (PORTC);
210  }
211 
212  if (mcu_port == IO_PORTD){
213  return (PORTD);
214  }
215 
216  if (mcu_port == IO_PORTE){
217  return (PORTE);
218  }
219  #endif
220 
221  return (0);
222 }
223 
224 
225 //************************************************************
226 // IO_read_port_bit function implementation
227 //************************************************************
228 port_size IO_read_port_bit (unsigned char mcu_port,port_size bit_mask ){
229 
230  port_size port_data = 0;
231 
232  #if (NUMBER_OF_IO_PORTS == 5)
233 
234  if (mcu_port == IO_PORTA){
235  port_data = PORTA;
236  }
237 
238  if (mcu_port == IO_PORTB){
239  port_data = PORTB;
240  }
241 
242  if (mcu_port == IO_PORTC){
243  port_data = PORTC;
244  }
245 
246  if (mcu_port == IO_PORTD){
247  port_data = PORTD;
248  }
249 
250  if (mcu_port == IO_PORTE){
251  port_data = PORTE;
252  }
253 
254  if ( (port_data & bit_mask) == 0){
255  return (0);
256  } else {
257  return (1);
258  }
259 
260  #endif
261 
262 }
263 
264 //************************************************************
265 // IO_enable_pull_up_resitors function implementation
266 //************************************************************
267 void IO_enable_pull_up_resistors (unsigned char mcu_port, port_size resistors_to_enable) {
268 
269  if (mcu_port == IO_PORTB){
270  //Enable the resistor if at least one is required
271  if (resistors_to_enable != 0)
272  PULL_UP_ENABLE_BIT = 0x00;
273  else
274  PULL_UP_ENABLE_BIT = 0x01;
275 
276  #ifdef PULL_UP_SINGLE_BIT_ENABLE
277  PULL_UP_ENABLE_REGISTER_B = resistors_to_enable;
278  #endif
279  }
280 
281 
282 }