野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 15535|回复: 5

这是51单片机端的代码通过can总线传输到arm上,但是他的ID我没看懂

[复制链接]
发表于 2015-11-23 22:52:48 | 显示全部楼层 |阅读模式
//=================================================================================
//′®¿úμ½can       4800bps
//
//sbit SCK=P2^3;
//sbit SI=P2^2;
//sbit SO=P2^1;
//sbit CS=P2^0;
//INT ½óP3.3
//
//=================================================================================

#include "reg51.h"
#include "2515.h"
#include "SPI.h"
#include "Function.h"

#include "CAN0.h"
#define uchar unsigned char
#define uint unsigned int
void CAN_Send_anylength(unsigned char *CAN_TX_Buf,unsigned char length1);


unsigned char gRXFlag=0,sRXFlag=0,yRXFlag=0;
unsigned char data Com_RecBuff[8];
unsigned char bdata flag_init;

//*************************
#define INBUF_LEN 8   //êy¾Y3¤¶è

unsigned char inbuf1[INBUF_LEN];
unsigned char checksum,count3=0;


unsigned char a[8];
unsigned char b[8]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};
sbit  P2_0  = P3^2;
unsigned char  U8FLAG,k;
unsigned char  U8count,U8temp;
unsigned char  U8T_data_H,U8T_data_L,U8RH_data_H,U8RH_data_L,U8checkdata;
unsigned char  U8T_data_H_temp,U8T_data_L_temp,U8RH_data_H_temp,U8RH_data_L_temp,U8checkdata_temp;
unsigned char  U8comdata;
unsigned char  shidu_shi,shidu_ge,wendu_shi,wendu_ge;
#define   DataPort P1    //LCD1602êy¾Y¶Ë¿ú
sbit      LCM_RS=P3^5;   //LCD1602Ãüáî¶Ë¿ú               
sbit      LCM_RW=P3^6;   //LCD1602Ãüáî¶Ë¿ú               
sbit      LCM_EN=P3^7;   //LCD1602Ãüáî¶Ë¿ú
/*******************************/

/*******************************/
void WaitForEnable(void)       
{                                       
DataPort=0xff;               
LCM_RS=0;LCM_RW=1;_nop_();
LCM_EN=1;_nop_();_nop_();
while(DataPort&0x80);       
LCM_EN=0;                               
}                                       
/*******************************/
void WriteCommandLCM(uchar CMD,uchar Attribc)
{                                       
if(Attribc)WaitForEnable();       
LCM_RS=0;LCM_RW=0;_nop_();
DataPort=CMD;_nop_();       
LCM_EN=1;_nop_();_nop_();LCM_EN=0;
}                                       
/*******************************/
void WriteDataLCM(uchar dataW)
{                                       
WaitForEnable();               
LCM_RS=1;LCM_RW=0;_nop_();
DataPort=dataW;_nop_();       
LCM_EN=1;_nop_();_nop_();LCM_EN=0;
}               
/***********************************/
void InitLcd()                               
{                       
WriteCommandLCM(0x38,1);       
WriteCommandLCM(0x08,1);       
WriteCommandLCM(0x01,1);       
WriteCommandLCM(0x06,1);       
WriteCommandLCM(0x0c,1);
}                       
/***********************************/
void DisplayOneChar(uchar X,uchar Y,uchar DData)
{                                               
Y&=1;                                               
X&=15;                                               
if(Y)X|=0x40;                                       
X|=0x80;                       
WriteCommandLCM(X,0);               
WriteDataLCM(DData);               
}
void Delay(unsigned int j)
{      
   unsigned char i;
   for(;j>0;j--)
   {        
    for(i=0;i<27;i++);
   }
}
void conversion(uint temp_data,uint temp1_data)  
{  

    wendu_shi=temp_data/10+0x30    ;
    temp_data=temp_data%10;      //è&#161;óà&#212;&#203;&#203;&#227;
    wendu_ge=temp_data+0x30;

        shidu_shi=temp1_data/10+0x30    ;
    temp1_data=temp1_data%10;      //è&#161;óà&#212;&#203;&#203;&#227;
    shidu_ge=temp1_data+0x30;
}
void  Delay_10us(void)
{
        unsigned char i;
        i--;
        i--;
        i--;
        i--;
        i--;
        i--;
}     
void  COM(void)
{
        unsigned char i;
        for(i=0;i<8;i++)          
        {
         U8FLAG=2;
          while((!P2_0)&&U8FLAG++);
                Delay_10us();Delay_10us();Delay_10us();
                U8temp=0;
         if(P2_0)U8temp=1;
            U8FLAG=2;
         while((P2_0)&&U8FLAG++);
        //3&#172;ê±&#212;òì&#248;3&#246;for&#209;-&#187;·                  
         if(U8FLAG==1)break;
        //&#197;D&#182;&#207;êy&#190;Y&#206;&#187;ê&#199;0&#187;1ê&#199;1         
        // è&#231;1&#251;&#184;&#223;μ&#231;&#198;&#189;&#184;&#223;1y&#212;¤&#182;¨0&#184;&#223;μ&#231;&#198;&#189;&#214;μ&#212;òêy&#190;Y&#206;&#187;&#206;a 1
           U8comdata<<=1;
           U8comdata|=U8temp;        //0
         }//rof
}
void RH(void)
{
  //&#214;÷&#187;úà-μí18ms
   P2_0=0;
   Delay(180);
   P2_0=1;
//×ü&#207;&#223;óéé&#207;à-μ&#231;×èà-&#184;&#223; &#214;÷&#187;ú&#209;óê±20us
   Delay_10us();
   Delay_10us();
   Delay_10us();
   Delay_10us();
//&#214;÷&#187;úéè&#206;aê&#228;è&#235; &#197;D&#182;&#207;′ó&#187;ú&#207;ìó|D&#197;o&#197;
   P2_0=1;
//&#197;D&#182;&#207;′ó&#187;úê&#199;·&#241;óDμíμ&#231;&#198;&#189;&#207;ìó|D&#197;o&#197; è&#231;2&#187;&#207;ìó|&#212;òì&#248;3&#246;£&#172;&#207;ìó|&#212;ò&#207;ò&#207;&#194;&#212;&#203;DD          
   if(!P2_0)                 //T !          
   {
                   U8FLAG=2;
                 //&#197;D&#182;&#207;′ó&#187;úê&#199;·&#241;·¢3&#246; 80us μ&#196;μíμ&#231;&#198;&#189;&#207;ìó|D&#197;o&#197;ê&#199;·&#241;&#189;áê&#248;         
                   while((!P2_0)&&U8FLAG++);
                   U8FLAG=2;
                 //&#197;D&#182;&#207;′ó&#187;úê&#199;·&#241;·¢3&#246; 80us μ&#196;&#184;&#223;μ&#231;&#198;&#189;£&#172;è&#231;·¢3&#246;&#212;ò&#189;&#248;è&#235;êy&#190;Y&#189;óê&#213;×′ì&#172;
                   while((P2_0)&&U8FLAG++);
                 //êy&#190;Y&#189;óê&#213;×′ì&#172;                 
                   COM();U8RH_data_H_temp=U8comdata;
                   COM();U8RH_data_L_temp=U8comdata;
                   COM();U8T_data_H_temp=U8comdata;
                   COM();U8T_data_L_temp=U8comdata;
                   COM();U8checkdata_temp=U8comdata;
                   P2_0=1;
                 //êy&#190;YD£&#209;é
                   U8temp=(U8T_data_H_temp+U8T_data_L_temp+U8RH_data_H_temp+U8RH_data_L_temp);
                   if(U8temp==U8checkdata_temp)
                   {
                             U8RH_data_H=U8RH_data_H_temp;
                             U8RH_data_L=U8RH_data_L_temp;
                          U8T_data_H=U8T_data_H_temp;
                             U8T_data_L=U8T_data_L_temp;
                             U8checkdata=U8checkdata_temp;
                   }//fi

             conversion(U8T_data_H,U8RH_data_H);
                    DisplayOneChar(5,0,wendu_shi);  //μú0DD£&#172;μú5áD
                       
                 DisplayOneChar(6,0,wendu_ge);  //μú0DD£&#172;μú6áD
                         DisplayOneChar(7,0,0xdf);  //μú0DD£&#172;μú7áD
                         DisplayOneChar(8,0,'C');
                 DisplayOneChar(5,1,shidu_shi);  //μú1DD£&#172;μú9áD
                 DisplayOneChar(6,1,shidu_ge);  //μú1DD£&#172;μú10áD
             DisplayOneChar(7,1,'%');  //μú1DD£&#172;μú11áD
   }//fi
}

