自己做的F103 + MCP2551的板,CAN死活不通,以为硬件设计有问题,又单独用焊片2551小板, 接到一个F405的板子上,能正常发送数据给别的CAN设备了,在接到103上,还是不通, 怀疑做的板有问题,又找一个F103的开发板来测,接这个2551小板,还是不通,现在软件硬件都查了无数回了,还是没找到原因:'(,
103回环模式,引脚有波形,正常下无,总线上以无波形。
测试代码:
//can function ------------------------------------------------
#define CAN_REMAP
#define USE_CAN
int canStat = 0;
static void can_rcc_config()
{
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
//RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd( RCC_APB1Periph_CAN1, ENABLE);
#ifdef CAN_REMAP
//Remap to PB8, PB9
GPIO_PinRemapConfig( GPIO_Remap1_CAN1, ENABLE);
#endif
}
static void can_gpio_config()
{
GPIO_InitTypeDef initStructure;
#ifdef CAN_REMAP
//CAN RX
initStructure.GPIO_Pin = GPIO_Pin_8;
initStructure.GPIO_Speed = GPIO_Speed_50MHz;
initStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init( GPIOB, &initStructure );
//CAN TX
initStructure.GPIO_Pin = GPIO_Pin_9;
initStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init( GPIOB, &initStructure );
#else
//CAN RX
initStructure.GPIO_Pin = GPIO_Pin_11;
initStructure.GPIO_Speed = GPIO_Speed_50MHz;
initStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init( GPIOA, &initStructure );
//CAN TX
initStructure.GPIO_Pin = GPIO_Pin_12;
initStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init( GPIOA, &initStructure );
#endif
//?
//GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_CAN0);
//GPIO_PinAFConfig(GPIOA,GPIO_PinSource12,GPIO_AF_CAN1); //GPIOA12复用为CAN1
}
static void can_config()
{
CAN_InitTypeDef initStructure;
CAN_FilterInitTypeDef filterInitStructure;
//CAN_DeInit( CAN1 );
CAN_StructInit(&initStructure );
initStructure.CAN_TTCM = DISABLE;
initStructure.CAN_ABOM = DISABLE; //如果ABOM位为’1’,bxCAN进入离线状态后,就自动开启恢复过程
initStructure.CAN_AWUM = DISABLE;
initStructure.CAN_NART = DISABLE; // DISABLE; //禁止自动重发
initStructure.CAN_RFLM = DISABLE;
initStructure.CAN_TXFP = DISABLE;
initStructure.CAN_Mode = CAN_Mode_Normal; //CAN_Mode_LoopBack , CAN_Mode_Normal;
initStructure.CAN_SJW = CAN_SJW_1tq; // CAN_SJW_1tq;
initStructure.CAN_BS1 = CAN_BS1_8tq; //CAN_BS1_5tq; // CAN_BS1_8tq;
initStructure.CAN_BS2 = CAN_BS2_7tq;// CAN_BS2_3tq; // CAN_BS2_7tq;
//36M / ( 1 + 5 + 3 ) / 16 = 250kbps
//2,1,9 = 1M; 8,7,9 = 250k
initStructure.CAN_Prescaler = 9; //16
CAN_Init(CAN1, &initStructure );
filterInitStructure.CAN_FilterNumber = 0;
filterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
filterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
filterInitStructure.CAN_FilterIdHigh = 0x0000;// << 3;
filterInitStructure.CAN_FilterIdLow = 0x0000;
filterInitStructure.CAN_FilterMaskIdHigh = 0x0000;// << 3;
filterInitStructure.CAN_FilterMaskIdLow = 0x0000;
filterInitStructure.CAN_FilterFIFOAssignment = CAN_FIFO0;
filterInitStructure.CAN_FilterActivation = ENABLE;
CAN_FilterInit( &filterInitStructure );
}
void can_sendData()
{
static uint32_t count = 0;
int tryCount = 0;
uint8_t ret = 0;
uint8_t transmitMailbox = 0;
CanTxMsg txMessage;
CanRxMsg rxMessage;
//txMessage.ExtId = 0x00AA0000;
txMessage.StdId = 0x123;
txMessage.ExtId = 0x12345;
txMessage.RTR = CAN_RTR_DATA;
txMessage.IDE = CAN_ID_EXT;
//txMessage.IDE = CAN_ID_STD;
txMessage.DLC = 8;
txMessage.Data[0] = 0x00;
txMessage.Data[1] = 0x12;
txMessage.Data[2] = 0x34;
txMessage.Data[3] = 0x56;
txMessage.Data[4] = ((char*)&count)[0];// 0x78;
txMessage.Data[5] = ((char*)&count)[1];//0xAB;
txMessage.Data[6] = ((char*)&count)[2];//0xCD;
txMessage.Data[7] = ((char*)&count)[3];//0xEF;
count++;
transmitMailbox = CAN_Transmit(CAN1, &txMessage );
if( transmitMailbox < 3 )
{
ret = CAN_TransmitStatus(CAN1, transmitMailbox);
while( tryCount < 1000 && (ret) != CANTXOK )
{
ret = CAN_TransmitStatus(CAN1, transmitMailbox);
tryCount++;
};
}
}
|