1
#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;                    \
                        }

对于最后一行,为什么需要将 RCSTA2bits.CREN 设置为 0?如果设置为o,我如何接收传入的数据?

使用高优先级接收中断初始化 UsART2

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

这是我的高中断代码

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

这是我的低中断代码

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;
}

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

这段代码不起作用,我不知道为什么。你们能帮我吗?

4

2 回答 2

1

那是连续接收启用位,您可能希望将其设置为 1。

RCSTA2bits.CREN = 1; //Enable UART Receiver

这使接收器处于异步模式。

哦,您可能想以这种方式初始化寄存器:

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

这样,如果您参考数据表,您可以直接将寄存器与您正在写入的值进行比较,而不是写入单个位并且不考虑其他的左侧(ADDEN)。

于 2014-07-08T11:42:47.857 回答
0
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 

还要考虑CREN到位覆盖 SREN

在此处输入图像描述

于 2014-07-08T12:33:48.487 回答