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

📄 canmain.c

📁 C8051F020控制的SJA1000can总线芯片的驱动程序
💻 C
字号:
#pragma  REGPARMS
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "c8051f020.h"
#include "can20.h"
#define BAUDRATE  115200              // Baud rate of UART in bps
#define SYSCLK    22118400          // SYSCLK frequency in Hz
unsigned char SendBuffer[11];
unsigned char GetBuffer[11];
unsigned char GETOK;
void config (void) {

//Local Variable Definitions
	WDTCN = 0x07;	// Watchdog Timer Control Register
    WDTCN = 0xDE;   // Disable WDT
    WDTCN = 0xAD;

	XBR0 = 0xEF;	// XBAR0: Initial Reset Value
	XBR1 = 0x07;	// XBAR1: Initial Reset Value
	XBR2 = 0x44;	// XBAR2: Initial Reset Value
// Port configuration (1 = Push Pull Output)
    P0MDOUT = 0x1D; // Output configuration for P0 
    P1MDOUT = 0x01; // Output configuration for P1 
    P2MDOUT = 0x03; // Output configuration for P2 
    P3MDOUT = 0x00; // Output configuration for P3 
    P74OUT = 0xff;  // Output configuration for P4-7
    P1MDIN = 0xFF;  // Input configuration for P1
// View port pinout

		// The current Crossbar configuration results in the 
		// following port pinout assignment:
		// Port 0
		// P0.0 = UART TX0        (Push-Pull Output)
		// P0.1 = UART RX0        (Open-Drain Output/Input)
		// P0.2 = SPI Bus SCK     (Push-Pull Output)
		// P0.3 = SPI Bus MISO    (Push-Pull Output)
		// P0.4 = SPI Bus MOSI    (Push-Pull Output)
		// P0.5 = SPI Bus NSS     (Open-Drain Output/Input)
		// P0.6 = SMBus SDA       (Open-Drain Output/Input)
		// P0.7 = SMBus SCL       (Open-Drain Output/Input)

		// Port 1
		// P1.0 = UART TX1        (Push-Pull Output)(Digital)
		// P1.1 = UART RX1        (Open-Drain Output/Input)(Digital)
		// P1.2 = PCA CEX0        (Open-Drain Output/Input)(Digital)
		// P1.3 = PCA CEX1        (Open-Drain Output/Input)(Digital)
		// P1.4 = PCA CEX2        (Open-Drain Output/Input)(Digital)
		// P1.5 = PCA CEX3        (Open-Drain Output/Input)(Digital)
		// P1.6 = PCA CEX4        (Open-Drain Output/Input)(Digital)
		// P1.7 = PCA ECI         (Open-Drain Output/Input)(Digital)
					
		// Port 2		
		// P2.0 = CP0             (Push-Pull Output)
		// P2.1 = CP1             (Push-Pull Output)
		// P2.2 = T0              (Open-Drain Output/Input)
		// P2.3 = /INT0           (Open-Drain Output/Input)
		// P2.4 = GP I/O          (Open-Drain Output/Input)
		// P2.5 = GP I/O          (Open-Drain Output/Input)
		// P2.6 = GP I/O          (Open-Drain Output/Input)
		// P2.7 = GP I/O          (Open-Drain Output/Input)

		// Port 3		
		// P3.0 = GP I/O          (Open-Drain Output/Input)
		// P3.1 = GP I/O          (Open-Drain Output/Input)
		// P3.2 = GP I/O          (Open-Drain Output/Input)
		// P3.3 = GP I/O          (Open-Drain Output/Input)
		// P3.4 = GP I/O          (Open-Drain Output/Input)
		// P3.5 = GP I/O          (Open-Drain Output/Input)
		// P3.6 = GP I/O          (Open-Drain Output/Input)
		// P3.7 = GP I/O          (Open-Drain Output/Input)

    EMI0CF = 0x24;    // External Memory Configuration Register
	EMI0CN=0x00;
    EMI0TC=0x01;
}
void SYSCLK_Init (void)
{
   int data i;                              // delay counter
   OSCXCN = 0x67;                      // start external oscillator with
                                       // 22.1184MHz crystal
   for (i=0; i < 256; i++) ;           // XTLVLD blanking interval (>1ms)
   while (!(OSCXCN & 0x80)) ;          // Wait for crystal osc. to settle
   OSCICN = 0x88;                      // select external oscillator as SYSCLK
                                       // source and enable missing clock
                                       // detector
}
void UART0_Init (void)
{
   SCON0   = 0x50;                     // SCON0: T1 mode 1, 8-bit UART, enable RX
                                      
   TMOD    = 0x21;                     // TMOD: timer 1, mode 2, 8-bit reload
                                       // T0 16 bit Timer
   T2CON &= 0x0cf;                     //select T1 as  baudrate 
   TH1    = -(SYSCLK/BAUDRATE/16);     // set Timer1 reload value for baudrate
   TR1    = 1;                         // start Timer1
   CKCON |= 0x10;                      // Timer1 uses SYSCLK as time base
   PCON  |= 0x80;                      // SMOD00 = 1
   
}
unsigned char Can_Init(void)
{
 unsigned char tt;
 unsigned int i;
 P3=0xff;
 for(i=0;i<1000;i++);
 P3=0x00;
 for(i=0;i<1000;i++);
 P3=0xff;
 for(i=0;i<1000;i++);
 ModeControlReg= RM_RR_Bit;
 tt=ModeControlReg;
 ClockDivideReg=0x88;
 InterruptEnReg=0xff;
 BusTiming0Reg= 0x03; //100 K
 BusTiming1Reg=0x2f;
 AcceptCode0Reg=0x57;
 AcceptCode1Reg=0x00;
 AcceptCode2Reg=0xaa;
 AcceptCode3Reg=0x55;
 AccepMask0Reg=0x00;
 AccepMask1Reg=0xff;
 AccepMask2Reg=0xff;
 AccepMask3Reg=0xff;
 
 RxBufstartAdr=0;
 TxErrCountReg=0;
 RxErrCountReg=0;
 ErrCodeCapReg=0;
 OutControlReg=0x1a;
 ModeControlReg=AFM_Bit;
 
 tt=StatusReg;
 while(tt==0x10)
 tt=StatusReg; 
 return tt;
}
unsigned char WriteCan()
{
 unsigned char * xdata tt;
 unsigned int i;
 unsigned char tt1;
 SendBuffer[0]=0x08;
 SendBuffer[1]=0x01;
 SendBuffer[2]=0x08;
 SendBuffer[3]=0x57;
 SendBuffer[4]=0x00;
 SendBuffer[5]=0x00;
 SendBuffer[6]=0x0;
 SendBuffer[7]=0x0;
 SendBuffer[8]=0x0;
 SendBuffer[9]=0x0;
 SendBuffer[10]=0x0;
 tt=&TxFramInFo;
 
 tt1=StatusReg;
 while((tt1&RS_Bit)!=0x00)
 tt1=StatusReg; 
 
 tt1=StatusReg;
 while((tt1&TCS_Bit)==0x00)
 tt1=StatusReg;
 for(i=0;i<11;i++)
 {*tt=SendBuffer[i];
  tt++;
 }
 CommandReg=TR_Bit;
 tt1=StatusReg;
 tt1=InterruptReg;
 return tt1;
}
void INTE0_ISR (void) interrupt 0
{
  unsigned char tt1;
  unsigned char * xdata tt;
  unsigned int i,count;
  tt1=InterruptReg;
  if((tt1&0x80)!=0)
  {ModeControlReg=AFM_Bit;
   return;
  }
  if((tt1&0x08)!=0)
  {CommandReg=0x0c;
   return;
  }
  if((tt1&0x01)!=0)
  {tt=&RxFramInFo;
   count=(*tt&0x0f);
   if((*tt&0x80)==0)
   count+=3;
   else
   count+=5;
   for(i=0;i<count;i++)
   {GetBuffer[i]=*tt;
    tt++;
   }
  for(i=0;i<count;i++)
  {SBUF0=GetBuffer[i];
   while(TI0==0);
   TI0=0;
   }
  CommandReg=RRB_Bit;
  GETOK=1;
  }
  tt1=ArbLostCapReg;
  tt1=ErrCodeCapReg; 
  return;
}
main()
{
 SYSCLK_Init();
 config();
 UART0_Init();


 Can_Init();
 EA=1;
 EX0=1;
 GETOK=1;
 while(1)
 if(GETOK==0x01)
 {WriteCan();
 // GETOK=0;
 } 
}




⌨️ 快捷键说明

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