void init_serialcomm(void)
{
  SCON  = 0x50;       //SCON: serail mode 1, 8-bit UART, enable ucvr
  TMOD |= 0x20;       //TMOD: timer 1, mode 2, 8-bit reload
  PCON |= 0x80;       //SMOD=1;
  TH1   = 0xF4;       //Baud:4800  fosc=11.0592MHz  
  TL1   = 0xF4;       //
  IE   |= 0x90;       //Enable Serial Interrupt
  TR1   = 1;          // timer 1 run
}


void timer0initial()
{
  TMOD|=0x1;        
  TH0=0xb8;TL0=0x00;  
  IE|=0x90;       //Enable Serial Interrupt
  ET0=0;
  TR0=0;
}

//&#207;ò′&#174;&#191;ú·¢&#203;íò&#187;&#184;&#246;×&#214;·&#251;
void send_char_com(unsigned char ch)
{
  SBUF=ch;
  while(!TI);
  TI=0;
}

void send_string_com(unsigned char *str,unsigned int strlen)
{
  unsigned int k=0;
  do
  {
    send_char_com(*(str+k));
    k++;
  }while(k<strlen);
}

void time_intt0(void) interrupt 1 using 2
{
  ET0=0;
  TR0=0;
  TH0=0xb8;
  TL0=0x00;
  sRXFlag=1;
}

//′&#174;&#191;ú&#189;óê&#213;&#214;D&#182;&#207;oˉêy
void serial() interrupt 4 using 1
{
  unsigned char ch;

  if(RI)
  {
    RI=0;
    ET0=1;TR0=1;
    ch=SBUF;

    inbuf1[count3]=ch;
    count3=count3+1;
    if(count3==INBUF_LEN)
    {
      ET0=0;
      TR0=0;
      TH0=0xb8;
      TL0=0x00;  //1&#216;±&#213;&#182;¨ê±&#214;D&#182;&#207;£&#172;í£&#214;1&#182;¨ê±&#214;D&#182;&#207;
      sRXFlag=1;
    }
  }
}



void ex1_int(void) interrupt 2 using 1
{
  gRXFlag=1;
}


void Init_Cpu(void)
{
  PX1=1;
  EX1=1;
  IT1=0;
  EA=1;
}
uchar num;
uchar code table2[]="temp:";
uchar code table3[]="Humi:";
void main()
{

  Init2515();
  init_serialcomm();     //3&#245;ê&#188;&#187;ˉ′&#174;&#191;ú
  timer0initial();       //&#182;¨ê±&#198;÷03&#245;ê&#188;&#187;ˉ
  Init_Cpu();
   InitLcd();
    for(num=0;num<5;num++)
        {  
                DisplayOneChar(num,0,table2[num]);   //&#207;&#212;ê&#190;×&#214;·&#251;′&#174;"temp:"
                Delay(20);
        }
        for(num=0;num<5;num++)
        {  
                DisplayOneChar(num,1,table3[num]);   //&#207;&#212;ê&#190;×&#214;·&#251;′&#174;"Humi:"
                Delay(20);
        }
  while(1)
  {
      RH();
          inbuf1[0]=wendu_shi;
          inbuf1[1]=wendu_ge;
          inbuf1[2]=shidu_shi;
          inbuf1[3]=shidu_ge;
          sRXFlag=0;
          CAN_Send_anylength(inbuf1,4);
      Delay(20000);

    if(sRXFlag)
    {
      sRXFlag=0;
      CAN_Send_anylength(inbuf1,count3);
      count3=0;
    }


    if(gRXFlag)
    {
      CAN_Receive_DLC(a);
      gRXFlag=0;
      send_string_com(a,DLC);
    }
  }
}

