对于9Bit UART,Atmega Toggle变量太慢

时间:2017-11-29 07:05:15

标签: c uart atmega 9-bit-serial

我已经编写了一些代码来向usart输出一个9bit的消息。

代码如下:

bool_t SERIAL__TX_SEND_NINE(U8_t ch, bool_t nine)         // send a character
{

  bool_t result = SE_TRUE;              // assume OK

  // transceiver is on so send as soon as the buffer is ready
  while(SERIAL__TX_READY() == SE_FALSE)
  {                                     // make sure the Tx buffer is ready for another character
  }
  if (nine == TRUE)
  {   
    //SERIAL_USART_B |= 0x01;   
    UCSR0B &= ~(1<<TXB80);
    UCSR0B |= (1<<TXB80);
  }
  else
  {     
    //SERIAL_USART_B &= 0xFE;   
    UCSR0B &= ~(1<<TXB80);
  }

  SERIAL_UDR = ch;                      // then send the next character
  SERIAL_USART_A |= SERIAL_TX_DONE;     // clear the done flag
  return result;                        // OK
}

//! \brief  send a byte on the serial communications bus if ready - return TRUE if successful
//!
//! \param  ch the byte to transmit
//! \return SE_TRUE if the byte was sent, SE_FALSE can't happen for this device
bool_t serial_0_tx_send_if_ready_nine(U8_t ch, bool_t nine)         // send a character if able to
{
    // if buffer ready?
    if(SERIAL__TX_READY() == FALSE)
    {
        return FALSE;
    }

return SERIAL__TX_SEND_NINE(ch, nine);           // send the next character
}

SERIAL_UDR = UDR0

SERIAL_USART_A = UCSR0A

当我为代码运行时输入断点时,无论是函数的开头还是函数的结尾,它都按预期工作。第9位为每个数据包打开和关闭。 (总共5个数据包)

当我没有断点时,第九位切换似乎完全随机。 当我在if语句中只有一个断点时,它只会命中一次。 所以,我猜测'九'值设置得不够快,无法全速运行切换。

切换位正在预先设置功能。<​​/ p>

// outgoing state machine
// only process it if the function pointer isn't NULL
if(handle->send != NULL)
{
  // try and get a byte to send
  if(RingBuffer_GetByte(&handle->out_rb, &data) == RINGBUFFER_OK)
  {
    // we got something to send
        if (nine_toggle == TRUE)
        {
            nine_toggle = FALSE;
        }
        else
        {
            nine_toggle = TRUE;
        }

  if(serial_0_tx_send_if_ready_nine(data, nine_toggle) == SE_FALSE)
  {
    // but couldn't send it so put it back
    RingBuffer_PutBackByte(&handle->out_rb, data);
  }
  // otherwise we sent another one. ringbuffer does all the data handling so nothing else to do
}
}

但我不明白为什么会这样。

存储无符号字符时,atemga324p是否有定时延迟(bool_t)

任何想法都会受到赞赏。

其他细节。 MCU:Atmega324p。操作系统:Windows 10.编译器:Atmel Studio 7.0。优化:无。

1 个答案:

答案 0 :(得分:1)

想出来。

serial_0_tx_send_if_ready_nine(...)在全速运行时返回false,因为通讯端口尚未准备好发送数据。

因此它保持该字节并重新运行发送功能,但是,切换已经改变,我没有反转切换。

我将部分代码更改为以下内容:

  if(serial_0_tx_send_if_ready_nine(data, nine_toggle) == SE_FALSE)
  {
    // but couldn't send it so put it back
    RingBuffer8_PutBackByte(&handle->out_rb, data);
    nine_toggle ^= 1; //<== Added this line
  }