View previous topic :: View next topic |
Author |
Message |
pilar
Joined: 30 Jan 2008 Posts: 197
|
Multiplexing multiple #INT_RDA |
Posted: Thu Jul 23, 2009 1:33 pm |
|
|
Hi, I am using the PIC18F452. This PIC has only one UART, and I need to capture the strings of three peripherals, they have different speeds (KB) and number of characters, the idea is multiplex the UART input by hardware, but the problem is that I will have three different routines interruption (#INT_RDA) .
How I can select the routine interruption according to peripheral selected |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jul 23, 2009 1:58 pm |
|
|
You can only have one #int_rda routine in your program. You can
use if() statements inside the routine to execute different code.
Do you have some external hardware to select between the 3 rs232
signals coming into the PIC ?
You can use the setup_uart() function to change the baud rate of the
hardware UART from within the program. |
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Thu Jul 23, 2009 2:22 pm |
|
|
Hi, PCM programmer, yes I have a external hardware to select between the 3 rs232, I am use a multiplexer
My problem is how to do to select the correct subroutine in #int_rda routine?
sorry can you give an example? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jul 23, 2009 2:27 pm |
|
|
How do you control the external multiplexor circuit ? Probably, you use
two PIC i/o pins to select one of 3 different inputs.
In the code that controls the multiplexor circuit, you can set a global
variable that identifies the input signal which is currently in use.
Also setup the correct baud rate for the input signal.
Then read the global variable inside the #int_rda routine, and use if()
statements to execute the correct code. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Thu Jul 23, 2009 3:35 pm |
|
|
PCM programmer wrote: | Then read the global variable inside the #int_rda routine, and use if() statements to execute the correct code. |
Or a switch/case statement setup.
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Thu Jul 23, 2009 3:40 pm |
|
|
Hi PCM programmer, I am use a multiplexor and use two output of PIC, the PD0 PD1 to select the address of channel on the multiplexer.
Here is a sample how I want to do my code, I'm going on the right ?
Code: | #include <18F452.h>
#include <string.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,PUT
#use delay(clock=20000000)
#use rs232(xmit=PIN_C6, rcv=PIN_C7)
#define AD0 PIN_D0
#define AD1 PIN_D1
#define High output_high
#define Low output_low
int8 UART_Modo = 0;
#INT_RDA
void serial_isr() { // Serial Interrupt
if ( UART_Modo = 1){
...............
}
if ( UART_Modo = 2){
...........
}
if ( UART_Modo = 3){
.............
}
}
void process0(){
...........
}
void process1(){
...........
}
void process2(){
...........
}
void Modo_Multimer(){
UART_Modo = 1;
setup_uart(9600);
enable_interrupts(global);
enable_interrupts(int_rda);
Low (AD0); // Address selector Modo1
Low (AD1);
process1();
disable_interrupts(global);
disable_interrupts(int_rda);
}
void Modo_Ampmeter(){
UART_Modo = 2;
setup_uart(4800);
enable_interrupts(global);
enable_interrupts(int_rda);
Low (AD0); // Address selector Modo2
High (AD1);
process2();
disable_interrupts(global);
disable_interrupts(int_rda);
}
void Modo_Omhmeter(){
UART_Modo = 3;
setup_uart(19200);
enable_interrupts(global);
enable_interrupts(int_rda);
High (AD0); // Address selector Modo3
High (AD1);
process3();
disable_interrupts(global);
disable_interrupts(int_rda);
}
void main() {
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
delay_ms(500);
while (TRUE){
Modo_Multimer();
Modo_Ampmeter();
Modo_Omhmeter();
}
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jul 23, 2009 3:44 pm |
|
|
You didn't show most of the code inside the #int_rda routine.
But make sure that you don't enable Global interrupts inside that routine. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Thu Jul 23, 2009 3:47 pm |
|
|
I think I just need to tell my bud at Microchip that we need a PIC with like 8 serial ports or something. ;) hahahaha
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Thu Jul 23, 2009 3:58 pm |
|
|
I do not understand
but here is my code:
Code: | #INT_RDA
void serial_isr() {
if ( UART_Modo = 1){
rcvchar=0x00;
rcvchar=getc();
addcbuff(rcvchar);
}
if ( UART_Modo = 2){
int t;
buffer[next_in]=getc();
t=next_in;
next_in=(next_in+1) % BUFFER_SIZE;
if(next_in==next_out)
next_in=t;
}
if ( UART_Modo = 3){
Add_buffrec(getc());
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jul 23, 2009 4:06 pm |
|
|
Quote: | if ( UART_Modo = 1){ |
You need to use == for a test.
Also, what if 'UART_Modo' somehow becomes a number other than 1, 2
or 3 ? Your program will lock up, because it did not get the character
in the #int_rda routine.
It's better to call getc() one time, at the start of the #int_rda routine.
Put it in a local variable, such as 'c'. Then use 'c' in your if() code. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Thu Jul 23, 2009 4:10 pm |
|
|
pilar wrote: | I do not understand |
Like this:
Code: | #INT_RDA
void serial_isr() {
unsigned int t, c;
c = getc();
switch (UART_Modo) {
case 1: addcbuff(c);
break;
case 2: buffer[next_in]=c;
t=next_in;
next_in=(next_in+1) % BUFFER_SIZE;
if(next_in==next_out) {
next_in=t;
}
break;
case 3: Add_buffrec(c);
break;
}
} |
I just edited this to reflect what PCM Programmer said. _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Thu Jul 23, 2009 4:17 pm |
|
|
Can I use a break?
Code: | # INT_RDA
vacĂo serial_isr () (
buffer_temp = getc ();
if (UART_Modo == 1) (
Rcvchar = 0x00;
Rcvchar = buffer_temp;
Addcbuff (rcvchar);
)
if (UART_Modo == 2) (
Int t;
Buffer [next_in] = buffer_temp;
T = next_in;
Next_in = (next_in +1) BUFFER_SIZE%;
If (next_in == next_out)
next_in = t;
)
if (UART_Modo == 3) (
Add_buffrec (buffer_temp);
)
break;
)
|
|
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Thu Jul 23, 2009 4:18 pm |
|
|
not like that - no.
and you can say, buffer_temp = getc (); without defining buffer_temp first.
Before that line you should have
Code: |
unsigned int buffer_temp;
|
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
|