调试环境:两个STM32的CAN对联(STM32 PB8和PB9连接到ISO1050)
现在:在环回模式下,CAN自己发送接收都可以,但是在正常模式下,发送接收都不行,用CAN_TransmitStatus查看,发现发送的消息一直在pengding,不知道为什么,各位高手指点一下
代码如下:
#define MAX_MAIL_NUM 3
//CAN总线调试:0=运行 1=自环调试
#define CAN_DEBUG 0
//CAN总线波特率:0=250kbps,1=500kbps,2=1Mbps
#define CAN1_BPS 0
u8 CAN_msg_num[MAX_MAIL_NUM];
void CAN1_Config_init(void)
{
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
/* CAN register init */
CAN_DeInit(CAN1);
CAN_StructInit(&CAN_InitStructure);
/* CAN cell init */ //36MHz 500Kbps
CAN_InitStructure.CAN_TTCM=DISABLE;//禁止时间触发通信模式
CAN_InitStructure.CAN_ABOM=DISABLE;//软件对CAN_MCR寄存器的INRQ位进行置1随后清0后,一旦硬件检测
//到128次11位连续的隐性位,就退出离线状态
CAN_InitStructure.CAN_AWUM=DISABLE;//睡眠模式通过清除CAN_MCR寄存器的SLEEP位,由软件唤醒
CAN_InitStructure.CAN_NART=DISABLE;//CAN报文是否只发1次,不管发送的结果如何(成功/出错或仲裁丢失)
CAN_InitStructure.CAN_RFLM=DISABLE;//在接收到溢出时FIFO未被锁定,当接收到FIFO报文未被读出,下一个收到的报文会覆盖原有的报文
CAN_InitStructure.CAN_TXFP=DISABLE;//发送的FIFO优先级由报文的标识符来决定
//#if CAN_DEBUG
//CAN_InitStructure.CAN_Mode= CAN_Mode_LoopBack;
//#else
CAN_InitStructure.CAN_Mode= CAN_Mode_Normal;
//#endif
//传输波特率
if(CAN1_BPS == 0)
{
//CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;//重新同步跳跃宽度1个时间单位
//CAN_InitStructure.CAN_BS1=CAN_BS1_12tq;//时间段1为9个时间单位
//CAN_InitStructure.CAN_BS2=CAN_BS2_3tq;//时间段2为8个时间单位
//CAN_InitStructure.CAN_Prescaler= 9;//36M/(1+12+3)/9= 250kbps
//36M/(1+5+2)/9 = 500kbps
//36M(1+2+1)/9 = 1M
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1=CAN_BS1_8tq;
CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;
CAN_InitStructure.CAN_Prescaler=9;
}
else if(CAN1_BPS == 1)
{
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;//重新同步跳跃宽度1个时间单位
CAN_InitStructure.CAN_BS1=CAN_BS1_5tq;//时间段1为9个时间单位
CAN_InitStructure.CAN_BS2=CAN_BS2_2tq;//时间段2为8个时间单位
CAN_InitStructure.CAN_Prescaler= 9;//36M/(1+12+3)/9= 250kbps
}
else
{
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;//重新同步跳跃宽度1个时间单位
CAN_InitStructure.CAN_BS1=CAN_BS1_2tq;//时间段1为9个时间单位
CAN_InitStructure.CAN_BS2=CAN_BS2_1tq;//时间段2为8个时间单位
CAN_InitStructure.CAN_Prescaler= 9;//36M/(1+12+3)/9= 250kbps
}
CAN_Init(CAN1,&CAN_InitStructure);
/* CAN filter init */
CAN_FilterInitStructure.CAN_FilterNumber=0; //指定了待初始化的过滤器0
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;//指定了过滤器将被初始化到的模式为标识符屏蔽位模式
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;;//给出了过滤器位宽1个32位过滤器
CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;//用来设定过滤器标识符(32位位宽时为其高段位,16位位宽时为第一个)
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;;//用来设定过滤器标识符(32位位宽时为其低段位,16位位宽时为第二个)
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;//用来设定过滤器屏蔽标识符或者过滤器标识符(32位位宽时为其高段位,16位位宽时为第一个)
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;//用来设定过滤器屏蔽标识符或者过滤器标识符(32位位宽时为其低段位,16位位宽时为第二个)
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0;//设定了指向过滤器的FIFO0
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;//使能过滤器
CAN_FilterInit(&CAN_FilterInitStructure);
CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
}
// ***************************************************************
// BaudRate = 1 / NominalBitTime
// NominalBitTime = 1tq + tBS1 + tBS2
// tq = (BRP[9:0] + 1) x tPCLK
// tPCLK = CAN's clock = APB1's clock
// 1Mbps 速率下,采用点的位置在6tq位置处,BS1=5, BS2=2
// 500kbps 速率下,采用点的位置在8tq位置处,BS1=7, BS2=3
// 250kbps 速率下,采用点的位置在14tq位置处,BS1=13, BS2=2
// 125k, 100k, 50k, 20k, 10k 的采用点位置与 250K 相同
// ****************************************************************
void CAN1_Com_init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable CAN RX0 interrupt IRQ channel */
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USB_HP_CAN1_TX_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
//Can Rx
//GPIO_InitStruct.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
//GPIO_Init(GPIOA,&GPIO_InitStruct);
GPIO_Init(GPIOB,&GPIO_InitStruct);
//Can Tx
//GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
//GPIO_Init(GPIOA,&GPIO_InitStruct);
GPIO_Init(GPIOB,&GPIO_InitStruct);
GPIO_PinRemapConfig(GPIO_Remap1_CAN1, ENABLE);
CAN1_Config_init();
//CAN_ITConfig(CAN1,CAN_IT_FMP0 | CAN_IT_FF0 | CAN_IT_FOV0, ENABLE); // fifo0中断
//CAN_ITConfig(CAN1,CAN_IT_FMP1 | CAN_IT_FF1 | CAN_IT_FOV1, ENABLE); // fifo1中断
//CAN_ITConfig(CAN1,CAN_IT_TME, DISABLE); // 发送中断
//CAN_ITConfig(CAN1,CAN_IT_EWG | CAN_IT_EPV | CAN_IT_BOF | CAN_IT_LEC | CAN_IT_ERR | CAN_IT_WKU | CAN_IT_SLK, ENABLE); // ERR中断
// CAN缓存初始化
//memset(CAN_msg_num,0,MAX_MAIL_NUM);
}
int CAN1_Tx_data(void)
{
CanTxMsg TxMessage;
u8 TransmitMailbox = 0,i=0;
u8 message_tx_status= 0;
u8 Can1_Send_Buf[8];
/* transmit */
//CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
TxMessage.StdId=0x00;//设定标准标识符
TxMessage.ExtId=0x1234;//设置扩展标识符
TxMessage.RTR=CAN_RTR_DATA;//设定待传输消息的帧类型
TxMessage.IDE=CAN_ID_EXT;//CAN_ID_STD;//设定消息标识符的类型
TxMessage.DLC=8; //数据长度
Can1_Send_Buf[0]=1; //圈数
Can1_Send_Buf[1]=2; //总角度
Can1_Send_Buf[2]=3;
Can1_Send_Buf[3]=4;
Can1_Send_Buf[4]=5;
Can1_Send_Buf[5]=6;
Can1_Send_Buf[6]=7;
Can1_Send_Buf[7]=8;
PUT("TxMessage.StdId =");
send_short_to_hex_string_uart1(TxMessage.StdId);
PUT("
");
PUT("TxMessage.ExtId =");
send_short_to_hex_string_uart1(TxMessage.ExtId);
PUT("
");
PUT("TxMessage.RTR =");
send_char_to_hex_string_uart1(TxMessage.RTR);
PUT("
");
PUT("TxMessage.IDE =");
send_char_to_hex_string_uart1(TxMessage.IDE);
PUT("
");
PUT("TxMessage.DLC =");
send_char_to_hex_string_uart1(TxMessage.DLC);
PUT("
");
PUT("TxMessage.Data =");
for(i=0;i < TxMessage.DLC;i++)
{
TxMessage.Data[i] = Can1_Send_Buf[i];
send_char_to_hex_string_uart1(TxMessage.Data[i]);
}
PUT("
");
TransmitMailbox = CAN_Transmit(CAN1,&TxMessage);
/* CAN FIFO0 message pending interrupt enable */
//CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE);
/* disable interrupt handling */
// CAN_ITConfig(CAN1,CAN_IT_FMP0, DISABLE);
if(CAN_NO_MB == TransmitMailbox)
{
//发送失败,没有空邮箱
PUT("Mail box is nok
");
return 0;
}
else
{
PUT("Mail box is ok
");
CAN_msg_num[TransmitMailbox] = 1;
}
do
{
message_tx_status = CAN_TransmitStatus(CAN1, TransmitMailbox);
if(message_tx_status == CANTXOK)
{
PUT("message send ok
");
}
else if(message_tx_status == CANTXPENDING)
{
PUT("message send pending
");
}
else if(message_tx_status == CANTXFAILED)
{
PUT("message send failed
");
}
delay_GSM(30000);
}while(message_tx_status != CANTXOK);
//CAN_ITConfig(CAN1, CAN_IT_FMP0, DISABLE);
return 1;
}
//接收中断函数
void USB_LP_CAN1_RX0_IRQHandler(void)
{
CanRxMsg RxMessage;
u8 i = 0;
u8 u8_RxLen = 0;
RxMessage.StdId = 0x00;
RxMessage.ExtId = 0x00;
RxMessage.IDE = 0;
RxMessage.RTR = 0;
RxMessage.DLC = 0;
RxMessage.FMI = 0;
for(i=0;i<8;i++)
{
RxMessage.Data[i]=0x00;
}
CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
u8_RxLen = RxMessage.DLC;
PUT("Rx CAN Message ******
");
PUT("RxMessage.StdId =");
send_short_to_hex_string_uart1(RxMessage.StdId);
PUT("
");
PUT("RxMessage.ExtId =");
send_short_to_hex_string_uart1(RxMessage.ExtId);
PUT("
");
PUT("RxMessage.RTR =");
send_char_to_hex_string_uart1(RxMessage.RTR);
PUT("
");
PUT("RxMessage.IDE =");
send_char_to_hex_string_uart1(RxMessage.IDE);
PUT("
");
PUT("RxMessage.DLC =");
send_char_to_hex_string_uart1(RxMessage.DLC);
PUT("
");
PUT("RxMessage.Data =");
for(i=0;i < RxMessage.DLC;i++)
{
send_char_to_hex_string_uart1(RxMessage.Data[i]);
}
PUT("
");
PUT("Rx CAN Message end***
");
}
此帖出自
小平头技术问答
一周热门 更多>