Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
56 johboh 1
/*********************************************************************
2
 *
3
 *                  UART
4
 *
5
 *********************************************************************
6
 * FileName:        uart.c
7
 *
8
 * Author               Date    Comment
9
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10
 * Johan Böhlin     21-10-06    Original        (Rev 1.0)
11
 ********************************************************************/
12
 
13
#include <uart.h>
14
#include <stackTasks.h>
15
 
16
#ifdef USE_UART
17
 
18
static char uartBusy(void);
19
 
20
 
21
/*
22
*   Function: uartInit
23
*
24
*   Input:  none
25
*   Output: none
26
*   Pre-conditions: none
27
*   Affects: none
28
*   Depends: none
29
*/
30
void uartInit()
31
{
32
        // Configure USART
33
        TRISCbits.TRISC7=1;
34
        TRISCbits.TRISC6=0;
64 johboh 35
        TXSTA=0b00100000;
36
        #ifdef HIGH_BRG
37
            TXSTAbits.BRGH=1; // High BRG speed
38
            BAUDCONbits.BRG16=1;
39
            SPBRGH = SPBRGH_VAL;
40
            SPBRG = SPBRG_VAL;
41
        #else
42
            TXSTAbits.BRGH=0;  // Low BRG speed
43
            SPBRG = SPBRG_VAL;
44
        #endif
45
 
56 johboh 46
        RCSTA=0b10010000;
64 johboh 47
 
56 johboh 48
        // USART RX interrupt
49
        PIR1bits.RCIF=0;
50
        PIE1bits.RCIE=1;
51
        // High interrupt
52
        IPR1bits.RCIP=1;
53
}
54
 
55
 
56
/*
57
*   Function: uartPutc
58
*
59
*   Input:  BYTE c to send on uart
60
*   Output: none
61
*   Pre-conditions: call to uartInit()
62
*   Affects: none
63
*   Depends: none
64
*/
65
void uartPutc(BYTE c)
66
{
67
    // Write the data byte to the USART
68
    TXREG = c;      
69
    while(uartBusy());
70
}
71
 
72
 
73
/*
74
*   Function: uartPuts
75
*
76
*   Input:  Byte array to send on uart
77
*   Output: none
78
*   Pre-conditions: call to uartInit()
79
*   Affects: none
80
*   Depends: none
81
*/
82
void uartPuts(BYTE c[])
83
{
84
    do
85
    {  // Transmit a byte
86
        while(uartBusy());
87
        uartPutc(*c);
88
    } while( *c++ );
89
}
90
 
91
/*
92
*   Function: uartPutrs
93
*
94
*   Input:  Byte array to send on uart (static)
95
*   Output: none
96
*   Pre-conditions: call to uartInit()
97
*   Affects: none
98
*   Depends: none
99
*/
100
void uartPutrs(const rom char *c)
101
{
102
    do
103
    {  // Transmit a byte
104
        while(uartBusy());
105
        uartPutc(*c);
106
    } while( *c++ );
107
}
108
 
109
 
110
 
111
 
112
/*
113
*   Function: uartBusy
114
*
115
*   Input:  none.
116
*   Output: If usart busy or not.
117
*   Pre-conditions: call to uartInit()
118
*   Affects: none
119
*   Depends: none
120
*/
121
char uartBusy(void)
122
{
123
    return !TXSTAbits.TRMT;
124
}
125
 
126
 
127
/*
128
*   Function: uartGet
129
*
130
*   Input:  none
131
*   Output: Byte read
132
*   Pre-conditions: call to uartInit()
133
*   Affects: none
134
*   Depends: none.
135
*/
136
BYTE uartGet(void)
137
{
138
    // Return the received data
139
    return RCREG;  
140
}
141
 
142
 
143
/*
144
*   Function: uartISR
145
*
146
*   Input:  none
147
*   Output: none
148
*   Pre-conditions: call to uartInit()
149
*   Affects: call to uartParse
150
*   Depends: none.
151
*/
152
void uartISR(void)
153
{
154
    if (PIR1bits.RCIF==1 && PIE1bits.RCIE==1)
155
    {
156
        if (RCSTAbits.OERR) { RCSTAbits.CREN=0; RCSTAbits.CREN=1; }
157
 
158
        uartParse(uartGet());
159
        PIR1bits.RCIF=0;
160
    }
161
}
162
 
163
 
164
/*
165
*   Function: uartDataRedy
166
*
167
*   Input:  none.
168
*   Output: If usart data are redy to be read.
169
*   Pre-conditions: call to uartInit()
170
*   Affects: none
171
*   Depends: none
172
*/
173
char uartDataRedy(void)
174
{
175
    if(RCSTAbits.OERR)
176
    {
177
        RCSTAbits.CREN = 0;
178
        RCSTAbits.CREN = 1;
179
    }
180
  return PIR1bits.RCIF;
181
}
182
 
183
#endif