⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 can.c

📁 DSP部分功能测试 串口
💻 C
字号:
//-------------------------------//
//---can.c-----------------------//
//-------------------------------//

#include    "f2407_c.h" 


#define LED port2040                           
volatile ioport unsigned int port2040;/*LED*/ 


unsigned int test_can(void);

void can_init(void)
{
	 *MCRB = *MCRB | 0x0c0;				//IOPC6,7 as CANTX,CANRX
	 *CAN_IFR=0xffff;					//clear CAN int pending
	 *LAM1_H=0x7fff;					//mailbox 2,3 mask ID R
	 *LAM1_L=0xffff;					//donot need to check the ID

/*config CAN baud rate*/ 
	 *MCR=0x1040;   					/*set CCR=1,self test*/
	 while(((*GSR)&0x0010) == 0)  		/*if CCE=0 then wait*/
	 	continue; 	
	 *BCR1=0x0023;						/*TSEG1 = 4, TSEG2 = 3*/;
	 *BCR2=0x0003;						/*BRP = 3*/ 
	 									/*CAN Baud= 1M = 40M/(BRP+1 + TSEG1 + TSEG2 + 3)*/
	 *MCR=(*MCR)&0xefff;          		/*CCR=0*/
	 while(((*GSR)&0x0010) == 0x0010)	/*if CCE=0 config success*/  
	 	continue;
	 
/*config mailbox*/
/*2 as receive box, 3 as send box, use self test mode, send data from mailbox3 to mailbox2*/
	 *MDER=0x40;			/*disabled mailbox 0-5, config box2 as receive box, box3 as send box*/ 
	 *MCR=0x0143;			/*CDR=1, self check mode, select mail box3*/ 
	
	//config mailbox2 control byte and ID
	 *MSGID2H=0x05555;
	 *MSGID2L=0x0aaaa;
	 *MSGCTRL2=0x0008;		/*DCL = 8, RTR = 0 data frame*/
	//mailbox2 initilize	 
	 *MBX2A=0x0;
	 *MBX2B=0x0;
	 *MBX2C=0x0;
	 *MBX2D=0x0; 

	//config mailbox2 and 3 control byte and ID
	 *MSGID3H=0x05555;
	 *MSGID3L=0x0aaaa;
	 *MSGCTRL3=0x0008;		/*DCL = 8, RTR = 0 data frame*/
	//mailbox3 initilize	 
	 *MBX3A=0x01;
	 *MBX3B=0x02;
	 *MBX3C=0x03;
	 *MBX3D=0x04; 
	 
	 	 
	 	 
	 *MCR=0x4c0;  				/*DBO = 1, ABO =1, STM = 1, self test mode*/
	 *MDER=0x4c;				/*enable mailbox2,3, box2 as receivebox, box3 as sendbox*/
	 *CAN_IMR=0x0ffff;			/*disable can int*/
	 *CAN_IFR=0x0ffff;
} 
	                        	
/*send mailbox3 value to mailbox2, use request mode, non interrupt*/	 
unsigned int test_can(void)
{
	 unsigned int data_rec1,data_rec2,data_rec3,data_rec4;
	 can_init();
	
	 *TCR=0x20;					/*mailbox3 request send*/ 
/*send data frame*/
	 while(((*TCR)&0x2000)==0)  	
	 	continue;				/*if TA3=0 then wait*/
	 *TCR=(*TCR)|0x2000;		/*clear TA3 and MIF3*/ 

/*wait to receive*/
	 while(((*RCR)&0x0040)==0) 
	 	continue;				/*if RMP0=0 then wait*/
	 *RCR=(*RCR)|0x0040;		/*RMP2=1*/ 
	 
	 data_rec1=*MBX2A;
	 data_rec2=*MBX2B;
	 data_rec3=*MBX2C;
	 data_rec4=*MBX2D;
/*please set break pointer here, watch data_rec1,2,3,4 at watch window to see
  if the value is equal to :1,2,3,4*/


	 if(data_rec1!=*MBX3A)return(0);
	 if(data_rec2!=*MBX3B)return(0);
	 if(data_rec3!=*MBX3C)return(0);
	 if(data_rec4!=*MBX3D)return(0);
	 return(1);
}
	 
	 
	 	
	 	
		 
	 
	 
	 
	 
	
	 
	 
	 
	 
	 
	 
	 

                                   


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -