我编写了一些代码输出给usart 9位消息。
守则如下:
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
每当代码运行时,无论是函数的开始还是函数的结束,当我输入断点时,它都会像预期的那样工作。第九位切换每个数据包的开关。(共5个数据包)
当我没有断点时,当第九位切换似乎完全是随机的。当if语句中只有一个断点时,它只命中一次。因此,我猜‘9’值设置得不够快,无法全速运行切换。
切换位在手动之前被设置为一个函数。
// 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)时是否有定时延迟?
任何想法都会很感激。
其他细节。单片机: Atmega324p。操作系统: Windows 10.编译器: Atmel Studio 7.0。优化:无。
发布于 2017-11-30 00:22:23
弄明白了。
Serial__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
}https://stackoverflow.com/questions/47546898
复制相似问题