Subversion Repositories HomeAutomation

Rev

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

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