回复

使用道具 举报

 楼主| 发表于 2015-11-23 22:53:26 | 显示全部楼层
#ifndef __CAN_H__
#define __CAN_H__


#include "2515.h"
#include "SPI.h"
#include "reg51.h"
#include "Function.h"

#define CAN_10Kbps 0x31
#define CAN_25Kbps 0x13
#define CAN_50Kbps 0x09
#define CAN_100Kbps 0x04
#define CAN_125Kbps 0x03
#define CAN_250Kbps 0x01
#define CAN_500Kbps 0x00


unsigned char DLC,dummy;

void Init2515(void)
{
  SPIReset();
  delay1ms(1);

  //SPIByteWrite(CANCTRL,0x80);//CAN1¤×÷&#212;ú&#197;&#228;&#214;&#195;&#196;£ê&#189;

  SPIByteWrite(RXM0SIDH,0x00);
  SPIByteWrite(RXM0SIDL,0x00);

  SPIByteWrite(RXF0SIDH,0x00);
  SPIByteWrite(RXF0SIDL,0x00);
  //éè&#214;&#195;2¨ì&#216;&#194;ê&#206;a10Kbps
  //set CNF1,SJW=00,3¤&#182;è&#206;a1TQ,BRP=49,TQ=[2*(BRP+1)]/Fsoc=2*50/8M=12.5us
  SPIByteWrite(CNF1,CAN_125Kbps);
  //set CNF2,SAM=0,&#212;ú2é&#209;ùμ&#227;&#182;&#212;×ü&#207;&#223;&#189;&#248;DDò&#187;′&#206;2é&#209;ù£&#172HSEG1=(2+1)TQ=3TQ,PRSEG=(0+1)TQ=1TQ
  SPIByteWrite(CNF2,0x80|PHSEG1_3TQ|PRSEG_1TQ);
  //set CNF3,PHSEG2=(2+1)TQ=3TQ,í&#172;ê±μ±CANCTRL.CLKEN=1ê±éè&#182;¨CLKOUTòy&#189;&#197;&#206;aê±&#188;&#228;ê&#228;3&#246;ê1&#196;ü&#206;&#187;
  SPIByteWrite(CNF3,PHSEG2_3TQ);

  //set TXB0£&#172;éè&#214;&#195;·¢&#203;í&#187;o3&#229;&#198;÷0μ&#196;±êê&#182;·&#251;oí·¢&#203;íμ&#196;êy&#190;Y£&#172;ò&#212;&#188;°·¢&#203;íμ&#196;êy&#190;Y3¤&#182;è
  SPIByteWrite(TXB0SIDH,0xFF);//éè&#214;&#195;·¢&#203;í&#187;o3&#229;&#198;÷0μ&#196;±ê×&#188;±êê&#182;·&#251;£&#172;′yDT&#184;&#196;***
  SPIByteWrite(TXB0SIDL,0xE0);//ó&#195;μ&#189;±ê×&#188;±êê&#182;·&#251;

  //SPIByteWrite(TXB0D0,0x1E);//óD′yDT&#184;&#196;&#188;°è·&#182;¨ê&#199;·&#241;ê1ó&#195;
  //SPIByteWrite(TXB0D1,0x10)£&#187;//óD′yDT&#184;&#196;&#188;°è·&#182;¨ê&#199;·&#241;ê1ó&#195;

  /*set TXB1
  SPIByteWrite(TXB1SIDH,0x50);    //Set TXB0 SIDH
  SPIByteWrite(TXB1SIDL,0x00);    //Set TXB0 SIDL
  SPIByteWrite(TXB1DLC,0x40 | DLC_8);    //Set DLC = 3 bytes and RTR bit*/

  //éè&#214;&#195;&#189;óê&#213;&#187;o3&#229;&#198;÷0μ&#196;±êê&#182;·&#251;oí3&#245;ê&#188;&#187;ˉêy&#190;Y
  SPIByteWrite(RXB0SIDH,0x00);//éè&#214;&#195;&#189;óê&#213;&#187;o3&#229;&#198;÷0μ&#196;±ê×&#188;±êê&#182;·&#251;£&#172;′yDT&#184;&#196;***
  SPIByteWrite(RXB0SIDL,0x60);//ó&#195;μ&#189;±ê×&#188;±êê&#182;·&#251;
  SPIByteWrite(RXB0CTRL,0x60);//&#189;&#246;&#189;&#246;&#189;óê&#213;±ê×&#188;±êê&#182;·&#251;μ&#196;óDD§D&#197;&#207;¢£&#172;FIILHIT0=0±íê&#190;RXB0 £&#172;2éó&#195;FILHIT0
  SPIByteWrite(RXB0DLC,DLC_8);//éè&#214;&#195;&#189;óê&#213;êy&#190;Yμ&#196;3¤&#182;è&#206;a8&#184;&#246;×&#214;&#189;ú

  SPIByteWrite(RXF0SIDH,0xFF);//3&#245;ê&#188;&#187;ˉ&#189;óê&#213;&#194;&#203;2¨&#198;÷0£&#172;′yDT&#184;&#196;***
  SPIByteWrite(RXF0SIDL,0xE0);
  SPIByteWrite(RXM0SIDH,0xFF);//3&#245;ê&#188;&#187;ˉ&#189;óê&#213;&#198;á±&#206;&#198;÷0£&#172;′yDT&#184;&#196;***
  SPIByteWrite(RXM0SIDL,0xE0);

  //éè&#214;&#195;&#189;óê&#213;&#187;o3&#229;&#198;÷0&#214;D&#182;&#207;
  SPIByteWrite(CANINTF,0x00);//&#199;&#229;&#191;&#213;&#214;D&#182;&#207;±ê&#214;&#190;&#206;&#187;
  SPIByteWrite(CANINTE,0x01);//&#189;óê&#213;&#187;o3&#229;&#198;÷0&#194;ú&#214;D&#182;&#207;ê1&#196;ü&#206;&#187;

  SPIByteWrite(CANCTRL,REQOP_NORMAL|CLKOUT_ENABLED);//éè&#214;&#195;&#213;y3£&#196;£ê&#189;

  dummy=SPIByteRead(CANSTAT);
  if(OPMODE_NORMAL!=(dummy&&0xE0))
  {
    SPIByteWrite(CANCTRL,REQOP_NORMAL|CLKOUT_ENABLED);//&#197;D&#182;&#207;&#189;&#248;è&#235;&#213;y3£1¤×÷&#196;£ê&#189;
  }

}

