View previous topic :: View next topic |
Author |
Message |
SEO
Joined: 20 Oct 2003 Posts: 2
|
RS-232 Questions: 2 Ports Used, Only 1 Interrupt |
Posted: Mon Oct 20, 2003 12:17 pm |
|
|
I am programming a PIC 18F6520 to send and retrieve information on two serial ports. If I poll the serial ports at 10 times the baud rate I have no problems. However in the real app, I will not have time for this polling cycle. I need an interrupt driven solution so that I can send and retrieve data on both ports. The only problem is that the #INT_RDA interrupt only works with one of the serial ports.
In my test app I can send and receive from both serial ports without problem. Here is the code I used to set up the serial ports:
Code: | #use delay (clock=6144000) //6.144MHz
#use RS232 (baud=2400, xmit=PIN_C6, rcv=PIN_C7, stream=COMM_1)
#use RS232 (baud=9600, xmit=PIN_G1, rcv=PIN_G2, stream=COMM_2)
|
However when I add the rest of the code, I no longer have time to poll the serial ports. Therefore I used the #INT_RDA interrupt. The problem that I have run into is that the interrupt only handles the "COMM_1" serial port.
Is there a way to get an interrupt for the second serial port "COMM_2"?
If there is not, do you have any examples of programs where two serial ports are used to send and retrieve on one PIC?
Thanks for your help,
Rob |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Oct 20, 2003 12:28 pm |
|
|
The second port is
INT_RDA2 & INT_TBE2
per the device header file.
Regards,
Mark |
|
|
SEO
Joined: 20 Oct 2003 Posts: 2
|
Thank You! |
Posted: Mon Oct 20, 2003 12:37 pm |
|
|
Thanks for the quick reply!
Guess I should have looked a bit closer.
Thanks,
Rob |
|
|
Guest
|
|
Posted: Mon Oct 20, 2003 6:10 pm |
|
|
Is it really necessary to put a stream parameter in each #use rs232() function?
How can the PIC system know if #int_rda2 is for the second #use rs232? unless #int_rda is intended for the hardware rs232 of PIC18F452.
Need a light on this...
Mark wrote: | The second port is
INT_RDA2 & INT_TBE2
per the device header file.
Regards,
Mark |
|
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Tue Oct 21, 2003 7:51 am |
|
|
Code: |
#INT_RDA
void isr_COMM1()
{
if(kbhit(COMM1)
{
set_uart_speed(2400);
// your code.....
}
}
#INT_RDA2
void isr_COMM2()
{
if(kbhit(COMM2)
{
set_uart_speed(9600);
// your code.....
}
}
|
Regards,
Humberto |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
|
Posted: Tue Oct 21, 2003 8:26 am |
|
|
Anonymous wrote: | Is it really necessary to put a stream parameter in each #use rs232() function?
How can the PIC system know if #int_rda2 is for the second #use rs232? unless #int_rda is intended for the hardware rs232 of PIC18F452.
Need a light on this...
Mark wrote: | The second port is
INT_RDA2 & INT_TBE2
per the device header file.
Regards,
Mark |
|
There are 2 hardware USARTS that use dedicated pins. These use seperate interupts. If a stream is declared it is associated with a set pins an optionaly a stream. Streams are used to identify what port to use. Without the stream name the last port declared will be used. To use both ports without defining streams the ports must be redefined on the fly before each use. |
|
|
rrb011270
Joined: 07 Sep 2003 Posts: 51
|
|
Posted: Thu Oct 23, 2003 4:50 am |
|
|
Mabuhay!
I have an RF Reader from one rs232 port of PIC18F452 and after I will send it to another rs232 port define on my PIC18F452..
My Problem is that #int_rda2 --> invalid pre-processor directive..
Anybody who can help me? Please also review my code...
maybe a simple code snippet will help...
My application is: using one PIC18452 --> will recieve data at COM_2 and transmit the decoded data to COM_1?
Thnx
Code: | #include <18F452.h>
#fuses HS,NOPROTECT,NOWDT
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=COM_1) // RS485 port --> Console
#use rs232(baud=9600,parity=N,xmit=PIN_B2,rcv=PIN_B3,bits=8,stream=COM_2) // RS232 port --> RF Reader
//#define BUFFER_SIZE 32
#define BUFFER_SIZE 12
byte buffer[BUFFER_SIZE];
byte next_in = 0;
byte next_out = 0;
//#define T_BUFFER_SIZE 64
#define T_BUFFER_SIZE 12
byte t_buffer[T_BUFFER_SIZE];
byte t_next_in = 0;
byte t_next_out = 0;
#include <LCD.C>
char gaRfRxBuffer[BUFFER_SIZE];
char gaRfTxBuffer[BUFFER_SIZE];
#int_rda2
void COM2_isr() {
int t;
buffer[next_in]=getc();
t=next_in;
next_in=(next_in+1) % BUFFER_SIZE;
if(next_in==next_out)
next_in=t; // Buffer full !!
}
#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);
}
#int_tbe
void COM1_isr() {
putc(t_buffer[t_next_out]);
t_next_out=(t_next_out+1) % T_BUFFER_SIZE;
if(t_next_in==t_next_out)
disable_interrupts(int_tbe);
}
void bputc(char c) {
short restart;
int ni;
restart=t_next_in==t_next_out;
t_buffer[t_next_in]=c;
ni=(t_next_in+1) % T_BUFFER_SIZE;
while(ni==t_next_out);
t_next_in=ni;
if(restart)
enable_interrupts(int_tbe);
}
main() {
int8 uI;
lcd_init();
enable_interrupts(global);
enable_interrupts(int_rda);
printf("\fRunning...");
// The program will delay for 10 seconds and then display
// any data that came in during the 10 second delay
do {
delay_ms(10000);
printf("\fBuffered data => \n");
uI = 0;
while(bkbhit){
gaRfRxBuffer[uI];
lcd_putc( bgetc() );
uI++;
//putc( bgetc() );
}
} while (TRUE);
} |
|
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Oct 23, 2003 5:09 am |
|
|
18452 has only one port. See the datasheet. The port is comprises of 1 transmits pin and 1 receive pin. You can create a software UART but it will not have interrupts. If you are only using the transmit of one UART and the receive of another, then just use one UART with interrupts. It doesn't matter that one is 485 and the other is 232. Just keep the baud rate the same.
Regards
Mark |
|
|
rrb011270
Joined: 07 Sep 2003 Posts: 51
|
|
Posted: Thu Oct 23, 2003 11:16 pm |
|
|
Do you have a sample code snippet on this method?
Mark wrote: | 18452 has only one port. See the datasheet. The port is comprises of 1 transmits pin and 1 receive pin. You can create a software UART but it will not have interrupts. If you are only using the transmit of one UART and the receive of another, then just use one UART with interrupts. It doesn't matter that one is 485 and the other is 232. Just keep the baud rate the same.
Regards
Mark |
|
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Oct 24, 2003 7:51 am |
|
|
If all you are doing receiving data on one pin and transmitting it on another, then you should already have all the snippets that you need. It is no different than if you were just transmitting with a single RS232 port. There is no magic here. However, you will have to handle the enable pin of the RS485 transceiver.
Regards,
Mark |
|
|
|