001 #include <xc.h>
002 #include <stdio.h>
003 #include <stdlib.h>
004 
005 char recieveFlag1 = 0;
006 char recieveBuffer1;
007 char recieveFlag2 = 0;
008 char recieveBuffer2;
009 
010 char recieveRingFlag1 = 0;
011 char recieveRingBuffer1[32];
012 char recieveRingFlag2 = 0;
013 char recieveRingBuffer2[32];
014 
015 unsigned char getParity(char data) {
016 
017     data ^= data >> 4;
018     data ^= data >> 2;
019     data ^= data >> 1;
020     return((data & 0x01) ^ 0x01);
021 }
022 
023 // Functions for USART1
024 char ReadUSART1parity(void) {
025     char data;
026 
027     data = RCREG1;
028     // if(RCSTA1bits.OERR1 || RCSTA1bits.FERR1 || (getParity(data) ^ RCSTA1bits.RX9D1)) {
029     if(((getParity(data) ^ RCSTA1) & 0x07) != 0) {  // Detect Errors
030         RCSTA1bits.SPEN1 = 0;
031         NOP();
032         RCSTA1bits.SPEN1 = 1;
033         return(NULL);
034     } else {
035         recieveFlag1 = 1;
036         return(data);
037     }
038 }
039 
040 void WriteUSART1parity(char data) {
041 
042     while(!TXSTA1bits.TRMT);
043     TXSTA1bits.TX9D = getParity(data);
044     TXREG1 = data;
045     return;
046 }
047 
048 void putsUSART1(char *data) {
049 
050     while(*data != NULL) {
051         WriteUSART1parity(*data++);
052     }
053     return;
054 }
055 
056 void setRecieveRingBufferUSART1(void) {
057     char data;
058     static char counter = 0;
059 
060     data = ReadUSART1parity();
061     if(data != NULL) {
062         recieveRingBuffer1[counter++] = data;
063         if(counter >= sizeof(recieveRingBuffer1)) {
064             counter = 0;
065         }
066         if(data == '\n') {
067             recieveRingFlag1 = 1;
068         }
069     }
070     return;
071 }
072 
073 void getsRecieveRingBufferUSART1(char *buffer, unsigned char len) {
074     char i;
075     char data;
076     static char counter = 0;
077 
078     for(i = 0; i < len; i++) {
079         data = recieveRingBuffer1[counter++];
080         if(counter >= sizeof(recieveRingBuffer1)) {
081             counter = 0;
082         }
083         buffer[i] = data;
084         if(data == '\n') {
085             buffer[i + 1] = NULL;
086             recieveRingFlag1 = 0;
087             break;
088         }
089     }
090     return;
091 }
092 
093 // Functions for USART2
094 char ReadUSART2parity(void) {
095     char data;
096     data = RCREG2;
097 
098     // if(RCSTA2bits.OERR2 || RCSTA2bits.FERR2 || (getParity(data) ^ RCSTA2bits.RX9D2)) {
099     if(((getParity(data) ^ RCSTA2) & 0x07) != 0) {  // Detect Errors
100         RCSTA2bits.SPEN2 = 0;
101         NOP();
102         RCSTA2bits.SPEN2 = 1;
103         return(NULL);
104     } else {
105         recieveFlag2 = 1;
106         return(data);
107     }
108 }
109 
110 void WriteUSART2parity(char data) {
111 
112     while(!TXSTA2bits.TRMT);
113     TXSTA2bits.TX9D = getParity(data);
114     TXREG2 = data;
115     return;
116 }
117 
118 void putsUSART2(char *data) {
119 
120     while(*data != NULL) {
121         WriteUSART2parity(*data++);
122     }
123     return;
124 }
125 
126 void setRecieveRingBufferUSART2(void) {
127     char data;
128     static char counter = 0;
129 
130     data = ReadUSART2parity();
131     if(data != NULL) {
132         recieveRingBuffer2[counter++] = data;
133         if(counter >= sizeof(recieveRingBuffer2)) {
134             counter = 0;
135         }
136         if(data == '\n') {
137             recieveRingFlag2 = 1;
138         }
139     }
140     return;
141 }
142 
143 void getsRecieveRingBufferUSART2(char *buffer, unsigned char len) {
144     char i;
145     char data;
146     static char counter = 0;
147 
148     for(i = 0; i < len; i++) {
149         data = recieveRingBuffer2[counter++];
150         if(counter >= sizeof(recieveRingBuffer2)) {
151             counter = 0;
152         }
153         buffer[i] = data;
154         if(data == '\n') {
155             buffer[i + 1] = NULL;
156             recieveRingFlag2 = 0;
157             break;
158         }
159     }
160     return;
161 }