//CAN·¢&#203;íè&#206;òa3¤&#182;è×&#214;&#189;ú
void CAN_Send_anylength(unsigned char *CAN_TX_Buf,unsigned char length1)
{
  unsigned char tempdata,j;
  tempdata=SPIByteRead(CAN_RD_STATUS);
  SPIByteWrite(TXB0DLC,length1);
  for(j=0;j<length1;j++)
  {
    SPIByteWrite(TXB0D0+j,CAN_TX_Buf[j]);
  }

  if(tempdata&0x04)//&#197;D&#182;&#207;TXREQ±ê&#214;&#190;&#206;&#187;
  {
    delay1ms(1);
    SPIByteWrite(TXB0CTRL,0);//&#199;&#229;3yTXREQ±ê&#214;&#190;&#206;&#187;
    while(SPIByteRead(CAN_RD_STATUS)&0x04);//μè′yTXREQ&#199;&#229;á&#227;
  }
  CS=0;
  WriteSPI(CAN_RTS_TXB0);//·¢&#203;í&#187;o3&#229;&#198;÷0&#199;&#235;&#199;ó·¢&#203;í
  CS=1;
}

//CAN&#189;óê&#213;è&#206;òa3¤&#182;èμ&#196;êy&#190;Y
void CAN_Receive_DLC(unsigned char *CAN_RX_Buf)
{
  unsigned char i;
  DLC=SPIByteRead(RXB0DLC);
  for(i=0;i<DLC;i++)
  {
    //°&#209;&#189;óê&#213;&#187;o3&#229;&#199;&#248;à&#239;μ&#196;êy&#190;Y£&#172;·&#197;μ&#189;&#196;ú2&#191;RAM&#187;o3&#229;&#199;&#248;
    CAN_RX_Buf[i]=SPIByteRead(RXB0D0+i);
  }

  SPIByteWrite(CANINTF,0);
}

#endif
回复 支持 反对

使用道具 举报

发表于 2015-11-24 09:06:54 | 显示全部楼层
没看懂
回复

使用道具 举报

发表于 2015-11-24 12:24:50 | 显示全部楼层
kankan aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-11-24 15:12:59 | 显示全部楼层
dzxmt 发表于 2015-11-24 12:24
kankan aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#ifndef        __CAN_H__
#define        __CAN_H__
//        write your header here
#include "2515.h"
#include "SPI.h"
#include "reg51.h"
#include "Function.h"

#define CAN_10Kbps 0x31
#define CAN_25Kbps 0x13
#define CAN_50Kbps 0x09
#define CAN_100Kbps 0x04
#define CAN_125Kbps 0x03
#define CAN_250Kbps 0x01
#define CAN_500Kbps 0x00



//void SPIReset(void);
//void SPIByteWrite(unsigned char ,unsigned char );
//unsigned char SPIByteRead(unsigned char );
unsigned char dummy;

