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

📄 can-demo-1.c

📁 AVR Studio下开发的CAN-232串口通信代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*C**************************************************************************
* CAN-Demo-1.c 
*----------------------------------------------------------------------------
* Copyright (c) 2002 Atmel.
*----------------------------------------------------------------------------
*----------------------------------------------------------------------------
* PURPOSE: Test bit timing/transmit/receive/IT routines of CAN.lib
*          All interrupts "on"
*     - 1st Part: Sending the following messages
*       MOb   ID        IDE   Conf  DLC   Data
*       0     00        0     Tx    8     00 10 20 30 40 50 60 70                                                                                             
*       1     1DE0001x  1     Tx    4     10 11 12 13                                                                                             
*       2     02        0     Tx    8     20 21 22 23 24 25 26 27                                                                                             
*       3     1DE0003x  1     Tx    4     30 31 32 33                                                                                             
*       4     04        0     Tx    8     40 41 42 43 44 45 46 47                                                                                             
*       5     1DE0005x  1     Tx    4     50 51 52 53                                                                                             
*       6     06        0     Tx    8     60 61 62 63 64 65 66 67                                                                                             
*       7     1DE0007x  1     Tx    4     70 71 72 73                                                                                             
*       8     08        0     Tx    8     80 81 82 83 84 85 86 87                                                                                             
*       9     1DE0009x  1     Tx    4     90 91 92 93                                                                                             
*       10    0A        0     Tx    8     0A 1A 2A 3A 4A 5A 6A 7A                                                                                             
*       11    1DE000Bx  1     Tx    4     B0 B1 B2 B3                                                                                             
*       12    0C        0     Tx    8     C0 C1 C2 C3 C4 C5 C6 C7                                                                                             
*       13    1DE000Dx  1     Tx    4     D0 D1 D2 D3
*       14    0E        1     Tx    8     E0 E1 E2 E3 E4 E5 E6 E7
*
*     - 2nd Part: Pseudo echo on MOb14 of incomming messages MOb0
*       * if Rx message 0-data or remote => TX =ID with REG 1 upto 8
*       * if Rx message 1-data => TX=ID with the data then REG 2 upto 8
*       * if Rx message 2-data => TX=ID with the 2 data then REG 3 upto 8
*       * ...................................................
*       * if Rx message 8-data => TX=ID with the 8 data 
*         REG:  1-CANGCON
*               2-CANGSTA
*               3-CANGIT
*               4-CANGIE
*               5-CANEN2
*               6-CANEN1
*               7-CANSTMPH
*               8-CANSTMPL
*         Eemple:
*             123       0     Rx    0                                                                                             
*             123       0     Tx    8     02 14 00 FF FF 7F 00 00                                                                                             
*             1111000x  1     Rx    8     80 81 82 83 84 85 86 87                                                                                             
*             1111000x  1     Tx    8     80 81 82 83 84 85 86 87
*
*             ...                                                                                             
*     - 3rd Part: Avaibility to monitor the following events in
*                 watch window (AVRStudio):
*               1- can_OVRT_count - number of CAN timer Overrun
*               1- can_DLCW_count - number of DLC warning
*               1- can_Tx_count - number of transmited message
*               1- can_Rx_count - number of received message 
*               1- can_Bx_count - number of received buffer message 
*               1- can_ErrMOb_count - number of MOb error
*               1- can_ErrGen_count - number of general error
*               1- can_BusOff_count - number of bus off error
*
*
* ICE50 Setting:  Clock = 8MHz, Internal XTAL Oscillator
*   CPU Setting:  Clock = 8MHz, CAN Baudrate = 1Mbit/s down to 100Kbit/s
*****************************************************************************/

/*__________________________________________________________________________*/
/*_____ I N C L U D E S ____________________________________________________*/
/*__________________________________________________________________________*/

#include "config.h"
#include "can_lib.h"
#include "uartnoint_lib.h"

/*__________________________________________________________________________*/
/*_____ C O N S T A N T E S - D E F I N I T I O N  _________________________*/
/*__________________________________________________________________________*/

/* BitRate=0 */
#define BRP_1M    0x00
#define SJW_1M    0x00
#define PRS_1M    0x02
#define PHS2_1M   0x01
#define PHS1_1M   0x01

/* BitRate=1 */
//#define BRP_500K    0x00
//#define SJW_500K    0x00
//#define PRS_500K    0x06
//#define PHS2_500K   0x03
//#define PHS1_500K   0x03
#define BRP_500K    0x01
#define SJW_500K    0x00
#define PRS_500K    0x02
#define PHS2_500K   0x01
#define PHS1_500K   0x01

