有不明之处,请教大家。上次见jsbychm 问过关于MOTO芯片的MSCAN通讯问题,那个程序我还看不明白,现在找了一个比较简单的,其中最关键的,就是如何进行can通讯的,还是不明要领:
/* 该例子来自邵老师那本关于S12的书里头的一个can通讯的范例程序,大致意思是将数据利用mscan0传,mscan4接收
#include "printp.h"
#define PORTB (*((volatile unsigned char*)(0x0001)))
#define DDRB (*((volatile unsigned char*)(0x0003)))
#define CAN0CTL0 (*((volatile unsigned char*)(0x0140)))
…
相关的寄存器地址定义略
…
void MSCAN_INITIAL(void)
{
CAN0CTL0|=0x01; // CAN Control Register 0000,0001-->TNITRQ=1
CAN4CTL0|=0x01;
while(!(CAN0CTL0&0x01)) // CAN0CTL0 为0x01,则往下执行,实为等待CAN0CTL0为0x01
{}
while(!(CAN4CTL0&0x01))
{}
CAN0IDAC=0x20; // CAN Identifier Acceptance Control 0010,0000
CAN4IDAC=0x20;
CAN0IDAR0=0x01; //验收寄存器,不太明白
CAN4IDAR0=0x01;
CAN0IDAR1=0x00;
CAN4IDAR1=0x00;
CAN0IDAR2=0x04;
CAN4IDAR2=0x04;
CAN0IDAR3=0x08;
CAN4IDAR3=0x08;
CAN0IDAR4=0x41;
CAN4IDAR4=0x41;
CAN0IDAR5=0x41;
CAN4IDAR5=0x41;
CAN0IDAR6=0x41;
CAN4IDAR6=0x41;
CAN0IDAR7=0x41;
CAN4IDAR7=0x41;
CAN0IDMR0=0xff; //验收屏蔽寄存器
CAN0IDMR1=0xff;
CAN0IDMR2=0xff;
CAN0IDMR3=0xff;
CAN0IDMR4=0xff;
CAN0IDMR5=0xff;
CAN0IDMR6=0xff;
CAN0IDMR7=0xff;
CAN4IDMR0=0xff;
CAN4IDMR1=0xff;
CAN4IDMR2=0xff;
CAN4IDMR3=0xff;
CAN4IDMR4=0xff;
CAN4IDMR5=0xff;
CAN4IDMR6=0xff;
CAN4IDMR7=0xff;
CAN0BTR0=0x61; //Bus Timing Register
CAN4BTR0=0x61;
CAN0BTR1=0x13;
CAN4BTR1=0x13;
CAN0CTL1=0x80;
CAN4CTL1=0x80;
CAN0CTL0&=0xfe;
CAN4CTL0&=0xfe;
while(CAN0CTL1&0x01)
{}
while(CAN4CTL1&0x01)
{}
CAN0CTL0=0x08;
CAN4CTL0=0x08;
CAN0RIER=0x0f;
CAN4RIER=0x0f;
CAN4TBSEL=CAN4TFLG; //CAN Transmit Buffer Selection=CAN Transmitter Flag Register
CAN4TXFG1=0x01; //发送区域写数据
CAN4TXFG2=0x00;
CAN4TXFG4=0x41;
CAN4TXFG5=0x42;
CAN4TXFG6=0x43;
CAN4TXFG7=0x44;
CAN4TXFG8=0x45;
CAN4TXFG9=0x46;
CAN4TXFGA=0x47;
CAN4TXFGB=0x48;
CAN4TXFGC=0x49;
CAN4TFLG=CAN4TBSEL;
}
void main(void) {
MSCAN_INITIAL();
while(!(CAN4TFLG&0x01))
{printp("\nCAN4 SEND\n");}
}
void CAN0_RECEIVE(void)
{
asm
{
sei //中断屏蔽开
}
printp("CAN0 RECEIVE \n");
asm
{
cli //中断屏蔽关
}
asm
{
rti //return from interrupt
}
}
void UnimplementedISR(void)
{
/* Unimplemented ISRs trap.*/
asm stop;
}
[此贴子已经被作者于2005-3-25 15:00:34编辑过] |