发表于:2005-04-25 17:21:00
楼主
大家好,以下是我的CAN总线程序,当我调试的时候,刚开始初始化定时器0和CPU,而程序总是从Data的62开始,而且变化为EC,为什么?希望大家讨论
#include "absacc.h"
#include "stdio.h"
#include "string.h"
#include "intrins.h"
//#include "AT89X51.H"
#include "reg52.h"
//定义SJA1000的基址
#define SJA_BaseAdr 0X7F00
// 控制寄存器
#define REG_CONTROL SJA_BaseAdr+0X00
#define REG_COMMAND SJA_BaseAdr+0X01
#define REG_STATUS SJA_BaseAdr+0X02
#define REG_INTERRUPT SJA_BaseAdr+0X03
#define REG_ACR SJA_BaseAdr+0X04
#define REG_AMR SJA_BaseAdr+0X05
#define REG_BTR0 SJA_BaseAdr+0X06
#define REG_BTR1 SJA_BaseAdr+0X07
#define REG_OCR SJA_BaseAdr+0X08
#define REG_TEST SJA_BaseAdr+0X09
//发送缓冲区寄存器定义
#define REG_TxBuffer1 SJA_BaseAdr+0X0a
#define REG_TxBuffer2 SJA_BaseAdr+0X0b
#define REG_TxBuffer3 SJA_BaseAdr+0X0c
#define REG_TxBuffer4 SJA_BaseAdr+0X0d
#define REG_TxBuffer5 SJA_BaseAdr+0X0e
#define REG_TxBuffer6 SJA_BaseAdr+0X0f
#define REG_TxBuffer7 SJA_BaseAdr+0X10
#define REG_TxBuffer8 SJA_BaseAdr+0X11
#define REG_TxBuffer9 SJA_BaseAdr+0X12
#define REG_TxBuffer10 SJA_BaseAdr+0X13
//接受缓冲区寄存器定义
#define REG_RxBuffer1 SJA_BaseAdr+0X14
#define REG_RxBuffer2 SJA_BaseAdr+0X15
#define REG_RxBuffer3 SJA_BaseAdr+0X16
#define REG_RxBuffer4 SJA_BaseAdr+0X17
#define REG_RxBuffer5 SJA_BaseAdr+0X18
#define REG_RxBuffer6 SJA_BaseAdr+0X19
#define REG_RxBuffer7 SJA_BaseAdr+0X1a
#define REG_RxBuffer8 SJA_BaseAdr+0X1b
#define REG_RxBuffer9 SJA_BaseAdr+0X1c
#define REG_RxBuffer10 SJA_BaseAdr+0X1d
//时钟分频寄存器
#define REG_CDR SJA_BaseAdr+0X1F
/*
功能说明: CAN控制器SJA1000通讯波特率.SJA1000的晶振为必须为
16MHZ*/
#define ByteRate_40k 0x01
#define ByteRate_50k 0x02
#define ByteRate_80k 0x03
#define ByteRate_100k 0x04
#define ByteRate_200k 0x05
#define ByteRate_400k 0x06
#define ByteRate_500k 0x07
#define ByteRate_800k 0x08
#define ByteRate_1000k 0x09
//命令字
#define TR_CMD 0X01
#define AT_CMD 0X02
#define RRB_CMD 0X04
#define COS_CMD 0X08
#define GTS_CMD 0X10
//错误字
#define CAN_INTERFACE_OK 0
#define CAN_BUS_OK 0
#define CAN_INTERFACE_ERR 0XFF
#define CAN_ENTERSET_ERR 0XFE
#define CAN_QUITSET_ERR 0XFD
#define CAN_INITOBJECT_ERR 0XFC
#define CAN_INITBTR_ERR 0XFB
#define CAN_INITOUTCTL_ERR 0XFA
#define CAN_INTCLKDIV_ERR 0XF9
#define CAN_BUS_ERR 0XF8
//系统指针,指向SJA1000
unsigned char xdata *SJA_BCANAdr;
unsigned char data Tmod_data;
unsigned char run_lamp_flush_count = 0;
unsigned char run_lamp_flush_time = 10;
sbit run_lamp = P1^1;
unsigned char data send_data[10],rcv_data[10];
unsigned int data rxbuffer[10]={REG_RxBuffer1,REG_RxBuffer2,REG_RxBuffer3,REG_RxBuffer4,REG_RxBuffer5,REG_RxBuffer6,REG_RxBuffer7,REG_RxBuffer8,REG_RxBuffer9,REG_RxBuffer10};
unsigned int data txbuffer[10]={REG_TxBuffer1,REG_TxBuffer2,REG_TxBuffer3,REG_TxBuffer4,REG_TxBuffer5,REG_TxBuffer6,REG_TxBuffer7,REG_TxBuffer8,REG_TxBuffer9,REG_TxBuffer10};
unsigned char bdata flag_init;
sbit rcv_flag=flag_init^0;
sbit err_flag=flag_init^0;
//bit bunflag;
sbit cs=P2^7;
bit BCAN_CREAT_COMMUNATION(void)
{ SJA_BCANAdr=REG_TEST;
*SJA_BCANAdr=0XAA;
if(*SJA_BCANAdr==0XAA)
{return 0;}
else
{return 1;}
}
bit BCAN_ENTER_RETMODEL(void)
{
unsigned char tempdata;
SJA_BCANAdr=REG_CONTROL;
tempdata=*SJA_BCANAdr;
*SJA_BCANAdr=(tempdata|0x01);
if(*SJA_BCANAdr&0X01==0X01)
{return 0;}
else
{return 1;}
}
bit BCAN_QUIT_RETMODEL(void)
{
unsigned char tempdata;
SJA_BCANAdr=REG_CONTROL;
tempdata=*SJA_BCANAdr;
*SJA_BCANAdr=(tempdata&0xFE);
if(*SJA_BCANAdr&0X01==0X00)
{return 0;}
else
{return 1;}
}
bit BCAN_SET_BA