View previous topic :: View next topic |
Author |
Message |
nina
Joined: 20 Apr 2007 Posts: 111
|
int_rda or while |
Posted: Tue Sep 07, 2010 10:15 am |
|
|
I would like to know if it is possible instead of using int_rda use fget or get command inside while loop to read what is coming from serial port.
What are disadvantage of work inside while loop instead of int_rda?
Many thanks
Nina |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Tue Sep 07, 2010 10:27 am |
|
|
If you use a while() loop then your program will be stuck inside it until you decide to exit. The MCU will not be able to perform any other function until it exits. The int_RDA is entered only when a character has been received. You simply stuff that character some place and then exit, leaving the MCU available to do whatever else it can.
So, would you rather be stuck doing one thing or free to do many things?
Ronald |
|
|
nina
Joined: 20 Apr 2007 Posts: 111
|
rda |
Posted: Tue Sep 07, 2010 4:13 pm |
|
|
Thank you for your prompt answer.
As my pic has just one usart...how could I use rda for two usart?
tks |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Tue Sep 07, 2010 4:19 pm |
|
|
You can't..
Interrupts are only for hardware features on the PIC.
If you use 2 #USE RS232 statements,
one can be hardware and the other can will software.
Keep in mind that software uarts are timing intensive, so if you had an INT_RDA, you may have to turn it off while sending/receiving data. _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
nina
Joined: 20 Apr 2007 Posts: 111
|
rda |
Posted: Tue Sep 07, 2010 5:18 pm |
|
|
Íf I use the following code:
#USE RS232(BAUD=9600, XMIT=PIN_B3, RCV=PIN_B4, STREAM=COM_MODEM)
#USE RS232(BAUD=9600, XMIT=PIN_B5, RCV=PIN_B6, STREAM=COM_PC)
and fprintf (stream, xxxxx);
Would I have 2 serials port?
Tks
Nina |
|
|
nina
Joined: 20 Apr 2007 Posts: 111
|
rds x while |
Posted: Tue Sep 07, 2010 7:06 pm |
|
|
I tried use this code but no working.
Could someone explain me why?
Thank you in advanced
#include <16F628A.h>
#include <stdio.h>
#use delay(clock=4000000)
#fuses INTRC_IO, NOLVP, NOWDT, PUT, BROWNOUT
#use rs232(baud=9600, xmit=PIN_B2, rcv=PIN_B1, stream = com_pc)
char receive[4] = {""};
main() {
enable_interrupts(GLOBAL);
enable_interrupts(INT_RDA);
delay_ms(1000);
output_low (PIN_B6);
while(true) {
receive[0]=fgetc(com_pc);
receive[1]=fgetc(com_pc);
receive[2]=fgetc(com_pc);
if ((receive[0] == 1) && (receive[1] == 2) && (receive[2] == 3))
{
output_high(PIN_B6);
}
}
} |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
Re: rda |
Posted: Tue Sep 07, 2010 7:31 pm |
|
|
nina wrote: | Íf I use the following code:
#USE RS232(BAUD=9600, XMIT=PIN_B3, RCV=PIN_B4, STREAM=COM_MODEM)
#USE RS232(BAUD=9600, XMIT=PIN_B5, RCV=PIN_B6, STREAM=COM_PC)
and fprintf (stream, xxxxx);
Would I have 2 serials port?
|
Barring the PIC only has one hardware UART and one of the #USE statements above had PIN_XX designations to match that PIC, no.
What you would have there is a single UART that's etched in SILICON on the PIC to take care of your serial needs.
The other one is made up in software by essentially sampling the RX pin at a really fast speed, or using precise timing for sending data on the TX pin.
Software UARTs are very sensitive to timing and therefore have to be used with caution.
Using 2 software UART's in a system with 2 incoming lines where data is random is asking for trouble.
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
Re: rds x while |
Posted: Tue Sep 07, 2010 7:36 pm |
|
|
nina wrote: | I tried use this code but no working.
Could someone explain me why?
Thank you in advanced
#include <16F628A.h>
#include <stdio.h>
#use delay(clock=4000000)
#fuses INTRC_IO, NOLVP, NOWDT, PUT, BROWNOUT
#use rs232(baud=9600, xmit=PIN_B2, rcv=PIN_B1, stream = com_pc)
char receive[4] = {""};
main() {
enable_interrupts(GLOBAL);
enable_interrupts(INT_RDA);
delay_ms(1000);
output_low (PIN_B6);
while(true) {
receive[0]=fgetc(com_pc);
receive[1]=fgetc(com_pc);
receive[2]=fgetc(com_pc);
if ((receive[0] == 1) && (receive[1] == 2) && (receive[2] == 3))
{
output_high(PIN_B6);
}
}
} |
For one, you are enabling interrupts for RDA with no #IN_RDA service routine. You can't do that.
Next, in your example:
Code: |
receive[0]=fgetc(com_pc);
receive[1]=fgetc(com_pc);
receive[2]=fgetc(com_pc);
if ((receive[0] == 1) && (receive[1] == 2) && (receive[2] == 3))
{
output_high(PIN_B6);
}
|
Also, do you want the ASCII keys "1", "2" and "3"?? If so, that's very different from the numbers 1, 2 and 3 (look at an ASCII chart for 0x01, 0x02 and 0x03. It's not the same as the typed numbers on your keyboard when transmitted via Asynchronous RS232.)
There's a lot of examples on how to write serial receivers on this forum in the code examples section as well as the CCS code examples. I suggest studying them. You're close - but not quite there yet. _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
nina
Joined: 20 Apr 2007 Posts: 111
|
rda |
Posted: Wed Sep 08, 2010 1:13 pm |
|
|
tks for your help..
what i am trying to do is when press number 1 2 and 3 turn on a led attached at port_B6.
What do i need search on this forum? because i am trying to use inside the while loop instead of int_rda.
Thank you
Nina |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
Re: rda |
Posted: Wed Sep 08, 2010 1:43 pm |
|
|
nina wrote: | tks for your help..
what i am trying to do is when press number 1 2 and 3 turn on a led attached at port_B6.
What do i need search on this forum? because i am trying to use inside the while loop instead of int_rda.
|
1, 2 and 3 on your keyboard are not the same as a stored variable, 0x01, 0x02 and 0x03.
http://www.cs.utk.edu/~pham/ascii.html is one of many online ASCII charts/tables that will show you that
ASCII 1 == 0x31
ASCII 2 == 0x32
ASCII 3 == 0x33
So with that, the"numbers" you want to detect in your loop are 0x31, 0x32 and 0x33.
You can either write, if ( c == 0x031 ) OR if (c == '1'). (or you can use decimal as well)
If you want to insist that 1, 2 and 3 and 3 be typed in sequence, then you can either write a state machine to step through the sequence with a bad response resetting the state machine...
Or you can append the received chars to a string where on receiving 3 chars use strcmp() to check the string matching "123" or not.
It's up to you.
I could write it for you -- but then I'd take away the fun of you learning how to do it yourself.
Cheers,
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
nina
Joined: 20 Apr 2007 Posts: 111
|
rda |
Posted: Wed Sep 08, 2010 5:11 pm |
|
|
Thanks everyone for help me on this.
I followed your advice and it is working.
Code: |
main() {
enable_interrupts(GLOBAL);
delay_ms(1000);
output_low (PIN_B6);
while(true) {
recebe[0]=fgetc(com_pc);
recebe[1]=fgetc(com_pc);
recebe[2]=fgetc(com_pc);
if ((recebe[0] == '1') && (recebe[1] == '2') && (recebe[2] == '3'))
{
output_high(PIN_B6);
}
}
}
|
One question. If I use int_rda, can I specify which serial I am reading or writing?
For example
Code: |
receive[0]=fgetc(com_pc);
receve [0] = fgetc(com_pic)
|
Both instruction inside int_rda loop.
Thank you again.
nina |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
Re: rda |
Posted: Wed Sep 08, 2010 6:09 pm |
|
|
nina wrote: | thanks everyone for help me on this..
I followed your advise and it is working
main() {
enable_interrupts(GLOBAL);
|
Again, if you're not using any interrupts, you don't need the enable_interrupts(GLOBAL) --- it's best to just remove it until you have an interrupt routine defined.
Quote: |
delay_ms(1000);
output_low (PIN_B6);
while(true) {
recebe[0]=fgetc(com_pc);
recebe[1]=fgetc(com_pc);
recebe[2]=fgetc(com_pc);
if ((recebe[0] == '1') && (recebe[1] == '2') && (recebe[2] == '3'))
{
output_high(PIN_B6);
}
}
}
one question. If i use int_rda, can i specify wich serial i am reading or writing?
for exemple
receive[0]=fgetc(com_pc);
receve [0] = fgetc(com_pic)
both instruction inside int_rda loop.
thank you again
nina |
No.
You can only have an interrupt for a hardware based UART and the service routine must only apply to that UART (in case you use a PIC that has 2. Then there's INT_RDA and INT_RDA2 available)
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
nina
Joined: 20 Apr 2007 Posts: 111
|
rda |
Posted: Wed Sep 08, 2010 7:19 pm |
|
|
Thank you again for your help.
So...how is it possible use
Code: |
#use rs232(baud=9600, xmit=PIN_B2, rcv=PIN_B1, stream = com_pc)
#use rs232(baud=9600, xmit=PIN_B5, rcv=PIN_B4, stream = com_pic) |
with a 16f628 (there is just one uart)?
Can I play with fgetc(com_pc) and fgetc(com_pic) just out of int_rda?
tks
nina |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
Re: rda |
Posted: Wed Sep 08, 2010 8:15 pm |
|
|
nina wrote: | Thank you again for your help.
So...how is it possible use
Code: |
#use rs232(baud=9600, xmit=PIN_B2, rcv=PIN_B1, stream = com_pc)
#use rs232(baud=9600, xmit=PIN_B5, rcv=PIN_B4, stream = com_pic) |
with a 16f628 (there is just one uart)?
|
Because CCS figure out that the 16F628 has a single hardware uart and it's pins are connected to B1 and B2. So it will implement the code needed to drive that single hardware UART.
Quote: |
Can I play with fgetc(com_pc) and fgetc(com_pic) just out of int_rda?
|
What do you mean "just out of INT_RDA"??
You're not understanding that INT_RDA is the interrupt handler for the one hardware UART on that particular PIC.
When the hardware UART has a byte, it signals the user by firing an interrupt... and the whole IRQ process begins.
With software, there is no "hardware bit" that automatically fires to signal that a character has been received.
I'm getting the impression that all this is currently beyond your scope of understanding. I think a lot of experimentation will help you the most.
Go ahead and test out these permutations while examining the resulting .LST file (which will assist your learning assembly -- which is good knowledge for any microcontroller software programmer).
I'm guessing you're going to figure out a lot by just seeing what works and what doesn't.
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
nina
Joined: 20 Apr 2007 Posts: 111
|
rda |
Posted: Thu Sep 09, 2010 3:51 am |
|
|
tks again for give direction on this.
I am searching about the following code
Code: |
#use rs232(baud=9600, xmit=PIN_B2, rcv=PIN_B1, stream = com_pc)
#use rs232(baud=9600, xmit=PIN_B5, rcv=PIN_B4, stream = com_pic) |
to understand that, what do I need study?
Can I use the piece of code just with pics that has 2 uarts?
tks
nina |
|
|
|