|
|
View previous topic :: View next topic |
Author |
Message |
litoman
Joined: 04 Nov 2005 Posts: 3
|
Multiple RS232 port |
Posted: Tue Jan 03, 2006 6:16 am |
|
|
I need two serial port in my proyect but the problem is that when i use #int_RDA the pic hang up.It run into the interrupt routine all time and never go out.It seems like a INT_RDA flag not cleared.Here is my code.What is wrong?.
Code: |
#include <18F252.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES HS //High speed Osc (> 4mhz)
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOOSCSEN //Oscillator switching is disabled, main oscillator is source
#FUSES BROWNOUT //Reset when brownout detected
#FUSES BORV45 //Brownout reset at 4.5V
#FUSES PUT //Power Up Timer
#FUSES NOSTVREN //Stack full/underflow will not cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOCPD //No EE protection
#FUSES NOCPB //No Boot Block code protection
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#use delay(clock=20000000)
#use rs232(baud=2400,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=VIEJO)
#use rs232(baud=9600,parity=N,xmit=PIN_C5,rcv=PIN_B0,bits=8,stream=NUEVO)
|
Main c
Code: |
#include "c:\Pic\Interfaz Paneles\18F252\Prueba Puertos\INTERFAZ COMUNICACIONES_V1_5.h"
char caracter,caracter2;
#priority EXT,RDA
#define BUFFER_SIZE 20
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;
#int_RDA
RDA_isr()
{
int t;
buffer[next_in]=fgetc(VIEJO);
t=next_in;
next_in=(next_in+1) % BUFFER_SIZE;
if(next_in==next_out)
next_in=t; // Buffer lleno !!
}
#define bkbhit (next_in!=next_out)
BYTE bgetc() {
BYTE c;
while(!bkbhit) ;
c=buffer[next_out];
next_out=(next_out+1) % BUFFER_SIZE;
return(c);
}
#define BUFFER_SIZE2 16
BYTE buffer2[BUFFER_SIZE2];
BYTE next_in2 = 0;
BYTE next_out2 = 0;
/************* INTERRUPCION DEL PUERTO SERIE NUEVO **********************/
#int_EXT
EXT_isr()
{
int t2;
buffer2[next_in2]=fgetc(NUEVO);
t2=next_in2;
next_in2=(next_in2+1) % BUFFER_SIZE2;
if(next_in2==next_out2)
next_in2=t2; // Buffer lleno !!
}
#define bkbhit2 (next_in2!=next_out2)
BYTE bgetc2() {
BYTE c2;
while(!bkbhit2) ;
c2=buffer2[next_out2];
next_out2=(next_out2+1) % BUFFER_SIZE2;
return(c2);
}
void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
ext_int_edge(H_TO_L);
enable_interrupts(INT_RDA);
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
SET_TRIS_A( 0b00111111 );
SET_TRIS_B( 0xFF );
SET_TRIS_C( 0b10011111 );
delay_ms(100);
fprintf(VIEJO,"VIEJO");
fprintf(NUEVO,"NUEVO");
while(1)
{
if(bkbhit)
{
caracter=bgetc();
fprintf(VIEJO,"%c",caracter);
}
if(bkbhit2)
{
caracter2=bgetc2();
fprintf(NUEVO,"%c",caracter2);
}
}
}
|
I use 3.235 compiler version |
|
|
Storic
Joined: 03 Dec 2005 Posts: 182 Location: Australia SA
|
|
Posted: Tue Jan 03, 2006 6:53 am |
|
|
Have you seen the examples (EX_PBUSM.C and EX_PBUSR.C) as stated from the header description "The program ////
//// also shows how to handle two serial ports within one program."
also try http://www.ccsinfo.com/faq/?5
it is a one wire PBUS and a Serial RS232 setup. It may help.
I will be looking at this type of application in due caurse, I will be setting up a RS485 LAN and Serial RS232 LAN on the same micro (including ethernet)
ANdrew _________________ What has been learnt if you make the same mistake? |
|
|
KenMacfarlane
Joined: 20 Sep 2005 Posts: 23 Location: Glasgow, Scotland, UK
|
did you ever get to the bottom of this? |
Posted: Tue Mar 07, 2006 3:12 am |
|
|
Litoman,
I've got dual com port circular buffer based code, which I thought was working, but I'm finding that rda2 loses chars when rda is busy receiving a 6 char packet. I've been trying to promote rda2 to be the sole high priority interrupt (18f6621, pcwh v3.235) but to no avail - the compiler won't take it. |
|
|
Ttelmah Guest
|
|
Posted: Tue Mar 07, 2006 4:09 am |
|
|
It is important to understand the limits of what can be done.
If you generate a dual serial system, with one hardware, and one software UART, then you are faced with the following choices:
If the serial data arriving on the software UART, has at any point, blocks that exceed two character times for the data on the hardware UART, without a break, then data _will_ be lost on the hardware UART.
If instead you make the hardware UART have priority (you can do this with the 'priority' command, without having to use the hardware priority at all), then instead, data will be lost on the software UART, since except at very low data rates, the time needed to service the hardware UART interrupt will generate too much latency for the software handler.
This is distinct from the situation when dealing with two hardware interrupts (on the 6621). Here it should be perfectly possible to receive data happily on both ports, but the normal interrupt caveats apply. How long does each handler take?. How high is the UART data rate?. Again, with two interrupts declared, the one declared first, will have 'priority' (in the software sense). Handling an interrupt, typically takes a minimum of about 80 instruction times, plus the time in the actual handler routine. Assuming an array access, the latter will probably involve another 40 instruction times. So if the 'byte' interval on one port, is shorter than something around 120 instruction times, and the handler for this is declared first, then if data arrives continuously, it will block anything else from working. If this is shifted to have a lower priority than the other port, the second port will now be serviced, but the odds are that data wll now be lost on the first port...
The timings will get worse, if the routines take longer than the bare minimum time.
The hardware interrupt will allow you to make the second event interrupt the first, but brings the same problem, and adds another caveat. On most chips, not all interrupts can be allocated to the low priority, if hardware priorities are enabled. If a interrupt does not have a priority bt in the interrupt registers, then it defaults to 'high priority'. Most of the chips have at least one interrupt wch this applies to. On your chip, 'INT0', will behave this way. This is not a compiler limitation, but a hardware limitation of the chip. So if you use the INT0 ability, this will end up on the high priority interrupt.
I'd perhaps suggest that it may be possible to get the hardware version to work, but by being a bit more ingenious in the interrupt handling. You would have to consider 'cross linking' the interrupt handlers. So something like:
Code: |
#int_RDA NOCLEAR
void serial_interrupt(void) {
int1 flag=true;
while (flag) {
if (RDAIF) sub_to_getchar_for_this_stream();
else flag=false;
if (RDA2IF) {
sub_to_getchar_for_second_stream();
flag=true;
}
}
}
|
And have the same sort of code on the second interrupt as well (just reverse the order of the two tests).
You will have to declare the two interrupt flag bits (RDAIF, and RDA2IF), and _clear_ these at the start of each of the getchar subroutines.
Now what happens is that on either interrupt, it will service the arriving character, on both serial streams, without leaving the interrupt handler (with the massive time saving this involves). There will be a slight extra 'overhead' for branching to the routines. It'll stay inside the handler, till both streams report 'empty'. Because you take over clearing the interrupt flags (note the 'noclear' in the interrupt declaration), once the data is handled, this prevents the other handler being called at all, if the data has already been dealt with here (and vice versa, when the other handler is called first).
I have used this approach in the past, in another similar situation, and it solved the problem for me. :-)
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
|