这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 综合技术 » 基础知识 » CAN 紧急求助:急急急,各位调过单片机CAN通讯的大侠指点小弟一二??

共2条 1/1 1 跳转至

CAN 紧急求助:急急急,各位调过单片机CAN通讯的大侠指点小弟一二??

院士
2006-09-17 18:14:16     打赏
CAN 紧急求助:急急急,各位调过单片机CAN通讯的大侠指点小弟一二??



关键词: 紧急     求助     急急     各位     调过     单片机     通讯     大侠         

院士
2006-12-22 22:43:00     打赏
2楼
问  小弟现在在用C8051F040调CAN通讯,一个多星期过去了,我从实例程序入手对照着写了一个简单的发送程序(我第一次调单片机程序),却一直没有效果.我现在属于两眼一摸黑在调,也不知道程序如何调,更不知道哪里写错了.各位大侠帮帮忙,指点小弟一下,该如何调试CAN通讯接口啊??
     眼看就要月末了,马上就要向老大交差了~~~~~~~调不出来,就惨了!各位调过CAN通讯的,特别调过040CAN的帮帮小弟吧!
1: 请大侠们看一看我写的着程序,看有什么错误#include <c8051F040.h>                 // SFR declarations
#include <stdio.h>
/////////////////////////////////////////////////////////////////////////////////////
//CAN Protocol Register Index for CAN0ADR, from TABLE 18.1 of the C8051F040 datasheet
/////////////////////////////////////////////////////////////////////////////////////
#define CANCTRL            0x00                 //Control Register
#define CANSTAT            0x01                 //Status register
#define ERRCNT             0x02                 //Error Counter Register
#define BITREG             0x03                 //Bit Timing Register
#define INTREG             0x04                 //Interrupt Low Byte Register
#define CANTEST            0x05                 //Test register
#define BRPEXT             0x06                 //BRP Extension         Register
/////////////////////////////////////////////////////////////////////////////////
//IF1 Interface Registers
/////////////////////////////////////////////////////////////////////////////////
#define IF1CMDRQST         0x08                 //IF1 Command Rest      Register
#define IF1CMDMSK          0x09                 //IF1 Command Mask      Register
#define IF1MSK1            0x0A                 //IF1 Mask1             Register
#define IF1MSK2            0x0B                 //IF1 Mask2             Register
#define IF1ARB1            0x0C                 //IF1 Arbitration 1     Register
#define IF1ARB2            0x0D                 //IF1 Arbitration 2     Register
#define IF1MSGC            0x0E                 //IF1 Message Control   Register
#define IF1DATA1           0x0F                 //IF1 Data A1           Register
#define IF1DATA2           0x10                 //IF1 Data A2           Register
#define IF1DATB1           0x11                 //IF1 Data B1           Register
#define IF1DATB2           0x12                 //IF1 Data B2           Register
/////////////////////////////////////////////////////////////////////////////////
//IF2 Interface Registers
/////////////////////////////////////////////////////////////////////////////////
#define IF2CMDRQST         0x20                 //IF2 Command Rest      Register
#define IF2CMDMSK          0x21                 //IF2 Command Mask      Register
#define IF2MSK1            0x22                 //IF2 Mask1             Register
#define IF2MSK2            0x23                 //IF2 Mask2             Register
#define IF2ARB1            0x24                 //IF2 Arbitration 1     Register
#define IF2ARB2            0x25                 //IF2 Arbitration 2     Register
#define IF2MSGC            0x26                 //IF2 Message Control   Register
#define IF2DATA1           0x27                 //IF2 Data A1           Register
#define IF2DATA2           0x28                 //IF2 Data A2           Register
#define IF2DATB1           0x29                 //IF2 Data B1           Register
#define IF2DATB2           0x2A                 //IF2 Data B2           Register
/////////////////////////////////////////////////////////////////////////////////
//Message Handler Registers
/////////////////////////////////////////////////////////////////////////////////
#define TRANSREQ1          0x40                 //Transmission Rest1 Register
#define TRANSREQ2          0x41                 //Transmission Rest2 Register

#define NEWDAT1            0x48                 //New Data 1            Register
#define NEWDAT2            0x49                 //New Data 2            Register

#define INTPEND1           0x50                 //Interrupt Pending 1   Register
#define INTPEND2           0x51                 //Interrupt Pending 2   Register

#define MSGVAL1            0x58                 //Message Valid 1       Register
#define MSGVAL2            0x59                 //Message Valid 2       Register

sfr16 CAN0DAT  = 0xD8;
//////////////////////////////////////////////////////////////////////////////////
// Function PROTOTYPES
//////////////////////////////////////////////////////////////////////////////////

// Initialize Message Object

void SYSCLK_Init (void);
void PORT_Init (void);
void start_CAN (void);
void transmit_message (void);


//**************************************************main****************************//


void main (void)
{
   WDTCN=0xde;
   WDTCN=0xad;

   SYSCLK_Init ();
   PORT_Init ();
   start_CAN ();

   SFRPAGE=CONFIG_PAGE;
   IE=0x80;

   transmit_message ();

   //while(1)
   //{
    //transmit_message ();
   //
   //}
}

//**************************************************tranmit****************************//

void transmit_message (void)

{
  //  int i;
    
    SFRPAGE=CAN0_PAGE;
    
    CAN0ADR=IF1CMDMSK;
    
    CAN0DAT=0x0083;
    
    CAN0ADR=IF1ARB2;
    
    CAN0DATH |= 0x02;

        CAN0ADR=IF1MSGC;
    
    CAN0DAT=0X08;
    
    CAN0ADR = IF1DATA1;
    
        CAN0DAT=0x05;
    
    CAN0DAT=0x05;
    
    CAN0DAT=0x05;
    
    CAN0DAT=0x05;
    
    CAN0ADR=IF1CMDRQST;
    
    CAN0DATL=0X04;

     
}


//**************************************************SYSCLK_Init****************************//

void SYSCLK_Init (void)
{

   int i;
   
   SFRPAGE=CONFIG_PAGE;
   
   OSCICN=0X82;

   for(i=0;i<256;i++);
   
   while(!(OSCICN&0x06));

   CLKSEL=0x00;

}


//**************************************************PORT_Init*****************************//

void PORT_Init (void)
{
   SFRPAGE = CONFIG_PAGE;
   
//   XBR2|=0x04;

   XBR3=0X80;

}

//**************************************************CAN_Init*****************************//

void start_CAN (void)

{
   SFRPAGE  = CAN0_PAGE;
   CAN0CN  |= 0x41;
   CAN0ADR  = BITREG;
   CAN0DAT  = 0x5EC0;
   CAN0ADR  = IF1CMDMSK;
   CAN0DAT  = 0x0087;
   //SFRPAGE=CONFIG_PAGE;
   CAN0CN |=0x06;
   
   CAN0CN &=~0x41;
}
2: 开发板价格:285元/块(主控单片机为ADuC812,适用于工程应用);
          180元/块(主控单片机为AT89c51,适用于实验板)
以上不含邮资
联系我:13671387011(短信联系,QQ详谈)
       QQ: 531706356
完整的程序给您! 3: 呵呵,别着急,慢慢来首先理解CAN协议,然后才谈得上调试软件 4: 哎!要是这样就好了!老大催我明天给结果~~郁闷啊!前后给我不到十天的时间~~~~

共2条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]