user3732308
user3732308

Reputation: 11

Initializes the UART with high priority receive interrupt in PIC18LF6722

#define InitUART( BRGVal, BRGHighVal )      {                       \
                        UARTTxD = UARTTxDInit;                  \
                        DirUARTTxD = DirOutput;                 \
                        DirUARTRxD = DirInput;                  \
                        SPBRG2 = BRGVal;                        \
                        TXSTA2bits.BRGH = BRGHighVal;           \
                        TXSTA2bits.SYNC = 0;                    \
                        TXSTA2bits.TX9 = 0;                     \
                        TXSTA2bits.TXEN = 1;                    \
                        RCSTA2bits.SPEN = 1;                    \
                        RCSTA2bits.RX9 = 0;                     \
                        IPR3bits.RC2IP = 1;                     \
                        IPR3bits.TX2IP = 0;                     \
                        PIE3bits.TX2IE = 0;                     \
                        PIE3bits.RC2IE = 1;                     \
                        RCSTA2bits.CREN = 0;                    \
                        }

for the last line, why RCSTA2bits.CREN need to be set to 0? how can i receive the data that come in if it set to be o?

initializes the UsART2 with high priority receive interrupt

InitUART (9600,1);// initialise the USART2 with high priority receive interrupt

this is my high interrupt code

//when some data is transmitted in through USART2
if (UARTRxIntFlag == 1) {
//receive interrupt occurs, do receive function(UARTChIn = ?)
}

this is my low interrupt code

rom unsigned char * szHello = "Hello\r\n";
if (IsSWI( SWI_LMTData ) ){
    unsigned char ch = *LMTRxCh;

    // if the received character from USART1 is an 'H'
    if (ch=='h' || ch=='H'){
        // say hello back through USART1
        LMTTransmit( szHello, 0, 7, 255, LogicalChannel );
        // send 'H' through USART2
        UARTChOut = ch;
    }
    // remove the character from the receive buffer
    LMTRxAdvanceCh;
    //Receive enable for USART2(RCSTA2bits.CREN = 1;)
    UARTEnable = 1;
    ClearSWI( SWI_LMTData );        // Clear interrupt flag
    return;
}
if (IsSWI( SWI_Tick ) ){
    ClearSWI( SWI_Tick );       // Clear interrupt flag
    return;
}

PAGE 106 0F http://www.flexipanel.com/Docs/Toothpick%202.1%20DS484.pdf

this code is not working and i don't know why. can you guys help me with that?

Upvotes: 1

Views: 616

Answers (2)

Ruslan Gerasimov
Ruslan Gerasimov

Reputation: 1782

CREN - Continuous Receive Enable bit
Asynchronous mode:
1 = Enables continuous receive
0 = Disables continuous receive
Synchronous mode:
1 = Enables continuous receive until enable bit CRE
N is cleared (CREN overrides
SREN)
0 = Disables continuous receive 

Also take into account that CREN bit overrides SREN

enter image description here

Upvotes: 0

Jean-francois
Jean-francois

Reputation: 316

That's the Continuous Receive Enable Bit, and you may want to set it up to 1.

RCSTA2bits.CREN = 1; //Enable UART Receiver

This enables the receiver in Asynchronous Mode.

Oh, and you may want to initialize registers this way :

RCSTA2 = 0b10010000; //Enable SPEN, CREN, no 9th bit

This way, if you refer to the datasheet, you can compare the register directly to the value you are writing instead of writing single bits and not taking care of the other's left (ADDEN).

Upvotes: 1

Related Questions