【分享帖】stm32can通信
1363
0
15
2015-03-26
我们在做stm32f4底盘主控与stm32f1底盘驱动的can通信时,发现无法正常通信。但是stm32f4can回环测试显示正常,stm32f1与stm32f1之间通信正常,已经在这一条上卡了几天了,求大神指导f4驱动代码:
void CAN1_Configuration(void)
{
CAN_InitTypeDef can;
CAN_FilterInitTypeDef can_filter;
GPIO_InitTypeDef gpio;
NVIC_InitTypeDef nvic;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_CAN1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_CAN1);
gpio.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_11;
gpio.GPIO_Mode = GPIO_Mode_AF;
GPIO_Init(GPIOA, &gpio);
nvic.NVIC_IRQChannel = CAN1_RX0_IRQn;
nvic.NVIC_IRQChannelPreemptionPriority = 2;
nvic.NVIC_IRQChannelSubPriority = 1;
nvic.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvic);
nvic.NVIC_IRQChannel = CAN1_TX_IRQn;
nvic.NVIC_IRQChannelPreemptionPriority = 1;
nvic.NVIC_IRQChannelSubPriority = 1;
nvic.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvic);
CAN_DeInit(CAN1);
CAN_StructInit(&can);
can.CAN_TTCM = DISABLE;
can.CAN_ABOM = DISABLE;
can.CAN_AWUM = DISABLE;
can.CAN_NART = DISABLE;
can.CAN_RFLM = DISABLE;
can.CAN_TXFP = ENABLE;
can.CAN_Mode = CAN_Mode_Normal;
can.CAN_SJW = CAN_SJW_1tq;
can.CAN_BS1 = CAN_BS1_9tq;
can.CAN_BS2 = CAN_BS2_4tq;
can.CAN_Prescaler = 3; //CAN BaudRate 42/(1+9+4)/3=1Mbps
CAN_Init(CAN1, &can);
can_filter.CAN_FilterNumber=0;
can_filter.CAN_FilterMode=CAN_FilterMode_IdMask;
can_filter.CAN_FilterScale=CAN_FilterScale_32bit;
can_filter.CAN_FilterIdHigh=0x0000;
can_filter.CAN_FilterIdLow=0x0000;
can_filter.CAN_FilterMaskIdHigh=0x0000;
can_filter.CAN_FilterMaskIdLow=0x0000;
can_filter.CAN_FilterFIFOAssignment=0;
can_filter.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&can_filter);
CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);
CAN_ITConfig(CAN1,CAN_IT_TME,ENABLE);
}
void CAN1WriteData(unsigned char add,char *s)
{
int i = 0;
unsigned char TransmitMailbox;
CanTxMsg tx_message;
tx_message.StdId = 0xFF;
tx_message.ExtId = add;
tx_message.IDE = CAN_ID_EXT;
tx_message.RTR = CAN_RTR_Data;
tx_message.DLC = 0x08;
tx_message.Data[0] = s[0];
tx_message.Data[1] = s[1];
tx_message.Data[2] = s[2];
tx_message.Data[3] = s[3];
tx_message.Data[4] = s[4];
tx_message.Data[5] = s[5];
tx_message.Data[6] = s[6];
tx_message.Data[7] = s[7];
//CAN_Transmit(CAN1,&tx_message);
TransmitMailbox=CAN_Transmit(CAN1,&tx_message); //·¢Ëͱ¨ÎIJ¢·µ»Ø·¢ËÍÐÅÏäµÄµØÖ·
i = 0;
while((CAN_TransmitStatus(CAN1,TransmitMailbox)== CANTXOK) && (i != 0xFF)) //¼ì²é±¨ÎÄ·¢ËÍÊÇ·ñÍê³É
{
i++;
}
i = 0;
while((CAN_MessagePending(CAN1,CAN_FIFO0) < 1) && (i != 0xFF)) //·µ»Ø±»¹ÒÆðµÄ±¨ÎÄÁ¿ÊÇ·ñΪ0
{
i++;
}
}
void CAN1_TX_IRQHandler(void)
{
if (CAN_GetITStatus(CAN1,CAN_IT_TME)!= RESET)
{
CAN_ClearITPendingBit(CAN1,CAN_IT_TME);
printf("main control transmit data\n");
}
}
void CAN1_RX0_IRQHandler(void)
{
CanRxMsg rx_message;
if (CAN_GetITStatus(CAN1,CAN_IT_FMP0)!= RESET)
{
CAN_ClearITPendingBit(CAN1, CAN_IT_FMP0);
CAN_Receive(CAN1, CAN_FIFO0, &rx_message);
}
}
f1驱动代码:
void CAN_Configuration(void)
{
CAN_InitTypeDef can;
CAN_FilterInitTypeDef can_filter;
GPIO_InitTypeDef gpio;
NVIC_InitTypeDef nvic;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB |
RCC_APB2Periph_AFIO , ENABLE);
GPIO_PinRemapConfig(GPIO_Remap1_CAN1,ENABLE);
gpio.GPIO_Pin = GPIO_Pin_8;
gpio.GPIO_Mode = GPIO_Mode_IN_FLOATING;
gpio.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &gpio); //CAN_RX
gpio.GPIO_Pin = GPIO_Pin_9;
gpio.GPIO_Mode = GPIO_Mode_AF_PP;
gpio.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &gpio); //CAN_TX
nvic.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
nvic.NVIC_IRQChannelPreemptionPriority = 0;
nvic.NVIC_IRQChannelSubPriority = 0;
nvic.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvic);
CAN_DeInit(CAN1);
can.CAN_TTCM = DISABLE;
can.CAN_ABOM = DISABLE;
can.CAN_AWUM = DISABLE;
can.CAN_NART = DISABLE;
can.CAN_RFLM = DISABLE;
can.CAN_TXFP = ENABLE;
can.CAN_Mode = CAN_Mode_Normal;
can.CAN_SJW = CAN_SJW_1tq;
can.CAN_BS1 = CAN_BS1_5tq;
can.CAN_BS2 = CAN_BS2_3tq;
can.CAN_Prescaler = 4; //CAN BaudRate 36/(1+5+3)/4=1Mbps
if(CAN_Init(CAN1, &can) == CANINITFAILED)
{
LED_RED_ON();
return ;
}
can_filter.CAN_FilterNumber = 0;
can_filter.CAN_FilterMode = CAN_FilterMode_IdMask;
can_filter.CAN_FilterScale = CAN_FilterScale_32bit;
can_filter.CAN_FilterIdHigh = 0;
can_filter.CAN_FilterIdLow = 0;
can_filter.CAN_FilterMaskIdHigh = 0;
can_filter.CAN_FilterMaskIdLow = 0;
can_filter.CAN_FilterFIFOAssignment = CAN_FIFO0;
can_filter.CAN_FilterActivation = ENABLE;
CAN_FilterInit(&can_filter);
CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);
CAN_ITConfig(CAN1,CAN_IT_TME,ENABLE);
}
//CAN1·¢ËÍÊý¾Ý²ÎÊý
//ÊäÈë²ÎÊý£º·¢ËÍ8¸ö×Ö½ÚµÄ×Ö·ûÊý×é
void CAN1WriteData(unsigned char add,char *s)
{
int i = 0;
unsigned char TransmitMailbox;
CanTxMsg tx_message;
tx_message.StdId = 0xFF;
tx_message.ExtId = add;
tx_message.IDE = CAN_ID_EXT;
tx_message.RTR = CAN_RTR_Data;
tx_message.DLC = 0x08;
tx_message.Data[0] = s[0];
tx_message.Data[1] = s[1];
tx_message.Data[2] = s[2];
tx_message.Data[3] = s[3];
tx_message.Data[4] = s[4];
tx_message.Data[5] = s[5];
tx_message.Data[6] = s[6];
tx_message.Data[7] = s[7];
//CAN_Transmit(CAN1,&tx_message);
TransmitMailbox=CAN_Transmit(CAN1,&tx_message); //·¢Ëͱ¨ÎIJ¢·µ»Ø·¢ËÍÐÅÏäµÄµØÖ·
i = 0;
while((CAN_TransmitStatus(CAN1,TransmitMailbox)== CANTXOK) && (i != 0xFF)) //¼ì²é±¨ÎÄ·¢ËÍÊÇ·ñÍê³É
{
i++;
}
i = 0;
while((CAN_MessagePending(CAN1,CAN_FIFO0) < 1) && (i != 0xFF)) //·µ»Ø±»¹ÒÆðµÄ±¨ÎÄÁ¿ÊÇ·ñΪ0
{
i++;
}
}
extern int Speed;
void USB_LP_CAN1_RX0_IRQHandler(void)
{
CanRxMsg rx_message;
if (CAN_GetITStatus(CAN1,CAN_IT_FMP0)!= RESET)
{
CAN_Receive(CAN1, CAN_FIFO0, &rx_message);
switch(rx_message.Data[1])
{
case 'A' :Speed = 2500;break;
case 'B' :Speed = 2000;break;
case 'C' :Speed = 1500;break;
case 'D' :Speed = 1000; break;
case 'E' :Speed = 500; break;
default : Speed = 0; break;
}
if(rx_message.Data[0] == 'F')
Speed = Speed;
else if(rx_message.Data[0] == 'B')
Speed = -Speed;
CAN_ClearITPendingBit(CAN1, CAN_IT_FMP0);
}
}
void CAN1_Configuration(void)
{
CAN_InitTypeDef can;
CAN_FilterInitTypeDef can_filter;
GPIO_InitTypeDef gpio;
NVIC_InitTypeDef nvic;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_CAN1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_CAN1);
gpio.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_11;
gpio.GPIO_Mode = GPIO_Mode_AF;
GPIO_Init(GPIOA, &gpio);
nvic.NVIC_IRQChannel = CAN1_RX0_IRQn;
nvic.NVIC_IRQChannelPreemptionPriority = 2;
nvic.NVIC_IRQChannelSubPriority = 1;
nvic.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvic);
nvic.NVIC_IRQChannel = CAN1_TX_IRQn;
nvic.NVIC_IRQChannelPreemptionPriority = 1;
nvic.NVIC_IRQChannelSubPriority = 1;
nvic.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvic);
CAN_DeInit(CAN1);
CAN_StructInit(&can);
can.CAN_TTCM = DISABLE;
can.CAN_ABOM = DISABLE;
can.CAN_AWUM = DISABLE;
can.CAN_NART = DISABLE;
can.CAN_RFLM = DISABLE;
can.CAN_TXFP = ENABLE;
can.CAN_Mode = CAN_Mode_Normal;
can.CAN_SJW = CAN_SJW_1tq;
can.CAN_BS1 = CAN_BS1_9tq;
can.CAN_BS2 = CAN_BS2_4tq;
can.CAN_Prescaler = 3; //CAN BaudRate 42/(1+9+4)/3=1Mbps
CAN_Init(CAN1, &can);
can_filter.CAN_FilterNumber=0;
can_filter.CAN_FilterMode=CAN_FilterMode_IdMask;
can_filter.CAN_FilterScale=CAN_FilterScale_32bit;
can_filter.CAN_FilterIdHigh=0x0000;
can_filter.CAN_FilterIdLow=0x0000;
can_filter.CAN_FilterMaskIdHigh=0x0000;
can_filter.CAN_FilterMaskIdLow=0x0000;
can_filter.CAN_FilterFIFOAssignment=0;
can_filter.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&can_filter);
CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);
CAN_ITConfig(CAN1,CAN_IT_TME,ENABLE);
}
void CAN1WriteData(unsigned char add,char *s)
{
int i = 0;
unsigned char TransmitMailbox;
CanTxMsg tx_message;
tx_message.StdId = 0xFF;
tx_message.ExtId = add;
tx_message.IDE = CAN_ID_EXT;
tx_message.RTR = CAN_RTR_Data;
tx_message.DLC = 0x08;
tx_message.Data[0] = s[0];
tx_message.Data[1] = s[1];
tx_message.Data[2] = s[2];
tx_message.Data[3] = s[3];
tx_message.Data[4] = s[4];
tx_message.Data[5] = s[5];
tx_message.Data[6] = s[6];
tx_message.Data[7] = s[7];
//CAN_Transmit(CAN1,&tx_message);
TransmitMailbox=CAN_Transmit(CAN1,&tx_message); //·¢Ëͱ¨ÎIJ¢·µ»Ø·¢ËÍÐÅÏäµÄµØÖ·
i = 0;
while((CAN_TransmitStatus(CAN1,TransmitMailbox)== CANTXOK) && (i != 0xFF)) //¼ì²é±¨ÎÄ·¢ËÍÊÇ·ñÍê³É
{
i++;
}
i = 0;
while((CAN_MessagePending(CAN1,CAN_FIFO0) < 1) && (i != 0xFF)) //·µ»Ø±»¹ÒÆðµÄ±¨ÎÄÁ¿ÊÇ·ñΪ0
{
i++;
}
}
void CAN1_TX_IRQHandler(void)
{
if (CAN_GetITStatus(CAN1,CAN_IT_TME)!= RESET)
{
CAN_ClearITPendingBit(CAN1,CAN_IT_TME);
printf("main control transmit data\n");
}
}
void CAN1_RX0_IRQHandler(void)
{
CanRxMsg rx_message;
if (CAN_GetITStatus(CAN1,CAN_IT_FMP0)!= RESET)
{
CAN_ClearITPendingBit(CAN1, CAN_IT_FMP0);
CAN_Receive(CAN1, CAN_FIFO0, &rx_message);
}
}
f1驱动代码:
void CAN_Configuration(void)
{
CAN_InitTypeDef can;
CAN_FilterInitTypeDef can_filter;
GPIO_InitTypeDef gpio;
NVIC_InitTypeDef nvic;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB |
RCC_APB2Periph_AFIO , ENABLE);
GPIO_PinRemapConfig(GPIO_Remap1_CAN1,ENABLE);
gpio.GPIO_Pin = GPIO_Pin_8;
gpio.GPIO_Mode = GPIO_Mode_IN_FLOATING;
gpio.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &gpio); //CAN_RX
gpio.GPIO_Pin = GPIO_Pin_9;
gpio.GPIO_Mode = GPIO_Mode_AF_PP;
gpio.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &gpio); //CAN_TX
nvic.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
nvic.NVIC_IRQChannelPreemptionPriority = 0;
nvic.NVIC_IRQChannelSubPriority = 0;
nvic.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvic);
CAN_DeInit(CAN1);
can.CAN_TTCM = DISABLE;
can.CAN_ABOM = DISABLE;
can.CAN_AWUM = DISABLE;
can.CAN_NART = DISABLE;
can.CAN_RFLM = DISABLE;
can.CAN_TXFP = ENABLE;
can.CAN_Mode = CAN_Mode_Normal;
can.CAN_SJW = CAN_SJW_1tq;
can.CAN_BS1 = CAN_BS1_5tq;
can.CAN_BS2 = CAN_BS2_3tq;
can.CAN_Prescaler = 4; //CAN BaudRate 36/(1+5+3)/4=1Mbps
if(CAN_Init(CAN1, &can) == CANINITFAILED)
{
LED_RED_ON();
return ;
}
can_filter.CAN_FilterNumber = 0;
can_filter.CAN_FilterMode = CAN_FilterMode_IdMask;
can_filter.CAN_FilterScale = CAN_FilterScale_32bit;
can_filter.CAN_FilterIdHigh = 0;
can_filter.CAN_FilterIdLow = 0;
can_filter.CAN_FilterMaskIdHigh = 0;
can_filter.CAN_FilterMaskIdLow = 0;
can_filter.CAN_FilterFIFOAssignment = CAN_FIFO0;
can_filter.CAN_FilterActivation = ENABLE;
CAN_FilterInit(&can_filter);
CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);
CAN_ITConfig(CAN1,CAN_IT_TME,ENABLE);
}
//CAN1·¢ËÍÊý¾Ý²ÎÊý
//ÊäÈë²ÎÊý£º·¢ËÍ8¸ö×Ö½ÚµÄ×Ö·ûÊý×é
void CAN1WriteData(unsigned char add,char *s)
{
int i = 0;
unsigned char TransmitMailbox;
CanTxMsg tx_message;
tx_message.StdId = 0xFF;
tx_message.ExtId = add;
tx_message.IDE = CAN_ID_EXT;
tx_message.RTR = CAN_RTR_Data;
tx_message.DLC = 0x08;
tx_message.Data[0] = s[0];
tx_message.Data[1] = s[1];
tx_message.Data[2] = s[2];
tx_message.Data[3] = s[3];
tx_message.Data[4] = s[4];
tx_message.Data[5] = s[5];
tx_message.Data[6] = s[6];
tx_message.Data[7] = s[7];
//CAN_Transmit(CAN1,&tx_message);
TransmitMailbox=CAN_Transmit(CAN1,&tx_message); //·¢Ëͱ¨ÎIJ¢·µ»Ø·¢ËÍÐÅÏäµÄµØÖ·
i = 0;
while((CAN_TransmitStatus(CAN1,TransmitMailbox)== CANTXOK) && (i != 0xFF)) //¼ì²é±¨ÎÄ·¢ËÍÊÇ·ñÍê³É
{
i++;
}
i = 0;
while((CAN_MessagePending(CAN1,CAN_FIFO0) < 1) && (i != 0xFF)) //·µ»Ø±»¹ÒÆðµÄ±¨ÎÄÁ¿ÊÇ·ñΪ0
{
i++;
}
}
extern int Speed;
void USB_LP_CAN1_RX0_IRQHandler(void)
{
CanRxMsg rx_message;
if (CAN_GetITStatus(CAN1,CAN_IT_FMP0)!= RESET)
{
CAN_Receive(CAN1, CAN_FIFO0, &rx_message);
switch(rx_message.Data[1])
{
case 'A' :Speed = 2500;break;
case 'B' :Speed = 2000;break;
case 'C' :Speed = 1500;break;
case 'D' :Speed = 1000; break;
case 'E' :Speed = 500; break;
default : Speed = 0; break;
}
if(rx_message.Data[0] == 'F')
Speed = Speed;
else if(rx_message.Data[0] == 'B')
Speed = -Speed;
CAN_ClearITPendingBit(CAN1, CAN_IT_FMP0);
}
}
文章标签
请问这篇文章对你有用吗?
【分享帖】stm32can通信