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_UART.c
Go to the documentation of this file.
1 /*******************************************************************************
2 
3 Autore : Mauro Laurenti
4 Versione : 1.0
5 
6 Created on Date : 25/11/2015
7 Last update : 25/11/2015
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_UART.h"
42 
43 
44 
45 //************************************************************
46 // UART1_open function implementation
47 //************************************************************
48 void UART1_open (unsigned int baud_rate){
49 
50  //IO Pin Initialization
51  UART1_TX_LINE_TRIS = PIN_AS_INPUT;
52  UART1_RX_LINE_TRIS = PIN_AS_INPUT;
53 
54  //Module Initialization
55  TXSTA = 0x00;
56  RCSTA =0x00;
57 
58  //Module funtional settings
59  //Slave
60  TXSTAbits.CSRC = 0;
61  //Asyncrone
62  TXSTAbits.SYNC = 0;
63  //High Boud Rate
64  TXSTAbits.BRGH = 1;
65  // 8 bit mode
66  TXSTAbits.TX9 = 0;
67  RCSTAbits.RX9 = 0;
68 
69  //Continuos Reception
70  RCSTAbits.CREN = 1;
71  RCSTAbits.SREN = 0;
72 
73  //Baud rate
74 
75  switch(baud_rate) {
76  case UART_BAUDRATE_9600:
77  SPBRG = UART_BAUDRATE_9600;
78  break;
79  case UART_BAUDRATE_19200:
80  SPBRG = UART_BAUDRATE_19200;
81  break;
82  case UART_BAUDRATE_57600:
83  SPBRG = UART_BAUDRATE_57600;
84  break;
85  case UART_BAUDRATE_115200:
86  SPBRG = UART_BAUDRATE_115200;
87  break;
88  default:
89 
90  SPBRG = UART_BAUDRATE_9600;
91  }
92 
93  // Enable TX
94  TXSTAbits.TXEN = 1;
95  //Enable RX
96  RCSTAbits.SPEN = 1;
97 
98 }
99 
100 #ifdef UART_MODULE_TYPE_2
101 
102  void UART2_open (unsigned int baud_rate){
103 
104  //IO Pin Initialization
105  UART2_TX_LINE_TRIS = 0x01;
106  UART2_RX_LINE_TRIS = 0x01;
107 
108  //Module Initialization
109  TXSTA2 = 0x00;
110  RCSTA2 =0x00;
111 
112  //Module funtional settings
113  //Slave
114  TXSTA2bits.CSRC = 0;
115  //Asyncrone
116  TXSTA2bits.SYNC = 0;
117  //High Boud Rate
118  TXSTA2bits.BRGH = 1;
119  // 8 bit mode
120  TXSTA2bits.TX9 = 0;
121  RCSTA2bits.RX9 = 0;
122 
123  //Continuos Reception
124  RCSTA2bits.CREN = 1;
125  RCSTA2bits.SREN = 0;
126 
127  //Baud rate
128 
129  switch(baud_rate) {
130  case UART_BAUDRATE_9600:
131  SPBRG2 = UART_BAUDRATE_9600;
132  break;
133  case UART_BAUDRATE_19200:
134  SPBRG2 = UART_BAUDRATE_19200;
135  break;
136  case UART_BAUDRATE_57600:
137  SPBRG2 = UART_BAUDRATE_57600;
138  break;
139  case UART_BAUDRATE_115200:
140  SPBRG2 = UART_BAUDRATE_115200;
141  break;
142  default:
143 
144  SPBRG2 = UART_BAUDRATE_9600;
145  }
146 
147  // Enable TX
148  TXSTA2bits.TXEN = 1;
149  //Enable RX
150  RCSTA2bits.SPEN = 1;
151  }
152 #endif
153 
154 //************************************************************
155 // UART1_write_byte function implementation
156 //************************************************************
157 void UART1_write_byte (unsigned char data) {
158  TXREG = data;
159  while (UART1_TX_busy( ));
160 }
161 
162 #ifdef UART_MODULE_TYPE_2
163  void UART2_write_byte (unsigned char data) {
164  TXREG2 = data;
165  while (UART2_TX_busy( ));
166  }
167 #endif
168 
169 //************************************************************
170 // UART1_write_message function implementation
171 //************************************************************
172 void UART1_write_message(const unsigned char *data){
173 
174  while(*data) {
175  UART1_write_byte(*data);
176  while(UART1_TX_busy());
177  data++;
178  }
179 }
180 
181 #ifdef UART_MODULE_TYPE_2
182  void UART2_write_message(const unsigned char *data){
183 
184  while(*data) {
185  UART2_write_byte(*data);
186  while(UART2_TX_busy());
187  data++;
188  }
189  }
190 #endif
191 
192 //************************************************************
193 // UART1_write_string function implementation
194 //************************************************************
195 void UART1_write_string(unsigned char *data){
196 
197  while(*data) {
198  UART1_write_byte(*data);
199  while(UART1_TX_busy());
200  data++;
201  }
202 }
203 
204 #ifdef UART_MODULE_TYPE_2
205  void UART2_write_string(unsigned char *data){
206 
207  while(*data) {
208  UART2_write_byte(*data);
209  while(UART2_TX_busy());
210  data++;
211  }
212  }
213 #endif
214 
215 //************************************************************
216 // UART1_read_byte function implementation
217 //************************************************************
218 unsigned char UART1_read_byte(void){
219 
220  unsigned char data;
221 
222  data = RCREG;
223 
224  return (data);
225 }
226 
227 #ifdef UART_MODULE_TYPE_2
228  unsigned char UART2_read_byte(void){
229 
230  unsigned char data;
231 
232  data = RCREG2;
233 
234  return (data);
235  }
236 #endif