微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > CAN总线通信程序

CAN总线通信程序

时间:12-13 来源:互联网 点击:

armeType = 1为扩展帧,FarmeType = 0为
//标准帧
//-----------------------------------
void CAN_Transmit(bit FarmeType)
{

unsigned char i;
unsigned char m;
unsigned char can_status;
unsigned char xdata *pointer;

if (FarmeType == 0)//标准帧
{
for (m=0;m<(UART_Length/8);m++)
{
W_WDT();
do//发送缓冲区空么?
{
can_status = SR_CAN1;
}
while ((can_status&0x04) != 0x04);

TXFrameInfo1 = 0x00 + 0x08;
pointer = &TXID1;
for (i=0;i<2;i++)
{
*(pointer++) = CAN_TX_ID[i];
}

pointer = &TXID3;
for (i=0;i<8;i++)
{
*(pointer++) = CAN_TX_Data[i+8*m];
}
CMR_CAN1 = Request_TX;
W_WDT();
}

if ((UART_Length%8) != 0)
{
W_WDT();
do //发送缓冲区空么?
{
can_status = SR_CAN1;
}
while ((can_status&0x04) != 0x04);
TXFrameInfo1 = 0x00 + UART_Length%8;
pointer = &TXID1;
for (i=0;i<2;i++)
{
*(pointer++) = CAN_TX_ID[i];
}

pointer = &TXID3;
for (i=0;i<(UART_Length%8);i++)
{
*(pointer++) = CAN_TX_Data[i+8*(UART_Length/8)];
}
CMR_CAN1 = Request_TX;
W_WDT();
}
else
{
}
}
else//扩展帧
{
for (m=0;m<(UART_Length/8);m++)
{
W_WDT();
do//发送缓冲区空么?
{
can_status = SR_CAN1;
}
while ((can_status&0x04) != 0x04);

TXFrameInfo1 = 0x80 + 0x08;
pointer = &TXID1;
for (i=0;i<4;i++)
{
*(pointer++) = CAN_TX_ID[i];
}

pointer = &TXDATA1;
for (i=0;i<8;i++)
{
*(pointer++) = CAN_TX_Data[i+8*m];
}
CMR_CAN1 = Request_TX;
W_WDT();
}

if ((UART_Length%8) != 0)
{
W_WDT();
do//发送缓冲区空么?
{
can_status = SR_CAN1;
}
while ((can_status&0x04) != 0x04);
TXFrameInfo1 = 0x80 + UART_Length%8;
pointer = &TXID1;
for (i=0;i<4;i++)
{
*(pointer++) = CAN_TX_ID[i];
}

pointer = &TXDATA1;
for (i=0;i<(UART_Length%8);i++)
{
*(pointer++) = CAN_TX_Data[i+8*(UART_Length/8)];
}
CMR_CAN1 = Request_TX;
W_WDT();
}
else
{
}
}

UART_Length = 0;

}

//-----------------------------------
//
//CAN接收中断服务程序
//
//----判断是否是RX中断,如果是
//把接受到的CAN信息通过串行口发送出去
//----其他的中断说明CAN总线出现错误或脱离
//
//-----------------------------------
void CAN_ISR(void) interrupt 0 using 1
{
unsigned char can_int;

EA = 0;

can_int = IR_CAN1;
if ((can_int&0x01) == 0x01)//接收中断
{
CAN_Receive();
CMR_CAN1 |= ReleaseRXBuf;
}
else
{
CAN_ERROR_flag = YES;//其他中断,暂时未用
}

//UART_flag = YES;
//CAN_flag = YES;
UART_Transmit();

EA = 1;
}

//-----------------------------------
//
//CAN接收数据函数
//
//----根据接受到的帧信息,按不同的长度存储
//报文数据
//
//-----------------------------------
void CAN_Receive(void) using 1
{
unsigned char i;
unsigned char xdata *pointer;
unsigned char Info;

Info = RXFrameInfo1;

if ((Info&0x80) == 0)//standard Frame
{
//CAN_RX_ID[0] = RXID1;
//CAN_RX_ID[1] = RXID2;

CAN_DataLength = Info&0x0f;
pointer = &RXID3;
for (i=0;i{
CAN_RX_Data[i] = *(pointer++);
}
for (;i<8;i++)
{
CAN_RX_Data[i] = 0x00;
}
}
else//Ex Frame
{
//CAN_RX_ID[0] = RXID1;
//CAN_RX_ID[1] = RXID2;
//CAN_RX_ID[2] = RXID3;
//CAN_RX_ID[3] = RXID4;
CAN_DataLength = Info&0x0f;
pointer = &RXDATA1;
for (i=0;i{
CAN_RX_Data[i] = *(pointer++);
//pointer += 1;
}
for (;i<8;i++)
{
CAN_RX_Data[i] = 0x00;
}
}
}
//-----------------------------------
//
//清0缓冲区
//
//----pointer,指向待清0 的缓冲区首地址
//----length 清0 的长度
//-----------------------------------
void Clear_Buffer(unsigned char *pointer,unsigned char length)
{
unsigned char i;

for (i=0;i{
*(pointer++) = 0x00;
}
}

另外头文件为:

#ifndef _CANCOM_H
#define _CANCOM_H

#define CS1_SJA1000 0x7f00//SJA1000 Pin /CS ----> P2.7,low level active

#define MOD_CAN1XBYTE[CS1_SJA1000+0] //Peli
#define CMR_CAN1XBYTE[CS1_SJA1000+1]//command
#define SR_CAN1XBYTE[CS1_SJA1000+2] //state
#define IR_CAN1XBYTE[CS1_SJA1000+3] //interrupt
#define IER_CAN1XBYTE[CS1_SJA1000+4]//interrupt enable //Peli
#define BTR0_CAN1XBYTE[CS1_SJA1000+6] //bus timing0
#define BTR1_CAN1XBYTE[CS1_SJA1000+7] //bus timing1
#define OCR_CAN1XBYTE[CS1_SJA1000+8]
#define TEST_CAN1XBYTE[CS1_SJA1000+9]
#define ECC_CAN1XBYTE[CS1_SJA1000+12]//error catch
#define EWLR_CAN1XBYTE[CS1_SJA1000+13]//error warning limit
#define RXERR_CAN1XBYTE[CS1_SJA1000+14]//
#define TXERR_CAN1XBYTE[CS1_SJA1000+15]
#define ACR0_CAN1XBYTE[CS1_SJA1000+16]
#define ACR1_CAN1XBYTE[CS1_SJA1000+17]
#define ACR2_CAN1XBYTE[CS1_SJA1000+18]
#define ACR3_CAN1XBYTE[CS1_SJA1000+19]
#define AMR0_CAN1XBYTE[CS1_SJA1000+20]
#define AMR1_CAN1XBYTE[CS1_SJA1000+21]
#define AMR2_CAN1XBYTE[CS1_SJA1000+22]
#define AMR3_CAN1XBYTE[CS1_SJA1000+23]
#define RBSA_CAN1XBYTE[CS1_SJA1000+30]//beginning of receive
#define CDR_CAN1XBYTE[CS1_SJA1000+31]//clock devide

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top