PDA

View Full Version : Triggering the First UART TX Interrupt..How To?



tjkelly
- 15th November 2011, 14:27
Yesterday I successfully implemented UART RX interrupts using DT Interrupts.

As far as TX interrupts go, my plan is to use ArrayWrite within my main program to write string messages and A/D readings to the transmit buffer and ArrayRead within the interrupt service routine to transmit character by character.

Do I have to write the first character of the buffer to the TX register from the main program to get things started or is there a different way to handle this?

tjkelly
- 15th November 2011, 16:05
OK, after using DT Interrupts to implement TX_INT, I can see that immediately upon enabling the interrupt, I get an interrupt due to TXIF being set (TXREG is empty).
This would indicate to me that in my main program I would write characters to the TX buffer, enable TX interrupts and within the ISR I would disable TX interrupts upon transmitting the last character.
I had thought that transmit interrupts might be left enabled such as I am doing with RX interrupts.

HenrikOlsson
- 15th November 2011, 17:46
Hi,
The way I usually do it is as follows.

1) An index pointer is used to point at the character in the array which is to be sent.
2) When I'm about to start send, set the index pointer to 0 (or whatever) and enable the TX interrupt - interrupt will fire instantly.
3) In the ISR, grab the byte at location Array[Index], if the grabbed array is my end-of-string byte (usually null) disable interrupt
4) If it's not the end-of-string byte put it in the TX reg
5) Increment the index pointer
6) Leave the ISR

As you can see the TX interrupt stays enabled untill the end-of-string byte is found, then it's turned off. As soon as the TX reg is free the interrupt will refire and the process starts over at step (3). It keeps going untill the end-of-strin byte is found.

Obviosuly there are other ways of doing it, if you know how many bytes to send there's no way to check each byte - just keep count of the bytes going out.

/Henrik.

amgen
- 15th November 2011, 18:46
Good points about just leaving TX int on,(just don't load any chars), but I use HWserout on same pin if I want to do small serout from basic code line (no arraywrite etc). Then its safer to turn off TXint so the sends don't interfere.
Don

HenrikOlsson
- 15th November 2011, 18:52
Hi Don,
What I mean is leave it on untill the string at hand is fully sent - then turn it off. If you leave the TX interrupt enabled ALL the time then it would keep firing and firing and firing which would hang up the main application - that's not what I suggest.

If your main app uses HSEROUT to send and it could do that at the same time as something else using an interrupt driven TX routine then I'd suggest implementing some kind of interlock in such way that the main application isn't allowed to execute HSEROUT if it sees that the TX interrupt is enabled.

/Henrik.

amgen
- 15th November 2011, 19:36
Henrik,
Yes, thinking a little further it really does need to be :

-turn on TXint (in basic), send from INT routine, then disable in TXint when done sending all.
And just be carefull not to conflict, ie..... turn off one send mode if the other is active, for TX and hwserout.
(just leaving TXint on without loading chars would just stop the works cold there)(dah)
Don

Charles Linquis
- 16th November 2011, 03:34
Although it is not directly related, you can do all sorts of neat things by directly writing the interrupt flag registers. For example: If you have a receive
buffer that needs to be processed quickly, but can't block incoming data, you can have a high-priority receive buffer that sets an (unused) low-priority
interrupt that does the processing.

tjkelly
- 17th November 2011, 00:54
Thanks for all the advice.
I have successfully got it all to work even with a circulating TX buffer that was a pain to get working correctly but I finally got it.