您的位置 首页 厂商

stm32 CAN LoopBack 自测形式成功

移植了stm32外设库里面的代码。//CAN_Polling成功DS1亮//CAN_Interrupt成功DS2亮失败时候分别对应DS3DS4亮下面显示关键代…

移植了stm32 外设库里面的代码。

//CAN_Polling 成功 DS1亮

//CAN_Interrupt 成功 DS2亮

失利时分 别离对应 DS3 DS4亮
下面显现要害代码

main.c

===========================

int main(void)
{
/* Setup STM32 system (clock, PLL and Flash configuration) */
SystemInit();

/* CAN1 Periph clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);

/* NVIC Configuration */
NVIC_Configuration();

LED_Init();

/* CAN transmit at 100Kb/s and receive by polling in loopback mode */
TestRx = CAN_Polling();

if (TestRx == FAILED)
{
/* Turn on led DS3 */
GPIO_ResetBits(GPIOC, GPIO_Pin_11);
}
else
{
/* Turn on led DS1 */ //CAN_Polling 成功 DS1亮
GPIO_ResetBits(GPIOD, GPIO_Pin_2);
}

/* CAN transmit at 500Kb/s and receive by interrupt in loopback mode */
TestRx = CAN_Interrupt();

if (TestRx == FAILED)
{
/* Turn on led DS4 */
GPIO_ResetBits(GPIOC, GPIO_Pin_10);
}
else
{
/* Turn on led DS2 */ //CAN_Interrupt 成功 DS2亮
GPIO_ResetBits(GPIOC, GPIO_Pin_12);
}

/* Infinite loop */
while (1)
{
}
}

/**
* @brief Configures the CAN, transmit and receive by polling
* @param None
* @retval PASSED if the reception is well done, FAILED in other case
*/
TestStatus CAN_Polling(void)
{
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
CanTxMsg TxMessage;
CanRxMsg RxMessage;
uint32_t i = 0;
uint8_t TransmitMailbox = 0;

/* CAN register init */
CAN_DeInit(CAN1);
CAN_StructInit(&CAN_InitStructure);

/* CAN cell init */
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;
CAN_InitStructure.CAN_TXFP=DISABLE;
CAN_InitStructure.CAN_Mode=CAN_Mode_LoopBack;
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=5;
CAN_Init(CAN1, &CAN_InitStructure);

/* CAN filter init */
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=0;
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);

/* transmit */
TxMessage.StdId=0x11;
TxMessage.RTR=CAN_RTR_DATA;
TxMessage.IDE=CAN_ID_STD;
TxMessage.DLC=2;
TxMessage.Data[0]=0xCA;
TxMessage.Data[1]=0xFE;

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]=0x00;
CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);

if (RxMessage.StdId!=0x11)
{
return FAILED;
}

if (RxMessage.IDE!=CAN_ID_STD)
{
return FAILED;
}

if (RxMessage.DLC!=2)
{
return FAILED;
}

if ((RxMessage.Data[0]<<8|RxMessage.Data[1])!=0xCAFE)
{
return FAILED;
}

return PASSED; /* Test Passed */
}

/**
* @brief Configures the CAN, transmit and receive using interrupt.
* @param None
* @retval PASSED if the reception is well done, FAILED in other case
*/
TestStatus CAN_Interrupt(void)
{
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
CanTxMsg TxMessage;
uint32_t i = 0;

/* CAN register init */
CAN_DeInit(CAN1);
CAN_StructInit(&CAN_InitStructure);

/* CAN cell init */
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;
CAN_InitStructure.CAN_TXFP=DISABLE;
CAN_InitStructure.CAN_Mode=CAN_Mode_LoopBack;
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=1;
CAN_Init(CAN1, &CAN_InitStructure);

/* CAN filter init */
CAN_FilterInitStructure.CAN_FilterNumber=1;
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_FIFO0;
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);

/* CAN FIFO0 message pending interrupt enable */
CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);

/* transmit 1 message */
TxMessage.StdId=0x00;
TxMessage.ExtId=0x1234;
TxMessage.IDE=CAN_ID_EXT;
TxMessage.RTR=CAN_RTR_DATA;
TxMessage.DLC=2;
TxMessage.Data[0]=0xDE;
TxMessage.Data[1]=0xCA;
CAN_Transmit(CAN1, &TxMessage);

/* initialize the value that will be returned */
ret = 0xFF;

/* receive message with interrupt handling */
i=0;
while((ret == 0xFF) && (i < 0xFFF))
{
i++;
}

if (i == 0xFFF)
{
ret=0;
}

/* disable interrupt handling */
CAN_ITConfig(CAN1, CAN_IT_FMP0, DISABLE);

return (TestStatus)ret;
}

/**
* @brief Configures the NVIC and Vector Table base address.
* @param None
* @retval None
*/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;

/* Enable CAN1 RX0 interrupt IRQ channel */
#ifndef STM32F10X_CL
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
#else
NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
#endif /* STM32F10X_CL*/
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}

=======================

stm32f10x_it.c

/******************************************************************************/
/* STM32F10x Peripherals Interrupt Handlers */
/******************************************************************************/

/**
* @brief This function handles CAN1 Handler.
* @param None
* @retval None
*/
#ifndef STM32F10X_CL //假如没有运用运用了STM32互联系列微处理器
//也便是行将问世的STM32F105和STM32F107系列处理器
void USB_LP_CAN1_RX0_IRQHandler(void)
#else
void CAN1_RX0_IRQHandler(void)
#endif
{
CanRxMsg RxMessage;

RxMessage.StdId=0x00;
RxMessage.ExtId=0x00;
RxMessage.IDE=0;
RxMessage.DLC=0;
RxMessage.FMI=0;
RxMessage.Data[0]=0x00;
RxMessage.Data[1]=0x00;

CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);

if((RxMessage.ExtId==0x1234) && (RxMessage.IDE==CAN_ID_EXT)
&& (RxMessage.DLC==2) && ((RxMessage.Data[1]|RxMessage.Data[0]<<8)==0xDECA))
{
ret = 1;
}
else
{
ret = 0;
}
}

stm32 CAN LoopBack 自测形式成功

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/changshang/272979.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部