基于AT91RM9200的CAN通讯程序
#include "sja1000.h"
unsigned int T_flag0=0;
unsigned int T_flag1=0;
//unsigned char data[11] = {\0};
//unsigned char Rx_Buf0[11] = {\0};
unsigned char Tx_Buf0[9] = {\0};
unsigned char Tx_Buf1[9] = {\0};
//unsigned char Rx_Buf1[11] = {\0};
/*writ to the register with tht data
chanal--通道号,可选CAN总线1,CAN总线2
reg-----寄存器号
data----寄存器数据*/
void setcanport(unsigned char chanal, unsigned int reg, unsigned char data1)
{
if(!chanal)
BASEADD = 0x50000000;
else
BASEADD = 0x60000000;
printf("\nThe %d will be writed to register %d\n ",data1,reg);
__asm
{
STRB reg,[BASEADD,#4]
STRB data1,[BASEADD]
}
}
/*read the data of the register to the return
chanal--通道号,可选CAN总线1,CAN总线2
reg-----寄存器号
返回值--寄存器数据*/
unsigned char getcanport(unsigned char chanal, unsigned char reg)
{
unsigned char temp;
if(!chanal)
BASEADD = 0x50000000;
else
BASEADD = 0x60000000;
__asm
{
STRB reg,[BASEADD,#4]
LDRB temp,[BASEADD]
}
return (temp);
}
/*Receive one message
chanal--通道号,可选CAN总线1,CAN总线2
返回值--数据个数*/
unsigned char getdata(unsigned char chanal, unsigned char *buf)
{
int num,i;
unsigned char data[11] = {\0};
if(!chanal)
BASEADD = 50000000;
else
BASEADD = 60000000;
do
{
}while((getcanport(chanal,StatusReg) & RBS_Bit) != RBS_Bit);
data[0] = getcanport(chanal,RxBuffer1);
data[1] = getcanport(chanal,RxBuffer2);
data[2] = getcanport(chanal,RxBuffer3);
data[3] = getcanport(chanal,RxBuffer4);
data[4] = getcanport(chanal,RxBuffer5);
data[5] = getcanport(chanal,RxBuffer6);
data[6] = getcanport(chanal,RxBuffer7);
data[7] = getcanport(chanal,RxBuffer8);
data[8] = getcanport(chanal,RxBuffer9);
data[9] = getcanport(chanal,RxBuffer10);
num = data[1]&0x0f;
for(i=0;i
*(buf++)=data[i+2];
}
setcanport(chanal,CommandReg,RRB_Bit);
*buf=0;
return (num);
}
/*Transmit one message
num-----数据个数
data----数据
返回值--0 忙*/
unsigned char senddata(unsigned char chanal, unsigned char *buf)
{
unsigned char temp = 0;
unsigned char data[11] = {\0};
int i;
if(!chanal)
BASEADD = 0x50000000;
else
BASEADD = 0x60000000;
//temp = getcanport(chanal, StatusReg);
if((getcanport(chanal,StatusReg) & TBS_Bit) == TBS_Bit)
{
for(i=0;i<8;i++)
{
data[i]=*(buf+i);
}
temp = getcanport(chanal, StatusReg);
setcanport(chanal,TxBuffer1,0x00);
setcanport(chanal,TxBuffer2,0x08);
setcanport(chanal,TxBuffer3,data[0]);
setcanport(chanal,TxBuffer4,data[1]);
setcanport(chanal,TxBuffer5,data[2]);
setcanport(chanal,TxBuffer6,data[3]);
setcanport(chanal,TxBuffer7,data[4]);
setcanport(chanal,TxBuffer8,data[5]);
setcanport(chanal,TxBuffer9,data[6]);
setcanport(chanal,TxBuffer10,data[7]);
setcanport(chanal,CommandReg,0x01);
do
{
temp = getcanport(chanal, StatusReg);
if ((temp & TCS_Bit)==TCS_Bit)
break;
} while(1); //If it is transmitted succeded?
return (1);
}
else if(!chanal)
{
if(!T_flag0)
{
for(i=0;i<8;i++)
{
Tx_Buf0[i]=*(buf+i);
}
T_flag0=1;//传输标志,1表示一个报文等待发送
}
else return (0);
}
else
{
if(!T_flag1)
{
for(i=0;i<8;i++)
{
Tx_Buf1[i]=*(buf+i);
}
T_flag1=1;//传输标志,1表示一个报文等待发送
}
else return (0);
}
}
/*Initial the SJA1000 It just is BasicCAN */
void init_sja1000(unsigned char chanal)
{
unsigned char temp = 0;
//?SJA中断禁止
//外部中断禁止
if(!chanal)//禁止can0中断
{
__asm
{
str 0x00000000,[0xfffff120]
}
__asm
{
str 0x00000000,[0xfffff124]
}
}
else//禁止can1中断
{
__asm
{
str 0x00000000,[0xfffff120]
}
__asm
{
str 0x00000000,[0xfffff124]
}
}
//进入复位请求设置模式
while((temp&RM_RR_Bit)!=RM_RR_Bit)
{
printf("\n0x01 will be set to ModeControlReg\n");
setcanport(chanal, ModeControlReg,0x07);/*Config MOD&CR register,初始化INT*/
temp = getcanport(chanal, ModeControlReg);
printf("\nModeControlReg=%d\n",temp);
}
//设置接收码
setcanport(chanal, AcceptCodeReg, 0x01);
temp = getcanport(chanal, AcceptCodeReg);
//设置屏蔽码
setcanport(chanal, AcceptMaskReg, 0xff);
temp = getcanport(chanal, AcceptMaskReg);
//设置总线bps
setcanport(chanal, BusTiming0Reg, 0x47);
temp = getcanport(chanal, BusTiming0Reg);//400kbps
setcanport(chanal, BusTiming1Reg, 0xaf);
temp = getcanport(chanal, BusTiming1Reg);
//设置CAN输出模式
setcanport(chanal, OutControlReg, 0xda);
temp = getcanport(chanal,OutControlReg);
//设置时钟分频寄存器
setcanport(chanal, ClockDivideReg, 0x48);
temp = getcanport(chanal, ClockDivideReg);
AT91RM9200CAN通 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)