View previous topic :: View next topic |
Author |
Message |
Ttelmah
Joined: 11 Mar 2010 Posts: 19369
|
|
Posted: Thu Jul 30, 2015 2:08 pm |
|
|
INT_TBE, is called _continuously_ if the transmit buffer is empty. It is at boot.
So with INT_TBE enabled, the code will never work. |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9174 Location: Greensville,Ontario
|
|
Posted: Thu Jul 30, 2015 5:01 pm |
|
|
two tips about interrurpts
1) NEVER enable an interrupt unless you have a 'handler'( ISR) for it. The PIC won't know what to do with it.....it'll either crash or do 'strange' things !!
2) Whenever using the hardware UART, ALWAYS add 'ERRORS' to the use rs232(...options...). This will allow the program to carry on when the UART gets more than 3 incoming characters( the limit of the hardware buffer.
Jay |
|
![](templates/subSilver/images/spacer.gif) |
Jean FOUGERON
Joined: 30 Nov 2012 Posts: 110 Location: France
|
|
Posted: Fri Jul 31, 2015 3:34 am |
|
|
Thanks to you two
OK, I need Serial com but I didn't implement it yet, I will do it.
For the time being, I supposed that when there was no #INT_TBE routine nothing hapened, but ... mysteries of PIC
I will add ERRORS
Nevertheless it does not explain why a routine works in a timer and not in main (even when it is surely not necessary to do the job in main because it would be done too often). It would be pleasant to understand it (French say "I don't wand to dead stupid") |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19369
|
|
Posted: Fri Jul 31, 2015 10:57 am |
|
|
The point is that it never gets beyond the interrupt enables. Because there is no INT_TBE handler it'll be looping permanently back trying to find this handler. Except when a timer interrupt triggers, and then the timer interrupt routine gets called.
INT_TBE, says that the USART transmit buffer is empty. The routine then called loads a character to send. Since the buffer _is_ empty at boot, INT_TBE will be called, and unless a character is loaded, will be called again and again.
This is why you must have this interrupt disabled, until you have data to send, and once it is sent, then disable it again. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jul 31, 2015 11:08 am |
|
|
Here is a simple test program to illustrate what Ttelmah says.
Code: | #include <18F4520.h>
#fuses INTRC_IO, NOWDT
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)
#define LED1 PIN_B0
#define LED2 PIN_B1
#int_timer1
void timer1_isr(void)
{
output_toggle(LED1); // This will always blink at about 1 Hz
}
//===================================
void main()
{
output_low(LED1); // Initially turn both leds off
output_low(LED2);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_TBE); // This will cause continuous interrupts
enable_interrupts(GLOBAL);
// The program will never reach the next line and LED2 will never
// turn on, unless you comment out enable_interrupts(INT_TBE).
output_high(LED2);
while(TRUE);
} |
|
|
![](templates/subSilver/images/spacer.gif) |
|