why its called using an interrupt

Thread Starter

yef smith

Joined Aug 2, 2020
756
Hello, in the code bellow i see the use of USART register .
Why do we need USART1->IEN = USART_IEN_RXDATAV; and NVIC_EnableIRQ(USART1_RX_IRQn);
An interupt is a simething that stops the general code to execute another code.
I cant see it here.
I cant see how the operations in the code bellow are interupts?

Code:
    void USART1_RX_IRQHandler(void)
    {
        if(USART1->STATUS & (1 << 7)) {   // if RX buffer contains valid data
                          rx_char = USART1->RXDATA;       // store the data
                        }
                        if(rx_char) {                     // if we have a valid character
                          if(USART1->STATUS & (1 << 6)) { // check if TX buffer is empty
                            USART1->TXDATA = rx_char;     // echo received char
                            rx_char = 0;                  // reset temp variable
                          }
                        }
    
    
    }
 

djsfantasi

Joined Apr 11, 2010
9,163
Hello, in the code bellow i see the use of USART register .
Why do we need USART1->IEN = USART_IEN_RXDATAV; and NVIC_EnableIRQ(USART1_RX_IRQn);
An interupt is a simething that stops the general code to execute another code.
I cant see it here.
I cant see how the operations in the code bellow are interupts?

Code:
    void USART1_RX_IRQHandler(void)
    {
        if(USART1->STATUS & (1 << 7)) {   // if RX buffer contains valid data
                          rx_char = USART1->RXDATA;       // store the data
                        }
                        if(rx_char) {                     // if we have a valid character
                          if(USART1->STATUS & (1 << 6)) { // check if TX buffer is empty
                            USART1->TXDATA = rx_char;     // echo received char
                            rx_char = 0;                  // reset temp variable
                          }
                        }
   
   
    }
The code is not the interrupt. It is the interrupt handler. Thus this code is only executed when an interrupt occurs.
 

Papabravo

Joined Feb 24, 2006
21,228
Tell us what processor this is for and we can explain the mechanism that is used to suspend the background (non interrupt level) code, execute the interrupt code, and then return to the background code EXACTLY where it left off. You'll need some familiarity with processor hardware and assembly language programming for the explanation to make sense.

If I was a betting man I would say it is some flavor of Atmel (now Microchip) processor.
IMHO, the code is brain dead for dropping the echo if the transmitter is busy.
BTW -- where is rx_char declared? I hope to GALM that it is NOT a global variable.
 

Delta Prime

Joined Nov 15, 2019
1,311
Hello there :)
what happens when an interrupt occurs? The answer is that the status of the system is saved (called the context), then the processor is pointed towards your interrupt code. The interrupt code runs and when it is done, the status (context) is restored and the processor is returned to where it left off. The regular code then proceeds as before.

The code that is run when an interrupt occurs is called an Interrupt Service Routine (ISR). It is a special function that only runs when there is an interrupt. The PIC16F690 can only have one Interrupt Service Routine. Other more advanced devices can have a separate routine for each type of interrupt.

If you are using interrupts, you must tell XC8 which function in your code is the interrupt handler. You do this when you declare the function by adding "interrupt" to the declaration:
void interrupt isr()
{
... interrupt code
}
This tells the compiler that isr() is the function that will get called when an interrupt occurs. Note that an interrupt service routine can not take any arguments and does not return anything. This makes sense because you do not call the ISR manually. It is called automatically when an interrupt occurs :) sorry guys I meant no disrespect by answering I was typing when all this was going on.
 
Top