求助各位大神帮小弟指点下调试的方向;
STM32F417与STM32F103先各自进行回环模式调通的情况下,再将两块板子用线接起来进行普通模式调试,我尝试了各种方法,修改了时钟,波特率;检查两线间的电阻值60欧姆,检查了CAN_H和CAN_L的电压值,以及接线对应问题都正确的情况下,两个单片机相互发送数据,各自还是无法进入接收中断。以下是我这个项目的代码与原理图:
//STM32F103R8T6 CAN初始化配置
void CAN_Mode_Init(u8 tsjw,u8 tbs2,u8 tbs1,u16 brp,u8 mode)
{
GPIO_InitTypeDef GPIO_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //TX
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
CAN_InitStructure.CAN_TTCM=DISABLE;
CAN_InitStructure.CAN_ABOM=DISABLE;
CAN_InitStructure.CAN_AWUM=DISABLE;
CAN_InitStructure.CAN_NART=ENABLE;
CAN_InitStructure.CAN_RFLM=DISABLE;
CAN_InitStructure.CAN_TXFP=DISABLE;
CAN_InitStructure.CAN_Mode= mode;
CAN_InitStructure.CAN_SJW=tsjw;
CAN_InitStructure.CAN_BS1=tbs1;
CAN_InitStructure.CAN_BS2=tbs2;
CAN_InitStructure.CAN_Prescaler=brp;
CAN_Init(CAN1, &CAN_InitStructure);
CAN_FilterInitStructure.CAN_FilterNumber=0;
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0;
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//STM32F103R8T6 CAN接收中断
void USB_LP_CAN1_RX0_IRQHandler(void)
{
CanRxMsg RxMessage;
u8 i;
CAN_Receive(CAN1, 0, &RxMessage);
for(i=0;i<8;i++)
{
can_buf[i]=RxMessage.Data[i];
}
CAN_GET_BUF=1;
}
//STM32F103R8T6 主函数
extern volatile u8 can_buf[8];
volatile u8 CAN_GET_BUF = 0;
volatile u8 *p;
//u8 i;
int main(void)
{
p = GetCanBuf();
//for(i=0;i<8;i++)
//{
//can_buf[i] = 0xAA;
//}
CAN_Mode_Init(CAN_SJW_1tq,CAN_BS2_2tq,CAN_BS1_3tq,6,CAN_Mode_Normal);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
while(1)
{
if(CAN_GET_BUF == 1)
{
CommandResult(*(p+1));
CAN_GET_BUF=0;
}
//CAN1_Send_Msg((u8 *)can_buf,8);
}
}
/****************************************************************************/
//STM32F417ZGT6 CAN初始化配置
void CAN1_Mode_Init(u8 tsjw,u8 tbs2,u8 tbs1,u16 brp,u8 mode)
{
GPIO_InitTypeDef GPIO_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8| GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource8,GPIO_AF_CAN1);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource9,GPIO_AF_CAN1);
CAN_InitStructure.CAN_TTCM=DISABLE;
CAN_InitStructure.CAN_ABOM=DISABLE;
CAN_InitStructure.CAN_AWUM=DISABLE;
CAN_InitStructure.CAN_NART=ENABLE;
CAN_InitStructure.CAN_RFLM=DISABLE;
CAN_InitStructure.CAN_TXFP=DISABLE;
CAN_InitStructure.CAN_Mode= mode;
CAN_InitStructure.CAN_SJW=tsjw;
CAN_InitStructure.CAN_BS1=tbs1;
CAN_InitStructure.CAN_BS2=tbs2;
CAN_InitStructure.CAN_Prescaler=brp;
CAN_Init(CAN1, &CAN_InitStructure);
CAN_FilterInitStructure.CAN_FilterNumber=0;
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0;
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//STM32F417ZGT6接收中断配置
void CAN1_RX0_IRQHandler(void)
{
CanRxMsg RxMessage;
uchar i;
CAN_Receive(CAN1, 0, &RxMessage);
for(i=0;i<8;i++)
{
can_RX[i] = RxMessage.Data[i];
}
SETbCANBUF();
}
extern uchar flags1;
extern volatile u8 g_aSerBuf[605];
extern volatile u8 can_TX[8];
extern volatile u8 can_RX[8];
//STM32F417ZGT6主函数
int main(void)
{
volatile u8 *p;
p = GetSerBuf();
//u8 i;
//u8 res;
//for(i=0;i<8;i++)
//{
//can_TX[i] = 0x0F;
//}
CLRbSERIALBUF();
CLRbCANBUF();
//Stm32_Clock_Init(336,8,2,7);
uart3_init(9600);
LED1_INIT();
LED2_INIT();
CAN1_Mode_Init(CAN_SJW_1tq,CAN_BS2_6tq,CAN_BS1_7tq,3,CAN_Mode_Normal);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
while(1)
{
if(bSERIALBUF())
{
CommandResult(*(p+1)); //ÃüÁî½âÎö
CLRbSERIALBUF();
}
else if(bCANBUF())
{
PutDataPackage(8);
CLRbCANBUF();
}
//CAN1_Send_Msg((u8 *)can_TX,8);
//if(res == 0)
//{
//Uart0Putc(0x1d);
//break;
//}
}
}
/****************************************************/
原理图:
|