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

📄 can.c

📁 基于EP7312 MCU和uC/OS-II的CAN BUS通讯程序
💻 C
字号:
/*
 *
 * Copyright (C) 2003 liyq ( lisim51@126.com )
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.1
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include "can.h"

static unsigned char CanReadByte(unsigned int addr);
static void  CanWriteByte(unsigned int addr,unsigned char data);

int Uart2Init()
{
	/*Set UART2: baud rate 115200; 1 stop bit; no parity check; enable FIFO; data length 8*/
	(*(volatile unsigned long *)0x800014c0) = 0x00070001;
	
	/* Enable UART2*/
	(*(volatile unsigned long *)0x80001100) |= 0x0100;

	return 0;
}

/* 
 * Send character 'c' out through UART2.
 */
void uart2_putc(char c)
{
	while(((*(volatile unsigned long *)0x80001140) & (1 << 23)));

        (*(volatile unsigned char *)0x80001480) = c;
}

/* 
 * Read a character from UART2, store it in variable 'data' and return 1 if success,
 * or return 0 if fail. 
 */
int uart2_readc(unsigned char *data)
{        
	while(((*(volatile unsigned long *)0x80001140) & (1 << 22))==0x00)
	{

		*data = ( (*(volatile unsigned char *)0x80001480)  & 0xff);
         	return   1;
	}
	return 0;
}
void  CanInit()
{   
    unsigned char temp;             
    CanWriteByte(REG_MODE,0x01);  /*REG_MODE =0x01;enter setup mode*/
    CanWriteByte(REG_MODE,0x01);
    temp=CanReadByte(REG_MODE);
   
    temp=temp&0x01;
    if(temp==0x01) /*in setup mode,init*/
    {
    	 CanWriteByte(REG_BTR0,0x85);
	
     	 CanWriteByte(REG_BTR1,0xb4);/*band rate 100k*/
          
    	 CanWriteByte(REG_OCR,0x1a);/*normal output mode*/
	 
    	 CanWriteByte(REG_CDR,0xc0);/*pelican mode*/
	
    	 CanWriteByte(REG_RBSA,0x00);/*Rx buffer start addr*/
	
    //双滤波
    	 CanWriteByte(REG_ACR0,0xff);//接收所有数据
    
    	 CanWriteByte(REG_ACR1,0xff);
	
    	 CanWriteByte(REG_ACR2,0xff);
	
    	 CanWriteByte(REG_ACR3,0xff);

    
    	 CanWriteByte(REG_AMR0,0xff);
    	 CanWriteByte(REG_AMR1,0xff);
    	 CanWriteByte(REG_AMR2,0xff);
   	 CanWriteByte(REG_AMR3,0xff);

   	 CanWriteByte(REG_IR_ABLE,0xff);/*enable all interupter*/
    }
    
    do{
    CanWriteByte(REG_MODE,0x08); /*enter operating mode and single direction filter*/
    CanWriteByte(REG_MODE,0x08);
    temp=CanReadByte(REG_MODE);
    }while(!(temp&0x08));
    
} 

void  CanSend(unsigned char* data)
{
     unsigned char temp_data1;
     int i;
          do
	{
		temp_data1=CanReadByte(REG_SR);
        }while((temp_data1&0x10)==0x10);/*wait when bus is closed*/
        	
	temp_data1=CanReadByte(REG_SR);
	if((temp_data1&0x04)==0x04)/*cpu can write to send buffer*/
	{
 			CanWriteByte(REG_RxBuffer0,0x08);/*standard frame ,long 8*/

			CanWriteByte(REG_RxBuffer1,0xff);/*identifier 1*/
			CanWriteByte(REG_RxBuffer1,0xff);

			CanWriteByte(REG_RxBuffer2,0xff);/*identifier 2*/
			CanWriteByte(REG_RxBuffer2,0xff);
		

			CanWriteByte(REG_RxBuffer3,*data);
			
			CanWriteByte(REG_RxBuffer4,*(data+1));
			
			CanWriteByte(REG_DataBuffer1,*(data+2));
			
			CanWriteByte(REG_DataBuffer2,*(data+3));
			
			CanWriteByte(REG_DataBuffer3,*(data+4));
			
			CanWriteByte(REG_DataBuffer4,*(data+5));
			
			CanWriteByte(REG_DataBuffer5,*(data+6));
			
			CanWriteByte(REG_DataBuffer6,*(data+7));
			
			CanWriteByte(REG_DataBuffer7,0x09);/*standard frame no used*/
			CanWriteByte(REG_DataBuffer8,0x0a);/*standard frame no used*/
			
	}
         
 	CanWriteByte(REG_CMD,0x01);/*send*/
          	
	temp_data1=CanReadByte(REG_SR);
        for(i=0;i<5000;i++);
	do{
		temp_data1=CanReadByte(REG_SR);
         }while(temp_data1==0x20);/*check out if send over,0x20 is sending now*/

}   
int CanReceive(unsigned char *recv_data)
{        
         
         unsigned char temp;
         unsigned char *tmpdata;
         
         temp=CanReadByte(REG_SR);
	temp=temp&0x01;
	
	tmpdata = recv_data;
         
      if(temp==0x01)
      {
          
	/*read from REG_Rx*/    
         tmpdata[0]=CanReadByte(REG_RxBuffer3);
         tmpdata[1]=CanReadByte(REG_RxBuffer4);
         tmpdata[2]=CanReadByte(REG_DataBuffer1);
         tmpdata[3]=CanReadByte(REG_DataBuffer2);
         tmpdata[4]=CanReadByte(REG_DataBuffer3);
         tmpdata[5]=CanReadByte(REG_DataBuffer4);
         tmpdata[6]=CanReadByte(REG_DataBuffer5);
         tmpdata[7]=CanReadByte(REG_DataBuffer6);

         CanWriteByte(REG_CMD,0x04);/*clear the rxfifo*/
         return 1;
      }
      return 0;
}
static void  CanWriteByte(unsigned int addr,unsigned char data)
{
  	CANALE=addr;
        CANCS=data;
}
static unsigned char CanReadByte(unsigned int addr)
{
	unsigned char back;
	CANALE=addr;
	back=CANCS;
	return back;
}

⌨️ 快捷键说明

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