Continue to Site

# [SOLVED]UART data (in interrupt) reception not receive data ,after coming in the endless while(1); loop even interrupt occur

Status
Not open for further replies.

#### thannara123

UART data (in interrupt) reception not receive data ,after coming in the endless while(1); loop even interrupt occur.

I am using Pic16f877a code work well without the while loop which is comment offed
what is the reason why dont go the code to ISR_UART after went into the while loop

C:
char Uart_RC_Data ;
void interrupt UART_Interrupt()
{
if(RCIF )    // if interrupt occurred RCIF will be one
{
while(OERR)  // if there is error
{
// __delay_us(100);
CREN=0;  // restart UART
OERR=0;
}
CREN=1;
}
}

void main(void)
{
//OPTION_REG &= 0x7F;
TRISB =0x00;
TRISD = 0x00;
LCD_init();
TRISC6=0;   //Output (TX)
TRISC7=1;   //Input (RX)
ser_int();

while(1)
{
LINE1;
string(" Uart Data Rcved");
LINE2;
LCD_data(Uart_RC_Data);
__delay_ms(100);

while(1);  // whenever use this loop its not working
}

}

Last edited:

#### betwixt

##### Super Moderator
Staff member
Please show all the code. There is nothing shown to enable the interrupt.

Brian.

thannara123

### thannara123

Points: 2

#### thannara123

its working if i remove the while(1)

Code:
void ser_int()
{   GIE=1;
PEIE=1;
RCIE=1;
TXSTA=0x20; //BRGH=0, TXEN = 1, Asynchronous Mode, 8-bit mode
RCSTA=0b10010000; //Serial Port enabled,8-bit reception
SPBRG=17;           //9600 baudrate for 11.0592Mhz
SPEN = 1; //Enables Serial Port
TXIF=RCIF=0;
}

#### doraemon

##### Super Moderator
Staff member
Hello!

You may consider first looking at existing (and working) sample code, tutorials.
What you are asking belongs to basics, and I'm sure there is a tutorial that you could
first try, verify, modify, try if it still works and then ask very specific questions if it
doesn't.
For instance, if you say: I tried this tutorial, and it works. I then change line ##
to <something else> and it doesn't work anymore. Why?
This way, somebody who wants to help you would check only what has changed in line##
and debug it.

Second thing: you should avoid hard coded values. For instance 0x20. Why not
#define TXEN 0x20 ?
Why not #define BAUD_115k 0x17
and then write TXSTA = TXEN; and SPBRG = BAUD_115k;

By the way, I don't know if it's the case, but TXEN looks like TX enable. What about
RXEN? I mean: if the reception is not enabled, you will likely wait a while until the
interrupt comes. Just guessing, I have never used PIC.

Regards,

Dora.

#### betwixt

##### Super Moderator
Staff member
Also note that your code can only receive at best 10 characters per second because you pass each character to the LCD and then delay 100mS before accepting any more. If you are trying to display text as it is received, you need to store it first and then display when it is all in the store.

Why do you use "while(OERR)" then follow it with "OERR = 0", there is no need to put it inside a loop if you reset it anyway.

Brian.

thannara123

### thannara123

Points: 2

#### SWRmeter

##### Member level 1
while(1); // whenever use this loop its not working
This last while loop. It'll never exit. Why is it there ?

thannara123

### thannara123

Points: 2

#### thannara123

This last while loop. It'll never exit. Why is it there ?
Hi
if is it in the while loop ,whenever the intrreput occur ,is it jump to the ISR ? that is the doubt ?

Why is it there ? just for learning purpose

thanks

#### SWRmeter

##### Member level 1
Hi
if is it in the while loop ,whenever the intrreput occur ,is it jump to the ISR ? that is the doubt ?

Why is it there ? just for learning purpose

thanks
That's true, it will jump to the ISR. But after completion of ISR it'll jump right back into this same loop and do nothing else.

thannara123

Points: 2