Demo entry 6338731

1

   

Submitted by anonymous on Dec 16, 2016 at 19:44
Language: C. Code size: 1.6 kB.

void Can_Init()
{
	GPIO_InitTypeDef 		GPIO_InitStructure;
  	CAN_InitTypeDef        	CAN_InitStructure;
  	NVIC_InitTypeDef 		NVIC_InitStructure;

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
  	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  	GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_CAN1);
  	GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_CAN1); 
  
  	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  	GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
  	GPIO_Init(GPIOB, &GPIO_InitStructure);

  	CAN_DeInit(CAN1);

  	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 = ENABLE;
  	CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;

	
  	CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
  	CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
  	CAN_InitStructure.CAN_BS2 = CAN_BS2_1tq;
  	CAN_InitStructure.CAN_Prescaler = 42;
	
  	CAN_Init(CAN1, &CAN_InitStructure);

   	CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
	  
  	NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
  	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  	NVIC_Init(&NVIC_InitStructure);
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).