⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 motorola单片机MCHC908的使用方法以及例程
💻 C
字号:
#include <hidef.h> /* for EnableInterrupts macro */
#include <MC68HC908GZ16.h> /* include peripheral declarations */

#pragma MESSAGE DISABLE C1825

#define DATA_FRAME 0
#define REMOTE_FRAME 1
#define ERR_VALUE 0
/* Standard ID, 11 bit, Data */
#define ID100 0x20000000
//指针数组
static byte* IDR1regs[3] = {&TB0IDR1,&TB1IDR1,&TB2IDR1}; /* adresses of IDR1 registers */
static dword* IDRregs[3] = {&TB0IDR0,&TB1IDR0,&TB2IDR0}; /* adresses of IDR registers */
static byte* IDR3regs[3] = {&TB0IDR3,&TB1IDR3,&TB2IDR3}; /* adresses of IDR3 registers */
static byte* DSRregs[3] = {&TB0DSR0,&TB1DSR0,&TB2DSR0}; /* adresses of DSR registers  */
static byte* DLRregs[3] = {&TB0DLR,&TB1DLR,&TB2DLR}; /* adresses of DLR registers  */
static byte* TBPRregs[3] = {&TB0TBPR,&TB1TBPR,&TB2TBPR}; /* adresses of TBPR registers */

__interrupt void CAN1_FullRxBuffer(void)
{

  /* include your code here */
  CRFLG_RXF = 1;                       /* Reset the reception complete flag */
}


byte CAN1_SendFrame(byte BufferNum,dword MessageID,byte FrameType,byte Length,byte* Data)
{
  byte i;                              /* Temorary variables */
  byte bufmask=((word)1<<BufferNum);   /* Buffer mask */

  if ( (BufferNum > 2) || (Length > 8) ) /* Is BufferNum greater than 2 or Length greater than 8?*/
    return ERR_VALUE;                  /* If yes then error */
  if (FrameType > REMOTE_FRAME)        /* Is FrameType other than REMOTE_FRAME or DATA_FRAME */
    return ERR_VALUE;                  /* If yes then error */
  if (!(CTFLG & bufmask))              /* Is the transmit buffer full? */
    return ERR_VALUE;                  /* If yes then error */
  
  *IDRregs[BufferNum] = MessageID;     /* Sets the message as "standard" */

  if (FrameType == DATA_FRAME) {       /* Is it a data frame? */
    for(i=0; i<Length; i++)
      *((DSRregs[BufferNum])+i) = Data[i]; /* Store data to the transmit register */
  }
  *DLRregs[BufferNum] = Length;        /* Set the length of the message */
  *TBPRregs[BufferNum] = 0;            /* Set the priority (high) */
  CTFLG = 1 << BufferNum;              /* Start transmission */
  return 1;                       /* OK */
}


void main(void) {
  
  unsigned char sendmsg[8] ="12345678";
  
  /* include your code here */
  CONFIG1= 0x01;				// COP Disable
  CONFIG2= 0x08;
  
  /* CAN Inititalization */
    /* CMCR0: bit7=0,bit6=0,bit5=0,SYNCH=0,TLNKEN=0,SLPAK=0,SLPRQ=0,SFTRES=1 */
  CMCR0 = 0x01;                           /* CAN reset */
  /* CMCR1: bit7=0,bit6=0,bit5=0,bit4=0,bit3=0,LOOPB=1,WUPM=0,CLKSRC=0 */
  CMCR1 = 0x04;
  CIDAC_IDAM = 0;                      /* Set the acceptance mode: 32 bit mode */

  /* Checks all bits in the ID */
  CIDAR0 = 0;                           /* Set the acceptance code */
  CIDAR1 = 0;                           /* Set the acceptance code */
  CIDAR2 = 0;                           /* Set the acceptance code */
  CIDAR3 = 0;                           /* Set the acceptance code */

  /* Set acceptance mask for Standard ID 0x100 */
  CIDMR0 = 0x20;                   /* Set the acceptance mask */
  CIDMR1 = 0x00;                   /* Set the acceptance mask */
  CIDMR2 = 0x00;                   /* Set the acceptance mask */
  CIDMR3 = 0x00;                   /* Set the acceptance mask */

  CBTR0=64;                            /* Set the device timing register */
  CBTR1=122;                           /* Set the device timing register */
  CMCR1_CLKSRC=0;                      /* Select the clock source */

  CMCR0 &= ~1;                       /* Start the device */
  while(!CMCR0_SYNCH);               /* Wait for synchronization */
  CRIER = 0x01;                       /* Enable Reception interrupts */

  EnableInterrupts; /* enable interrupts */

  for(;;) {
  
  	CAN1_SendFrame(1,ID100,DATA_FRAME,8,sendmsg);
  
  } /* loop forever */
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -