• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Program controls jumps to interrupt handler after RCSTA1 = 0x90;

Prototype21

New Member
Hey guys,
I am using pic18f25k80 microcontroller and XC8 compiler for my development. The control of the program jumps from RCSTA1 = 0x90; to interrupt handler when debugged. The TX1IF flags gets sets every time when RCSTA1 = 0x90;. Any idea guys?

C:
#include <xc.h>
#define _XTAL_FREQ 8000000UL


// PIC18F25K80 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1L
#pragma config RETEN = OFF      // VREG Sleep Enable bit (Ultra low-power regulator is Disabled (Controlled by REGSLP bit))
//#pragma config INTOSCSEL = HIGH // LF-INTOSC Low-power Enable bit (LF-INTOSC in High-power mode during Sleep)
//#pragma config SOSCSEL = HIGH   // SOSC Power Selection and mode Configuration bits (High Power SOSC circuit selected)
#pragma config XINST = OFF       // Extended Instruction Set (Enabled)

// CONFIG1H
#pragma config FOSC = HS1       // Oscillator (HS oscillator (Medium power, 4 MHz - 16 MHz))
#pragma config PLLCFG = OFF     // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF       // Internal External Oscillator Switch Over Mode (Disabled)

// CONFIG2L
#pragma config PWRTEN = OFF     // Power Up Timer (Disabled)
//#pragma config BOREN = SBORDIS  // Brown Out Detect (Enabled in hardware, SBOREN disabled)
//#pragma config BORV = 3         // Brown-out Reset Voltage bits (1.8V)
//#pragma config BORPWR = ZPBORMV // BORMV Power level (ZPBORMV instead of BORMV is selected)

// CONFIG2H
//#pragma config WDTEN = SWDTDIS  // Watchdog Timer (WDT enabled in hardware; SWDTEN bit disabled)
//#pragma config WDTPS = 1048576  // Watchdog Postscaler (1:1048576)

// CONFIG3H
#pragma config CANMX = PORTB    // ECAN Mux bit (ECAN TX and RX pins are located on RB2 and RB3, respectively)
#pragma config MSSPMSK = MSK7   // MSSP address masking (7 Bit address masking mode)
#pragma config MCLRE = ON       // Master Clear Enable (MCLR Enabled, RE3 Disabled)

// CONFIG4L
#pragma config STVREN = OFF      // Stack Overflow Reset (Enabled)
//#pragma config BBSIZ = BB2K     // Boot Block Size (2K word Boot Block size)

// CONFIG5L
#pragma config CP0 = OFF        // Code Protect 00800-01FFF (Disabled)
#pragma config CP1 = OFF        // Code Protect 02000-03FFF (Disabled)
#pragma config CP2 = OFF        // Code Protect 04000-05FFF (Disabled)
#pragma config CP3 = OFF        // Code Protect 06000-07FFF (Disabled)

// CONFIG5H
#pragma config CPB = OFF        // Code Protect Boot (Disabled)
#pragma config CPD = OFF        // Data EE Read Protect (Disabled)

// CONFIG6L
#pragma config WRT0 = OFF       // Table Write Protect 00800-01FFF (Disabled)
#pragma config WRT1 = OFF       // Table Write Protect 02000-03FFF (Disabled)
#pragma config WRT2 = OFF       // Table Write Protect 04000-05FFF (Disabled)
#pragma config WRT3 = OFF       // Table Write Protect 06000-07FFF (Disabled)

// CONFIG6H
#pragma config WRTC = OFF       // Config. Write Protect (Disabled)
#pragma config WRTB = OFF       // Table Write Protect Boot (Disabled)
#pragma config WRTD = OFF       // Data EE Write Protect (Disabled)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protect 00800-01FFF (Disabled)
#pragma config EBTR1 = OFF      // Table Read Protect 02000-03FFF (Disabled)
#pragma config EBTR2 = OFF      // Table Read Protect 04000-05FFF (Disabled)
#pragma config EBTR3 = OFF      // Table Read Protect 06000-07FFF (Disabled)

// CONFIG7H
#pragma config EBTRB = OFF      // Table Read Protect Boot (Disabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.



