我的自测模式通过了,Normal就是怎么有问题,我的源码:
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1 , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC
|RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOE|RCC_APB2Periph_GPIOF
|RCC_APB2Periph_GPIOG|RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //PB8 CAN1_RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
/*************CAN1_TX映射端口*****************/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PB9 CAN1_TX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_Remap1_CAN1,ENABLE); //GPIO_Remap2_CAN1,GPIO_Remap_CAN2
TestStatus CAN_Polling(void)
{
u8 Error;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
CanTxMsg TxMessage;
CanRxMsg RxMessage;
u32 i = 0;
u8 TransmitMailbox = 0;
/***CAN寄存器初始化**/
CAN_DeInit(CAN1);
CAN_StructInit(&CAN_InitStructure);
/* CAN cell init */
//CAN_InitStructure.CAN_TTCM = DISABLE;
CAN_InitStructure.CAN_TTCM = DISABLE; //禁止时间触发模式
CAN_InitStructure.CAN_ABOM = DISABLE; //失能自动离线管理
CAN_InitStructure.CAN_AWUM = DISABLE; //失能自动唤醒模式
CAN_InitStructure.CAN_NART = DISABLE; //失能非自动重传输模式
CAN_InitStructure.CAN_RFLM = DISABLE; //失能接收FIFO锁定模式
CAN_InitStructure.CAN_TXFP = DISABLE; //失能发送FIFO优先级
//CAN_InitStructure.CAN_Mode = CAN_Mode_LoopBack; //CAN工作在环回模式
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal; //CAN工作在正常模式
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; //重新同步跳跃宽度1个时间单位
CAN_InitStructure.CAN_BS1 = CAN_BS1_9tq; //时间段1为8个时间单位
CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq; //时间段2为7个时间单位
CAN_InitStructure.CAN_Prescaler = 40; //时间单位的长度
//BaudRate = APB1 / ((BS1 + BS2 + 1) * Prescaler)
//CAN_bps = PCLK1 / ((1 + 7 + 8) * 5) = 450k
Error = CAN_Init(CAN1,&CAN_InitStructure);
if(Error == 0)
return FAILED;
/****CAN滤波寄存器初始化****/
CAN_FilterInitStructure.CAN_FilterNumber = 0; //待初始化的过滤器,它的范围是 1到 13。
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask; //标识符屏蔽位模式
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit; //过滤器位宽32位
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000; //过滤器高段位标识符,16位时第一个
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000; //过滤器低段位标识符,16位时第二个
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000; //用来设定过滤器屏蔽标识符或者过滤器标识符
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0; //过滤器 FIFO0 指向过滤器0
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE; //使能过滤器
CAN_FilterInit(&CAN_FilterInitStructure);
/* transmit */
TxMessage.StdId = 0x01; //设定标准标识符。它的取值范围为 0到 0x7FF
//TxMessage.ExtId = 0xFF; //设定扩展标识符。它的取值范围为 0到 0x3FFFF
TxMessage.RTR = CAN_RTR_DATA; //数据帧
TxMessage.IDE = CAN_ID_STD; //标准标识符
TxMessage.DLC = 2; //设定待传输消息的帧长度。它的取值范围是 0 到0x8。
TxMessage.Data[0] = 0x00; //传送的数据 先发高位再发低位
TxMessage.Data[1] = 0x02;
//TxMessage.Data[2] = 0x03;
//TxMessage.Data[3] = 0x04;
//TxMessage.Data[4] = 0x05;
//TxMessage.Data[5] = 0x06;
//TxMessage.Data[6] = 0x07;
//TxMessage.Data[7] = 0x08;
TransmitMailbox = CAN_Transmit(CAN1,&TxMessage);
i = 0;
while( ( CAN_TransmitStatus(CAN1,TransmitMailbox) != CANTXOK ) && ( i != 0xFF ))
{
i++;
}
i = 0;
while( ( CAN_MessagePending(CAN1,CAN_FIFO0) < 1 )&&(i != 0xFF ) )//返回挂号的信息数量
{
i++;
}
/* receive */
RxMessage.StdId = 0x00;
RxMessage.IDE = CAN_ID_STD; //标准标识符
RxMessage.DLC = 0;
RxMessage.Data[0] = 0x00;
RxMessage.Data[1] = 0x03;
//RxMessage.Data[2] = 0x00;
//RxMessage.Data[3] = 0x00;
//RxMessage.Data[4] = 0x00;
//RxMessage.Data[5] = 0x00;
//RxMessage.Data[6] = 0x00;
//RxMessage.Data[7] = 0x00;
CAN_Receive(CAN1,CAN_FIFO0,&RxMessage);
CAN_Data = RxMessage.Data[0];
/*if (RxMessage.StdId!=0x11)
{
return FAILED;
}
if(RxMessage.IDE != CAN_ID_STD)
{
return FAILED;
}
if (RxMessage.DLC!=8)
{
return FAILED;
} */
//if ((RxMessage.Data[0]<<8|RxMessage.Data[1])!=0xCAFE)
{
//return FAILED;
}
return PASSED; /* Test Passed */
}
我的配置有问题吗?请指教啊!!用中断接收的 |