|
|
View previous topic :: View next topic |
Author |
Message |
Ricar
Joined: 08 Apr 2013 Posts: 1
|
receive a string by the EUSART |
Posted: Tue Apr 09, 2013 12:59 am |
|
|
Hello, I have a problem when I try to receive a string via serial port PIC. What happens to me is this:
P1
What I try is from the PIC transmitter send the word "hello" to the EUSART2 the PIC18F25K22 and then send the same PIC EUSART1 this back to COM2.
What I think is that for some reason the receive buffer of the micro hardware EUSART PIC18F25K22 collapses and only load 2 data, the two data can receive in both records. The way to avoid overburdening the buffer is read each time new data arrives and the buffer is empty. The way to read the buffer is to "getc (), gets (), getchar (), etc," that I do. But for some reason only sends two data, the first two data of the word "HELLO". The compiler is PCWH and is PROTEUS simulator, the code is in CCS.
P2
If I send "HELLO" separately and with a minimum delay of 50 ms, go right
Took time with this and I do not know how to do it
thanks
Sorry, but I do not know how to upload files
Here's the code:
P1 ( no funciona )
Transmitter:
Code: |
#include <16F876.h>
#FUSES XT,NOWDT
#use delay(clock=4MHZ)
#use rs232(baud=19200, xmit=pin_c6, rcv=pin_c7,bits=8)
void main()
{
printf("HOLA");
}
|
PIC18F25K22
Code: |
#include <18F25K22.h>
#fuses NOWDT,NOBROWNOUT,NOPBADEN,NOIESO,NOFCMEN
#use delay(internal=8MHz)
#use rs232(baud=19200,xmit=pin_b6,rcv=pin_b7,UART1,stream=EUSART1)
#use rs232(baud=19200,xmit=pin_c6,rcv=pin_c7,UART2,stream=EUSART2)
int i;
void main()
{
setup_oscillator(OSC_8MHZ|OSC_STATE_STABLE);
setup_adc_ports(NO_ANALOGS);
set_tris_b(0xBF);
set_tris_c(0xBF);
// Estabilizar los puertos
fprintf(EUSART1,0x00);
fprintf(EUSART2,0x00);
delay_ms(100);
char dato;
while(true){
if(kbhit(EUSART1))
{
dato=getc(EUSART1);
fprintf(EUSART2,"%s",dato);
}
if(kbhit(EUSART2))
{
dato=getc(EUSART2);
fprintf(EUSART1,"%c",dato);
}
}
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Tue Apr 09, 2013 3:32 am |
|
|
There is only two characters of hardware buffering.
That is all.
To use more, use code like EX_SISR.c, which shows how to implement an interrupt driven receive buffer.
Your slave waits for 100mSec at the start, so the whole four characters will have come, and only two characters will have been kept.
Secondly, repeat the mantra fifty times:
"I must always have 'ERRORS' enabled, when using a hardware UART, unless I am adding the extra code to handle errors myself".
This is a stupidity of CCS, that it should _default_ to adding ERRORS when the hardware UART is used. Without this the UART can never recover from an overrun, and will be hung.
As a general comment, move variable declarations to the top of the code sections. C _requires_ this. Later languages do allow declarations to be inline, and CCS does not complain, but it has a habit of giving random problems when variables are declared mid block, which will come to haunt you later. So get into the habit of following the traditional C standard.
Best Wishes |
|
|
|
|
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
|