Page 1 of 1

How to set nTimeout for the RS232 component ?

Posted: Tue Jul 01, 2008 3:45 pm
by WalkOver
Hello the Flowcode company,

I do two electronic cards who must communicate in RS232. it works but not completely. I send 5 bytes on one side and I must receive them in the same order on the other side. The first byte is a detection byte. it doesn't work, I have the impression that the receiver receives the bytes randomly.
Maybe it's come from the macro nTimeout? How to calculate this value?

Thanks in advance !

Re: How to set nTimeout for the RS232 component ?

Posted: Wed Jul 02, 2008 10:08 am
by Benj
Hello

The bytes travelling over the RS232 will always arrive in the order you sent them due to the nature of their transportation on the bus.

The timeout in the receive macro is essentially how long the macro should wait before returning the value 255 representing no incoming byte. If an incoming byte is detected then the macro returns straight away without further delay. However a timeout of 255 means wait forever. There have been a few problems with this infinite timeout as it has the possibility of causing your program to crash or at least appear to crash.

Re: How to set nTimeout for the RS232 component ?

Posted: Wed Jul 02, 2008 7:27 pm
by WalkOver
Thanks Benj,

I hadn't tried the value 255 yet. In the meantime I changed a little bit my program and I set nTimeout to 255 .... and it works much better!

Is it the setting nTimeout or my program? I don't know, I will test it more later.

Thanks again.

Re: How to set nTimeout for the RS232 component ?

Posted: Wed Feb 25, 2009 7:00 pm
by WalkOver
Hello,

I have an other question about nTimeout. For exemple, if i set 20, how many time the PIC wait fo an incoming data ?

Thank you!

Re: How to set nTimeout for the RS232 component ?

Posted: Thu Feb 26, 2009 2:33 pm
by Benj
Hello

If the nTimeout is set to 255 then the macro will wait forever for an incoming byte. Have to be carefull if using this as if an error has occurred on the bus it may cause the system to wait forever for an incoming byte that will never arrive.

If the nTimeout is less then 255 then the macro will check for an incoming byte 256 x the timeout variable. Eg a timeout of 20 will make the component poll the incoming data register 20 x 256 times or until a byte is received.

So if the clock speed is 19.6608MHz then here is the calculation to work out the approximate delay to wait for incoming data.

Program frequency = Instruction speed / 4 = 4.9152MHz
Instruction period = 1 / Instruction speed = 0.20us
Length of timeout = Timeout x 256 x Instruction period = 1.03ms for a timeout of 20.

Using Flowcode 3.6.11 you can use the RXINT interrupt to detect an incoming byte and then in your interrupt routine you can use a byte receive macro with a timeout of 1 and be sure that you will always catch the data as it comes in.

Hope this helps.

Re: How to set nTimeout for the RS232 component ?

Posted: Thu Feb 26, 2009 8:46 pm
by WalkOver
Thank you very much for your (full) answer !

In fact, I receive data from anyone but time to time I lose bytes. I can better estimate the nTimeout value now.

Interruption is very interesting! I got the latest Flowcode release but I don't have that option in the interruption icon. It must be a user interruption user as your examples ?

Good night!

Re: How to set nTimeout for the RS232 component ?

Posted: Fri Feb 27, 2009 10:55 am
by Benj
Hello

Which chip are you using and I will send you the interrupts to add to the end of the FCD file. This will then add the interrupts to the interrupt icon.

Re: How to set nTimeout for the RS232 component ?

Posted: Fri Feb 27, 2009 10:56 am
by Benj
Hello

Which chip are you using and I will send you the interrupts to add to the end of the FCD file. This will then add the RXINT interrupt as an option for the interrupt icon.

Re: How to set nTimeout for the RS232 component ?

Posted: Fri Feb 27, 2009 11:27 am
by WalkOver
Im using 18F6627.

An other thing : I have to switch between two RS232 baudrate. 38400 for Rx data and 19200 for Tx. I just put a C icon with spbrg and I put 64 for 38400 and 129 for 19200. Rx works well but 19200 doesn't.

Any ideas ?

Re: How to set nTimeout for the RS232 component ?

Posted: Fri Feb 27, 2009 11:39 am
by Benj
Hello

Open the FCD file for the 18F6627 located in the Flowcode V3/FCD directory and scroll down to the section starting [Interrupts]. Edit the interrupt count by adding one and then add the RXint interrupt as shown below.. Then paste the [RXINT] section into the end of the file. Restart Flowcode and you should then have the extra interrupt.

[Interrupts]
Count=4
1=TMR0
2=RB0INT
3=PORTB
4=RXINT

Code: Select all

[RXINT]
Name=RXINT
FlagReg=pir1
FlagBit=RCIF
EnReg=pie1
EnBit=RCIE
OptCnt=0
TmrOpt=0
What clock speed are you using and I may be able to help with baud rates.

Re: How to set nTimeout for the RS232 component ?

Posted: Fri Feb 27, 2009 2:25 pm
by WalkOver
Thank you Benj !

My clock speed is 40 Mhz but I think i put the good setting for spbrg because I look into a .C generated by flowcode when using 19200 or 38400 baudrate. Do you think its possible to set different baudrate on the same UART ?

