因此,有Master和Slave Attiny2313。主机发送9位数据(第9位TXB8设置为1),但从机未检测到第9位(RXB8仍为0)。
我认为,如果主机中的TXB8位置1,从机上的RXB8位置是否应该自动设置? (在代码中,我检查UCSRB中的RXB8是否设置为1。
这不是问题所在)

void USART_Init(void)
{
    /* Set baud rate */
    UBRRH = (unsigned char)(BAUDRATE>>8);
    UBRRL = (unsigned char)BAUDRATE;

    /* Enable receiver and transmitter */
    UCSRB = (1<<RXEN)|(1<<TXEN);

    /* Set frame format: 9data, 1stop bit */
    UCSRC = (7<<UCSZ0);
}

void USART_Transmit(unsigned int data)
{
    /* Wait for empty transmit buffer */
    while ( !( UCSRA & (1<<UDRE)) );

    /* Copy 9th bit to TXB8 */
    UCSRB &= ~(1<<TXB8);
    if ( data & 0x0100 )
        UCSRB |= (1<<TXB8);

    /* Put data into buffer, sends the data */

    UDR = data;

//Slave Receive Code

gned int USART_Receive( void )
{
    unsigned char status, resh, resl;

    /* Wait for data to be received */
    while ( !(UCSRA & (1<<RXC)) );



    /* Get status and 9th bit, then data from buffer */
    status = UCSRA;
    resh = UCSRB;
    resl = UDR;
        return resh; ///test
    /* If error, return -1 */
    if ( status & ((1<<FE)|(1<<DOR)|(1<<UPE)) )
    return -1;

    /* Filter the 9th bit, then return */
    resh = (resh >> 1) & 0x01;
    return ((resh << 8) | resl);

}

最佳答案

我发现了问题所在,USART的初始化如下:

/* Enable receiver and transmitter */
    UCSRB = (1<<RXEN)|(1<<TXEN);

    /* Set frame format: 9data, 1stop bit */
    UCSRC = (7<<UCSZ0);


但是UCSZ2位在USCRB寄存器中,而不在UCSRC中,因此正确的代码将是:

UCSRB = (1<<RXEN)|(1<<TXEN)|(1<<UCSZ2);
UCSRC  = (1<<UCSZ0)|(1<<UCSZ1);

关于c - AVR 9位Usart不起作用(MPCM),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33431374/

10-12 21:51