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

📄 intmy.c

📁 2个F040之间的CAN通讯
💻 C
字号:
#include "c8051f040.h"
#include "can.h"

extern unsigned char   DO_Address;
extern unsigned char  temp1,temp2;
extern unsigned char  DO_Control_Data_New0,DO_Control_Data_New1;

xdata unsigned char ss[8]={0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88};
xdata unsigned char rr[8]={0,0,0,0,0,0,0,0};

//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
void SYSCLK_Init (void) {
  SFRPAGE = 0xF;
  OSCXCN |= 0x67;					   //Running at 22.1184MHz.	
  while (!((OSCXCN & 0x80) == 0x80));
  CLKSEL |= 0x01;
}
 
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
void PORT_Init (void) {
  SFRPAGE = 0xF;
XBR1 |= 0x00;
  XBR1 |= 0x04;              //INT0 Routed To Port Pin
  XBR2 |= 0x40;              //If crossbar is disabled, all ports are in input mode.  
  XBR3 |= 0x80;              //CTX pin's output mode is configured as push-pull.
P0MDOUT = 0x00;
P1MDIN =0xFF;
 // P4MDOUT = 0x03;            //CANLED and ENABLE
 // P5MDOUT = 0x00;            //OUTPUT CONTROL PORT
 // P6MDOUT = 0x00;            //OUTPUT CONTROL PORT
}

//-----------------------------------------------------------------------------
// INT_Init
// Interrupts By Priority : INT0 > CAN0
//-----------------------------------------------------------------------------
void INT_Init (void) {
//  IE   |= 0x03;              //INT0 Interrupt Enabled        
                             //Timer0 Interrupt Enabled
//  IE   |= 0x01;              //INT0 Interrupt Enabled
  EIE2 |= 0x20;              //CAN0 Interrupt Enabled
}


//-----------------------------------------------------------------------------
// CAN_Init
// CAN0CN  :  Automatic Retransmission is Disable. Error and Status Interrrupt 
//            is Disabled. Module Interrrup is eabled.
// BITREG  :  BaudRate is 1M/8(b/s).
// CAN0TST :  Tx1, Tx0, LBack and Silenct are all 0. Use Basic Mode.
//-----------------------------------------------------------------------------
void CAN_Init (void) {
  SFRPAGE = CAN0_PAGE;
  CAN0CN = 0xC3;
  CAN0ADR = 0x03;
  CAN0DATH = 0x5E;
  CAN0DATL = 0xC7;
  CAN0TST = 0x10;      //LBack
  CAN0CN = 0x82;		//disable automatic retransmission
}

//-----------------------------------------------------------------------------
// CAN_Transmit_Init
//-----------------------------------------------------------------------------
void CAN_Transmit_Init (unsigned char MsgNum, unsigned int id)
{
  unsigned char tempH,tempL;

  SFRPAGE = CAN0_PAGE;
  CAN0ADR = IF1CMDMSK;
  CAN0DATL = 0xb3;    //wr/rd=1,mask=0,arb=1,control=1,,clrintpnd=0,txrqet=0,dataA=1,dataB=1

  tempL=(unsigned char)id;
  tempL<<=2;
  tempH=(unsigned char)(id>>6);
  tempH &= 0x1f;
  tempH |= 0xa0;

  CAN0ADR = IF1ARB1;
  CAN0DATH = 0x00;
  CAN0DATL = 0x00;
  CAN0DATH = 0xA0;      //101(01)00
  CAN0DATL = 0X04;    //0x80 | (((DO_Address & 0x0F) << 2)); //IF1ARB
                 //MsgVal=1, Xad=0,Dir=1:send  id= 0x001
  CAN0DATH = 0x00;           // use the filter,UMask=1,RmtEn=1
  CAN0DATL = 0x88;           //IF1MSGC: eob=1, dlc =8

  CAN0ADR = IF1CMDRQST;      
  CAN0DATL = 0x01;           //Message Object 1 (The First One)
}

