How to set nTimeout for the RS232 component ?

Forum for problems or queries regarding Flowcode Comms Components. Eg LIN, I2C, SPI, RS232, CAN, IrDA etc

Moderators: Benj, Mods

Post Reply
WalkOver
Posts: 65
Joined: Fri Nov 23, 2007 11:24 pm
Contact:

How to set nTimeout for the RS232 component ?

Post 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 !

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: How to set nTimeout for the RS232 component ?

Post 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.

WalkOver
Posts: 65
Joined: Fri Nov 23, 2007 11:24 pm
Contact:

Re: How to set nTimeout for the RS232 component ?

Post 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.

WalkOver
Posts: 65
Joined: Fri Nov 23, 2007 11:24 pm
Contact:

Re: How to set nTimeout for the RS232 component ?

Post 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!

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: How to set nTimeout for the RS232 component ?

Post 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.

WalkOver
Posts: 65
Joined: Fri Nov 23, 2007 11:24 pm
Contact:

Re: How to set nTimeout for the RS232 component ?

Post 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!

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: How to set nTimeout for the RS232 component ?

Post 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.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: How to set nTimeout for the RS232 component ?

Post 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.

WalkOver
Posts: 65
Joined: Fri Nov 23, 2007 11:24 pm
Contact:

Re: How to set nTimeout for the RS232 component ?

Post 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 ?

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: How to set nTimeout for the RS232 component ?

Post 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.

WalkOver
Posts: 65
Joined: Fri Nov 23, 2007 11:24 pm
Contact:

Re: How to set nTimeout for the RS232 component ?

Post 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 ?

WalkOver
Posts: 65
Joined: Fri Nov 23, 2007 11:24 pm
Contact:

Re: How to set nTimeout for the RS232 component ?

Post 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 !

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: How to set nTimeout for the RS232 component ?

Post 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.

WalkOver
Posts: 65
Joined: Fri Nov 23, 2007 11:24 pm
Contact:

Re: How to set nTimeout for the RS232 component ?

Post 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 !

WalkOver
Posts: 65
Joined: Fri Nov 23, 2007 11:24 pm
Contact:

Re: How to set nTimeout for the RS232 component ?

Post by WalkOver »

In the attached file, you have my test program who doesn't work with interupt macro but works without...
Attachments
RS232_RX_INT.fcf
(7.5 KiB) Downloaded 642 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: How to set nTimeout for the RS232 component ?

Post 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.

WalkOver
Posts: 65
Joined: Fri Nov 23, 2007 11:24 pm
Contact:

Re: How to set nTimeout for the RS232 component ?

Post 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...

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: How to set nTimeout for the RS232 component ?

Post 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);

WalkOver
Posts: 65
Joined: Fri Nov 23, 2007 11:24 pm
Contact:

Re: How to set nTimeout for the RS232 component ?

Post 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 !

ALAN_26
Posts: 84
Joined: Sat Apr 29, 2006 3:36 pm
Location: MALTA
Contact:

Re: How to set nTimeout for the RS232 component ?

Post by ALAN_26 »

HELLO

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

any one got an idea ?

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: How to set nTimeout for the RS232 component ?

Post by Benj »

Hello

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

xxxxxx
Posts: 1
Joined: Mon Jan 09, 2012 4:43 pm
Contact:

Re: How to set nTimeout for the RS232 component ?

Post 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!

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: How to set nTimeout for the RS232 component ?

Post by Benj »

Hello,

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

Post Reply