void main(void)
{
    TRISC6=0;       /* Make Tx pin as output*/
    TRISC7=1;       /* Make Rx pin as input*/
 
    INTCONbits.GIE = 1; /* Enable Global Interrupt */
    INTCONbits.PEIE = 1;/* Enable Peripheral Interrupt */
 
    //PIE1bits.RC1IE = 1;   /* Enable Receive Interrupt*/
    PIE1bits.TX1IE = 1;     /* Enable Transmit Interrupt*/  
 
    //TXSTA1bits.BRGH = 0;
 
    TXSTA1 = 0x20;
    RCSTA1 = 0x90;                               //<---------------------- From here

    BAUDCON1bits.BRG16 = 0;
    SPBRG1 = 12;
     
    while(1)
    {
        TXREG1 = 'A';
        while(TXSTA1bits.TRMT1 != 0);    
    }
 
    return;
}

__interrupt() void ISR(void)                //<----------------------- To here
{
    if(PIR1bits.TX1IF != 0)                  
    {
        PIR1bits.TX1IF = 0;
    }
}
 

Nigel Goodwin

Super Moderator
Most Helpful Member
My immediate thought is don't set the serial port up while you already have the interrupts enabled, disable them before hand, and enable them afterwards.

C:
    EUSART1_Initialize();
    EUSART2_Initialize();
    
    TMR1_Initialize();
    TMR2_Initialize();
    tmr4_init();                // timer for DHT22
    
    FVR_Initialize();
    ADCC_Initialize();
    
    INTCONbits.GIE = 1;         // Enable global interrupt
    INTCONbits.PEIE = 1;        // Enable peripheral interrupt
The EUSART Initialize routines also disable their own interrupts before setting up, and reinitialise them afterwards.
 

Prototype21

New Member
Yes, you shouldn't make changes while interrupts are active.
I tried as you mentioned but no change still the same.


Code:
void main(void)
{
    TRISC6=0;        /* Make Tx pin as output*/
    TRISC7=1;        /* Make Rx pin as input*/
    //TXSTA1bits.BRGH = 0;   
    TXSTA1 = 0x20;
    RCSTA1 = 0x90;
    BAUDCON1bits.BRG16 = 0;
    SPBRG1 = 12;
    
    INTCONbits.GIE = 1;    /* Enable Global Interrupt */
    INTCONbits.PEIE = 1;/* Enable Peripheral Interrupt */
    PIE1bits.TX1IE = 1;    /* Enable Transmit Interrupt*/
    //PIE1bits.RC1IE = 1;    /* Enable Receive Interrupt*/
        
    while(1)
    {
        TXREG1 = 'A';
        while(TXSTA1bits.TRMT1 != 0);     
    }
    
    return;
}

__interrupt() void ISR(void)
{
    if(PIR1bits.TX1IF != 0)                     
    {
        PIR1bits.TX1IF = 0;
    }
}
This time control skips from PIE1bits.TX1IE = 1; to interrupt handler.
 

Nigel Goodwin

Super Moderator
Most Helpful Member
I tried as you mentioned but no change still the same.


Code:
void main(void)
{
    TRISC6=0;        /* Make Tx pin as output*/
    TRISC7=1;        /* Make Rx pin as input*/
    //TXSTA1bits.BRGH = 0;  
    TXSTA1 = 0x20;
    RCSTA1 = 0x90;
    BAUDCON1bits.BRG16 = 0;
    SPBRG1 = 12;
   
    INTCONbits.GIE = 1;    /* Enable Global Interrupt */
    INTCONbits.PEIE = 1;/* Enable Peripheral Interrupt */
    PIE1bits.TX1IE = 1;    /* Enable Transmit Interrupt*/
    //PIE1bits.RC1IE = 1;    /* Enable Receive Interrupt*/
       
    while(1)
    {
        TXREG1 = 'A';
        while(TXSTA1bits.TRMT1 != 0);    
    }
   
    return;
}

__interrupt() void ISR(void)
{
    if(PIR1bits.TX1IF != 0)                    
    {
        PIR1bits.TX1IF = 0;
    }
}
This time control skips from PIE1bits.TX1IE = 1; to interrupt handler.
Isn't it supposed to?, you appear to be sending data out of the USART.

Have you not considered using MCC to configure your serial routines?.
 

Latest threads

EE World Online Articles

Loading

 
Top