Subversion Repositories Code-Repo

Rev

Rev 127 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 127 Rev 128
Line 3... Line 3...
3
#include "xbee.h"
3
#include "xbee.h"
4
#include <string.h>
4
#include <string.h>
5
#include <stdio.h>
5
#include <stdio.h>
6
 
6
 
7
static UART_DATA uart_1_data;
7
static UART_DATA uart_1_data;
-
 
8
static UART_DATA *uart_1_data_p = &uart_1_data;
8
 
9
 
9
void UART1_Init() {
10
void UART1_Init() {
10
    // Configure the hardware USART device
11
    // Configure the hardware USART device
11
    // UART1 TX RC6
12
    // UART1 TX RC6
12
    // UART1 RX RC7
13
    // UART1 RX RC7
Line 32... Line 33...
32
#endif
33
#endif
33
 
34
 
34
    PIE1bits.RC1IE = 1; // Enable RX interrupt
35
    PIE1bits.RC1IE = 1; // Enable RX interrupt
35
 
36
 
36
    // Initialize the buffer that holds UART messages
37
    // Initialize the buffer that holds UART messages
37
    uart_1_data.buffer_in_read_ind = 0;
38
    uart_1_data_p->buffer_in_read_ind = 0;
38
    uart_1_data.buffer_in_write_ind = 0;
39
    uart_1_data_p->buffer_in_write_ind = 0;
39
    uart_1_data.buffer_in_len = 0;
40
    uart_1_data_p->buffer_in_len = 0;
40
    uart_1_data.buffer_in_len_tmp = 0;
41
    uart_1_data_p->buffer_in_len_tmp = 0;
41
}
42
}
42
 
43
 
43
//void uart_2_init() {
44
//void uart_2_init() {
44
//    // Configure the PPS USART ports
45
//    // Configure the PPS USART ports
45
//
46
//
Line 60... Line 61...
60
    unsigned char c;
61
    unsigned char c;