void Init2515(unsigned int IDF)
{
        SPIReset();
        delay_ms(1);
       
        //SPIByteWrite(CANCTRL,0x80);//CAN1¤×÷&#212;ú&#197;&#228;&#214;&#195;&#196;£ê&#189;
       
    SPIByteWrite(RXM0SIDH,0x00);
        SPIByteWrite(RXM0SIDL,0x00);
        SPIByteWrite(RXF0SIDH,0x00);
        SPIByteWrite(RXF0SIDL,0x00);
       
        SPIByteWrite(RXM1SIDH,0x00);
        SPIByteWrite(RXM1SIDL,0x00);
        SPIByteWrite(RXF2SIDH,0x00);
        SPIByteWrite(RXF2SIDL,0x00);
       
        //éè&#214;&#195;2¨ì&#216;&#194;ê&#206;a10Kbps
        //set CNF1,SJW=00,3¤&#182;è&#206;a1TQ,BRP=49,TQ=[2*(BRP+1)]/Fsoc=2*50/8M=12.5us
        SPIByteWrite(CNF1,CAN_125Kbps);
        //set CNF2,SAM=0,&#212;ú2é&#209;ùμ&#227;&#182;&#212;×ü&#207;&#223;&#189;&#248;DDò&#187;′&#206;2é&#209;ù£&#172HSEG1=(2+1)TQ=3TQ,PRSEG=(0+1)TQ=1TQ
        SPIByteWrite(CNF2,0x80|PHSEG1_3TQ|PRSEG_1TQ);
        //set CNF3,PHSEG2=(2+1)TQ=3TQ,í&#172;ê±μ±CANCTRL.CLKEN=1ê±éè&#182;¨CLKOUTòy&#189;&#197;&#206;aê±&#188;&#228;ê&#228;3&#246;ê1&#196;ü&#206;&#187;
        SPIByteWrite(CNF3,PHSEG2_3TQ);
       
        //set TXB0£&#172;éè&#214;&#195;·¢&#203;í&#187;o3&#229;&#198;÷0μ&#196;±êê&#182;·&#251;oí·¢&#203;íμ&#196;êy&#190;Y£&#172;ò&#212;&#188;°·¢&#203;íμ&#196;êy&#190;Y3¤&#182;è
         SPIByteWrite(TXB0CTRL,0x03);//éè&#214;&#195;·¢&#203;í&#187;o3&#229;&#198;÷0&#191;&#216;&#214;&#198;&#188;&#196;′&#230;&#198;÷&#199;&#229;á&#227;TXREQ,·¢&#203;íó&#197;&#207;è&#188;&#182;×&#238;&#184;&#223;
        //BitModiMcp2515(TXB0CTRL,0x08,0x00);
       
        SPIByteWrite(TXB0SIDH,IDF);//éè&#214;&#195;·¢&#203;í&#187;o3&#229;&#198;÷0μ&#196;±ê×&#188;±êê&#182;·&#251;£&#172;′yDT&#184;&#196;***
        SPIByteWrite(TXB0SIDL,0xE0);//ó&#195;μ&#189;±ê×&#188;±êê&#182;·&#251;
        SPIByteWrite(TXB0DLC,DLC_8);//éè&#214;&#195;·¢&#203;íêy&#190;Yμ&#196;3¤&#182;è&#206;a8&#184;&#246;×&#214;&#189;ú
        //SPIByteWrite(TXB0D0,0x1E);//óD′yDT&#184;&#196;&#188;°è·&#182;¨ê&#199;·&#241;ê1ó&#195;
        //SPIByteWrite(TXB0D1,0x10)£&#187;//óD′yDT&#184;&#196;&#188;°è·&#182;¨ê&#199;·&#241;ê1ó&#195;
               
        /*set TXB1
        SPIByteWrite(TXB1SIDH,0x50);    //Set TXB0 SIDH
        SPIByteWrite(TXB1SIDL,0x00);    //Set TXB0 SIDL
        SPIByteWrite(TXB1DLC,0x40 | DLC_8);    //Set DLC = 3 bytes and RTR bit*/
       
        //éè&#214;&#195;&#189;óê&#213;&#187;o3&#229;&#198;÷0μ&#196;±êê&#182;·&#251;oí3&#245;ê&#188;&#187;ˉêy&#190;Y
        //SPIByteWrite(RXB0SIDH,0x55);//éè&#214;&#195;&#189;óê&#213;&#187;o3&#229;&#198;÷0μ&#196;±ê×&#188;±êê&#182;·&#251;£&#172;′yDT&#184;&#196;***
        //SPIByteWrite(RXB0SIDL,0xE0);//ó&#195;μ&#189;±ê×&#188;±êê&#182;·&#251;
        SPIByteWrite(RXB0CTRL,0x20);//&#189;&#246;&#189;&#246;&#189;óê&#213;±ê×&#188;±êê&#182;·&#251;μ&#196;óDD§D&#197;&#207;¢£&#172;FIILHIT0=0±íê&#190;RXB0 £&#172;2éó&#195;FILHIT0
        SPIByteWrite(RXB0DLC,DLC_8);//éè&#214;&#195;&#189;óê&#213;êy&#190;Yμ&#196;3¤&#182;è&#206;a8&#184;&#246;×&#214;&#189;ú
       
    SPIByteWrite(RXF0SIDH,0xE0);//3&#245;ê&#188;&#187;ˉ&#189;óê&#213;&#194;&#203;2¨&#198;÷0£&#172;′yDT&#184;&#196;***
        SPIByteWrite(RXF0SIDL,0xE0);
        SPIByteWrite(RXM0SIDH,0xFF);//3&#245;ê&#188;&#187;ˉ&#189;óê&#213;&#198;á±&#206;&#198;÷0£&#172;′yDT&#184;&#196;***
        SPIByteWrite(RXM0SIDL,0xE0);            
       
        //éè&#214;&#195;&#189;óê&#213;&#187;o3&#229;&#198;÷1μ&#196;±êê&#182;·&#251;oí3&#245;ê&#188;&#187;ˉêy&#190;Y
         //SPIByteWrite(RXB1SIDH,IDF); //éè&#214;&#195;&#189;óê&#213;&#187;o3&#229;&#198;÷0μ&#196;±ê×&#188;±êê&#182;·&#251;£&#172;′yDT&#184;&#196;***
     //SPIByteWrite(RXB1SIDL,0xE0);//ó&#195;μ&#189;±ê×&#188;±êê&#182;·&#251;
         SPIByteWrite(RXB1CTRL,0x20);//&#189;&#246;&#189;&#246;&#189;óê&#213;±ê×&#188;±êê&#182;·&#251;μ&#196;óDD§D&#197;&#207;¢£&#172;FIILHIT0=0±íê&#190;RXB0 £&#172;2éó&#195;FILHIT0
         SPIByteWrite(RXB1DLC,DLC_8);//éè&#214;&#195;&#189;óê&#213;êy&#190;Yμ&#196;3¤&#182;è&#206;a8&#184;&#246;×&#214;&#189;ú
       
         SPIByteWrite(RXF2SIDH,0x00);//3&#245;ê&#188;&#187;ˉ&#189;óê&#213;&#194;&#203;2¨&#198;÷2£&#172;′yDT&#184;&#196;***
         SPIByteWrite(RXF2SIDL,0xE0);
         
         SPIByteWrite(RXF3SIDH,0x01);//3&#245;ê&#188;&#187;ˉ&#189;óê&#213;&#194;&#203;2¨&#198;÷3£&#172;′yDT&#184;&#196;***
         SPIByteWrite(RXF3SIDL,0xE0);
         
         SPIByteWrite(RXF4SIDH,0x02);//3&#245;ê&#188;&#187;ˉ&#189;óê&#213;&#194;&#203;2¨&#198;÷4£&#172;′yDT&#184;&#196;***
         SPIByteWrite(RXF4SIDL,0xE0);
         
         SPIByteWrite(RXF5SIDH,0x56);//3&#245;ê&#188;&#187;ˉ&#189;óê&#213;&#194;&#203;2¨&#198;÷5£&#172;′yDT&#184;&#196;***
         SPIByteWrite(RXF5SIDL,0xE0);
         
         SPIByteWrite(RXM1SIDH,0xFF);//3&#245;ê&#188;&#187;ˉ&#189;óê&#213;&#198;á±&#206;&#198;÷1£&#172;′yDT&#184;&#196;***
         SPIByteWrite(RXM1SIDL,0xE0);
       
        //éè&#214;&#195;&#189;óê&#213;&#187;o3&#229;&#198;÷0&#214;D&#182;&#207;
        SPIByteWrite(CANINTF,0x00);//&#189;óê&#213;íêò&#187;′&#206;±&#216;D&#235;&#182;&#212;&#214;D&#182;&#207;±ê&#214;&#190;&#206;&#187;&#199;&#229;0
        SPIByteWrite(CANINTE,0x02);//&#189;óê&#213;&#187;o3&#229;&#198;÷0&#194;ú&#214;D&#182;&#207;ê1&#196;ü&#206;&#187;***DT&#184;&#196;3é01-02
        //éè&#214;&#195;μ¥′¥·¢&#196;£ê&#189;1 ê1&#196;ü£&#172;±¨&#206;&#196;3¢ê&#212;·¢&#203;íò&#187;′&#206;£&#187;0&#189;&#251;&#214;1&#161;£è&#231;óDDèòa±¨&#206;&#196;&#187;á&#214;&#216;·¢
        //BitModiMcp2515(CANCTRL,0x08,0x00);
        SPIByteWrite(CANCTRL,REQOP_NORMAL | CLKOUT_ENABLED);//éè&#214;&#195;&#213;y3£&#196;£ê&#189;
        BitModiMcp2515(CANCTRL,0xE7,0x04);
          dummy=SPIByteRead(CANSTAT);
                if (OPMODE_NORMAL != (dummy && 0xE0))
           SPIByteWrite(CANCTRL,REQOP_NORMAL | CLKOUT_ENABLED);//&#197;D&#182;&#207;&#189;&#248;è&#235;&#213;y3£1¤×÷&#196;£ê&#189;
          //SPIByteWrite(CANCTRL,0x00);//&#209;&#161;&#182;¨&#213;y3£1¤×÷&#196;£ê&#189;
         
}