/* BitRate=2 */
#define BRP_250K    0x03
#define SJW_250K    0x00
#define PRS_250K    0x02
#define PHS2_250K   0x01
#define PHS1_250K   0x01

/*__________________________________________________________________________*/
/*_____ D E C L A R E ______________________________________________________*/
/*__________________________________________________________________________*/

  Uint16  can_OVRT_count;
  Uint16  can_DLCW_count;
  Uint16  can_Tx_count;
  Uint16  can_Rx_count;
  Uint16  can_Bx_count;
  Uint16  can_ErrMOb_count;
    Uint16  can_ACK_ErrMOb_count;
    Uint16  can_For_ErrMOb_count;
    Uint16  can_CRC_ErrMOb_count;
    Uint16  can_Stu_ErrMOb_count;
    Uint16  can_Bit_ErrMOb_count;
  Uint16  can_ErrGen_count;
    Uint16  can_ACK_ErrGen_count;
    Uint16  can_For_ErrGen_count;
    Uint16  can_CRC_ErrGen_count;
    Uint16  can_Stu_ErrGen_count;
  Uint16  can_BusOff_count;
  
  Uchar  Tx_message_flag; /* Equ TxOK */
  Uchar  MOb_Tx_message;  /* MOb number with TxOK */
  Uchar  Rx_message_flag; /* Equ RxOK */
  Uchar  MOb_Rx_message;  /* MOb number with RxOK */
  Uchar  Ovr_Timer_flag;

  Uchar BitRate_1M [5]   = {BRP_1M,   SJW_1M,   PRS_1M,   PHS2_1M,   PHS1_1M  };
  Uchar BitRate_500K [5] = {BRP_500K, SJW_500K, PRS_500K, PHS2_500K, PHS1_500K};
  Uchar BitRate_250K [5] = {BRP_250K, SJW_250K, PRS_250K, PHS2_250K, PHS1_250K};

  Uchar BitRate=2; /* BitRate = 250 Kbit/s */
//  Uchar BitRate=1; /* BitRate = 500 Kbit/s */
//  Uchar BitRate=0; /* BitRate = 1   Mbit/s */
  
/*__________________________________________________________________________*/
/*_____ F U N C T I O N S __________________________________________________*/
/*__________________________________________________________________________*/

void delay (void)
 {
  Uint16 countx;
  countx = 0xFFFF;
  while (countx != FALSE) countx--;
 }

/*__________________________________________________________________________*/
/*_____ F U N C T I O N S   C A L L E D   W H E N   I N T E R R U P T S ____*/
/*__________________________________________________________________________*/

extern void can_fct_it (void);

/* _____ CANOVRT interrupt ____________________ */

void can_fct_it_timovf (void)
  {  can_OVRT_count++;
     PORTA=(Uchar)(can_OVRT_count);
     Ovr_Timer_flag = TRUE;  }

/* _____ CANIT  interrupt _____________________ */

void can_fct_it_rxok (void)
  {  can_fct_it();  }
  
void can_fct_it_txok (void)
  {  can_fct_it();  }
  
void can_fct_it_error(void)
  {  can_fct_it();  }
  
void can_fct_it_gen (void)
  {  can_fct_it();  }
  
/* _____ CANIT  interrupt (main) ______________ */

