Subversion Repositories HomeAutomation

Rev

Rev 32 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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