Subversion Repositories HomeAutomation

Rev

Rev 25 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 25 Rev 32
Line 14... Line 14...
14
#include "../Include/uart.h"
14
#include "../Include/uart.h"
15
 
15
 
16
#ifdef USE_UART
16
#ifdef USE_UART
17
 
17
 
18
static char uartBusy(void);
18
static char uartBusy(void);
-
 
19
 
19
 
20
 
20
/*
21
/*
21
*   Function: uartInit
22
*   Function: uartInit
22
*
23
*
23
*   Input:  none
24
*   Input:  none
Line 44... Line 45...
44
 
45
 
45
/*
46
/*
46
*   Function: uartPutc
47
*   Function: uartPutc
47
*
48
*
48
*   Input:  BYTE c to send on uart
49
*   Input:  BYTE c to send on uart
49
*   Output: none
50
*   Output: none
50
*   Pre-conditions: call to uartInit()
51
*   Pre-conditions: call to uartInit()
51
*   Affects: none
52
*   Affects: none
52
*   Depends: none
53
*   Depends: none
53
*/
54
*/
54
void uartPutc(BYTE c)
55
void uartPutc(BYTE c)
55
{
56
{
56
    // Write the data byte to the USART
57
    // Write the data byte to the USART
57
    TXREG = c;      
58
    TXREG = c;      
58
    while(uartBusy());
59
    while(uartBusy());
59
}
60
}
60
 
61
 
61
 
62
 
62
/*
63
/*
63
*   Function: uartPuts
64
*   Function: uartPuts
64
*
65
*
65
*   Input:  Byte array to send on uart
66
*   Input:  Byte array to send on uart
66
*   Output: none
67
*   Output: none
67
*   Pre-conditions: call to uartInit()
68
*   Pre-conditions: call to uartInit()
68
*   Affects: none
69
*   Affects: none
69
*   Depends: none
70
*   Depends: none
70
*/
71
*/
71
void uartPuts(BYTE c[])
72
void uartPuts(BYTE c[])
72
{
73
{
73
    do
74
    do
Line 79... Line 80...
79
 
80
 
80
/*
81
/*
81
*   Function: uartPutrs
82
*   Function: uartPutrs
82
*
83
*
83
*   Input:  Byte array to send on uart (static)
84
*   Input:  Byte array to send on uart (static)
84
*   Output: none
85
*   Output: none
85
*   Pre-conditions: call to uartInit()
86
*   Pre-conditions: call to uartInit()
86
*   Affects: none
87
*   Affects: none
87
*   Depends: none
88
*   Depends: none
88
*/
89
*/
89
void uartPutrs(const rom char *c)
90
void uartPutrs(const rom char *c)
Line 91... Line 92...
91
    do
92
    do
92
    {  // Transmit a byte
93
    {  // Transmit a byte
93
        while(uartBusy());
94
        while(uartBusy());
94
        uartPutc(*c);
95
        uartPutc(*c);
95
    } while( *c++ );
96
    } while( *c++ );
96
}
97
}
97
 
98
 
98
 
99
 
99
 
100
 
100
 
101
 
101
/*
102
/*
102
*   Function: uartBusy
103
*   Function: uartBusy
103
*
104
*
104
*   Input:  none.
105
*   Input:  none.
105
*   Output: If usart busy or not.
106
*   Output: If usart busy or not.
106
*   Pre-conditions: call to uartInit()
107
*   Pre-conditions: call to uartInit()
Line 126... Line 127...
126
{
127
{
127
    // Return the received data
128
    // Return the received data
128
    return RCREG;  
129
    return RCREG;  
129
}
130
}
130
 
131
 
-
 
132
 
-
 
133
/*
-
 
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
}
131
 
171
 
132
/*
172
/*
133
*   Function: uartISR
173
*   Function: uartISR
134
*
174
*
135
*   Input:  none
175
*   Input:  none
Line 145... Line 185...
145
        uartParse(uartGet());
185
        uartParse(uartGet());
146
        PIR1bits.RCIF=0;
186
        PIR1bits.RCIF=0;
147
    }
187
    }
148
}
188
}
149
 
189
 
-
 
190
 
-
 
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
}
150
 
209
 
151
#endif
210
#endif