Re: How to set nTimeout for the RS232 component ?

Posted: Mon Mar 09, 2009 2:45 pm
by WalkOver
I try your interupt code but it doesn't work. I don't understand very well interupt yet so I think i make something wrong.

Is it possible to have an example receiving more than one octet ? ( In my program, I have to receive 14 octets )

Thank you so muh !

Re: How to set nTimeout for the RS232 component ?

Posted: Mon Mar 09, 2009 3:02 pm
by Benj
Yes it is possible to change the UART baud rate mid program. Look at the C code generated for the two different baud speeds you wish to use. Then simply use C code blocks to change the register settings mid program. The baud rate values will be shown in the defines section at the top of the C code file and you can see which register they are passed to by looking at the top of the Main C function. I recommend creating a blank program with a single RS232 read / write to view the C code as this will make the code much smaller and easier to interpret.

For the interrupt code to work you will need to do a single RS232 read byte before the interrupt is enabled. This allows any error present on the bus caused by startup etc to be removed removing any potential blockages in the receive pipeline.

If you are always going to receive 14 bytes then you can have 14 read macros inside your interrupt handler macro.

If the number may vary then why not create a simple loop and make the first value you send be equal to the number of following bytes. This way you could read 1 - 256 bytes. To make the program simple its probably going to be easier if you store the incoming bytes into an array so you can use the loop index etc.

Re: How to set nTimeout for the RS232 component ?

Posted: Tue Mar 10, 2009 12:30 pm
by WalkOver
I'm sorry but I need your help....

I try different things but I doesn't work. I test a little program who generate 2 PWM when receiving 6 bytes. Without interupt, it works well. With interup....nothing happen...

I light up a led if the program enter in the interupt macro when receiving incoming bytes but this LED never light up. I try to put a RS232 RX macro before the RS232 RX interupt macro but the pb is always here.

I have this message from the compiler :

Building CASM file
Serious Warning: Possible sw stack corruption, function 'FCD_RS2320_ReceiveRS232Char' called by more than one asynchronous thread (main/Task, interrupt, interrupt low)

Thank you in advance !

Re: How to set nTimeout for the RS232 component ?

Posted: Tue Mar 10, 2009 12:33 pm
by WalkOver
In the attached file, you have my test program who doesn't work with interupt macro but works without...

Re: How to set nTimeout for the RS232 component ?

Posted: Tue Mar 10, 2009 4:30 pm
by Benj
OK I think I have spotted the problem. If you open the 18F6627.FCD file located in the Flowcode V3/FCD folder in notepad and scroll to the bottom of the file then you will find the following section of code.

Code: Select all

[RXINT]
Name=RXINT
FlagReg=pir1
FlagBit=RCIF
EnReg=pie1
EnBit=RCIE
OptCnt=0
TmrOpt=0
Change to this, save the file and restart Flowcode and hopefully your receive interrupt will work correctly.

Code: Select all

[RXINT]
Name=RXINT
FlagReg=pir1
FlagBit=RC1IF
EnReg=pie1
EnBit=RC1IE
OptCnt=0
TmrOpt=0
If the interrupt is still not working then you can add a C code block after the interrupt enable macro but before the while loop to increase the priority of the interrupt.

Code: Select all

set_bit(ipr1, RC1IP);
Let me know how you get on.

Re: How to set nTimeout for the RS232 component ?

Posted: Tue Mar 10, 2009 5:00 pm
by WalkOver
In fact, I had already changed RCIF by RC1IF and RCIE by RC1IE without improvement. The error message is always here.
I try to put your C code without any change.

By coincidence, I added an interrupt by TIMER0 overflow ( but the associated macro is empty ) and my LED lights up but communication works very bad, my card does not receive the transmission every time...

Re: How to set nTimeout for the RS232 component ?

Posted: Wed Mar 11, 2009 10:07 am
by Benj
Hello

Ok I have spotted the problem I think. The RX interrupt is a peripheral interrupt so the following line will need to be added to the C code block after the interrupt enable.

set_bit(intcon, PEIE);

Re: How to set nTimeout for the RS232 component ?

Posted: Thu Mar 12, 2009 12:44 pm
by WalkOver
Thank You Benj,

I tested and I think it's works well. I have a little problem but I think it's a problem of interupt priority.

Have a nice day !

Re: How to set nTimeout for the RS232 component ?

Posted: Sat Aug 29, 2009 12:33 pm
by ALAN_26
HELLO

I have the same problem on PIC18f4620 RXINT NOT WORKING AT ALL

any one got an idea ?

Re: How to set nTimeout for the RS232 component ?

Posted: Thu Sep 03, 2009 8:39 am
by Benj
Hello

Do you want to attach your program to the forums and I will take a look for you.

Re: How to set nTimeout for the RS232 component ?

Posted: Mon Jan 09, 2012 4:57 pm
by xxxxxx
Hellloo

I am trying a demo version of flowcode, now seems to me that the RXINT interrupt is not working in simulation mode am I right??

thx in advance!

Re: How to set nTimeout for the RS232 component ?

Posted: Mon Jan 09, 2012 6:38 pm
by Benj
Hello,

Yes i'm afraid the RXINT interrupt does not simulate but does work well on hardware.