View previous topic :: View next topic |
Author |
Message |
pulurumbuluruciu
Joined: 06 Nov 2013 Posts: 27
|
Clearing UART HW 3 Byte Buffer |
Posted: Thu Dec 05, 2013 12:50 pm |
|
|
Hello , I`m trying to clear the UART incoming buffer but It just wont work...I look on the forum and looking on the 18f25k22 datasheet I come up with this code but it still wont work...
Code: |
#byte RCREG = 0xFAE // UART1 RCREG REGISTER
|
Code: |
char NULL;
NULL= RCREG;
NULL= RCREG;
NULL= RCREG;
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Dec 05, 2013 2:58 pm |
|
|
The 18F25K22 has two UARTS. RCREG1 at 0xFAE is for UART1.
Are you sure you are using UART1 (and not UART2) ?
Post a small test program that demonstrates to us how it doesn't work.
Also here in your example, you're using a reserved word in the C
language for a variable name. Look in stddef.h and you'll see that
NULL is defined there. It's not a good idea to use reserved words
for variable names. Use something else like "temp" or "dummy" or
something.
Quote: | char NULL;
NULL= RCREG;
NULL= RCREG;
NULL= RCREG; |
|
|
|
pulurumbuluruciu
Joined: 06 Nov 2013 Posts: 27
|
|
Posted: Thu Dec 05, 2013 4:07 pm |
|
|
Hi , the UART1 TX/RX are connected together via a open collector buffer , UART2 the same, the purpose is acting like a 1 wire databus , preventing the echo getting in the receive buffer.
Code: |
#include <18F25K22.h>
#FUSES NOWDT, WDT128, INTRC_IO, NOFCMEN, NOIESO, NOHFOFST, NOMCLR, NOLVP, NOXINST
#use delay(clock=64000000)
#use rs232(uart1, BITS=8,baud=9600, PARITY=N, STOP=1, STREAM=U1,errors)
#use rs232(uart2, BITS=8,baud=9600, PARITY=N, STOP=1, STREAM=U2,errors)
#byte RCREG1 = 0xFAE
#byte RCREG2 = 0xF74
#int_rda
void RDA_isr(void) {
DISABLE_interrupts(INT_RDA2);
fputc(fgetc(U1),U2);
ENABLE_interrupts(INT_RDA2);
CHAR DUMMY;
DUMMY=RCREG2;
DUMMY=RCREG2;
DUMMY=RCREG2;
}
#int_RDA2
void RDA2_isr(void)
{
DISABLE_interrupts(INT_RDA);
fputc(fgetc(U2),U1);
ENABLE_interrupts(INT_RDA);
CHAR DUMMY;
DUMMY=RCREG1;
DUMMY=RCREG1;
DUMMY=RCREG1;
}
main(){
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
setup_adc_ports( NO_ANALOGS ); // disable analog inputs
enable_interrupts(INT_RDA);
enable_interrupts(INT_RDA2);
enable_interrupts(GLOBAL);
while(true){
}
}
|
The programs just ..jams..if i use Getc() after each putc() to get rid of the echo ,it will work but it will eat to much time. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
pulurumbuluruciu
Joined: 06 Nov 2013 Posts: 27
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Dec 05, 2013 5:05 pm |
|
|
It takes 1.04 ms to finish transmitting the character (at 9600 baud).
After the character is completely transmitted, then it will also be in the
receive buffer (because of your connections). But, you are clearing
the buffer before you have received the character in it. This is why
the getc() method worked. It waits for a character to be in the buffer.
In other words, when you are doing the three reads of RCREG, the
character is still in the process of being shifted out at a slow bitrate
of 104us per bit. |
|
|
pulurumbuluruciu
Joined: 06 Nov 2013 Posts: 27
|
|
Posted: Thu Dec 05, 2013 5:31 pm |
|
|
I understand. I`m gonna use the TXSTA TRMT flag to check if the character has been shifted. Hope will work. Thank you very much |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Thu Dec 05, 2013 5:44 pm |
|
|
Code: |
byte junk;
int8 safety=0;
while(safety<100){
if (kbhit) junk=getc();
delay_ms(50);
safety++;
}
|
so long as 'ERRORS' defined in port open,
never known the code to fail to empty the system out,though it commits
up to 5 seconds to making sure, even at very high baud |
|
|
pulurumbuluruciu
Joined: 06 Nov 2013 Posts: 27
|
|
Posted: Thu Dec 05, 2013 6:05 pm |
|
|
Sorry Asmboy , but I dont understand this part "never known the code " ..could you please rephrase...I`m not such a fine english user. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Dec 05, 2013 6:55 pm |
|
|
asmboy,
He wants it done quickly. His preference would be to do it in 10 usec or
less, but he can't. |
|
|
|