本文共 846 字,大约阅读时间需要 2 分钟。
首先在中断例程中使用sprintf是一个非常糟糕的想法甚至id禁用中断 . 更糟糕的是将数据发送到那里:)你可能是一个非常非常初学者 . 始终保持中断例程尽可能短 .
将调试代码移到中断之外 .
其次,你只能读取你在中断中收到的内容(这是一个字节)而你只读了两个 .
最后,我不认为 UART_GetByte 被发明用于中断例程 . 只需读取数据寄存器即可 .
我个人更喜欢工会 .
typedef union
{
uint16_t u16;
int16_t i16;
uint8_t b[2];
}data16;
volatile data16 revcb, tb; // tb can be local and not volatile
volatile int pointer = 0;
volatile int flag = 0;
CY_ISR(UART_ISR){
Status_pin_Write(0xFF); //Used for debugging
revcb.b[pointer] = dataregister; // place the appripriate code here
pointer = ! pointer;
if(!pointer) flag = 1;
Status_pin_Write(0x00);
}
//in the main function
while(1)
{
if(flag)
{
ISR_Rx_Disable(); // make it atomic
tb = recv;
flag = 0;
ISR_Rx_Enable();
sprintf(TransmitBufferUSB,"%d\n\r",tb.u16);
UART_USB_PutString(TransmitBufferUSB);
}
}
但请记住 - 当您发送调试数据时 - 许多其他值可能会出现,您可能会丢失一些东西 . 您需要实现循环缓冲区 - 但这超出了此问题的范围 .
转载地址:https://blog.csdn.net/weixin_33443932/article/details/114879347 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!