|
/* 2.00 2005.1.18 Version2.0 MC68HC908GZ60中CAN调试主文件。 本自发自收的程序是成功的?可以实现发送接收中断,接收的数据完全正确。 *************************************************************************/ #include <hidef.h> /* for EnableInterrupts macro */ #include <MC68HC908GZ60.h> /* include peripheral declarations */ #define SelfTest CMCR1_LOOPB=1 //进入自测模式 #define EnableTransmitInt CTCR=0x07 //使能发送中断 #define EnableReceiveInt CRIER_RXFIE=1 //使能接收中断
void Can_Init(void); void Transmit_Frame(void); //发送一帧 void ISR_Receive(void); //接收中断函数 void ISR_Transmit(void); void Fill_Frame(byte); #define SetExt_Frame(Hit) Trans_Buf[Hit].ID1|=0x18 #define SetData_Frame(Hit) Trans_Buf[Hit].ID3&=0xfe #define SetStd_Frame(Hit) Trans_Buf[Hit].ID1&=0xe7 #define SetRem_Frame(Hit) Trans_Buf[Hit].ID3|=0x01
typedef struct _Tr_Rv_BUF_{ //Transmit and Receive buffer,16 bytes in all byte ID0; //ID21---ID28 byte ID1; //ID20,19,18,SRR,IDE,ID17,16,15 byte ID2; //ID7--ID14 byte ID3; //ID0-ID6,SRR byte Data_Segment[8]; //Data Segment, 8 bytes byte Data_Len; //Data Length, 4 bits //#ifdef Can_Trans byte Tran_Buf_Pri; //Transmit Frame priority,only for Transmit Buffer, 1 byte //#else //byte unimplemented; //1 byte //#endif byte unused[2]; //2 bytes }CanBufFrame,*p_CanBufFrame; extern volatile CanBufFrame Rec_Buf; extern volatile CanBufFrame Trans_Buf[3]; extern volatile byte Rcv_Data[8]; //used to save Received Data extern volatile CanBufFrame *Frame_Buf; void main() { //CONFIG1=1; CONFIG2_MSCANEN=1; //Enable Can Module,using PTC0/PTC1 Can_Init(); EnableTransmitInt; EnableReceiveInt; EnableInterrupts; Transmit_Frame(); for(;;); } void Can_Init(void) { CMCR0_SFTRES=1; //SoftWare Reset enable 进入初始化状态 do{ }while(!(CMCR0_SFTRES));//判断是否设置成功
CMCR1=0x00; // loop back configuration and Wake up mode features. SelfTest;//进入自测试模式 CBTR0=0x22; // bit timing portion CBTR1=0x38; //boud rate=5K Hz
CIDAC=0; //Single 32-bit acceptance filter CIDAR0=0b00000000; CIDAR1=0b00000000; CIDAR2=0b00000000; CIDAR3=0b00000000; CIDMR0=CIDMR1=0xff; CIDMR2=CIDMR3=0xff; //不和标识符接收寄存器的内容比较 do{CMCR0_SFTRES=0;} while(CMCR0_SFTRES); //SoftWare Enable Can } void interrupt 20 ISR_Receive(void) //Receive Frame successfully! { byte Rcv_Data[8],i; if(CRFLG_RXF) for(i=0;i<=8;i++){ Rcv_Data[i]=Rec_Buf.Data_Segment[i]; } CRFLG_RXF=1; } void interrupt 21 ISR_Transmit(void) { for(;;); //发送成功! } void Fill_Frame(byte Hit){ byte i; Trans_Buf[Hit].ID0=0x80; Trans_Buf[Hit].ID1=0x00; Trans_Buf[Hit].ID2=0x80; Trans_Buf[Hit].ID3=0x00; for(i=0;i<8;i++){ Trans_Buf[Hit].Data_Segment[i]=i; } Trans_Buf[Hit].Data_Len=0x08; Trans_Buf[Hit].Tran_Buf_Pri=0x01; SetExt_Frame(Hit); SetData_Frame(Hit); } void Transmit_Frame(void) { if(1==CTFLG_TXE0){ Fill_Frame(0); CTFLG_TXE0=0; //Start Transmitting } else if(1==CTFLG_TXE1){ Fill_Frame(1); CTFLG_TXE1=0; } else if(1==CTFLG_TXE2){ Fill_Frame(2); CTFLG_TXE2=0; } }
|