//CAN·¢&#203;í3ìDò
void CAN_Send(unsigned char *CAN_TX_Buf)
{
        unsigned char tempdata;
        tempdata=SPIByteRead(CAN_RD_STATUS);
        if(tempdata&0x04)            //&#197;D&#182;&#207;TXREQ±ê&#214;&#190;&#206;&#187;
        {
                delay_ms(1);
      //SPIByteWrite(TXB0CTRL,0);//&#199;&#229;3yTXREQ±ê&#214;&#190;&#206;&#187;
        BitModiMcp2515(TXB0CTRL,0x08,0x00);
        while(SPIByteRead(CAN_RD_STATUS)&0x04);//μè′yTXREQ&#199;&#229;á&#227;
            }
        SPIByteWrite(TXB0D0,CAN_TX_Buf[0]);
        SPIByteWrite(TXB0D1,CAN_TX_Buf[1]);
        SPIByteWrite(TXB0D2,CAN_TX_Buf[2]);
        SPIByteWrite(TXB0D3,CAN_TX_Buf[3]);
        SPIByteWrite(TXB0D4,CAN_TX_Buf[4]);
        SPIByteWrite(TXB0D5,CAN_TX_Buf[5]);
        SPIByteWrite(TXB0D6,CAN_TX_Buf[6]);
        SPIByteWrite(TXB0D7,CAN_TX_Buf[7]);
/*        if(tempdata&0x04)            //&#197;D&#182;&#207;TXREQ±ê&#214;&#190;&#206;&#187;
        {
                delay_ms(2);
      //SPIByteWrite(TXB0CTRL,0);//&#199;&#229;3yTXREQ±ê&#214;&#190;&#206;&#187;
        BitModiMcp2515(TXB0CTRL,0x08,0x00);
                while(SPIByteRead(CAN_RD_STATUS)&0x04);//μè′yTXREQ&#199;&#229;á&#227;
        }*/
        CS=LOW;
        //WriteSPI(CAN_RTS_TXB0);//·¢&#203;í&#187;o3&#229;&#198;÷0&#199;&#235;&#199;ó·¢&#203;í
        BitModiMcp2515(TXB0CTRL,0x08,0xFF);
        CS=HIGH;
}

