* src/pic16/device.c (Pics16[]): added devices 18F2550, 18F4331,
[fw/sdcc] / device / lib / pic16 / libio / usart / uopen.c
1
2 /*
3  * uopen - initialize USART module
4  *
5  * written by Vangelis Rokas, 2004 <vrokas AT otenet.gr>
6  *
7  * Devices implemented:
8  *      PIC18F[24][45][28]
9  *
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  *
25  *
26  * $Id$
27  */
28
29
30 #include <pic18fregs.h>
31
32 #include <usart.h>
33
34 // USART Status Structure
35 extern union USART USART_Status;
36
37 void usart_open(unsigned char config, unsigned int spbrg) wparam
38 {
39   TXSTA = 0;           // Reset USART registers to POR state
40   RCSTA = 0;
41  
42   if(config&0x01)TXSTAbits.SYNC = 1;
43
44   if(config&0x02) {
45     TXSTAbits.TX9 = 1;
46     RCSTAbits.RX9 = 1;
47   }
48
49   if(config&0x04)TXSTAbits.CSRC = 1;
50
51   if(config&0x08)RCSTAbits.CREN = 1;
52   else RCSTAbits.SREN = 1;
53
54   if(config&0x10)TXSTAbits.BRGH = 1;
55   else TXSTAbits.BRGH = 0;
56
57   /* TX interrupts */
58   PIR1bits.TXIF = 0;
59
60   if(config&0x40)PIE1bits.RCIE = 1;
61   else PIE1bits.RCIE = 0;
62
63   /* RX interrupts */
64   PIR1bits.RCIF = 0;
65
66   if(config&0x80)PIE1bits.TXIE = 1;
67   else PIE1bits.TXIE = 0;
68
69   SPBRG = (char)spbrg;
70
71   TXSTAbits.TXEN = 1;
72   RCSTAbits.SPEN = 1;
73 }