LTlib LaurTec Library  4.0.3
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.1
5 
6 Created on Date : 15/01/2016
7 Last update : 25/02/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  #ifdef ADC_WITH_ANSEL_2_REG
85  ANSEL = 0x00;
86  ANSELH = 0x00;
87  #endif
88 
89  #ifdef ADC_WITH_NO_ANSEL_REG
90  //ADCON1 = 0x0F;
91  #endif
92 
93  #endif
94 
95  #ifndef _PIC18
96 
97  #ifdef ADC_WITH_ANSEL_REG
98  ANSEL = 0x00;
99  ANSELH = 0x00;
100  #endif
101 
102  #ifdef ADC_WITH_NO_ANSEL_REG
103  ADCON1 = 0x07;
104  #endif
105  #endif
106 
107  #endif
108 
109 
110  #if (NUMBER_OF_IO_PORTS == 3)
111 
112  #ifdef _PIC18
113  LATA = 0x00;
114  LATB = 0x00;
115  LATC = 0x00;
116  #endif
117 
118  #ifndef _PIC18
119  PORTA = 0x00;
120  PORTB = 0x00;
121  PORTC = 0x00;
122  #endif
123 
124  TRISA = 0xFF;
125  TRISB = 0xFF;
126  TRISC = 0xFF;
127 
128  //Disable ADC buffer
129  #ifdef _PIC18
130 
131  #ifdef ADC_WITH_ANSEL_REG
132  ANSELA = 0x00;
133  ANSELB = 0x00;
134  ANSELC = 0x00;
135  #endif
136 
137  #ifdef ADC_WITH_ANSEL_2_REG
138  ANSEL = 0x00;
139  ANSELH = 0x00;
140  #endif
141 
142  #ifdef ADC_WITH_NO_ANSEL_REG
143  //ADCON1 = 0x0F;
144  #endif
145 
146  #endif
147 
148  #ifndef _PIC18
149 
150  #ifdef ADC_WITH_ANSEL_REG
151  ANSEL = 0x00;
152  ANSELH = 0x00;
153  #endif
154 
155  #ifndef ADC_WITH_ANSEL_REG
156  ADCON1 = 0x07;
157  #endif
158  #endif
159 
160  #endif
161 
162 }
163 
164 
165 //************************************************************
166 // IO_set_port_direction function implementation
167 //************************************************************
168 void IO_set_port_direction (unsigned char mcu_port, port_size port_direction){
169 
170  #if (NUMBER_OF_IO_PORTS == 5)
171 
172  if (mcu_port == IO_PORTA){
173  TRISA = port_direction;
174  }
175 
176  if (mcu_port == IO_PORTB){
177  TRISB = port_direction;
178  }
179 
180  if (mcu_port == IO_PORTC){
181  TRISC = port_direction;
182  }
183 
184  if (mcu_port == IO_PORTD){
185  TRISD = port_direction;
186  }
187 
188  if (mcu_port == IO_PORTE){
189  TRISE = port_direction;
190  }
191 
192  #endif
193 
194 
195  #if (NUMBER_OF_IO_PORTS == 3)
196 
197  if (mcu_port == IO_PORTA){
198  TRISA = port_direction;
199  }
200 
201  if (mcu_port == IO_PORTB){
202  TRISB = port_direction;
203  }
204 
205  if (mcu_port == IO_PORTC){
206  TRISC = port_direction;
207  }
208 
209  #endif
210 
211 }
212 
213 
214 //************************************************************
215 // IO_set_port_direction function implementation
216 //************************************************************
217 void IO_write_port (unsigned char mcu_port, port_size port_data){
218 
219  #if (NUMBER_OF_IO_PORTS == 5)
220 
221  #ifdef _PIC18
222  if (mcu_port == IO_PORTA){
223  LATA = port_data;
224  }
225 
226  if (mcu_port == IO_PORTB){
227  LATB = port_data;
228  }
229 
230  if (mcu_port == IO_PORTC){
231  LATC = port_data;
232  }
233 
234  if (mcu_port == IO_PORTD){
235  LATD = port_data;
236  }
237 
238  if (mcu_port == IO_PORTE){
239  LATE = port_data;
240  }
241  #endif
242 
243  #ifndef _PIC18
244  if (mcu_port == IO_PORTA){
245  PORTA = port_data;
246  }
247 
248  if (mcu_port == IO_PORTB){
249  PORTB = port_data;
250  }
251 
252  if (mcu_port == IO_PORTC){
253  PORTC = port_data;
254  }
255 
256  if (mcu_port == IO_PORTD){
257  PORTD = port_data;
258  }
259 
260  if (mcu_port == IO_PORTE){
261  PORTE = port_data;
262  }
263  #endif
264 
265  #endif
266 
267  #if (NUMBER_OF_IO_PORTS == 3)
268 
269  #ifdef _PIC18
270  if (mcu_port == IO_PORTA){
271  LATA = port_data;
272  }
273 
274  if (mcu_port == IO_PORTB){
275  LATB = port_data;
276  }
277 
278  if (mcu_port == IO_PORTC){
279  LATC = port_data;
280  }
281 
282  #endif
283 
284  #ifndef _PIC18
285  if (mcu_port == IO_PORTA){
286  PORTA = port_data;
287  }
288 
289  if (mcu_port == IO_PORTB){
290  PORTB = port_data;
291  }
292 
293  if (mcu_port == IO_PORTC){
294  PORTC = port_data;
295  }
296 
297  #endif
298 
299  #endif
300 
301 }
302 
303 
304 //************************************************************
305 // IO_read_port function implementation
306 //************************************************************
307 port_size IO_read_port (unsigned char mcu_port){
308 
309  #if (NUMBER_OF_IO_PORTS == 5)
310 
311  if (mcu_port == IO_PORTA){
312  return (PORTA);
313  }
314 
315  if (mcu_port == IO_PORTB){
316  return (PORTB);
317  }
318 
319  if (mcu_port == IO_PORTC){
320  return (PORTC);
321  }
322 
323  if (mcu_port == IO_PORTD){
324  return (PORTD);
325  }
326 
327  if (mcu_port == IO_PORTE){
328  return (PORTE);
329  }
330  #endif
331 
332 
333  #if (NUMBER_OF_IO_PORTS == 3)
334 
335  if (mcu_port == IO_PORTA){
336  return (PORTA);
337  }
338 
339  if (mcu_port == IO_PORTB){
340  return (PORTB);
341  }
342 
343  if (mcu_port == IO_PORTC){
344  return (PORTC);
345  }
346 
347  #endif
348 
349  return (0);
350 }
351 
352 
353 //************************************************************
354 // IO_read_port_bit function implementation
355 //************************************************************
356 port_size IO_read_port_bit (unsigned char mcu_port,port_size bit_mask ){
357 
358  port_size port_data = 0;
359 
360  #if (NUMBER_OF_IO_PORTS == 5)
361 
362  if (mcu_port == IO_PORTA){
363  port_data = PORTA;
364  }
365 
366  if (mcu_port == IO_PORTB){
367  port_data = PORTB;
368  }
369 
370  if (mcu_port == IO_PORTC){
371  port_data = PORTC;
372  }
373 
374  if (mcu_port == IO_PORTD){
375  port_data = PORTD;
376  }
377 
378  if (mcu_port == IO_PORTE){
379  port_data = PORTE;
380  }
381 
382  if ( (port_data & bit_mask) == 0){
383  return (0);
384  } else {
385  return (1);
386  }
387 
388  #endif
389 
390 
391  #if (NUMBER_OF_IO_PORTS == 3)
392 
393  if (mcu_port == IO_PORTA){
394  port_data = PORTA;
395  }
396 
397  if (mcu_port == IO_PORTB){
398  port_data = PORTB;
399  }
400 
401  if (mcu_port == IO_PORTC){
402  port_data = PORTC;
403  }
404 
405  if ( (port_data & bit_mask) == 0){
406  return (0);
407  } else {
408  return (1);
409  }
410 
411  #endif
412 
413 }
414 
415 //************************************************************
416 // IO_enable_pull_up_resitors function implementation
417 //************************************************************
418 void IO_enable_pull_up_resistors (unsigned char mcu_port, port_size resistors_to_enable) {
419 
420  if (mcu_port == IO_PORTB){
421  //Enable the resistor if at least one is required
422  if (resistors_to_enable != 0)
423  PULL_UP_ENABLE_BIT = 0x00;
424  else
425  PULL_UP_ENABLE_BIT = 0x01;
426 
427  #ifdef PULL_UP_SINGLE_BIT_ENABLE
428  PULL_UP_ENABLE_REGISTER_B = resistors_to_enable;
429  #endif
430  }
431 
432 
433 }