PIC18F13K22 UART interrupt

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
HyperUniverse
Posts: 282
Joined: 17 Jun 2009 10:42

PIC18F13K22 UART interrupt

#1 Post by HyperUniverse » 04 Mar 2022 16:55

Hi,

Why isn't my interrupt for UART working?
PIC18F13K22

Code: Select all


    PIR1.RCIF = 0 ;  // EUSART Receive Interrupt Flag bit
                     // 1 = The EUSART receive buffer, RCREG, is full (cleared when RCREG is read)
                     // 0 = The EUSART receive buffer is empty

    PIR1.TXIF = 1 ;  // EUSART Transmit Interrupt Flag bit
                     // 1 = The EUSART transmit buffer, TXREG, is empty (cleared when TXREG is written)
                     // 0 = The EUSART transmit buffer is full

   RCSTA.SPEN = 1 ;  // 1 = serial port enabled (configures Rx/DT & Tx/CK pins as serial port pins).
   RCSTA.RX9 = 0  ;  // 0 = Selects 8-bit reception
   RCSTA.SREN = 1 ;  // 1 = Enables single receive
   RCSTA.CREN = 1 ;  // enable reception
   PIE1.RCIE = 1  ;  // enable UART_1 RX interrupt
   IPR1.RCIP = 1  ;  // EUSART high priority level

   RCON.IPEN = 1;       /* Enable priority levels on interrupts */
   GIEH_bit = 1;        /* Enable High Level Interrupts  */
   GIEL_bit = 1;        /* Enable Low Level Interrupts   */


  UART1_Init(19200) ;     // Initialize UART module at 19200
   Delay_ms(150) ;

Code: Select all

void Interrupt_High() iv 0x0008 ics ICS_AUTO {  /*** High Interrupt ***********/
  unsigned char uart_tmp_char ;
//RS_USART_Flag = ON ;  THIS FLAG IS NOT SET EVEN HERE, SO THE HIGH INTERRUPT IS NOT TRIGGERED
  if(PIR1.RCIF)  {  //  EUSART Receive Interrupt Flag bit

    PIE1.RCIE = 0  ;  // Disable UART_1 RX interrupt

    uart_tmp_char = RCREG  ;     // read character from Uart1  EUSART Receive Register
                                    // Get the data;  PIR1.RC1IF is cleared when RCREG1 is read
    RS_UART_Array[rx3i] = uart_tmp_char ;
    rx3i++;
    if(uart_tmp_char == 0x03) {   // 0x03 = ETX = End of Tx
       RS_USART_Flag = ON ;
    }

  //** it is necessary to clear manually the interrupt flag:
  PIR1.RCIF = 0  ;   // CLEAR UART1 INTERRUPT FLAG  PIR1.RC1IF is cleared when RCREG1 is read
  PIE1.RCIE = 1  ;  // Enable UART_1 RX interrupt
  } /*** End of if(PIR1.RC1IF == 1) *******************************************/
}/*** End of High Interrupt ***************************************************/
THIS FLAG IS NOT SET EVEN HERE,

Code: Select all

void Interrupt(){
    RS_USART_Flag = ON ;
}
Rx pin is on RB5
Tx pin is on RB7

Code: Select all

  /*  PORTB is 4-bit wide,  */
  PORTB = 0b1010 ;
  TRISB = 0b0010 ;  // RB5=In,
  LATB  = 0b1010 ;
  TRISB.TRISB5 = 1;   // RX - UART1
  TRISB.TRISB7 = 0;   // TX - UART1

paulfjujo
Posts: 1557
Joined: 24 Jun 2007 19:27
Location: 01800 St Maurice de Gourdans France
Contact:

Re: PIC18F13K22 UART interrupt

#2 Post by paulfjujo » 04 Mar 2022 21:19

hello,

bad coding !
too much mistakes

wich protocole STX ETX ?

Code: Select all

void Interrupt_High() iv 0x0008 ics ICS_AUTO {  /*** High Interrupt ***********/
  unsigned char uart_tmp_char ;
//RS_USART_Flag = ON ;  THIS FLAG IS NOT SET EVEN HERE, SO THE HIGH INTERRUPT IS NOT TRIGGERED
  if(PIR1.RCIF)  {  //  EUSART Receive Interrupt Flag bit
    uart_tmp_char = RCREG  ;     // read character from Uart1  EUSART Receive Register
                                  // Get the data;  PIR1.RC1IF is cleared when RCREG1 is read
    RS_UART_Array[rx3i] = uart_tmp_char ;
    if ( rx3i <79) rx3i++;  // give a limit !
    if(uart_tmp_char == 0x03)
     {   // 0x03 = ETX = End of Tx
       RS_USART_Flag = ON ;
    }
  } /*** End of if(PIR1.RC1IF == 1) *******************************************/
}/*** End of High Interrupt ***************************************************/
you can not write this
it is not an interrupt !
void Interrupt(){
RS_USART_Flag = ON ;
}

you can not clear RCIF ! only reading char can do it ...

use the flag inside your main ()
you must initialize the flag OFF ( or 0)
and counter rx3i

.......
if (RS_UART_Flag==1) UART1_Write_Text(RS_UART_Array[rx3i] );
.....

where is defined RS_UART_Array ?
wher is defined rx3i++; ?
example :
unsigned char RS_UART_Array [80];
volatile char RS_UART_Flag=0;
volatile int rx3i=0;

where is defined ON
#define ON 1
#define OFF 0

HyperUniverse
Posts: 282
Joined: 17 Jun 2009 10:42

Re: PIC18F13K22 UART interrupt

#3 Post by HyperUniverse » 04 Mar 2022 22:58

thanks for answering paulfjujo

unfortunately absolutely nothing from your post indicate why my interrupt doesn't trigger.

the fact that I did not show you my variables or declarations have got nothing to do with the interrupt not triggering.


So, can anybody tell me why the USART Interrupt doesn't work on my code, nor on paulfjujo's code, which by the way is exactly as mine?

HyperUniverse
Posts: 282
Joined: 17 Jun 2009 10:42

Re: PIC18F13K22 UART interrupt

#4 Post by HyperUniverse » 04 Mar 2022 23:21

the solution is:

Code: Select all

ANSELH.ANS11 = 0 ;
case closed!

paulfjujo
Posts: 1557
Joined: 24 Jun 2007 19:27
Location: 01800 St Maurice de Gourdans France
Contact:

Re: PIC18F13K22 UART interrupt

#5 Post by paulfjujo » 05 Mar 2022 08:41

hello,
HyperUniverse wrote:
04 Mar 2022 23:21
the solution is:

Code: Select all

ANSELH.ANS11 = 0 ;
case closed!
OK, it is enough to avoid UART interrupt .

for my information, can you show us, how do you handle the other interrupt
(how do you treat the result of interrupt : flag RS_USART_Flag)

and if you add another interrupt for Timer or other device ..?

Code: Select all

void Interrupt(){
    RS_USART_Flag = ON ;
}

HyperUniverse
Posts: 282
Joined: 17 Jun 2009 10:42

Re: PIC18F13K22 UART interrupt

#6 Post by HyperUniverse » 05 Mar 2022 23:47

paulfjujo, you got the wrong idea about what I am doing.

I have already shown you the Interrupt routine that I am using - the first one in the first post.

The one that it bothers you, and you cannot understand it, and you keep posting it thinking that this is the one that I am using in my program, and call my programming - bad coding !,
was just a short and quick test to see if my program responds to ANY interrupt.
you should have understood that when I've written:
THIS FLAG IS NOT SET EVEN HERE,
Do you understand now?

Post Reply

Return to “mikroC PRO for PIC General”