Subversion Repositories HomeAutomation

Rev

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

Rev Author Line No. Line
25 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
 
24 johboh 13
#include "../Include/stackTasks.h"
14
#include "../Include/uart.h"
15
 
16
#ifdef USE_UART
17
 
18
static char uartBusy(void);
19
 
32 johboh 20
 
24 johboh 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;
35
        TXSTA=0b00100000; // Low BRG speed
36
        RCSTA=0b10010000;
37
        SPBRG = SPBRG_VAL;
38
        // USART RX interrupt
39
        PIR1bits.RCIF=0;
40
        PIE1bits.RCIE=1;
41
        // High interrupt
42
        IPR1bits.RCIP=1;
43
}
44
 
45
 
46
/*
47
*   Function: uartPutc
48
*
49
*   Input:  BYTE c to send on uart
50
*   Output: none
51
*   Pre-conditions: call to uartInit()
52
*   Affects: none
53
*   Depends: none
54
*/
55
void uartPutc(BYTE c)
56
{
57
    // Write the data byte to the USART
58
    TXREG = c;      
59
    while(uartBusy());
60
}
61
 
62
 
63
/*
64
*   Function: uartPuts
65
*
66
*   Input:  Byte array to send on uart
67
*   Output: none
68
*   Pre-conditions: call to uartInit()
69
*   Affects: none
70
*   Depends: none
71
*/
72
void uartPuts(BYTE c[])
73
{
74
    do
75
    {  // Transmit a byte
76
        while(uartBusy());
77
        uartPutc(*c);
78
    } while( *c++ );
79
}
80
 
81
/*
82
*   Function: uartPutrs
83
*
84
*   Input:  Byte array to send on uart (static)
85
*   Output: none
86
*   Pre-conditions: call to uartInit()
87
*   Affects: none
88
*   Depends: none
89
*/
90
void uartPutrs(const rom char *c)
91
{
92
    do
93
    {  // Transmit a byte
94
        while(uartBusy());
95
        uartPutc(*c);
96
    } while( *c++ );
97
}
98
 
99
 
100
 
101
 
102
/*
103
*   Function: uartBusy
104
*
105
*   Input:  none.
106
*   Output: If usart busy or not.
107
*   Pre-conditions: call to uartInit()
108
*   Affects: none
109
*   Depends: none
110
*/
111
char uartBusy(void)
112
{
113
    return !TXSTAbits.TRMT;
114
}
115
 
116
 
117
/*
118
*   Function: uartGet
119
*
120
*   Input:  none
121
*   Output: Byte read
122
*   Pre-conditions: call to uartInit()
123
*   Affects: none
124
*   Depends: none.
125
*/
126
BYTE uartGet(void)
127
{
128
    // Return the received data
129
    return RCREG;  
130
}
131
 
132
 
133
/*
32 johboh 134
*   Function: uartGets
135
*
136
*   Input:  Byte array to read, number of bytes to read and timeout.
137
*   Output: Number of read bytes.
138
*   Pre-conditions: call to uartInit()
139
*   Affects: none
140
*   Depends: none
141
*/
142
BYTE uartGets(BYTE *buffer, BYTE length,unsigned int uart_data_wait)
143
{
144
    unsigned int wait = 0;
145
    BYTE data;
146
 
147
    while(length)
148
    {
149
        while(!uartDataRedy())
150
        {
151
            if(wait < uart_data_wait)
152
                wait++ ;                  /*wait for more data */
153
            else
154
                return(length);           /*Time out- Return words/bytes to be read */
155
        }
156
        wait=0;
157
        data = uartGet();
158
        *buffer = data;
159
        buffer++;              // Increment the string pointer
160
        length--;
161
    }
162
 
163
    return(length);                       /* number of data yet to be received i.e.,0 */
164
 
165
 
166
 
167
 
168
 
169
 
170
}
171
 
172
/*
24 johboh 173
*   Function: uartISR
174
*
175
*   Input:  none
176
*   Output: none
177
*   Pre-conditions: call to uartInit()
178
*   Affects: call to uartParse
179
*   Depends: none.
180
*/
181
void uartISR(void)
182
{
183
    if (PIR1bits.RCIF==1 && PIE1bits.RCIE==1)
184
    {
185
        uartParse(uartGet());
186
        PIR1bits.RCIF=0;
187
    }
188
}
189
 
190
 
32 johboh 191
/*
192
*   Function: uartDataRedy
193
*
194
*   Input:  none.
195
*   Output: If usart data are redy to be read.
196
*   Pre-conditions: call to uartInit()
197
*   Affects: none
198
*   Depends: none
199
*/
200
char uartDataRedy(void)
201
{
202
    if(RCSTAbits.OERR)
203
    {
204
        RCSTAbits.CREN = 0;
205
        RCSTAbits.CREN = 1;
206
    }
207
  return PIR1bits.RCIF;
208
}
209
 
24 johboh 210
#endif