|
|
View previous topic :: View next topic |
Author |
Message |
defoxe
Joined: 30 Jan 2015 Posts: 8
|
PIC18F4550 and two UART's |
Posted: Fri May 12, 2017 3:44 pm |
|
|
First I was trying to use my PIC18F4550 with UART and USB cdc, it's working but sometimes
UART rx is hanging up. Its working nice after PIC reset.
A6 GSM --(115200bps UART)-- PIC --(115200bps USB CDC)-- PC
So now I am trying one hardware UART and one software.
Ok, its working nice:
Code: |
#include <18F4550.h>
#fuses INTRC_IO, NOWDT, PUT ,BROWNOUT, NOLVP, NOMCLR
#use delay(internal = 8MHz)
#use rs232(uart1, baud=115200, xmit=PIN_C6, rcv=PIN_C7, parity=N, bits=8, stream = ua1)
#use rs232(baud=115200, xmit=PIN_D2, rcv=PIN_D3, stream = uapc)
//#define sc(x) {int8 i; for(i=0; i<sizeof((x));i++) { putc((x)[i], uapc); }}
void main(void) {
setup_oscillator(OSC_8MHZ|OSC_INTRC);
unsigned char c;
unsigned char z;
while (TRUE) {
if (kbhit(ua1)) {
z=getc(ua1);
putc(z,uapc);
}
if (kbhit(uapc)){
putc('O',uapc);
putc('K',uapc);
putc('\r',uapc);
putc('\n',uapc);
}
}
}
|
But this response for key from PC is a little bit crazy, because
Code: |
printf(uapc,"OK\r\n");
|
is not working this time... I am not sure where is a problem. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9272 Location: Greensville,Ontario
|
|
Posted: Fri May 12, 2017 4:39 pm |
|
|
With every HW UART you must always have 'errors' in the use rs232(...options...)
Whenever you use 2 UARTs buy a PIC with two HW UARTs unless the ONLY thing the SW UART has to to is TRANSMIT, never ever receive data
also whenever you have serio I/O you should( must?) use 'buffers'. CCS supplies an example ( ex_sisr.c) for an interrupt driven serial buffer. I know it works for 2 HW UARTS at 115K200 all dy long without any missed data.
I gave up on the 4550 years ago, went to the 18F46K22 as it has 2 HW UARTS, lost of mem, etc. You can buy TTL<>USB modules for $1 and eliminate 100% of the problems that the 4550 has.
Jay |
|
|
defoxe
Joined: 30 Jan 2015 Posts: 8
|
|
Posted: Sun May 14, 2017 3:51 am |
|
|
temtronic wrote: | With every HW UART you must always have 'errors' in the use rs232(...options...) |
Yes! It was this.
With "ERRORS" and manual set "RECEIVE_BUFFER" everything is working. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19592
|
|
Posted: Sun May 14, 2017 12:47 pm |
|
|
It's worth understanding.
The fault is happening, because when you transmit "OK\r\n", this takes four character times.
The hardware buffering on ual, can handle just under two characters. You have already not serviced 'ua1' for a character time (while you send the character out on uapc), so by the time you get to the end of all this, the hardware receive buffer overflows.
If this happens, the UART goes into an 'error' state, and will stop receiving.
The 'ERRORS' keyword, tells the compiler to add code to the UART handler, to automatically clear errors when they occur. It should always be used with the hardware UART, unless you are adding your own error handler code.
So, with ERRORS, the UART won't get hung. However you will still have lost the characters that are received to cause the overrun.
Provided there are gaps in the data (so the UART can catch up sometimes), adding the RECEIVE_BUFFER increases how many characters can be received before this happens. |
|
|
|
|
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
|