打印
[CAN-bus/DeviceNet]

关于CAN自收发程序

[复制链接]
1682|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
haikuo1486|  楼主 | 2012-8-16 14:57 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include<stdio.h>
#include<string.h>
#include<reg52.h>
#include"TxMCU.h"
#include"absacc.h"

main()
{
unsigned char i;
MCU_Init();
SJA_Init();
REG_MODE = 0x01; //"进入复位模式     "
temp_data1 = REG_MODE;
temp_data1 = temp_data1 & 0x01;
if(temp_data1 == 0x01) //"在复位模式中  "
{
REG_BTR0 = 0x85;
REG_BTR1 = 0xb4; //100k
REG_OCR = 0x1a;
REG_CDR = 0xc0;
REG_RBSA = 0x00;
REG_ACR0 = 0xff;
REG_ACR1 = 0xff;
REG_ACR2 = 0xff;
REG_ACR3 = 0xff;
REG_AMR0 = 0xff;
REG_AMR1 = 0xff;
REG_AMR2 = 0xff;
REG_AMR3 = 0xff;
REG_IR_ABLE = 0xff;
}
REG_MODE = 0x0c; //"进入自接收模式    "
REG_MODE = 0x0c;
for(i = 0;i<100;i++);
temp_data1 = REG_Receive_Counter;
send(temp_data1);
for(;;)
{
while(Tx_标志寄存器 == False);
Tx_标志寄存器 = False;
Tx_counter++;
send(Tx_counter);
temp_data1 = REG_SR;
while((temp_data1 & 0x10) == 0x10);
temp_data1 = REG_SR;
if((temp_data1 & 0x04) == 0x04)
{
REG_RxBuffer0 = 0x08; //"标准帧,长度为8      "
REG_RxBuffer1 = 0xff;
REG_RxBuffer2 = 0xff;
REG_RxBuffer3 = 0x01;
REG_RxBuffer4 = 0x02;
REG_DataBuffer1 = 0x03;
REG_DataBuffer2 = 0x04;
REG_DataBuffer3 = 0x05;
REG_DataBuffer4 = 0x06;
REG_DataBuffer5 = 0x07;
REG_DataBuffer6 = 0x08;
REG_DataBuffer7 = 0x09;
REG_DataBuffer8 = 0x0a;
}
REG_CMD = 0x10;
temp_data1 = REG_SR;
temp_data1 = temp_data1 & 0x20;
while(temp_data1 == 0x20) //"检查是否发送完成     "
{
//send(0xaa);
temp_data1 = REG_ECC;
send(temp_data1);
temp_data1 = REG_SR;
temp_data1 = temp_data1 & 0x20;
//send(temp_data1);
}
send(0x66);
temp_data1 = REG_ALC;
send(temp_data1);
temp_data1 = REG_ECC;
send(temp_data1);
temp_data1 = REG_SR;
send(temp_data1);
temp_data1 = REG_Receive_Counter;
send(temp_data1);
PC_RX_Buffer = 0x77;
LED0 = ~LED0;
// Tx_counter = 0x00;
}
}


void MCU_Init(void)
{
SJARst = 1;
LED0 = OFF;
LED1 = OFF;
PC_RX_Buffer = 0x77;
step_counter = 0x00;
step_标志寄存器 = False;
Tx_标志寄存器 = False;
temp_data1 = 0x00;
TMOD = 0x20;
TH1 = 0xff;
TL1 = 0xff;
TR1 = 1;
SCON = 0x50;
PCON = 0x80;
EA = 1;
ES = 1;
Tx_counter = 0x00;
}


void SJA_Init(void)
{
unsigned char i;
for(i = 0;i < 125;i++);
SJARst = 0;
for(i = 0;i < 125;i++);
SJARst = 1;
for(i = 0;i < 125;i++);
}


void send(unsigned char S_Data)
{
SBUF = S_Data;
while(TI == 0);
TI =0;
}


void Serial() interrupt 4 using 2
{
if(RI == 1)
{
PC_RX_Buffer = SBUF;
RI = 0;
if(PC_RX_Buffer == 0xaa)
{
send(0x13);
Tx_标志寄存器 = True;
PC_RX_Buffer = 0x77;
}
else if(PC_RX_Buffer == 0x55)
{
send(0x14);
Rx_标志寄存器 = True;
PC_RX_Buffer = 0x77;
}
else
{
send(0x15);
PC_RX_Buffer = 0x77;
}
}
}

相关帖子

沙发
杨爱林林| | 2012-10-26 15:56 | 只看该作者
看着很累

使用特权

评论回复
板凳
dingyubaobao| | 2012-10-27 15:53 | 只看该作者
厉害

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

3

帖子

0

粉丝