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

📄 can.c

📁 can口驱动程序!包括硬件的基本配置文件
💻 C
字号:


#define BASE_IO  0x7f00
#define BASE_CAN 0x7700
#define CON_REG	 (unsigned char xdata*)(BASE_CAN)
#define CMD_REG	 (unsigned char xdata*)(BASE_CAN+1)
#define STS_REG	 (unsigned char xdata*)(BASE_CAN+2)
#define INT_REG	 (unsigned char xdata*)(BASE_CAN+3)
#define ACC_REG	 (unsigned char xdata*)(BASE_CAN+4)
#define ACM_REG	 (unsigned char xdata*)(BASE_CAN+5)
#define BTR0_REG (unsigned char xdata*)(BASE_CAN+6)
#define BTR1_REG (unsigned char xdata*)(BASE_CAN+7)
#define OUT_REG  (unsigned char xdata*)(BASE_CAN+8)
#define CLK_REG	 (unsigned char xdata*)(BASE_CAN+31)

#define RX_REG   (unsigned char xdata*)(BASE_CAN+20)
#define TX_REG   (unsigned char xdata*)(BASE_CAN+10)

#define BASE_TAB 0xff00
#define TAB_INDEX  (unsigned char xdata*)(BASE_TAB)

#include "ds80c320.h"
#include <string.h>
#include <intrins.h>
#include "can.h"
#include "sio.h"

unsigned char code baud_table0[]={0x00,0x00,0x01,0x03,0x43,0x47,0x53,0x67,0x7f};
unsigned char code baud_table1[]={0x14,0x1c,0x1c,0x1c,0x2f,0x2f,0x2f,0x2f,0x7f};
MSG_BUF msg[256];



static unsigned char data  can_rx_in=0;
static unsigned char data  can_rx_out=0;
extern unsigned char xdata tbuf[256];
extern unsigned char xdata t_in;
extern unsigned char xdata t_disabled;

unsigned char bdata sts;
sbit rdy=sts^0;
sbit ovr=sts^1;
sbit tbs=sts^2;
sbit tcs=sts^3;
sbit rs=sts^4;
sbit ts=sts^5;
sbit es=sts^6;
sbit bs=sts^7;

unsigned char bdata int_;
sbit int_ri=sts^0;
sbit int_ti=sts^1;
sbit int_ei=sts^2;
sbit int_oi=sts^3;
sbit int_wi=sts^4;

static void can_isr (void) interrupt 0 using 3
{
unsigned char  i,len;
unsigned int id;
/*------------------------------------------------
Received data interrupt.
------------------------------------------------*/
EA=0;
//sts=*(STS_REG);
int_=*(INT_REG);
//com_putchar('B');
if(int_ri)
	{
	if((can_rx_in+1)!=can_rx_out)
	{
		id=*(RX_REG);
		id<<=8;
		id|=*(RX_REG+1);
		id>>=5;
		i=(*(RX_REG+1))&0x0f;
		len=i<8?i:8;
		msg[can_rx_in].ID=id;
 		msg[can_rx_in].LEN=i;
	        for (i=0; i < len; i++)
    	        (msg[can_rx_in].BUF[i]) = *(RX_REG+i+2); // copy bytes 
		can_rx_in++;
		clr_rxbuf();
		com_putchar('C');
	}
	}

EA=1;
}

void can_init(void)
{
unsigned char data  i;
EA=0;
//*(unsigned char xdata*)(BASE_IO+0x9)&=0x7f;
*(unsigned char xdata*)(BASE_IO+0x7)|=0x80;
*(unsigned char xdata*)(BASE_IO+0x5)|=0x80;
for(i=0;i<100;i++) _nop_();
*(CON_REG)=1;
for(i=0;i<100;i++) _nop_();

*(ACC_REG)=*(TAB_INDEX+3);
*(ACM_REG)=*(TAB_INDEX+4);

if((*(TAB_INDEX))<9)
{

*(BTR0_REG)=baud_table0[*(TAB_INDEX)];
*(BTR1_REG)=baud_table1[*(TAB_INDEX)];

}
else
{
*(BTR0_REG)=*(TAB_INDEX+1);
*(BTR1_REG)=*(TAB_INDEX+2);
}

//for debug
*(ACC_REG)=0xff;
*(ACM_REG)=0xff;
*(BTR0_REG)=baud_table0[2];       		//125kbps
*(BTR1_REG)=baud_table1[2];


*(OUT_REG)=0xaa;
*(CLK_REG)=0x48;
*(CON_REG)=0x62;

//*(CON_REG)=0x01;
//*(CLK_REG)=0xc0;
//*(CON_REG)=0x04;


IT0=0;
EX0=1;
PX0=1;
EA=1;
  while ((*(STS_REG)) & 0x80);           // wait until controller is active 
}

void clr_rxbuf()
{
*(CMD_REG)=0x04;
}

void clr_ovr()
{
*(CMD_REG)=0x08;
}

void go_sleep()
{
*(CMD_REG)=0x10;
}

void req_tx()
{
*(CMD_REG)=0x01;
}

void stop_tx()
{
*(CMD_REG)=0x02;
}

unsigned char check_busoff_state (void)
{
/* Check status register for bus-off state                              */
   if ((*(STS_REG)) & 0x80)
      return (0xFF);	            /* CAN controller is in bus-off */
   else
      return (0);                   /* CAN controller active */
}

//a example ,can_rxbuf---->rs232c txbuf
char can_getmsg (void)
{

 unsigned char i,len;
if (can_rbuflen () == 0)
  return (-1);
EA=0;                
tbuf[t_in++]=(unsigned char)(msg[can_rx_out].ID/256);
tbuf[t_in++]=(unsigned char)(msg[can_rx_out].ID%256);
tbuf[t_in++]=msg[can_rx_out].LEN;
len= msg[can_rx_out].LEN <8 ? msg[can_rx_out].LEN:8 ;
for (i=0; i < len; i++)
tbuf[t_in++]=msg[can_rx_out].BUF[i]; // copy bytes 
can_rx_out++;
if (t_disabled)			/* if transmitter is disabled */
  {
  t_disabled = 0;
  TI = 1;			/* enable it */
  }
EA = 1;                         /* Enable Interrupts */
      
return (0);
}   



unsigned char can_rbuflen (void)
{
return (can_rx_in - can_rx_out);
}
/*
unsigned char can_tbuflen (void)
{
return (can_tx_in - can_tx_out);
}*/


void transmit_message ( MSG_BUF *pTransmitBuf )
{
unsigned long Identifier;                   // CAN message identifier 
unsigned char  Length;                       // lenght of data frame 
unsigned char  i;                            // local loop counter 

// Prepare length code and identifier.                                 
   Length     = pTransmitBuf->LEN<8 ?pTransmitBuf->LEN :8;
   Identifier = (pTransmitBuf->ID << 5);

// Wait until write access to CAN controller buffer is allowed          
   while ( !((*(STS_REG)) & 0x04) );

// Write message to transmit buffer                                     

  
   *(TX_REG) = (unsigned char)(Identifier >> 8);
   *(TX_REG+1)= (unsigned char)Identifier+Length&0x0f;       // write identifier 
   for (i=0; i < Length; i++)       // write data bytes 
      *(TX_REG+2+i) = *(unsigned char *)(pTransmitBuf->BUF+i); // copy data byte  

// Init a transmission request.                                         
   *(CMD_REG)  = 0x01;
}

⌨️ 快捷键说明

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