61
    if (PIR1bits.RC1IF) { // Check if data receive flag is set
62
    if (PIR1bits.RC1IF) { // Check if data receive flag is set
62
        c = RCREG1;
63
        c = RCREG1;
63
#ifdef UART1_RX_TO_BUFFER
64
#ifdef UART1_RX_TO_BUFFER
64
        // Save received data into buffer
65
        // Save received data into buffer
65
        uart_1_data.buffer_in[uart_1_data.buffer_in_write_ind] = c;
66
        uart_1_data_p->buffer_in[uart_1_data_p->buffer_in_write_ind] = c;
66
        if (uart_1_data.buffer_in_write_ind == MAXUARTBUF - 1) {
67
        if (uart_1_data_p->buffer_in_write_ind == MAXUARTBUF - 1) {
67
            uart_1_data.buffer_in_write_ind = 0;
68
            uart_1_data_p->buffer_in_write_ind = 0;
68
        } else {
69
        } else {
69
            uart_1_data.buffer_in_write_ind++;
70
            uart_1_data_p->buffer_in_write_ind++;
70
        }
71
        }
71
 
72
 
72
        // Store the last MAXUARTBUF values entered
73
        // Store the last MAXUARTBUF values entered
73
        if (uart_1_data.buffer_in_len_tmp < MAXUARTBUF) {
74
        if (uart_1_data_p->buffer_in_len_tmp < MAXUARTBUF) {
74
            uart_1_data.buffer_in_len_tmp++;
75
            uart_1_data_p->buffer_in_len_tmp++;
75
        } else {
76
        } else {
76
            if (uart_1_data.buffer_in_read_ind == MAXUARTBUF - 1) {
77
            if (uart_1_data_p->buffer_in_read_ind == MAXUARTBUF - 1) {
77
                uart_1_data.buffer_in_read_ind = 0;
78
                uart_1_data_p->buffer_in_read_ind = 0;
78
            } else {
79
            } else {
79
                uart_1_data.buffer_in_read_ind++;
80
                uart_1_data_p->buffer_in_read_ind++;
80
            }
81
            }
81
        }
82
        }
82
 
83
 
83
        // Update buffer size upon receiving newline (0x0D)
84
        // Update buffer size upon receiving newline (0x0D)
84
        if (c == UART1_BREAK_CHAR) {
85
        if (c == UART1_BREAK_CHAR) {
85
            uart_1_data.buffer_in_len = uart_1_data.buffer_in_len_tmp;
86
            uart_1_data_p->buffer_in_len = uart_1_data_p->buffer_in_len_tmp;
86
            uart_1_data.buffer_in_len_tmp = 0;
87
            uart_1_data_p->buffer_in_len_tmp = 0;
87
        }
88
        }
88
#endif
89
#endif
89
#ifdef UART1_RX_TO_XBEE
90
#ifdef UART1_RX_TO_XBEE
90
        XBee_Serial_In(c);
91
        XBee_Serial_In(c);
91
#endif
92
#endif
Line 112... Line 113...
112
//    }
113
//    }
113
//}
114
//}
114
 
115
 
115
void UART1_Send_Interrupt_Handler() {
116
void UART1_Send_Interrupt_Handler() {
116
    // Put remaining data in TSR for transmit
117
    // Put remaining data in TSR for transmit
117
    if (uart_1_data.buffer_out_ind != uart_1_data.buffer_out_len) {
118
    if (uart_1_data_p->buffer_out_ind != uart_1_data_p->buffer_out_len) {
118
        TXREG1 = uart_1_data.buffer_out[uart_1_data.buffer_out_ind];
119
        TXREG1 = uart_1_data_p->buffer_out[uart_1_data_p->buffer_out_ind];
119
        uart_1_data.buffer_out_ind++;
120
        uart_1_data_p->buffer_out_ind++;
120
    } else {
121
    } else {
121
        while (!TXSTA1bits.TRMT); // Wait for last byte to finish sending
122
        while (!TXSTA1bits.TRMT); // Wait for last byte to finish sending
122
        TXSTA1bits.TXEN = 0; // End transmission and disable TX interrupt
123
        TXSTA1bits.TXEN = 0; // End transmission and disable TX interrupt
123
        uart_1_data.buffer_out_ind = 0;
124
        uart_1_data_p->buffer_out_ind = 0;
124
        uart_1_data.buffer_out_len = 0;
125
        uart_1_data_p->buffer_out_len = 0;
125
    }
126
    }
126
}
127
}
127
 
128
 
128
void UART1_WriteS(const rom char *fmt, ...) {
129
void UART1_WriteS(const rom char *fmt, ...) {
129
#ifdef _DEBUG
130
#ifdef _DEBUG
130
    unsigned char i;
131
    unsigned char i;
131
    va_list args;
132
    va_list args;
132
    va_start(args, fmt);
133
    va_start(args, fmt);
133
    vsprintf((char *) uart_1_data.buffer_out, fmt, args);
134
    vsprintf((char *) uart_1_data_p->buffer_out, fmt, args);
134
    va_end(args);
135
    va_end(args);
135
    uart_1_data.buffer_out_len = strlen((char *) uart_1_data.buffer_out);
136
    uart_1_data_p->buffer_out_len = strlen((char *) uart_1_data_p->buffer_out);
136
    uart_1_data.buffer_out_ind = 1;
137
    uart_1_data_p->buffer_out_ind = 1;
137
    for (i = 0; i < uart_1_data.buffer_out_len; i++) {
138
    for (i = 0; i < uart_1_data_p->buffer_out_len; i++) {
138
        TXREG1 = uart_1_data.buffer_out[i];
139
        TXREG1 = uart_1_data_p->buffer_out[i];
139
        Nop();
140
        Nop();
140
        while (!PIR1bits.TX1IF); // Wait for byte to be transmitted
141
        while (!PIR1bits.TX1IF); // Wait for byte to be transmitted
141
    }
142
    }
142
#else
143
#else
143
    va_list args;
144
    va_list args;
144
    while (TXSTA1bits.TXEN); // Wait for previous message to finish sending
145
    while (TXSTA1bits.TXEN); // Wait for previous message to finish sending
145
    va_start(args, fmt);
146
    va_start(args, fmt);
146
    vsprintf((char *) uart_1_data.buffer_out, fmt, args);
147
    vsprintf((char *) uart_1_data_p->buffer_out, fmt, args);
147
    va_end(args);
148
    va_end(args);
148
    uart_1_data.buffer_out_len = strlen((char *) uart_1_data.buffer_out);
149
    uart_1_data_p->buffer_out_len = strlen((char *) uart_1_data_p->buffer_out);
149
    uart_1_data.buffer_out_ind = 1;
150
    uart_1_data_p->buffer_out_ind = 1;
150
    TXREG1 = uart_1_data.buffer_out[0]; // Put first byte in TSR
151
    TXREG1 = uart_1_data_p->buffer_out[0]; // Put first byte in TSR
151
    TXSTA1bits.TXEN = 1; // Begin transmission
152
    TXSTA1bits.TXEN = 1; // Begin transmission
152
#endif
153
#endif
153
}
154
}
154
 
155
 
155
void UART1_WriteB(const char *msg, unsigned char length) {
156
void UART1_WriteB(const char *msg, unsigned char length) {
Line 160... Line 161...
160
        Nop();
161
        Nop();
161
        while (!PIR1bits.TX1IF); // Wait for byte to be transmitted
162
        while (!PIR1bits.TX1IF); // Wait for byte to be transmitted
162
    }
163
    }
163
#else
164
#else
164
    while (TXSTA1bits.TXEN); // Wait for previous message to finish sending
165
    while (TXSTA1bits.TXEN); // Wait for previous message to finish sending
165
    uart_1_data.buffer_out_len = length;
166
    uart_1_data_p->buffer_out_len = length;
166
    uart_1_data.buffer_out_ind = 1;
167
    uart_1_data_p->buffer_out_ind = 1;
167
    for (i = 0; i < length; i++) {
168
    for (i = 0; i < length; i++) {
168
        uart_1_data.buffer_out[i] = msg[i];
169
        uart_1_data_p->buffer_out[i] = msg[i];
169
    }
170
    }
170
    TXREG1 = uart_1_data.buffer_out[0]; // Put first byte in TSR
171
    TXREG1 = uart_1_data_p->buffer_out[0]; // Put first byte in TSR
171
    TXSTA1bits.TXEN = 1; // Begin transmission
172
    TXSTA1bits.TXEN = 1; // Begin transmission
172
#endif
173
#endif
173
}
174
}
174
 