//CAN&#189;óê&#213;3ìDò
/*void CAN_Receive(unsigned char *CAN_RX_Buf)
{
        CAN_RX_Buf[0]=SPIByteRead(RXB0D0);
        CAN_RX_Buf[1]=SPIByteRead(RXB0D1);
        CAN_RX_Buf[2]=SPIByteRead(RXB0D2);
        CAN_RX_Buf[3]=SPIByteRead(RXB0D3);
        CAN_RX_Buf[4]=SPIByteRead(RXB0D4);
        CAN_RX_Buf[5]=SPIByteRead(RXB0D5);
        CAN_RX_Buf[6]=SPIByteRead(RXB0D6);
        CAN_RX_Buf[7]=SPIByteRead(RXB0D7);
        SPIByteWrite(CANINTF,0x00);//&#199;&#229;&#191;&#213;&#189;óê&#213;&#214;D&#182;&#207;±ê&#214;&#190;&#206;&#187;
        //′ò&#191;a&#189;óê&#213;&#187;o3&#229;&#198;÷&#189;óê&#213;D&#194;D&#197;&#207;¢£&#172;&#184;&#195;&#190;&#228;è&#231;o&#206;D′
}*/
void CAN_Receive(unsigned char *CAN_RX_Buf)
{
        BitModiMcp2515(CANINTF,0x02,0x00);//&#199;&#229;&#191;&#213;&#189;óê&#213;&#214;D&#182;&#207;±ê&#214;&#190;&#206;&#187;RX1IF
       
        CAN_RX_Buf[0]=SPIByteRead(RXB1D0);
        CAN_RX_Buf[1]=SPIByteRead(RXB1D1);
        CAN_RX_Buf[2]=SPIByteRead(RXB1D2);
        CAN_RX_Buf[3]=SPIByteRead(RXB1D3);
        CAN_RX_Buf[4]=SPIByteRead(RXB1D4);
        CAN_RX_Buf[5]=SPIByteRead(RXB1D5);
        CAN_RX_Buf[6]=SPIByteRead(RXB1D6);
        CAN_RX_Buf[7]=SPIByteRead(RXB1D7);
        //SPIByteWrite(CANINTF,0x00);     //&#199;&#229;&#191;&#213;&#189;óê&#213;&#214;D&#182;&#207;±ê&#214;&#190;&#206;&#187;
        //BitModiMcp2515(CANINTF,0x02,0x00);//&#199;&#229;&#191;&#213;&#189;óê&#213;&#214;D&#182;&#207;±ê&#214;&#190;&#206;&#187;RX1IF
                                          //′ò&#191;a&#189;óê&#213;&#187;o3&#229;&#198;÷&#189;óê&#213;D&#194;D&#197;&#207;¢£&#172;&#184;&#195;&#190;&#228;è&#231;o&#206;D′
}
#endif是不是在这个里
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-11-24 15:15:47 | 显示全部楼层
dzxmt 发表于 2015-11-24 12:24
kankan aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#ifndef        __CAN_H__
#define        __CAN_H__
//        write your header here
#include "2515.h"
#include "SPI.h"
#include "reg51.h"
#include "Function.h"

#define CAN_10Kbps 0x31
#define CAN_25Kbps 0x13
#define CAN_50Kbps 0x09
#define CAN_100Kbps 0x04
#define CAN_125Kbps 0x03
#define CAN_250Kbps 0x01
#define CAN_500Kbps 0x00



//void SPIReset(void);
//void SPIByteWrite(unsigned char ,unsigned char );
//unsigned char SPIByteRead(unsigned char );
unsigned char dummy;

void Init2515(unsigned int IDF)
{
        SPIReset();
        delay_ms(1);
       
        //SPIByteWrite(CANCTRL,0x80);//CAN1¤×÷&#212;ú&#197;&#228;&#214;&#195;&#196;£ê&#189;
       
    SPIByteWrite(RXM0SIDH,0x00);
        SPIByteWrite(RXM0SIDL,0x00);
        SPIByteWrite(RXF0SIDH,0x00);
        SPIByteWrite(RXF0SIDL,0x00);
       
        SPIByteWrite(RXM1SIDH,0x00);
        SPIByteWrite(RXM1SIDL,0x00);
        SPIByteWrite(RXF2SIDH,0x00);
        SPIByteWrite(RXF2SIDL,0x00);
       
        //éè&#214;&#195;2¨ì&#216;&#194;ê&#206;a10Kbps
        //set CNF1,SJW=00,3¤&#182;è&#206;a1TQ,BRP=49,TQ=[2*(BRP+1)]/Fsoc=2*50/8M=12.5us
        SPIByteWrite(CNF1,CAN_125Kbps);
        //set CNF2,SAM=0,&#212;ú2é&#209;ùμ&#227;&#182;&#212;×ü&#207;&#223;&#189;&#248;DDò&#187;′&#206;2é&#209;ù£&#172HSEG1=(2+1)TQ=3TQ,PRSEG=(0+1)TQ=1TQ
        SPIByteWrite(CNF2,0x80|PHSEG1_3TQ|PRSEG_1TQ);
        //set CNF3,PHSEG2=(2+1)TQ=3TQ,
        SPIByteWrite(CNF3,PHSEG2_3TQ);
       
        //set TXB0£&#172;éè&#214;&#195;·¢&#203;í&#187;o3&#229;&#198;÷0μ&#196;±êê&#182;·&#251;oí·¢&#203;íμ&#196;êy&#190;Y£&#172;ò&#212;&#188;°·¢&#203;íμ&#196;êy&#190;Y3¤&#182;è
         SPIByteWrite(TXB0CTRL,0x03);//éè&#214;&#195;·¢&#203;í&#187;o3&#229;&#198;÷0&#191;&#216;&#214;&#198;&#188;&#196;′&#230;&#198;÷&#199;&#229;á&#227;TXREQ,·¢&#203;íó&#197;&#207;è&#188;&#182;×&#238;&#184;&#223;
        //BitModiMcp2515(TXB0CTRL,0x08,0x00);
       
        SPIByteWrite(TXB0SIDH,IDF);
        SPIByteWrite(TXB0SIDL,0xE0);
        SPIByteWrite(TXB0DLC,DLC_8);
        //SPIByteWrite(TXB0D0,0x1E);
        //SPIByteWrite(TXB0D1,0x10)
               
        /*set TXB1
        SPIByteWrite(TXB1SIDH,0x50);    //Set TXB0 SIDH
        SPIByteWrite(TXB1SIDL,0x00);    //Set TXB0 SIDL
        SPIByteWrite(TXB1DLC,0x40 | DLC_8);    //Set DLC = 3 bytes and RTR bit*/
       
        //SPIByteWrite(RXB0SIDH,0x55);/
        //SPIByteWrite(RXB0SIDL,0xE0);
        SPIByteWrite(RXB0CTRL,0x20);
        SPIByteWrite(RXB0DLC,DLC_8);
       
    SPIByteWrite(RXF0SIDH,0xE0);
        SPIByteWrite(RXF0SIDL,0xE0);
        SPIByteWrite(RXM0SIDH,0xFF);
        SPIByteWrite(RXM0SIDL,0xE0);            
       
       
         //SPIByteWrite(RXB1SIDH,IDF);
     //SPIByteWrite(RXB1SIDL,0xE0);
         SPIByteWrite(RXB1CTRL,0x20);
         SPIByteWrite(RXB1DLC,DLC_8)
       
         SPIByteWrite(RXF2SIDH,0x00)
         SPIByteWrite(RXF2SIDL,0xE0);
         
         SPIByteWrite(RXF3SIDH,0x01);//3&#245;ê&#188;&#187;ˉ&#189;óê&#213;&#194;&#203;2¨&#198;÷3£&#172;′yDT&#184;&#196;***
         SPIByteWrite(RXF3SIDL,0xE0);
         
         SPIByteWrite(RXF4SIDH,0x02);/
         SPIByteWrite(RXF4SIDL,0xE0);
         
         SPIByteWrite(RXF5SIDH,0x56);
         SPIByteWrite(RXF5SIDL,0xE0);
         
         SPIByteWrite(RXM1SIDH,0xFF);
         SPIByteWrite(RXM1SIDL,0xE0);
       
        //éè&#214;&#195;&#189;óê&#213;&#187;o3&#229;&#198;÷0&#214;D&#182;&#207;
        SPIByteWrite(CANINTF,0x00);
        SPIByteWrite(CANINTE,0x02);
        //BitModiMcp2515(CANCTRL,0x08,0x00);
        SPIByteWrite(CANCTRL,REQOP_NORMAL | CLKOUT_ENABLED);//éè&#214;&#195;&#213;y3£&#196;£ê&#189;
        BitModiMcp2515(CANCTRL,0xE7,0x04);
          dummy=SPIByteRead(CANSTAT);
                if (OPMODE_NORMAL != (dummy && 0xE0))
           SPIByteWrite(CANCTRL,REQOP_NORMAL | CLKOUT_ENABLED);
          //SPIByteWrite(CANCTRL,0x00);
}



