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_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_38400:
83  SPBRG = UART_BAUDRATE_38400;
84  break;
85  case UART_BAUDRATE_57600:
86  SPBRG = UART_BAUDRATE_57600;
87  break;
88  case UART_BAUDRATE_115200:
89  SPBRG = UART_BAUDRATE_115200;
90  break;
91  default:
92 
93  SPBRG = UART_BAUDRATE_9600;
94  }
95 
96  // Enable TX
97  TXSTAbits.TXEN = 1;
98  //Enable RX
99  RCSTAbits.SPEN = 1;
100 
101 }
102 
103 #ifdef UART_MODULE_TYPE_2
104 
105  void UART2_open (unsigned int baud_rate){
106 
107  //IO Pin Initialization
108  UART2_TX_LINE_TRIS = 0x01;
109  UART2_RX_LINE_TRIS = 0x01;
110 
111  //Module Initialization
112  TXSTA2 = 0x00;
113  RCSTA2 =0x00;
114 
115  //Module funtional settings
116  //Slave
117  TXSTA2bits.CSRC = 0;
118  //Asyncrone
119  TXSTA2bits.SYNC = 0;
120  //High Boud Rate
121  TXSTA2bits.BRGH = 1;
122  // 8 bit mode
123  TXSTA2bits.TX9 = 0;
124  RCSTA2bits.RX9 = 0;
125 
126  //Continuos Reception
127  RCSTA2bits.CREN = 1;
128  RCSTA2bits.SREN = 0;
129 
130  //Baud rate
131 
132  switch(baud_rate) {
133  case UART_BAUDRATE_9600:
134  SPBRG2 = UART_BAUDRATE_9600;
135  break;
136  case UART_BAUDRATE_19200:
137  SPBRG2 = UART_BAUDRATE_19200;
138  break;
139  case UART_BAUDRATE_38400:
140  SPBRG2 = UART_BAUDRATE_38400;
141  break;
142  case UART_BAUDRATE_57600:
143  SPBRG2 = UART_BAUDRATE_57600;
144  break;
145  case UART_BAUDRATE_115200:
146  SPBRG2 = UART_BAUDRATE_115200;
147  break;
148  default:
149 
150  SPBRG2 = UART_BAUDRATE_9600;
151  }
152 
153  // Enable TX
154  TXSTA2bits.TXEN = 1;
155  //Enable RX
156  RCSTA2bits.SPEN = 1;
157  }
158 #endif
159 
160 //************************************************************
161 // UART1_write_byte function implementation
162 //************************************************************
163 void UART1_write_byte (unsigned char data) {
164  TXREG = data;
165  while (UART1_TX_busy( ));
166 }
167 
168 #ifdef UART_MODULE_TYPE_2
169  void UART2_write_byte (unsigned char data) {
170  TXREG2 = data;
171  while (UART2_TX_busy( ));
172  }
173 #endif
174 
175 //************************************************************
176 // UART1_write_message function implementation
177 //************************************************************
178 void UART1_write_message(const unsigned char *data){
179 
180  while(*data) {
181  UART1_write_byte(*data);
182  while(UART1_TX_busy());
183  data++;
184  }
185 }
186 
187 #ifdef UART_MODULE_TYPE_2
188  void UART2_write_message(const unsigned char *data){
189 
190  while(*data) {
191  UART2_write_byte(*data);
192  while(UART2_TX_busy());
193  data++;
194  }
195  }
196 #endif
197 
198 //************************************************************
199 // UART1_write_string function implementation
200 //************************************************************
201 void UART1_write_string(unsigned char *data){
202 
203  while(*data) {
204  UART1_write_byte(*data);
205  while(UART1_TX_busy());
206  data++;
207  }
208 }
209 
210 #ifdef UART_MODULE_TYPE_2
211  void UART2_write_string(unsigned char *data){
212 
213  while(*data) {
214  UART2_write_byte(*data);
215  while(UART2_TX_busy());
216  data++;
217  }
218  }
219 #endif
220 
221 //************************************************************
222 // UART1_read_byte function implementation
223 //************************************************************
224 unsigned char UART1_read_byte(void){
225 
226  unsigned char data;
227 
228  data = RCREG;
229 
230  return (data);
231 }
232 
233 #ifdef UART_MODULE_TYPE_2
234  unsigned char UART2_read_byte(void){
235 
236  unsigned char data;
237 
238  data = RCREG2;
239 
240  return (data);
241  }
242 #endif