View previous topic :: View next topic |
Author |
Message |
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
Hardware UART, interrupt handler for RDA and TBE |
Posted: Tue Oct 09, 2018 12:22 pm |
|
|
Hi all!
MCU: PIC16LF1825
Compiler: PCM v5.051
I am trying to configure my device to run the hardware UART module, and utilize the TBE and RDA interrupts. I am facing some troubles though.
This is the code:
Code: | #include <16LF1825.h>
#include <stdint.h>
/*
*
*/
#fuses INTRC_IO, NOWDT,NOMCLR, NOCLKOUT, PLL
#USE DELAY (int=32MHZ, PLL_WAIT)
#USE RS232 (UART1, BAUD=115200)
void main(void) {
disable_interrupts(GLOBAL);
enable_interrupts(INT_RDA);
enable_interrupts(INT_TBE); //If I comment this line the Rx works well and I see 'R' printed on the terminal
enable_interrupts(GLOBAL);
while(TRUE){
output_high(PIN_C2);
delay_ms(500);
output_low(PIN_C2);
delay_ms(500);
putc('R');
}
}
#INT_RDA
void Rx_Da_INT(void){
char c;
c = getc();
putc(c);
}
#INT_TBE
void Tx_Be_INT(void){
output_low(PIN_C2);
}
|
If the line Quote: | enable_interrupts(INT_TBE); | is commented, the program runs well, but I need interrupt on Text Buffer Empty as well.
I just cant run them together (The Rx and the TX interrupts). _________________ A person who never made a mistake never tried anything new. |
|
|
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
|
Posted: Tue Oct 09, 2018 12:27 pm |
|
|
Ouch what a stupid mistake. The processor constantly re-enters the TBE ISR. I should disable the TBE interrupt once the string is sent.
Remains the question: Did I succeed to configure the RS232 directive to use the hardware UART module?
How can I know if the compiler setups hardware UART or software one? _________________ A person who never made a mistake never tried anything new.
Last edited by rikotech8 on Tue Oct 09, 2018 12:31 pm; edited 2 times in total |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1907
|
|
Posted: Tue Oct 09, 2018 12:28 pm |
|
|
The TBE interrupt is potentially very dangerous, as you've found out. All that it means is that the UART's transmit buffer is empty.
If you are not sending a message, the transmit buffer will be empty, and the TBE interrupt will fire. Continually. Because you aren't transmitting anything. This is what you've learned the hard way.
Enable the RDA interrupt but DO NOT enable the TBE interrupt until you actually have a message which needs to be transmitted. When you have assembled a message to transmit, enable the TBE interrupt. Inside the TBE interrupt, load the next character to be transmitted into the TXREG of the UART. If there are no more characters to transmit, disable the TBE interrupt. |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1907
|
|
Posted: Tue Oct 09, 2018 12:31 pm |
|
|
rikotech8 wrote: | Ouch what a stupid mistake. The processor constantly re-enters the TBE ISR. I should disable the TBE interrupt once the string is sent.
Remains the question: Did I succeed to configure the RS232 directive to configure the hardware UART module?
How can I know if the compiler setups hardware UART or software one? |
Glad you spotted it. Unless that processor has remappable pins, I'd say your #use rs232 directive is fine. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Tue Oct 09, 2018 12:59 pm |
|
|
Glad you found the issue.
This is where the examples are really useful. The STISR example, only sets INT_TBE, when you write a character in bputc, and it disables the interrupt in the ISR, if no more characters are waiting.
As one comment, add 'ERRORS' to the #use RS232. This is required, unless your RDA ISR has it's own error handling. CCS is a bit 'naughty' in having several of it's examples without this.... |
|
|
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
|
Posted: Wed Oct 10, 2018 9:25 am |
|
|
Thank you guys for your responses.
I am still not quite sure how CCS decides to use the hardware UART module instead of creating software instance.
From this text Quote: | FORCE_SW
Will generate software serial I/O routines even when the UART pins are specified.
| found in the manual, I would assume that it is just enough to specify the correct hardware UART pins for Rx(RCV) and Tx (XMIT) and the compiler will set up the hardware module.
In the other hand we have UARTx option to use in #USE RS232 (options).
May be there is more then one way? _________________ A person who never made a mistake never tried anything new. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Wed Oct 10, 2018 9:41 am |
|
|
The best way is the UARTx option. It is also great for use with #PIN_SELECT (have a look at the sticky at the top of the forum), for chips using this.
UARTx is equivalent to using the pins, and FORCE_HW, all in one nice little bundle... |
|
|
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
|
Posted: Wed Oct 10, 2018 10:46 am |
|
|
It seems that CCS designers change their minds quite frequently .
Nevertheless, I still find this compiler useful when I have to write simple program within a restricted time frame. Almost no datasheet looking.
Thanks Ttelmah. _________________ A person who never made a mistake never tried anything new. |
|
|
|