Code:
#include <hidef.h> /* for EnableInterrupts macro */#include <MC68HC908GZ60.h> /* include peripheral declarations */#include "IO.h"#include "TIM.h"#include "SCI.h"#include "CAN.h"#include "common.h"#define SaveStatusReg() { asm PSHA; asm TPA; asm SEI; asm STA CCR_reg; asm PULA; } /* This macro is used by Processor Expert. It saves CCR register and disable global interrupts. */#define RestoreStatusReg() { asm PSHA; asm LDA CCR_reg; asm TAP; asm PULA; } /* This macro is used by Processor Expert. It restores CCR register saved in SaveStatusReg(). */#define EnterCritical() SaveStatusReg()#define ExitCritical() RestoreStatusReg()static byte * IDR1regs[3] = {&IDR10,&IDR11,&IDR12}; /* adresses of IDR1 registers */static dword * IDRregs[3] = {&IDR0,&IDR1,&IDR2}; /* adresses of IDR registers */static byte * IDR3regs[3] = {&IDR30,&IDR31,&IDR32}; /* adresses of IDR3 registers */static byte * DSRregs[3] = {&DSR00,&DSR01,&DSR02}; /* adresses of DSR registers */static byte * DLRregs[3] = {&DLR0,&DLR1,&DLR2}; /* adresses of DLR registers */static byte * TBPRregs[3] = {&TBPR0,&TBPR1,&TBPR2}; /* adresses of TBPR registers *//**************************************************************** ** Definition of a variable ** *****************************************************************/byte CCR_reg; // Current CCR registerextern unsigned char LED_Flag;extern unsigned char SCI_Transmit_Flag;extern unsigned char SCI_Transmit_Data[15];extern unsigned char DP256;extern unsigned char GZ16;extern unsigned char AZ60;extern unsigned char MPC555;extern unsigned char LCDValueHigh;extern unsigned char LCDValueLow;extern unsigned char Command;extern unsigned char LEDValue;extern unsigned char CheckSum; extern unsigned char CAN_Transmit_Flag;extern unsigned char CAN_Receive_Flag;extern dword CAN_MessageID;extern byte CAN_FrameType;extern byte CAN_FrameFormat;extern byte CAN_DataLength;extern byte CAN_Data[8];/**************************************************************** ** Function definition ** *****************************************************************/void CAN_Init(void){ /* CMCR0: —–=0,˜™=0,??=0,SYNCH=0,TLNKEN=0,SLPAK=0,SLPRQ=0,SFTRES=1 */ CMCR0 = 1; /* CAN reset */ /* CMCR1: ??=0,??=0,??=0,??=0,??=0,LOOPB=0,WUPM=0,CLKsrc=0 */ CMCR1 = 0; //The clock source is exterior crystal oscillator/2 = 2Mhz CIDAC_IDAM = 0; //32 Members receive filters CIDAR = 0x00000000; //Set the acceptance code CIDMR = 0xFFFFFFFF; //Set the acceptance mask CBTR0 = 0x43; CBTR1 = 0xA3; //The baud rate is 62.5K CMCR0 = 0x00; //Establishes the CAN module as the data feeds output condition CRIER = 0xFF; //CAN Interruption in order to receive}/*** ===================================================================** Method : CAN_ReadFrame (dword *MessageID,byte *FrameType,byte *FrameFormat,byte *Length,byte* Data)**** Description :** From receiving a frame buffer, retrieval of data. ** Parameters :** NAME - DESCRIPTION** * MessageID - Signs at targets ** * FrameType - Frame type indicator** DATA_FRAME - Data frame** REMOTE_FRAME - Remote frame** * FrameFormat - Frame format for** STANDARD_FORMAT - Standard frame 11-bits** EXTENDED_FORMAT - Expansion frame 29-bits** * Length - Frame length** * Data - Receives data** Returns :** --- None** ===================================================================*/void CAN_ReadFrame(dword *MessageID,byte *FrameType,byte *FrameFormat,byte *Length,byte* Data){ dword ID; //Temporary variable ID = REC_IDR; //Reads takes the designator if ((REC_IDR1 & 8)) //Is "expansion frame"£¿ { ID = ( ((ID >> 1) & 0x0003FFFF) | ((ID >> 3) & 0xFFFC0000) ); //ID Results *FrameType = (REC_IDR3 & 1)? REMOTE_FRAME : DATA_FRAME; //Frame type£Long-distance frame or data frame£¿ } else //If is "standard frame" { ID >>= 21; //ID Results *FrameType = (REC_IDR1 & 16)? REMOTE_FRAME : DATA_FRAME; //Frame type } *MessageID = ID; //ID Results *FrameFormat = (REC_IDR1 & 8)? EXTENDED_FORMAT : STANDARD_FORMAT; //Frame type *Length = REC_DLR & 0x0F; //Data length if (*FrameType == DATA_FRAME) // If the data frame { Data[0] = REC_DSR0; Data[1] = REC_DSR1; Data[2] = REC_DSR2; Data[3] = REC_DSR3; Data[4] = REC_DSR4; Data[5] = REC_DSR5; Data[6] = REC_DSR6; Data[7] = REC_DSR7; //To the array of data storage } }/*** ===================================================================** Method : CAN1_SendFrame (byte BufferNum,dword MessageID,byte FrameType,byte Length,byte * Data)**** Description :** Transmits an information. ** Parameters :** NAME - DESCRIPTION** BufferNum - Buffer numeral ** MessageID - Information signs at ** FrameType - Frame types ** DATA_FRAME - data frame** REMOTE_FRAME - remote frame** Length - Frame length (0..8)** * Data - Data ** Returns :** --- - 0 Error** - 1 Success** ===================================================================*/byte CAN_SendFrame(byte BufferNum,dword MessageID,byte FrameType,byte Length,byte * Data){ byte i; //Temporary variables byte bufmask=((word)1<<BufferNum); //Buffer mask if ( (BufferNum > 2) || (Length > 8) ) //If the buffer marking is bigger than 2 Or greater than the length of data 8 return 0; // Return error if (FrameType > REMOTE_FRAME) //If the frame frequency is not the long-distance frame or the data frame return 0; //Return error if (!(CTFLG & bufmask)) //The transmission buffer is full return 0; //Return error EnterCritical(); //Prohibit a break CTCR_TXEIE |= bufmask; //To be able to interrupt if (MessageID < 2048) //Is the standard frame£¿ *IDRregs[BufferNum] = (MessageID << 21); // Establishes this news is the standard frame else *IDRregs[BufferNum] = ( ((MessageID<<1) & 0x0007FFFF) | ((MessageID << 3) & 0xFFE00000) ) | 0x00080000 ; //Establishment news for expansion frame if (FrameType == DATA_FRAME) //Is the data frame£¿ { for(i=0; i<Length; i++) *((DSRregs[BufferNum])+i) = Data[i]; /* Store data to the transmit register */ if (MessageID < 2048) /* Is it the standard frame? */ *IDR1regs[BufferNum] &= ~16; /* If yes then set message type as "data frame" */ else *IDR3regs[BufferNum] &= ~1; /* If no then set message type as "data frame" */ } else // { if (MessageID < 2048) //Is the standard frame£¿ *IDR1regs[BufferNum] |= 16; //Establishes this is the long-distance frame else *IDR3regs[BufferNum] |= 1; } *DLRregs[BufferNum] = Length; //A message length *TBPRregs[BufferNum] = 0; //A priority level CTFLG = bufmask; //Start sending ExitCritical(); //To be able to interrupt return 1; //Success}void SoftDelay(){ int i = 0; for(i=0;i<0xfff;i++); for(i=0;i<0xfff;i++); for(i=0;i<0xfff;i++); for(i=0;i<0xfff;i++); for(i=0;i<0xfff;i++); for(i=0;i<0xfff;i++); for(i=0;i<0xfff;i++); for(i=0;i<0xfff;i++); for(i=0;i<0xfff;i++); for(i=0;i<0xfff;i++);}void CAN_Task(void){ byte BufferNum; if(CAN_Receive_Flag==1) //After processes CAN to receive the information the duty { if(CAN_Data[1]==0x10) //Judges whether for feedback information { DP256 = CAN_Data[0]; GZ16 = CAN_Data[1]; AZ60 = CAN_Data[2]; MPC555 = CAN_Data[3]; LCDValueHigh = CAN_Data[4]; LCDValueLow = CAN_Data[5]; Command = CAN_Data[6]; LEDValue = CAN_Data[7]; SCI_Transmit_Data[0] = (byte)(CAN_MessageID&0xFF); SCI_Transmit_Data[1] = (byte)((CAN_MessageID>>8)&0xFF); SCI_Transmit_Data[2] = (byte)((CAN_MessageID>>16)&0xFF); SCI_Transmit_Data[3] = (byte)((CAN_MessageID>>24)&0xFF); SCI_Transmit_Data[4] = CAN_Data[0]; SCI_Transmit_Data[5] = CAN_Data[1]; SCI_Transmit_Data[6] = CAN_Data[2]; SCI_Transmit_Data[7] = CAN_Data[3]; SCI_Transmit_Data[8] = CAN_Data[4]; SCI_Transmit_Data[9] = CAN_Data[5]; SCI_Transmit_Data[10] = CAN_Data[6]; SCI_Transmit_Data[11] = CAN_Data[7]; SCI_Transmit_Data[12] = 8; SCI_Transmit_Data[13] = 1; SCI_Transmit_Data[14] = 5; } if(GZ16==0x10) //Judge whether there GZ16 node { LED_Flag = 1; } SCI_Transmit_Flag = 1; CAN_Receive_Flag = 0; LED8 = 0; } if(CAN_Transmit_Flag==1) //Processes duty which CAN needs to transmit { //LED7 = 0; //Test /* if (CAN_Data[2] == 0) { LED1 = 0; LED2 = 0; LED3 = 0; }else { LED1 = 0; LED2 = 0; LED3 = 1; } */ SoftDelay(); //Choose to buffer empty if(CTFLG_TXE0) { BufferNum = 0; } else if(CTFLG_TXE1) { BufferNum = 1; } else if(CTFLG_TXE2) { BufferNum = 2; } if(CAN_SendFrame(BufferNum,CAN_MessageID,CAN_FrameType,CAN_DataLength,CAN_Data)==1) { CAN_Transmit_Flag = 0; //If transmits successfully£Sent a request to remove signs //LED7 = 1; SoftDelay(); } } }interrupt void Int_CAN_Receive(void){ //CAN_Receive_Flag = 1; CAN_ReadFrame(&CAN_MessageID,&CAN_FrameType,&CAN_FrameFormat,&CAN_DataLength,CAN_Data); LED1 = CAN_Data[0]; LED2 = CAN_Data[1]; LED3 = CAN_Data[2]; LED4 = CAN_Data[3]; LED5 = CAN_Data[4]; LED6 = CAN_Data[5]; LED7 = CAN_Data[6]; LED8 = CAN_Data[7]; CRFLG = 1; //Reset the reception complete flag }interrupt void Int_CAN_Transmit(void){ CTCR = 0x00;}interrupt void Int_CAN_Error(void){ if(CRFLG_BOFFIF) { LED1 = 0; LED2 = 0; LED3 = 0; LED4 = 0; LED5 = 0; LED6 = 0; LED7 = 0; LED8 = 0; }}interrupt void Int_CAN_Wakeup(void){ }////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////This is my main file//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #include <hidef.h> /* for EnableInterrupts macro */#include <MC68HC908GZ60.h> /* include peripheral declarations */#include "IO.h"#include "TIM.h"#include "SCI.h"#include "CAN.h"#include "SPI.h"#include "TBM.h"#include "MC33989.h"#include "common.h"#include "port.h"/**************************************************************** ** ¶¨ÒåÈ«¾Ö±äÁ¿ ** *****************************************************************/unsigned char timer_10ms_flag;unsigned char timer_1s_flag;unsigned char timer_10ms_counter;unsigned int timer_1s_counter;unsigned char SCI_Receive_Data[9];unsigned char SCI_Transmit_Data[15];unsigned char SCI_Receive_Index;unsigned char SCI_Transmit_Index;unsigned char SCI_Receive_Flag;unsigned char SCI_Transmit_Flag;unsigned char DP256;unsigned char GZ16;unsigned char AZ60;unsigned char MPC555;unsigned char LCDValueHigh;unsigned char LCDValueLow;unsigned char Command;unsigned char LEDValue;unsigned char CheckSum;unsigned char LED_Flag;unsigned char CAN_Transmit_Flag;unsigned char CAN_Receive_Flag;dword CAN_MessageID;byte CAN_FrameType;byte CAN_FrameFormat;byte CAN_DataLength;byte CAN_Data[8] = "00001111";unsigned char LED_Delay_Counter;unsigned char SPI_Data;extern void SoftDelay();/**************************************************************** ** º¯Êý¶¨Òå ** *****************************************************************/void Hardware_Init(void){ CONFIG1_COPD = 1; //¿´ÃŹ·½ûÖ¹ //CONFIG2_MSCANEN = 1; //CANÄ£¿éʹÄÜ //CONFIG2_TMCLKSEL = 1; //ʱ»ùÄ£¿é128·ÖƵ //CONFIG2_OSCENINSTOP = 1; //STOP״̬Ï£¬Õñµ´Æ÷ʹÄÜ //CONFIG2_SCIBDsrc=1; //ÓÃÄÚ²¿×ÜÏßʱÖÓ×÷ΪSCIµÄʱÖÓÔ´ CONFIG2 = 0x0F; IO_Init(); SCI_Init(); CAN_Init(); TIM_Init(); TBM_Init(); SPI_Init(); MC33989_Init();}void Variable_Init(void) //±äÁ¿³õʼ»¯{ //¼ÆÊýÆ÷£¬±ê־λ timer_10ms_flag = 0; timer_1s_flag = 0; timer_10ms_counter = COUNTER_10MS_MAX; timer_1s_counter = COUNTER_1S_MAX; //SCI SCI_Receive_Flag = 0; SCI_Receive_Index = 0; SCI_Transmit_Flag = 0; SCI_Transmit_Index = 0; //LED LED_Flag = 1; LED_Delay_Counter = 0; //CAN CAN_Transmit_Flag = 0; CAN_Receive_Flag = 0; CAN_MessageID = 0x22FFFFFF; CAN_FrameType = DATA_FRAME; CAN_FrameFormat = EXTENDED_FORMAT; CAN_DataLength = 8; }void main(void) { int i = 0; Hardware_Init(); Variable_Init(); PORTF = 0xFF; DDRF = 0xFF; EnableInterrupts; /* enable interrupts */ for(;;) { CAN_Transmit_Flag = 1; if (i%2 == 0) { CAN_Data[0] = 0; CAN_Data[1] = 0; CAN_Data[2] = 0; CAN_Data[3] = 0; CAN_Data[4] = 1; CAN_Data[5] = 1; CAN_Data[6] = 1; CAN_Data[7] = 1; }else { CAN_Data[0] = 1; CAN_Data[1] = 1; CAN_Data[2] = 1; CAN_Data[3] = 1; CAN_Data[4] = 0; CAN_Data[5] = 0; CAN_Data[6] = 0; CAN_Data[7] = 0; } i = i +1; CAN_Task(); SoftDelay(); /* if(timer_10ms_flag==1) { LED1 = ~LED1; PORTF = 0x0F; //SCI_Task(); //LED_Task(); timer_10ms_flag = 0; }*/ } /* loop forever */ }