View previous topic :: View next topic |
Author |
Message |
Alex Guest
|
Problem with TXD interrupt |
Posted: Fri Sep 17, 2004 6:40 am |
|
|
Hello to everybody
I am using PIC16F877
and trying to write just a very simple program to send some characters through the RS232 to PC using interrupts just to start with.
So, could anybody help me and tell why the following code does not work
Thank you very much
#pragma device PIC16F877
#USE standard_IO(C)
#use delay (clock=4000000)
#include "SFR.h"
#include "DEFS.h"
void init(void);
void TXD_isr();
int index = 2;
unsigned char a;
unsigned char arr[] = {'a','b','c'};
//------------------------------------------------
void main(void)
{
init();
while(1)
{
if (ENABLE)
{
TXREG = a;
ENABLE = 0;
}
}
}
//---------------------------------------------------
#INT_TBE
void TXD_isr(void)
{
int i;
a = arr[i];
i++;
if (i>index)
{
i = 0;
}
ENABLE = 1;
PIR1bits.SSPIF = 0 ;
}
void init(void)
{
SET_TRIS_C(0xFE);
TXSTA = 0x26 ;
RCSTA = 0x90 ;
SPBRG = 25; // set_uart_speed(9600);
TXD_IE = 1 ;
SERIAL_IE = 1 ;
a = 'm';
ENABLE = 1 ;
INTCONbits.PEIE = 1 ;
enable_interrupts(INT_TBE);
enable_interrupts(GLOBAL); |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Sep 17, 2004 6:51 am |
|
|
#pragma device PIC16F877 <- no pragma
TXD_IE = 1 ; <- is this the same enable_interrupts(INT_TBE); ?
SERIAL_IE = 1 ; <- What does this do?
INTCONbits.PEIE = 1 ; <- Why are you mixing methods of enabling ints? This does the same thing enable_interrupts(GLOBAL);
Quote: |
#INT_TBE
void TXD_isr(void)
{
int i;
a = arr[i];
i++;
|
did you mean to do this
Code: |
#INT_TBE
void TXD_isr(void)
{
static int i=0;
a = arr[i];
i++;
|
Also to make sure that you turn off the Tx int after your last byte it sent or it will keep firing. |
|
|
Guest
|
|
Posted: Fri Sep 17, 2004 7:05 am |
|
|
TXD_IE = 1 ; <- is this the same enable_interrupts(INT_TBE); ?
yes
SERIAL_IE = 1 ; <- What does this do?
SERIAL_IE = PIE1bits.SSPIE
INTCONbits.PEIE = 1 ; <- Why are you mixing methods of enabling ints? This does the same thing enable_interrupts(GLOBAL);
I understood that enable_interrupts(GLOBAL); enables only GIE bit
Is not it?
Quote: |
#INT_TBE
void TXD_isr(void)
{
int i;
a = arr[i];
i++;
|
did you mean to do this
Code: |
#INT_TBE
void TXD_isr(void)
{
static int i=0;
a = arr[i];
i++;
|
If it is a static , so it won't change will it?
I meant the 'i' to be changed only within this function
anyway it is still not working :(
Also to make sure that you turn off the Tx int after your last byte it sent or it will keep firing.[/quote] |
|
|
alexbilo
Joined: 01 Jun 2004 Posts: 39 Location: Trois-Rivi�res
|
|
Posted: Fri Sep 17, 2004 7:16 am |
|
|
Quote: |
If it is a static , so it won't change will it? |
Actually, declaring a variable as static means that it'll keep its value from one call of the function to the next. On the other hand, if it isn't static its value will be lost at the end of the function call.
Knowing this, you should declare it as static.
Good luck, _________________ Alex |
|
|
alexz
Joined: 17 Sep 2004 Posts: 133 Location: UK
|
|
Posted: Fri Sep 17, 2004 7:20 am |
|
|
Thank you for the explanation Alex
But it did not solve the problem _________________ Alex |
|
|
alexz
Joined: 17 Sep 2004 Posts: 133 Location: UK
|
|
Posted: Fri Sep 17, 2004 9:29 am |
|
|
The program works fine wen I run it step by step
But it does not do anything when I run it normally _________________ Alex |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Sep 17, 2004 11:21 am |
|
|
CCS has an example program for #int_tbe called EX_STISR.C.
That file is in this folder on your computer:
c:\Program Files\Picc\Examples
I also once wrote an example for #int_tbe. It's here:
http://www.ccsinfo.com/wwwboard/messages/1848.html |
|
|
Guest
|
|
Posted: Mon Sep 20, 2004 5:54 am |
|
|
I have done it exactly the same way.
the problem is that the RBE interrupt occurs all the time
and the program never riches the main loop |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Sep 20, 2004 7:41 am |
|
|
Mark wrote: |
Also to make sure that you turn off the Tx int after your last byte it sent or it will keep firing. |
Yeah, that is what I told you |
|
|
|