void can_fct_it (void)

  {
  Uchar MOb, Err;

  /* Save current page done in IT_Handler*/ 

  /* __ MOB Interrupts __ */

  if ((MOb=FindFirstChIt()) != NO_CHANNEL)
    {
    CAN_SET_CHANNEL(MOb);
    if ((CANSTMOB & (1 << DLCW)) != FALSE) can_DLCW_count++;
    if ((CANSTMOB & (1 << TXOK)) != FALSE)
      { can_Tx_count++;
        Tx_message_flag = TRUE;
        MOb_Tx_message=MOb;
        DISABLE_CHANNEL;
      }
    if ((CANSTMOB & (1 << RXOK)) != FALSE)
      { can_Rx_count++;
        Rx_message_flag = TRUE;
        MOb_Rx_message=MOb;
        DISABLE_CHANNEL;
      }
    Err = CANSTMOB & ERR_MOB_msk;
    if (Err != FALSE)
      {
      can_ErrMOb_count++;
      CPrintfUART_0("\r\n  CAN Error MOb  ");
      if ((Err & 0x01) != FALSE) can_ACK_ErrMOb_count++;
      if ((Err & 0x02) != FALSE) can_For_ErrMOb_count++;
      if ((Err & 0x04) != FALSE) can_CRC_ErrMOb_count++;
      if ((Err & 0x08) != FALSE) can_Stu_ErrMOb_count++;
      if ((Err & 0x10) != FALSE) can_Bit_ErrMOb_count++;
      }
    /* Reset all MOb Int & Keep WDLC done in IT_Handler */
    }

/* __ General Interrupts - Reset Int_flag setting it "1" __ */
    
    Err = CANGIT & ERR_GEN_msk;
    if (Err != FALSE)
      {
      can_ErrGen_count++;
      CPrintfUART_0("\r\n  CAN Error General  ");
      if ((Err & 0x01) != FALSE) can_ACK_ErrGen_count++;
      if ((Err & 0x02) != FALSE) can_For_ErrGen_count++;
      if ((Err & 0x04) != FALSE) can_CRC_ErrGen_count++;
      if ((Err & 0x08) != FALSE) can_Stu_ErrGen_count++;
      /* Reset Int_ERR_GEN done (global) in IT_Handler */
      }
    if ((CANGIT & (1 << OVRBUF)) != FALSE)
      {
      can_Bx_count++;
      /* Reset Int_Buffer_OK done (global) in IT_Handler */
      }
    if ((CANGIT & (1 << BOFFIT)) != FALSE)
      {
      can_BusOff_count++;
      CPrintfUART_0("\r\n  CAN Error Bus Off  ");
      /* Reset Int_Bus_Off done in (global) IT_Handler */
      }

  /* Restore previous page done in IT_Handler */ 
}

/*__________________________________________________________________________*/
/*_____ M A I N ____________________________________________________________*/
/*__________________________________________________________________________*/

can_msg_t can_tx_MOb0, can_tx_MOb1, can_tx_MOb2, can_tx_MOb3, can_tx_MOb4;
can_msg_t can_tx_MOb5, can_tx_MOb6, can_tx_MOb7, can_tx_MOb8, can_tx_MOb9;
can_msg_t can_tx_MObA, can_tx_MObB, can_tx_MObC, can_tx_MObD, can_tx_MObE;
can_msg_t can_rxtxMOb;

can_data_t can_data0, can_data1, can_data2, can_data3, can_data4, can_data5;
can_data_t can_data6, can_data7, can_data8, can_data9, can_dataA, can_dataB;
can_data_t can_dataC, can_dataD, can_dataE;
can_data_t can_dataX;

void main(void)
{
  Uchar  i, j, UART_Temp, ID_Temp, ID_First;

  Uint16 standart_ID;
  Uint32 extended_ID;
  
  UART_Temp = 0x30;
  
  can_OVRT_count=CLR;
  can_DLCW_count=CLR;
  can_Tx_count=CLR;
  can_Rx_count=CLR;
  can_Bx_count=CLR;
  can_ErrMOb_count=CLR;
    can_ACK_ErrMOb_count=CLR;
    can_For_ErrMOb_count=CLR;
    can_CRC_ErrMOb_count=CLR;
    can_Stu_ErrMOb_count=CLR;
    can_Bit_ErrMOb_count=CLR;
  can_ErrGen_count=CLR;
    can_ACK_ErrGen_count=CLR;
    can_For_ErrGen_count=CLR;
    can_CRC_ErrGen_count=CLR;
    can_Stu_ErrGen_count=CLR;
  can_BusOff_count=CLR;
  
  MOb_Tx_message = CLR;
  MOb_Rx_message = CLR;

  Tx_message_flag = CLR; /* Message on MOb_for_Tx */
  Rx_message_flag = CLR; /* Message on MOb_for_Rx */
  Ovr_Timer_flag = CLR;
  
  j =0x00; for (i=0; i<8; i++) can_data0[i]= (j+i); 
  j+=0x10; for (i=0; i<8; i++) can_data1[i]= (j+i);
  j+=0x10; for (i=0; i<8; i++) can_data2[i]= (j+i);
  j+=0x10; for (i=0; i<8; i++) can_data3[i]= (j+i);
  j+=0x10; for (i=0; i<8; i++) can_data4[i]= (j+i);
  j+=0x10; for (i=0; i<8; i++) can_data5[i]= (j+i);
  j+=0x10; for (i=0; i<8; i++) can_data6[i]= (j+i);
  j+=0x10; for (i=0; i<8; i++) can_data7[i]= (j+i);
  j+=0x10; for (i=0; i<8; i++) can_data8[i]= (j+i);
  j+=0x10; for (i=0; i<8; i++) can_data9[i]= (j+i);
  j+=0x10; for (i=0; i<8; i++) can_dataA[i]= (j+i);

⌨️ 快捷键说明

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