//CAN·¢&#203;í3ìDò
void CAN_Send(unsigned char *CAN_TX_Buf)
{
        unsigned char tempdata;
        tempdata=SPIByteRead(CAN_RD_STATUS);
        if(tempdata&0x04)           
        {
                delay_ms(1);
      //SPIByteWrite(TXB0CTRL,0);
        BitModiMcp2515(TXB0CTRL,0x08,0x00);
        while(SPIByteRead(CAN_RD_STATUS)&0x04);
            }
        SPIByteWrite(TXB0D0,CAN_TX_Buf[0]);
        SPIByteWrite(TXB0D1,CAN_TX_Buf[1]);
        SPIByteWrite(TXB0D2,CAN_TX_Buf[2]);
        SPIByteWrite(TXB0D3,CAN_TX_Buf[3]);
        SPIByteWrite(TXB0D4,CAN_TX_Buf[4]);
        SPIByteWrite(TXB0D5,CAN_TX_Buf[5]);
        SPIByteWrite(TXB0D6,CAN_TX_Buf[6]);
        SPIByteWrite(TXB0D7,CAN_TX_Buf[7]);
/*        if(tempdata&0x04)            
                delay_ms(2);
      //SPIByteWrite(TXB0CTRL,0);
        BitModiMcp2515(TXB0CTRL,0x08,0x00);
                while(SPIByteRead(CAN_RD_STATUS)&0x04);
        }*/
        CS=LOW;
        //WriteSPI(CAN_RTS_TXB0);
        BitModiMcp2515(TXB0CTRL,0x08,0xFF);
        CS=HIGH;
}


/*void CAN_Receive(unsigned char *CAN_RX_Buf)
{
        CAN_RX_Buf[0]=SPIByteRead(RXB0D0);
        CAN_RX_Buf[1]=SPIByteRead(RXB0D1);
        CAN_RX_Buf[2]=SPIByteRead(RXB0D2);
        CAN_RX_Buf[3]=SPIByteRead(RXB0D3);
        CAN_RX_Buf[4]=SPIByteRead(RXB0D4);
        CAN_RX_Buf[5]=SPIByteRead(RXB0D5);
        CAN_RX_Buf[6]=SPIByteRead(RXB0D6);
        CAN_RX_Buf[7]=SPIByteRead(RXB0D7);
        SPIByteWrite(CANINTF,0x00);
       
}*/
void CAN_Receive(unsigned char *CAN_RX_Buf)
{
        BitModiMcp2515(CANINTF,0x02,0x00);
       
        CAN_RX_Buf[0]=SPIByteRead(RXB1D0);
        CAN_RX_Buf[1]=SPIByteRead(RXB1D1);
        CAN_RX_Buf[2]=SPIByteRead(RXB1D2);
        CAN_RX_Buf[3]=SPIByteRead(RXB1D3);
        CAN_RX_Buf[4]=SPIByteRead(RXB1D4);
        CAN_RX_Buf[5]=SPIByteRead(RXB1D5);
        CAN_RX_Buf[6]=SPIByteRead(RXB1D6);
        CAN_RX_Buf[7]=SPIByteRead(RXB1D7);
        //SPIByteWrite(CANINTF,0x00);     
        //BitModiMcp2515(CANINTF,0x02,0x00);
                                          
}
#endif是不是在这个里
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-11-28 00:16 , Processed in 0.037607 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表