Subversion Repositories HomeAutomation

Rev

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

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