예제 #1
0
void CAN_Init(void)
{
	SIM_SCGC6|=SIM_SCGC6_FLEXCAN0_MASK;

	PORTA_PCR13=PORT_PCR_MUX(0x02);
	PORTA_PCR12=PORT_PCR_MUX(0x02);

	CAN0_MCR|=CAN_MCR_FRZ_MASK;
	CAN0_MCR&=~CAN_MCR_MDIS_MASK;
	while(CAN0_MCR&CAN_MCR_LPMACK_MASK);

	CAN0_MCR|=CAN_MCR_SOFTRST_MASK;
	while(CAN0_MCR&CAN_MCR_SOFTRST_MASK);

	while(!(CAN0_MCR&CAN_MCR_FRZACK_MASK));

	CAN0_MCR|=CAN_MCR_SRXDIS_MASK;
	CAN0_MCR|=CAN_MCR_RFEN_MASK;

	CAN0_CTRL1=CAN_CTRL1_PROPSEG(2)|CAN_CTRL1_RJW(1)|CAN_CTRL1_PSEG1(7)|CAN_CTRL1_PSEG2(3)|CAN_CTRL1_PRESDIV(1); // 500KBPS
//	CAN0_CTRL1=CAN_CTRL1_PROPSEG(2)|CAN_CTRL1_RJW(1)|CAN_CTRL1_PSEG1(7)|CAN_CTRL1_PSEG2(3)|CAN_CTRL1_PRESDIV(9); // 100KBPS
	CAN0_CTRL1&=~CAN_CTRL1_LPB_MASK;

	CAN0_RXIMR0=CAN_RXIMR_MI(0x1FFFFFFF);
	CAN0_RXMGMASK=0;
	CAN0_RXFGMASK=0;

	CAN0_MCR&=~CAN_MCR_HALT_MASK;
	while(CAN0_MCR&CAN_MCR_FRZACK_MASK);
	while(CAN0_MCR&CAN_MCR_NOTRDY_MASK);

	CAN0_CS(txb)=CAN_CS_CODE(0/*INACTIVE*/);
}
예제 #2
0
파일: can.c 프로젝트: SproutOrc/CH-K-Lib
/***********************************************************************************************
 功能:CAN 设置波特率
 形参:CAN_Type: CAN结构
       @arg CAN0 : CAN0模块
			 @arg CAN1 : CAN1模块
			 baudrate: 波特率
			 @arg CAN_SPEED_33K
			 @arg CAN_SPEED_83K
			 @arg CAN_SPEED_50K
			 @arg CAN_SPEED_100K
			 @arg CAN_SPEED_125K
			 @arg CAN_SPEED_250K
			 @arg CAN_SPEED_500K
			 @arg CAN_SPEED_1000K			 
 返回:0
 详解:0
************************************************************************************************/
static uint8_t CAN_SetBaudrate(CAN_Type *can,uint8_t baudrate)
{
	switch(baudrate)
	{
		case CAN_SPEED_33K:
			 // 48M/120= 400k sclock, 12Tq
			 // PROPSEG = 3, LOM = 0x0, LBUF = 0x0, TSYNC = 0x0, SAMP = 1
			 // RJW = 3, PSEG1 = 4, PSEG2 = 4,PRESDIV = 120
			can->CTRL1 = (0 | CAN_CTRL1_PROPSEG(2) 
											| CAN_CTRL1_RJW(2)
											| CAN_CTRL1_PSEG1(3) 
											| CAN_CTRL1_PSEG2(3)
											| CAN_CTRL1_PRESDIV(119));
		  break;
		case CAN_SPEED_83K:
			 // 48M/48= 1M sclock, 12Tq
			 // PROPSEG = 3, LOM = 0x0, LBUF = 0x0, TSYNC = 0x0, SAMP = 1
			 // RJW = 3, PSEG1 = 4, PSEG2 = 4,PRESDIV = 48
			can->CTRL1 = (0 | CAN_CTRL1_PROPSEG(2) 
											| CAN_CTRL1_RJW(2)
											| CAN_CTRL1_PSEG1(3) 
											| CAN_CTRL1_PSEG2(3)
											| CAN_CTRL1_PRESDIV(47));
		  break;	
		case CAN_SPEED_50K:
			 // 48M/80= 0.6M sclock, 12Tq
			 // PROPSEG = 3, LOM = 0x0, LBUF = 0x0, TSYNC = 0x0, SAMP = 1
			 // RJW = 3, PSEG1 = 4, PSEG2 = 4, PRESDIV = 40
			can->CTRL1 = (0 | CAN_CTRL1_PROPSEG(2) 
											| CAN_CTRL1_RJW(1)
											| CAN_CTRL1_PSEG1(3) 
											| CAN_CTRL1_PSEG2(3)
											| CAN_CTRL1_PRESDIV(79));	
		break;
		case CAN_SPEED_100K:
			 // 48M/40= 1.2M sclock, 12Tq
			 // PROPSEG = 3, LOM = 0x0, LBUF = 0x0, TSYNC = 0x0, SAMP = 1
			 // RJW = 3, PSEG1 = 4, PSEG2 = 4, PRESDIV = 40
			can->CTRL1 = (0 | CAN_CTRL1_PROPSEG(2) 
											| CAN_CTRL1_RJW(2)
											| CAN_CTRL1_PSEG1(3) 
											| CAN_CTRL1_PSEG2(3)
											| CAN_CTRL1_PRESDIV(39));	
		break;
		case CAN_SPEED_125K:
			 // 48M/32= 1.5M sclock, 12Tq
			 // PROPSEG = 3, LOM = 0x0, LBUF = 0x0, TSYNC = 0x0, SAMP = 1
			 // RJW = 3, PSEG1 = 4, PSEG2 = 4, PRESDIV = 32
			can->CTRL1 = (0 | CAN_CTRL1_PROPSEG(2) 
											| CAN_CTRL1_RJW(2)
											| CAN_CTRL1_PSEG1(3) 
											| CAN_CTRL1_PSEG2(3)
											| CAN_CTRL1_PRESDIV(31));		
		break;
		case CAN_SPEED_250K:
			 // 48M/16= 3M sclock, 12Tq
			 // PROPSEG = 3, LOM = 0x0, LBUF = 0x0, TSYNC = 0x0, SAMP = 1
			 // RJW = 2, PSEG1 = 4, PSEG2 = 4, PRESDIV = 16
			can->CTRL1 = (0 | CAN_CTRL1_PROPSEG(2) 
											| CAN_CTRL1_RJW(1)
											| CAN_CTRL1_PSEG1(3) 
											| CAN_CTRL1_PSEG2(3)
											| CAN_CTRL1_PRESDIV(15));		
		break;
		case CAN_SPEED_500K:
			 // 48M/8=6M sclock, 12Tq
			 // PROPSEG = 3, LOM = 0x0, LBUF = 0x0, TSYNC = 0x0, SAMP = 1
			 // RJW = 2, PSEG1 = 4, PSEG2 = 4, PRESDIV = 6
			can->CTRL1 = (0 | CAN_CTRL1_PROPSEG(2) 
											| CAN_CTRL1_RJW(1)
											| CAN_CTRL1_PSEG1(3) 
											| CAN_CTRL1_PSEG2(3)
											| CAN_CTRL1_PRESDIV(7));		
		break;
		case CAN_SPEED_1000K:
			 // 48M/6=8M sclock
			 // PROPSEG = 4, LOM = 0x0, LBUF = 0x0, TSYNC = 0x0, SAMP = 1
			 // RJW = 1, PSEG1 = 1, PSEG2 = 2, PRESCALER = 6
			can->CTRL1 = (0 | CAN_CTRL1_PROPSEG(3) 
											| CAN_CTRL1_RJW(0)
											| CAN_CTRL1_PSEG1(0) 
											| CAN_CTRL1_PSEG2(1)
											| CAN_CTRL1_PRESDIV(5));	
		break;
		default: return 1;
	}
	return 0;
}