View previous topic :: View next topic |
Author |
Message |
bigfish
Joined: 08 Jan 2011 Posts: 18
|
rs232 sends 3 characters only |
Posted: Mon Jan 10, 2011 7:35 am |
|
|
Since I couldnt manage sending strings and converting them to numerical value,I decided to start it from the rough.
I used siow in csc s.I have pic16f877.
Following is the pic code :
Code: |
#include <16F877A.h>
#include <stdlib.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=19200,xmit=PIN_C6,rcv=PIN_C7)
#org 0x1F00,0x1FFF {}
#opt 9
void main()
{
int flag=1;
char ch;
char text[5];
output_high(PIN_B5);
*text="gotit";
while(1)
{
output_high(PIN_D3); //this is LED (on)
puts(text);
delay_ms(250);
ch=getc();
delay_ms(10);
putc(ch);
output_low(PIN_D3); // this is LED (off)
delay_ms(500);
}
|
Now, I open siow (serial port monitorl) and write 'a' as ascii and send.It is OK,pic sends me back 'agotit' and LED is on-off.
I write 'ab' and press send,pic sends me back 'agotit' and in a new line 'bgotit'.Its ok.
I write 'abc' and send.The result is again as expected.
BUT, when I write abcd,pic only sends three lines containing 'agotit' 'bgotit' 'cgotit'.It doesnt recognize 4th character.What may be the reason for this?
Sorry for long question,thanks for your answers. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Mon Jan 10, 2011 7:55 am |
|
|
It looks like you are overflowing the UART buffer. The buffer only holds 2 characters. The first character 'a' is read and immediately processed, but you have some long delays in your processing loop. The next 2 characters come in and are buffered. When the 4th character arrives the UART buffer overflows and generates an error. You don't have any code to handel a UART error.
You should start by looking at the ERRORS option of the #use RS232 command and seeing if it is applicable to your code. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
bigfish
Joined: 08 Jan 2011 Posts: 18
|
|
Posted: Mon Jan 10, 2011 8:22 am |
|
|
Hi, thanks for the reply.
Where can I find its options? Also can't we just empty the buffer after I gather the second character, using built in function? |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Mon Jan 10, 2011 9:46 am |
|
|
The help entry for use RS232 gives a list of the options and a short description of the ERRORS option.
Of course it is best to avoid errors by handling the characters as they come in. Either handle them in the main code, which means no long delays or printing while characters are coming, or use interrupts with a circular buffer to store as many characters as you need (and have RAM for) until you can deal with them. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
|