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

📄 main.c

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

#include <stdio.h>
#pragma MESSAGE DISABLE C1825

#define DATA_FRAME 0
#define REMOTE_FRAME 1
#define ERR_VALUE 0
/* Standard ID, 11 bit, Data */
#define ID100 0x00180000
//指针数组
static dword* IDRregs[3] = {&TB0IDR0,&TB1IDR0,&TB2IDR0}; /* adresses of IDR 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 */


char TR_buffer[13];    //发送缓冲数组(打包用)
char SbufSav[5];    //接收缓冲数组(解包用)
char DatSav[10];

dword* dd;
dword* ff;
dword ee,gg;
uchar biao1,biao2;
uint xunhuan;

char buf[20];




byte CAN1_SendFrame(dword MessageID,byte FrameType,byte Length,byte* Data)
{				//数据长度Length不能超过8,帧类型FrameType只能为0或1
byte BufferNum,bufmask;	//定义选择的发送缓冲区,值0~2
  byte i;    
  if(CTFLG_TXE0)BufferNum=0;
 else if(CTFLG_TXE1)BufferNum=1;
 else if(CTFLG_TXE2)BufferNum=2;
  bufmask=((word)1<<BufferNum);   /* Buffer mask */

  if (!(CTFLG & bufmask))              /* Is the transmit buffer full? */
    return ERR_VALUE;                  /* If yes then error */
  
  *IDRregs[BufferNum] = MessageID;    

  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 Transmit (unsigned char* sMessage){
	while(*sMessage!=0){
		SCDR = *sMessage;
	sMessage++;
		while(!SCS1_SCTE);			
	}
}

void main(void) {
  
 char sendmsg[10]="";

  /* include your code here */
  CONFIG1= 0x4b;				// COP Disable
  CONFIG2= 0x0f;
  
  // SCI发送调试成功,仅可以发送 (用自编接受的高级程序就可接受结果,在发送的字符之间插入B就可)
  
 SCBR = 0x02;				// Select 9600 Baud rate over a clock = 2.4576 MHz 
	SCC1 = 0x40;		        // Enable SCI 
	SCC2 = 0x08;        		// Receive disable, Transmit enable
	SCS1;						// Ackowledge any pending interrupts 
//while(1){
 // sprintf(buf,"%dB\n",sendmsg);
//Transmit (sendmsg);
//}
  /* 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 = 0x00;                           /* Set the acceptance code */
  CIDAR1 = 0x18;                           /* Set the acceptance code */
  CIDAR2 = 0x00;                           /* Set the acceptance code */
  CIDAR3 = 0x00;                           /* Set the acceptance code */

  /* Set acceptance mask for Standard ID 0x100 */
  CIDMR0 = 0x00;                   /* 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 */
 
  CMCR0 &= ~1;                       /* Start the device */
  while(!CMCR0_SYNCH);               /* Wait for synchronization */
  CRIER = 0x01;                       /* Enable Reception interrupts */

  EnableInterrupts; /* enable interrupts */

  for(xunhuan=100;xunhuan<1000;xunhuan++) {

   sprintf(sendmsg,"%dB",xunhuan);
  	
  	//biao1=
  	CAN1_SendFrame(ID100,DATA_FRAME,8,sendmsg);
 /* 	if((biao1)&&(biao2))
  	{ sprintf(buf,"%c",DatSav[0]);
 Transmit(buf);
 sprintf(buf,"%c",DatSav[1]);
 Transmit(buf);
 sprintf(buf,"%c",DatSav[2]);
 Transmit(buf);
 sprintf(buf,"%c",DatSav[3]);
 Transmit(buf);
 sprintf(buf,"%c",DatSav[4]);
 Transmit(buf);
 sprintf(buf,"%c",DatSav[5]);
 Transmit(buf);
 sprintf(buf,"%c",DatSav[6]);
 Transmit(buf);
 sprintf(buf,"%c",DatSav[7]);
 Transmit(buf);
  	biao2=0;
  asm nop;}*/
 // Transmit(DatSav);
 if(biao2)
{
 SCDR=RBDSR0;
while(!SCS1_SCTE);	
SCDR=RBDSR1;
while(!SCS1_SCTE);	
SCDR=RBDSR2;
while(!SCS1_SCTE);	
SCDR=RBDSR3;
while(!SCS1_SCTE);	
SCDR=RBDSR4;
while(!SCS1_SCTE);	
SCDR=RBDSR5;
while(!SCS1_SCTE);	
SCDR=RBDSR6;
while(!SCS1_SCTE);	
SCDR=RBDSR7;
while(!SCS1_SCTE);	
biao2=0;

}
 asm nop;
  } /* loop forever */
asm nop;
}


interrupt void CAN1_FullRxBuffer(void)
{

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

dd=&RBIDR0;
ee=*dd;
ff=&TB0IDR0;
gg=*ff;
SbufSav[0]=RBIDR0;
SbufSav[1]=RBIDR1;
SbufSav[2]=RBIDR2;
SbufSav[3]=RBIDR3;
DatSav[0]=RBDSR0;
DatSav[1]=RBDSR1;
DatSav[2]=RBDSR2;
DatSav[3]=RBDSR3;
DatSav[4]=RBDSR4;
DatSav[5]=RBDSR5;
DatSav[6]=RBDSR6;
DatSav[7]=RBDSR7;
DatSav[8]=RBDLR;
biao2=1;



//if(biao1)

}

⌨️ 快捷键说明

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