175
 
175
void UART1_WriteC(const unsigned char c) {
176
void UART1_WriteC(const unsigned char c) {
Line 177... Line 178...
177
    TXREG1 = c;
178
    TXREG1 = c;
178
    Nop();
179
    Nop();
179
    while (!PIR1bits.TX1IF);
180
    while (!PIR1bits.TX1IF);
180
#else
181
#else
181
    while (TXSTA1bits.TXEN);
182
    while (TXSTA1bits.TXEN);
182
    uart_1_data.buffer_out_len = 1;
183
    uart_1_data_p->buffer_out_len = 1;
183
    uart_1_data.buffer_out_ind = 1;
184
    uart_1_data_p->buffer_out_ind = 1;
184
    TXREG1 = c;
185
    TXREG1 = c;
185
    TXSTA1bits.TXEN = 1;
186
    TXSTA1bits.TXEN = 1;
186
#endif
187
#endif
187
 
188
 
188
}
189
}
189
 
190
 
190
unsigned char UART1_Buffer_Len() {
191
unsigned char UART1_Buffer_Len() {
191
    return uart_1_data.buffer_in_len;
192
    return uart_1_data_p->buffer_in_len;
192
}
193
}
193
 
194
 
194
/* Reader interface to the UART buffer, returns the number of bytes read */
195
/* Reader interface to the UART buffer, returns the number of bytes read */
195
unsigned char UART1_Read_Buffer(unsigned char *buffer) {
196
unsigned char UART1_Read_Buffer(unsigned char *buffer) {
196
    unsigned char i = 0;
197
    unsigned char i = 0;
197
    while (uart_1_data.buffer_in_len != 0) {
198
    while (uart_1_data_p->buffer_in_len != 0) {
198
        buffer[i] = uart_1_data.buffer_in[uart_1_data.buffer_in_read_ind];
199
        buffer[i] = uart_1_data_p->buffer_in[uart_1_data_p->buffer_in_read_ind];
199
        i++;
200
        i++;
200
        if (uart_1_data.buffer_in_read_ind == MAXUARTBUF - 1) {
201
        if (uart_1_data_p->buffer_in_read_ind == MAXUARTBUF - 1) {
201
            uart_1_data.buffer_in_read_ind = 0;
202
            uart_1_data_p->buffer_in_read_ind = 0;
202
        } else {
203
        } else {
203
            uart_1_data.buffer_in_read_ind++;
204
            uart_1_data_p->buffer_in_read_ind++;
204
        }
205
        }
205
        uart_1_data.buffer_in_len--;
206
        uart_1_data_p->buffer_in_len--;
206
    }
207
    }
207
    return i;
208
    return i;
208
}
209
}
209
210