我正在尝试通过 CAN 和 UART 与计算机
通信。代码在附件和下面。我想知道为什么我会出现图片中的情况
#include "Cpu.h"
#include "clockMan1.h"
#include "dmaController1.h"
#include "pin_mux.h"
#include "lpuart1.h"
#include
#include
#include
易失性 int exit_code = 0;
typedef enum
{
helloa = 0x00U,
hia = 0x01U
} can_commands_list;
#define TX_MAILBOX (1UL)
#define TX_MSG_ID (1UL)
#define RX_MAILBOX (0UL)
#define RX_MSG_ID (2UL)
#define welcomeMsg "现在你可以开始输入:
"
#define errorMsg "发生错误!应用程序将停止!
"
#define BUFFER_SIZE 256U
#define tiMEOUT 100U
uint8_t buffer[BUFFER_SIZE];
uint8_t bufferIdx;
uint8_t xinxi = (uint8_t)你好;
/* User includes (#include below this line below is not maintained by Processor Expert) */
void Init(void);
void rxCallback(void *driverState, uart_event_t event, void *userData)
{
/* 未使用的参数 */
(void)driverState;
(无效)用户数据;
/* 检查事件类型 */
if (event == UART_EVENT_RX_FULL)
{
/* 当接收到换行符或缓冲区已满时停止接收 */
if ((buffer[bufferIdx] != '
') && (bufferIdx ! = (BUFFER_SIZE - 2U)))
{
/* 更新缓冲区索引和 rx 缓冲区 */
bufferIdx++;
LPUART_DRV_SetRxBuffer(INST_LPUART1, &buffer[bufferIdx], 1U);
} }
}
void Init(void)
{
CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,
g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
CLOCK_SYS_UpdateConfiguration(0U,CLOCK_MANAGER_POLICY_FORCIBLE);
PINS_DRV_Init(NUM_OF_CONFIGURED_PINS,g_pin_mux_InitConfigArr);
LPUART_DRV_Init(INST_LPUART1, &lpuart1_State, &lpuart1_InitConfig0);
CAN_Init(&can_pal1_instance, &can_pal1_Config0);
}
/*!
rief 项目的主要功能。
details 启动初始化顺序如下:
* - 启动 asm 例程
* - main()
*/
int main(void)
{
/* 在这里写下你的局部变量定义 */
/*** Processor Expert 内部初始化。不要删除此代码!!!***/
#ifdef PEX_RTOS_INIT
PEX_RTOS_INIT(); /* 所选 RTOS 的初始化。宏由 RTOS 组件定义。*/
#endif
/*** Processor Expert 内部初始化结束。***/
uint32_t bytesRemaining;
status_t 状态;
/* 在这里写你的代码 */
Init();
LPUART_DRV_InstallRxCallback(INST_LPUART1,rxCallback,NULL);
LPUART_DRV_SendDataBlocking(INST_LPUART1,(uint8_t *)welcomeMsg,strlen(welcomeMsg),超时);
can_buff_config_t buffCfg = {
.enableFD = true,
.enableBRS = true,
.fdPadding = 0U,
.idType = CAN_MSG_ID_STD,
.isRemote = false
};
CAN_ConfigTxBuff(&can_pal1_instance,TX_MAILBOX, &buffCfg);
can_message_t message = {
.cs = 0U,
.id = TX_MSG_ID,
.data[0] = helloa,
.length = 1U
};
while(1)
{
LPUART_DRV_ReceiveData(INST_LPUART1,buffer,1U);
while(LPUART_DRV_GetReceiveStatus(INST_LPUART1,&bytesRemaining)==STATUS_BUSY);
status=LPUART_DRV_GetReceiveStatus(INST_LPUART1,&bytesRemaining);
if (status != STATUS_SUCCESS)
{
/* 如果发生错误,发送错误信息并退出循环 */
LPUART_DRV_SendDataBlocking(INST_LPUART1, (uint8_t *)errorMsg, strlen(errorMsg), TIMEOUT);
休息;
}
/* 将字符串终止符附加到接收到的数据 */
bufferIdx++;
缓冲区[bufferIdx] = 0U;
if(strcmp((char *)buffer, "Hello") != 0)
{
CAN_Send(&can_pal1_instance, TX_MAILBOX, &message);
CAN_ConfigRxBuff(&can_pal1_instance, RX_MAILBOX, &buffCfg, RX_MSG_ID);
can_message_t recvMsg;
CAN_Receive(&can_pal1_instance, RX_MAILBOX, &recvMsg);
while(CAN_GetTransferStatus(&can_pal1_instance, RX_MAILBOX) == STATUS_BUSY);
if((recvMsg.data[0] == helloa) &&
recvMsg.id == RX_MSG_ID)
{
/* 切换输出值 LED1 */
PINS_DRV_WritePin(PTD,0,1);
}
else if((recvMsg.data[0] == hia) &&
recvMsg.id == RX_MSG_ID)
{
/* 切换输出值 LED0 */
PINS_DRV_WritePin(PTD,16,1);
}
}
else if(strcmp((char *)buffer, "Hello world") != 0)
{
xinxi=hia;
}
else
{
PINS_DRV_WritePin(PTD,15,1);
}
/* Send the received data back */
LPUART_DRV_SendDataBlocking(INST_LPUART1, buffer, bufferIdx, TIMEOUT);
/* Reset the buffer index to start a new reception */
bufferIdx = 0U;
}
/* For example: for(;;) { } */
/*** 不要写任何经过这一行的代码,否则它会在代码生成时被删除。***/
/*** RTOS 启动代码。宏 PEX_RTOS_START 由 RTOS 组件定义。不要修改此代码!!!***/
#ifdef PEX_RTOS_START
PEX_RTOS_START(); /* 启动选定的 RTOS。宏由 RTOS 组件定义。*/
#endif
/*** RTOS 启动代码结束。***/
/*** Processor Expert 主例程结束。不要修改此代码!!!***/
for(;;) {
if(exit_code != 0) {
中断;
}
}
返回退出代码;
/*** Processor Expert 主例程结束。不要在下面写代码!!!***/
}