//-----------------------------------------------------------------------------
// CAN_Receive_Init
//-----------------------------------------------------------------------------
void CAN_Receive_Init (void) {
  SFRPAGE = CAN0_PAGE;
  CAN0ADR = IF2CMDMSK;
  CAN0DATL = 0xf3;

  CAN0ADR = IF2MSK1;
  CAN0DATH = 0x00;
  CAN0DATL = 0x00;
  CAN0DATH = 0x00;				//0x23;
  CAN0DATL = 0xF4;           //IF2MSK  0xco;

  CAN0ADR = IF2ARB1;
  CAN0DATH = 0x00;
  CAN0DATL = 0x00;

  CAN0DATH = 0x80;
  CAN0DATL = 0x04;           //IF2ARB
                 //MsgVal=1, Xad=0,Dir=0:get  id= 0x001
  CAN0DATH = 0x14;           //use the filter,RxEn=1
  CAN0DATL = 0x88;           //IF2MSGC

  CAN0ADR = IF2CMDRQST;      
  CAN0DATL = 0x02;           //Message Object 2 (The Second One)
}

//-----------------------------------------------------------------------------
// Timer_Init
// Timer0 For CAN Error -- If there is no CAN information form the bus for
//                         10ms, CPU will close all the outputs.
//-----------------------------------------------------------------------------
void Timer_Init (void) {
  SFRPAGE = TIMER01_PAGE;
  TMOD |= 0x01;
  CKCON |= 0x02;
  TH0 = 0xFF;
  TL0 = 0xFF;
}

void send (void)
{
  unsigned char num;

  SFRPAGE = CAN0_PAGE;
  CAN0ADR = IF1CMDMSK;
  CAN0DATL = 0x87;    //wr/rd=1,mask=0,arb=0,control=0,,clrintpnd=0,txrqet=1,dataa=1,datab=1

  CAN0ADR = IF1DATA1;
  for(num=0;num<8;num++)
  {
  	CAN0DATH=ss[num];
	num++;
	CAN0DATL=ss[num];
  }
  
  CAN0ADR = IF1CMDRQST;      
  CAN0DATL = 0x01;           //Message Object 1 (The First One)
  num=0;
}

void get (unsigned char MsgNum)
{
  SFRPAGE = CAN0_PAGE;
  CAN0ADR = IF2CMDMSK;
  CAN0DATL = 0x0F;
  CAN0ADR = IF2CMDRQST;
  CAN0DATL = MsgNum;
  CAN0ADR = IF2DATA1;
  
}


void remote_rqst_int(unsigned char MsgNum)        //id=1
{
  SFRPAGE = CAN0_PAGE;

  CAN0ADR = IF1CMDMSK;
  CAN0DATL = 0xb3;    //wr/rd=1,mask=0,arb=1,control=1,,clrintpnd=0,txrqet=0,dataa=1,datab=1
  //?? remote data??

  CAN0ADR = IF1ARB1;
  CAN0DATH = 0x00;
  CAN0DATL = 0x00;
  CAN0DATH = 0x80;
  CAN0DATL = 0x04;           //IF2ARB
                 //MsgVal=1, Xad=0,Dir=0:get  id= 0x001
  CAN0DATH = 0x04;           //not use the filter,RxEn=1
  CAN0DATL = 0x88;           //IF2MSGC

  CAN0ADR = IF1CMDRQST;      
  CAN0DATL = MsgNum;           	
}

void remote_rqst(unsigned char MsgNum)
{
  SFRPAGE = CAN0_PAGE;

  CAN0ADR = IF1CMDMSK;
  CAN0DATL = 0x87;    //wr/rd=1,mask=0,arb=0,control=0,,clrintpnd=0,txrqet=1,dataa=1,datab=1
  //?? remote data??

  CAN0ADR = IF1CMDRQST;      
  CAN0DATL = MsgNum;    
}

⌨️ 快捷键说明

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