|
|
View previous topic :: View next topic |
Author |
Message |
TokTok
Joined: 22 Jan 2014 Posts: 35
|
|
Posted: Fri Feb 07, 2014 8:00 am |
|
|
HI ttelmah,
Thanks for the reply. I've cross checked the polarities of the capacitors (I went through this guide http://www.wjoe.com/capacitorinfo.htm just to make sure I was cross checking the polarities in the right manner) and I can confirm the polarities are correct.
I've got all the five capacitors as expected.
Apologies for not giving the marking on the marking on the MAX232 IC the marking is an A - http://datasheets.maximintegrated.com/en/ds/MAX220-MAX249.pdf
I've also got another line driver/receiver chip from EXAR i e. http://www.farnell.com/datasheets/1562947.pdf which I'm thinking I give a try.
As per the datasheet it can perform the role of a MAX232. Do you think it is worth a try?
Thanks,
Alex. |
|
|
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Fri Feb 07, 2014 8:07 am |
|
|
The MAX232A use a 100nF capacitor it does not work with a 1uF, or did not in my application.
Regards |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9244 Location: Greensville,Ontario
|
|
Posted: Fri Feb 07, 2014 8:49 am |
|
|
question: Why do you have a 2nd MAX232 ? A single MAX232 can be used for both serial ports as it has 2 xmt and 2 rcv sections.
jay |
|
|
TokTok
Joined: 22 Jan 2014 Posts: 35
|
|
Posted: Fri Feb 07, 2014 10:23 am |
|
|
Hi ezflyr,
I'm trying the Code: | fprintf(PCTWO, "%c : %u\n\r", RcvChar, RxState); | at the moment and will get back with the outcome.
Thanks,
Alex |
|
|
TokTok
Joined: 22 Jan 2014 Posts: 35
|
|
Posted: Fri Feb 07, 2014 10:53 am |
|
|
Hi ezflyr,
If it were me, the next step would be to get the serial interrupt routine working correctly. I would not bother to use the LEDs for this stage of my troubleshooting as you'll get much more information by sending diagnostic messages to your 2nd serial port. I'd still be sending the PIC a single character at a time to see how the code recognizes it, and processes it inside the ISR. Remember, the goal should be to recognize the 'Start' character, and then start buffering all incoming data until the 'Stop' character is received.
I've applied the Code: | fprintf(PC, "%c : %u\n\r", RcvChar, RxState); | outside the switch statement as seen in the code below.
Code: |
#include<18F2580.h>
#fuses XT,PUT,NOBROWNOUT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=14400, parity=N, xmit=PIN_C6, rcv=PIN_C7, STREAM=PC, ERRORS)
#use rs232(baud=14400, parity=N, xmit=PIN_A2, Stream=PCTWO)
// ISR vars
#define BUFFER_SIZE 32
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;
int analysis_state = 0, RxState = 0, t;
#int_rda
void serial_isr() { // Use interrupt to read data from Rx pin and store the data in a buffer
// Test 1
output_toggle(SYNC_LED);
char RcvChar;
RcvChar = getc();
// Change RxState based on incoming character
switch (RxState) {
Case 0:
if (RcvChar == 'A')
RxState++; // 'A' or start character received, so RxState = 1
break;
Case 1: // We've received the correct start character, so now we wait for the EOT character i.e. '\r'...
if (RcvChar == 'Z')
RxState++; // "EOT" i.e. 'Z' character received, so RxState = 2
break;
}
fprintf(PCTWO, "%c : %u\n\r", RcvChar, RxState);
// If RxState > 0 we have a valid character from the PC, and we place it in the circular buffer!
if (RxState > 0) {
buffer[next_in] = RcvChar;
t=next_in;
next_in =(next_in + 1) % BUFFER_SIZE;
if(next_in==next_out)
next_in=t; // Buffer full !!
}
// We have now received the entire PC message, and now need to reset the RxState state machine...
if (RxState == 2) {
RxState = 0;
}
}
void main() {
enable_interrupts(int_rda);
enable_interrupts(global);
}
|
For simplicity as opposed to using the actual starting and ending chars i.e. <CR> <LF> I'm using 'A' as my starting character and 'Z' as my ending character. I fed the PIC with the single characters as follows string 'A' 'B' 'C' 'Z' and the outcome of the fprintf() was as follows:
A..B.C...Z... A..B..C..Z...
Using the output_toggle() at the very top of the ISR toggled the LED whenever I send across a character.
I assume my outcome should be ABCZABCZ and forgive me if I made an error in my code or what I'm about to ask but why the printing of dots? Assuming I was printing for example the buffer[next_in] then I would understand as it might mean i was buffer elements which might not be populated.
Edit:
Your effort is made a little bit more complicated by the fact that <CR> and <LF> are used at both ends of the data transmission, but that is entirely manageable. Another technique would be to look for the sequence of 'U', 'C', 'A', etc. with the state machine, and then only exit when the next <CR> is received. In other words, lots of ways to solve this problem.
Thanks for the above suggestions. Indeed I think there are several ways the problem can be dealt with.
Regards,
Alex. |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Fri Feb 07, 2014 3:14 pm |
|
|
Hi TokTok,
I'm a little bit confused. You wrote:
Quote: |
the outcome of the fprintf() was as follows:
A..B.C...Z... A..B..C..Z...
|
but, nowhere in your code is there a fprintf statement in that format? I believe that you are giving us your interpretation of what is being received without showing us the actual character and RxState values being output by the diagnostics in your code. Is that right?
If so, you are making it very hard to try to help you, and I'm about ready to throw in the towel......
My guess is that you still have a hardware problem if you are (apparently) receiving some additional spurious characters! There is no point in any further debugging of your code unless you are sure your hardware is 100% working correctly.....
John |
|
|
TokTok
Joined: 22 Jan 2014 Posts: 35
|
|
Posted: Mon Feb 10, 2014 3:31 am |
|
|
Hi ezflyr,
Thanks for the reply. I'm definitely not showing you my interpretation of which characters are being sent back by the PIC but showing you exactly what the PIC is giving back through the software UART.
I mention the above as I can confirm the printf() statement I've placed after the switch statement in the ISR is the statement printing out the A..B..C...Z... and this happens after I send across the single characters 'A' 'B' 'C' 'Z'.
Regarding a potential hardware issue I've utilised the 5 capacitors that ttelmah proposed hence I'm finding it difficult to see as to why there would be a hardware issue. In terms of eliminating a potential hardware error I'm thinking of purchasing and utilising an RS232 converter board such as the Mikro Elektornika board here http://www.mikroe.com/add-on-boards/communication/max232/
I believe this might help in eliminating any hardware issue that might be causing any occurrence of spurious characters.
Sorry if this approach or my question sound trivial but based on your experiences would this be of assistance in that sense?
I'm going to have a look at the code once again and the functioning of the fprintf() statement.
Regards,
Alex. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19540
|
|
Posted: Mon Feb 10, 2014 5:20 am |
|
|
Lets step to certain things:
1) The point about the MAX232, is that the original 'standard' MAX232, Specifies 8v out of the charge pump at 15mA load, rising to 10v at no load. That your chip is giving much less than this, says 'something wrong', and may be a hint to the core problem.
2) Many of the later low power chips have drivers with lower drops, and generate less from the charge pumps. The Atmel 'near equivalent' chip you pointed to, only generates about +/-7v, but it's drivers can still give +/-5v out from this lower voltage.
3) Now, data 'between' bytes, really says that the line is not being held low properly when in the 'idle' state. Obvious reasons would be things like the grounds not being properly connected between kit, and obviously if the ground you were using for your test point, when checking the voltages on the MAX232, was not actually the ground of the MAX232, this could explain everything. A simple poor connection somewhere between the PC ground (on the RS232 plug), the Maxim ground, and the PIC ground, could explain everything. Beware also of 'through' grounds. It is electrically bad practice to have grounds running 'through' kit. Much better practice to have a 'master' ground point, and run grounds radially from this. This particularly becomes important when currents increase.
4) As a separate comment, you talk about two MAX232's. You do realise that each of these chips has two drivers and two receivers?. |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Mon Feb 10, 2014 7:30 am |
|
|
Hi TokTok,
Code: |
fprintf(PCTWO, "%c : %u\n\r", RcvChar, RxState);
|
This fprintf statement is supposed to print the RcvChar character, then a space, then a colon, then a space, and then the RxState value. I'd expect something like this to be displayed on your Hyperterminal screen:
Quote: |
A : 1
B : 1
C : 1
Z : 2
|
Even if you were receiving 'spurious' characters, I'd expect the format to be more like I show than you show. Are you saying that the fprintf is printing *exactly* as you've shown us?
John |
|
|
TokTok
Joined: 22 Jan 2014 Posts: 35
|
|
Posted: Tue Feb 11, 2014 9:00 am |
|
|
Hi Ttelmah,
Thanks for the reply.
2) Many of the later low power chips have drivers with lower drops, and generate less from the charge pumps. The Atmel 'near equivalent' chip you pointed to, only generates about +/-7v, but it's drivers can still give +/-5v out from this lower voltage.
I've attempted utilising the other chip I've got i.e. the EXAR SP232AEP which you correctly point out since it is a low power chip generates around +/-7v (specifically the EXAR SP232AEP generates +/-6.5v). One of its output drivers i.e. pin 7 delivers around +/-6.5v with no load and the other output driver (i.e. pin 14 which is connected to the DB9 connector) delivers +/-6.5v with no load and around +/-5.0v with load.
I've tested this by only sending single characters as seen in the code below and I can note there's a drop with regards to the voltage level. Regarding the connection of the 4 capacitors to the EXAR SP232AEP and the connection between the EXAR SP232AEP to the PIC, I connected them in similar fashion to the MAX232 including the capacitors I used with the MAX232.
Code: |
#include<18F2580.h>
#fuses XT,PUT,NOBROWNOUT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=14400, parity=N, xmit=PIN_C6, rcv=PIN_C7, STREAM=PC, ERRORS)
void main() {
while(1) {
fputc('A', PC); // this works OK
delay_ms(1000);
}
}
|
As for the EXAR SP232AEP pins the voltage levels are as follows without load and then with load:
without load:
Pin 1: 0.53v
Pin 2: 6.60v
Pin 3: -3.30v
Pin 4: 4.72v
Pin 5: -3.60v
Pin 6: -6.96v
Pin 7: -6.96v
Pin 8: 0.00v
Pin 9: 4.99v
Pin 10: 3.72v
Pin 11: 3.77v
Pin 12: 4.99v
Pin 13: -9.92v
Pin 14: -6.60v
Pin 15: 0.00v
Pin 16 : 4.98v
with load:
Pin 1 - 0.52v
Pin 2: 6.64v
Pin 3: -3.28v
Pin 4: 4.84v
Pin 5: -3.76v
Pin 6: -6.96v
Pin 7: -6.95v
Pin 8: 0.00v
Pin 9: 4.98v
Pin 10: 3.61v
Pin 11: 4.98v
Pin 12: 4.99v
Pin 13: -9.92v
Pin 14: -5.62v
Pin 15: 0.00v
Pin 16: 4.98v
3) Now, data 'between' bytes, really says that the line is not being held low properly when in the 'idle' state. Obvious reasons would be things like the grounds not being properly connected between kit, and obviously if the ground you were using for your test point, when checking the voltages on the MAX232, was not actually the ground of the MAX232, this could explain everything. A simple poor connection somewhere between the PC ground (on the RS232 plug), the Maxim ground, and the PIC ground, could explain everything. Beware also of 'through' grounds. It is electrically bad practice to have grounds running 'through' kit. Much better practice to have a 'master' ground point, and run grounds radially from this. This particularly becomes important when currents increase.
I've always ensured a common ground with regards to my test point, the chip, the ground connected to the DB9 connector and the PIC. I've got a central ground rail whereby I to some extent radially run the ground.
4) As a separate comment, you talk about two MAX232's. You do realise that each of these chips has two drivers and two receivers?.
For now I've dropped the 2nd MAX232 and as mentioned earlier I'm giving a try the EXAR SP232AEP.
In mentioning the above I attempted resending characters using the code from my last post but the outcome was the same i.e. as Ttelmah mentioned there are random characters appearing in between the bytes.
Alex. |
|
|
TokTok
Joined: 22 Jan 2014 Posts: 35
|
|
Posted: Tue Feb 11, 2014 9:01 am |
|
|
Hi ezflyr,
As you mentioned in your post I'm also expecting a response in the following manner
Quote: |
A : 1
B : 1
C : 1
Z : 2
|
but what appears on my terminal program is around the lines
Alex |
|
|
TokTok
Joined: 22 Jan 2014 Posts: 35
|
|
Posted: Thu Feb 20, 2014 11:28 am |
|
|
Hi all,
I was able to overcome my RS232 hardware issues by purchasing the following add on board from Mikro Elektronika - http://www.mikroe.com/downloads/get/1276/max232_manual_v100.pdf.
I can confirm I'm not receiving spurious characters on my terminal program like earlier either using fputc() or fprintf().
I've conducted various tests i.e. sending single character when using both the hardware and software UARTs see in the code below bits labelled Test 1 and Test 2.
In mentioning this I have a small issue with regards to the uncommented fprintf() statement in my ISR i.e. what I labelled test 3 in my code below which is when I send a string with three characters e.g. 'ASZ' I get the following response which is correct:
Quote: |
RcvChar: A - RxState: 1
RcvChar: S - RxState: 1
RcvChar: Z - RxState: 2
|
When I send a string with more than 3 characters then only the first 3 characters e.g. 'AQWZ' or 'AQWERTYZ' get displayed i.e.
Quote: |
RcvChar: A - RxState: 1
RcvChar: Q - RxState: 1
RcvChar: W - RxState: 1
|
Before even attempting to send strings I sent indicidual chars e.g. 'A' 'Q' 'W' 'E' 'R' 'T' 'Y' 'Z' and the expected outcome was obtained i.e.
Quote: |
RcvChar: A - RxState: 1
RcvChar: Q - RxState: 1
RcvChar: W - RxState: 1
RcvChar: E - RxState: 1
RcvChar: R - RxState: 1
RcvChar: T - RxState: 1
RcvChar: Y - RxState: 1
RcvChar: Z - RxState: 2
|
Any ideas as to why only the first 3 characters are printed when sending a string?
My code:
Code: |
#include<18F2580.h>
#fuses XT,PUT,NOBROWNOUT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, parity=N, xmit=PIN_C6, rcv=PIN_C7, STREAM=HWAREUART, ERRORS)
#use rs232(baud=9600, parity=N, xmit=PIN_A2, Stream=SWAREUART)
// Test chars
//char TestChar1, TestChar2
// LED vars
#define DATA_LED PIN_B2
#define SYNC_LED PIN_B3
// ISR vars
#define BUFFER_SIZE 32
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;
// Main vars
int RxState = 0, t;
#int_rda
void serial_isr() { // Use interrupt to read data from Rx pin and store the data in a buffer
output_toggle(SYNC_LED);
char RcvChar;
RcvChar = getc();
// Test 1
//fputc(RcvChar, HWAREUART);
//fputc(RcvChar, SWAREUART);
// Test 2
//fprintf(HWAREUART, "Received value is: %c\n\r", RcvChar);
//fprintf(SWAREUART, "Received value is: %c\n\r", RcvChar);
// Change RxState based on incoming character
switch (RxState) {
Case 0:
if (RcvChar == 'A')
RxState++; // 'A' or start character received, so RxState = 1
break;
Case 1: // We've received the correct start character, so now we wait for the EOT character i.e. 'Z'...
if (RcvChar == 'Z')
RxState++; // "EOT" i.e. 'Z' character received, so RxState = 2
break;
}
// Test 3
fprintf(SWAREUART, "RcvChar: %c - RxState: %u\n\r", RcvChar, RxState);
// If RxState > 0 we have a valid character from the PC, and we place it in the circular buffer!
if (RxState > 0) {
buffer[next_in] = RcvChar;
t=next_in;
next_in =(next_in + 1) % BUFFER_SIZE;
if(next_in == next_out)
next_in=t; // Buffer full !!
}
// We have now received the entire PC message, and now need to reset the RxState state machine...
if (RxState == 2) {
RxState = 0;
}
}
#define bkbhit (next_in != next_out)
BYTE bgetc() {
BYTE c;
while(!bkbhit);
c=buffer[next_out];
next_out=(next_out+1);
return(c);
}
void main() {
enable_interrupts(int_rda);
enable_interrupts(global);
}
|
Thanks |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Thu Feb 20, 2014 12:56 pm |
|
|
TokTok,
I don't see an obvious reason for this behavior, so I assume that it's related to the fprintf causing 'blocking' of the incoming characters when you send them in rapid succession. Basically, the UART buffer is full, and no additional characters can be received while the fprintf statement is executing. Remember I said that the fprintf was a temporary troubleshooting tool for use when debugging the reception of individual characters? Well, now you know why!
At this point, remove the fprintf from the ISR, and put a loop in Main() that tests 'bkbhit', and then goes out and fetches characters from your receive buffer using bgetc(). Now, you should see all the characters that are being sent!
Also, your terminology might get you in trouble. You say you are transmitting a 'string', and what you mean is a collection of characters sent in quick succession. The term 'string' in most programming languages has a specific definition, and your useage is likely to cause confusion.
John |
|
|
TokTok
Joined: 22 Jan 2014 Posts: 35
|
|
Posted: Fri Feb 21, 2014 3:53 am |
|
|
Hi ezflyr,
Thanks for the reply. Sorry for my incorrect use of string. I'm used to saying string as I also write Java code.
I've added your suggestions in my code as seen below and when I send the characters individually e.g. 'A' 'Q' 'W' 'E' 'Z' I can see all of them being printed i.e.
Quote: |
RcvCharFromBuffer: A
RcvCharFromBuffer: Q
RcvCharFromBuffer: W
RcvCharFromBuffer: E
RcvCharFromBuffer: Z
|
My code:
Code: |
#include<18F2580.h>
#fuses XT,PUT,NOBROWNOUT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, parity=N, xmit=PIN_C6, rcv=PIN_C7, STREAM=HWAREUART, ERRORS)
#use rs232(baud=9600, parity=N, xmit=PIN_A2, Stream=SWAREUART)
// Test chars
//char TestChar1, TestChar2
// LED vars
int i, j;
#define DATA_LED PIN_B2
#define SYNC_LED PIN_B3
// ISR vars
#define BUFFER_SIZE 32
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;
// Main vars
int analysis_state = 0, RxState = 0, t, analysis_t;
#define ANALYSIS_BUFFER_SIZE 32
BYTE analysis_buffer[ANALYSIS_BUFFER_SIZE];
BYTE analysis_next_in = 0;
BYTE analysis_next_out = 0;
//
char RcvCharFromBuffer;
#int_rda
void serial_isr() { // Use interrupt to read data from Rx pin and store the data in a buffer
output_toggle(SYNC_LED);
char RcvChar;
RcvChar = getc();
// Test 1
//fputc(RcvChar, HWAREUART);
//fputc(RcvChar, SWAREUART);
// Test 2
//fprintf(HWAREUART, "Received value is: %c\n\r", RcvChar);
//fprintf(SWAREUART, "Received value is: %c\n\r", RcvChar);
// Change RxState based on incoming character
switch (RxState) {
Case 0:
if (RcvChar == 'A')
RxState++; // 'A' or start character received, so RxState = 1
break;
Case 1: // We've received the correct start character, so now we wait for the EOT character i.e. 'Z'...
if (RcvChar == 'Z')
RxState++; // "EOT" i.e. 'Z' character received, so RxState = 2
break;
}
// Test 3
//fprintf(SWAREUART, "RcvChar: %c - RxState: %u\n\r", RcvChar, RxState);
// If RxState > 0 we have a valid character from the PC, and we place it in the circular buffer!
if (RxState > 0) {
buffer[next_in] = RcvChar;
// Test 4
//fprintf(SWAREUART, "buffer[next_in]: %c - next_in: %u - RxState: %u\n\r", buffer[next_in], next_in, RxState);
t=next_in;
next_in =(next_in + 1) % BUFFER_SIZE;
if(next_in == next_out)
next_in=t; // Buffer full !!
}
// We have now received the entire PC message, and now need to reset the RxState state machine...
if (RxState == 2) {
/*for (j = 0; j < BUFFER_SIZE; j++) {
fprintf(HWAREUART, "buffer[j]: %c - j: %u - RxState: %u\n\r", buffer[j], j, RxState);
}*/
RxState = 0;
}
}
#define bkbhit (next_in != next_out)
BYTE bgetc() {
BYTE c;
while(!bkbhit);
c=buffer[next_out];
next_out=(next_out+1);
return(c);
}
void main() {
enable_interrupts(int_rda);
enable_interrupts(global);
do {
while(bkbhit) {
RcvCharFromBuffer = bgetc();
// Test
fprintf(SWAREUART, "RcvCharFromBuffer: %c\n\r", RcvCharFromBuffer);
}
} while (TRUE);
}
|
Now I when I send a set of characters e.g. 'AQWEZ' to the above code my terminal program tells me framing error. I read page 11 of the following AN774 document to further understand the error http://ww1.microchip.com/downloads/en/AppNotes/00774a.pdf.
I figure this is the point whereby I engage my second state logic which will copy the characters from the first buffer into a second buffer. I'll then attempt to print the contents of the second buffer once the end character has been read.
The outcome of this is as follows:
Quote: |
analysis_buffer: A - j: 0 - analysis_state: 2
analysis_buffer: Q - j: 1 - analysis_state: 2
analysis_buffer: W - j: 2 - analysis_state: 2
analysis_buffer: E - j: 3 - analysis_state: 2
analysis_buffer: R - j: 4 - analysis_state: 2
analysis_buffer: T - j: 5 - analysis_state: 2
analysis_buffer: Y - j: 6 - analysis_state: 2
analysis_buffer: Z - j: 7 - analysis_state: 2
analysis_buffer: . - j: 8 - analysis_state: 2
analysis_buffer: 3 - j: 9 - analysis_state: 2
analysis_buffer: . - j: 10 - analysis_state: 2
analysis_buffer: . - j: 11 - analysis_state: 2
analysis_buffer: . - j: 12 - analysis_state: 2
analysis_buffer: - j: 13 - analysis_state: 2
analysis_buffer: - j: 14 - analysis_state: 2
analysis_buffer: H - j: 15 - analysis_state: 2
analysis_buffer: . - j: 16 - analysis_state: 2
analysis_buffer: X - j: 17 - analysis_state: 2
analysis_buffer: . - j: 18 - analysis_state: 2
analysis_buffer: . - j: 19 - analysis_state: 2
analysis_buffer: . - j: 20 - analysis_state: 2
analysis_buffer: . - j: 21 - analysis_state: 2
analysis_buffer: | - j: 22 - analysis_state: 2
analysis_buffer: . - j: 23 - analysis_state: 2
analysis_buffer: . - j: 24 - analysis_state: 2
analysis_buffer: Q - j: 25 - analysis_state: 2
analysis_buffer: . - j: 26 - analysis_state: 2
analysis_buffer: t - j: 27 - analysis_state: 2
analysis_buffer: . - j: 28 - analysis_state: 2
analysis_buffer: . - j: 29 - analysis_state: 2
analysis_buffer: . - j: 30 - analysis_state: 2
analysis_buffer:
- j: 31 - analysis_state: 2
|
I sent several packets with a collection of characters i.e. 'AQWEZ' and the above quote was the outcome every single time.
Please see my code below:
Code: |
#include<18F2580.h>
#fuses XT,PUT,NOBROWNOUT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, parity=N, xmit=PIN_C6, rcv=PIN_C7, STREAM=HWAREUART, ERRORS)
#use rs232(baud=9600, parity=N, xmit=PIN_A2, Stream=SWAREUART)
// Test chars
//char TestChar1, TestChar2
// LED vars
int i, j;
#define DATA_LED PIN_B2
#define SYNC_LED PIN_B3
// ISR vars
#define BUFFER_SIZE 32
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;
// Main vars
int analysis_state = 0, RxState = 0, t, analysis_t;
#define ANALYSIS_BUFFER_SIZE 32
BYTE analysis_buffer[ANALYSIS_BUFFER_SIZE];
BYTE analysis_next_in = 0;
BYTE analysis_next_out = 0;
//
char RcvCharFromBuffer;
#int_rda
void serial_isr() { // Use interrupt to read data from Rx pin and store the data in a buffer
output_toggle(SYNC_LED);
char RcvChar;
RcvChar = getc();
// Test 1
//fputc(RcvChar, HWAREUART);
//fputc(RcvChar, SWAREUART);
// Test 2
//fprintf(HWAREUART, "Received value is: %c\n\r", RcvChar);
//fprintf(SWAREUART, "Received value is: %c\n\r", RcvChar);
// Change RxState based on incoming character
switch (RxState) {
Case 0:
if (RcvChar == 'A')
RxState++; // 'A' or start character received, so RxState = 1
break;
Case 1: // We've received the correct start character, so now we wait for the EOT character i.e. 'Z'...
if (RcvChar == 'Z')
RxState++; // "EOT" i.e. 'Z' character received, so RxState = 2
break;
}
// Test 3
//fprintf(SWAREUART, "RcvChar: %c - RxState: %u\n\r", RcvChar, RxState);
// If RxState > 0 we have a valid character from the PC, and we place it in the circular buffer!
if (RxState > 0) {
buffer[next_in] = RcvChar;
// Test 4
//fprintf(SWAREUART, "buffer[next_in]: %c - next_in: %u - RxState: %u\n\r", buffer[next_in], next_in, RxState);
t=next_in;
next_in =(next_in + 1) % BUFFER_SIZE;
if(next_in == next_out)
next_in=t; // Buffer full !!
}
// We have now received the entire PC message, and now need to reset the RxState state machine...
if (RxState == 2) {
/*for (j = 0; j < BUFFER_SIZE; j++) {
fprintf(HWAREUART, "buffer[j]: %c - j: %u - RxState: %u\n\r", buffer[j], j, RxState);
}*/
RxState = 0;
}
}
#define bkbhit (next_in != next_out)
BYTE bgetc() {
BYTE c;
while(!bkbhit);
c=buffer[next_out];
next_out=(next_out+1);
return(c);
}
void main() {
enable_interrupts(int_rda);
enable_interrupts(global);
do {
while(bkbhit) {
RcvCharFromBuffer = bgetc();
switch (analysis_state) {
Case 0: //
if (RcvCharFromBuffer == 'A')
analysis_state++;
break;
Case 1: //
if (RcvCharFromBuffer == 'Z')
analysis_state++;
break;
}
if (analysis_state > 0) {
analysis_buffer[analysis_next_in] = RcvCharFromBuffer;
analysis_t = analysis_next_in;
analysis_next_in = analysis_next_in + 1;
if(analysis_next_in == analysis_next_out)
analysis_next_in = analysis_t; // Buffer full !!
}
if (analysis_state == 2) {
for (j = 0; j < ANALYSIS_BUFFER_SIZE; j++) {
fprintf(SWAREUART, "analysis_buffer: %c - j: %u - analysis_state: %u\n\r", analysis_buffer[j], j, analysis_state);
}
analysis_state = 0;
analysis_next_in = 0;
}
}
} while (TRUE);
}
|
What do you reckon regarding the contents of the second buffer?
Thanks. |
|
|
TokTok
Joined: 22 Jan 2014 Posts: 35
|
|
Posted: Mon Feb 24, 2014 6:55 am |
|
|
HI Guys,
Any ideas regarding my last post as seen above?
Thanks. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|