PIC18FXX2 hardware UART Interrupt flag set upon initialization!

Status
Not open for further replies.

vladtess

Newbie level 3
Joined
Aug 20, 2012
Messages
4
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,283
Activity points
1,315
Hello Fellas

I have PIC18f452 and trying to work with hardware UART in Asynch mode with 8 bits. Here is the problem: When I initialize the UART as described in the datasheet, upon execution of main() and init of all the registers required by UART, the interrupt flag is set that the Data was transmitted, even though none was (TXREG was not assigned a value anywhere). Here is some code:

1. Main:
Code:
void main(void) {
    init_int();
    init_spi();
	ini_uart(0);
    setupee();

	while(1);
}

2. init_uart(0)
Code:
void ini_uart(unsigned char baud) {
	// initialize ports/bits
	RCSTAbits.SPEN = 1;  // "Serial port enabled (configures RX/DT and TX/CK pins as serial port pins)" 0 to disable
	TRISCbits.RC6 = 0;  // set Port C pin 6 as output for TX (output)
	TRISCbits.RC7 = 1;	// set Port C pin 7 as input for RX (input)

	//set baud
	TXSTAbits.BRGH = 1; // used only in Asynch mode; 1 for high speed and 0 for low speed.
	SPBRG = 25; // set baud

	// set up TX register (TXSTA: TRANSMIT STATUS AND CONTROL REGISTER)
	TXSTAbits.TX9 = 0; // set 8 bit transmission; 1 for 9bit
	TXSTAbits.TXEN = 1; // Transmit Enable bit; 1 for enable 0 for disable
	TXSTAbits.SYNC = 0; // set Asynchronous mode; 1 for Synchronous mode (mode select)
	
	// TXSTA<TRMT> bit is used to state when the TSR is empty or full (status bit)

}

3. interrupts initialization:
Code:
void init_int(void) {
    // Used to enable global interrupts.  Should not be really changed
    INTCONbits.GIE = 1;  // enable all HIGH priority interrupts.  Set 0 to disable high Priority inerrupts
    INTCONbits.PEIE = 1;  // enable all LOW priority interrupts.  0 to disable.
    RCONbits.IPEN = 1;  // enable priority levels with interrupts.  If 0, only HIGH levels interrupts will exexute

    // Here, enable what interrupts you need to enable and set if you want it to be high priority or low.
    PIE1bits.SSPIE = 1; // engable SPI interrupt; 0 to disable.
    IPR1bits.SSPIP = 0; // make SPI low priority interrupt; change to 0 to make it low priority

    // Here EEPROM interrupt Hardware is enabled
    PIE2bits.EEIE = 1; // enable interrupts
    IPR2bits.EEIP = 1; // high priority

	// UART interrupt bits loaded here

	PIE1bits.TXIE = 1; // enable interrupts
	IPR1bits.TXIP = 1; // high priority interrupt
    // ADD more interrupt enablings.
}

4. Interrupt functions:
Code:
#include "main_header.h"
// Low priority function
#pragma interruptlow low_isr
void low_isr (void)
{
	if (PIR1bits.SSPIF) {
        spi();
        PIR1bits.SSPIF = 0; // clear SPI int flag
        INTCONbits.GIE = 1;  // enable all HIGH priority interrupts.
    }


}


// High priority function
#pragma interrupt high_isr
void high_isr (void)
{
    if (PIR2bits.EEIF && !EECON1bits.WRERR && !EECON1bits.WR) PIR2bits.EEIF = 0;  // if EEPRROM-write-complete flag is set AND no error AND write i complete then reset the flag to 0
    if (PIR1bits.TXIF) {;  // bit set when the TX is complete and buffer is empty; clear to send more data
		// add instructions here;  bit is cleared when new data is loaded into TXREG register
		relay_set(11,1);

	}
	else eeerror = 1;
}


//*******************************************//
//vector, do NOT change from this point on!!!                   //
//*******************************************//
#pragma code low_vector=0x18
void interrupt_at_low_vector(void)
{
  _asm GOTO low_isr _endasm
}
#pragma code

#pragma code high_vector=0x08
void interrupt_at_high_vector(void)
{
  _asm GOTO high_isr _endasm
}
#pragma code
// end

In the last piece of code, the function relay_set(11,1); is executed even though no TXREG register was sent anywhere in the program. Can someone help me ,tell me what I am doing wrong?
When I set TXSTAbits.TXEN = 0;, the flag is not set and of course the UART is not initialized. Can anyone test this? Note: it has worked at some point, but it stopped......

Thanks much!!
 

Hi,
It is not necessary to enable the interrupt service routine you can try by polling the flag.
Or in your case, have you cleared that flag when received the interrupt?
If not then try clearing it.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…