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

📄 can_lib.c

📁 AVR CAN128单片机 基于IAR开发环境can通信程序
💻 C
📖 第 1 页 / 共 2 页
字号:

  CANCDMOB |= NB_DATA_MAX;
  /* If DLC of received message < NB_DATA_MAX, DLCW will be set */

//  if (rtr == TRUE)
  if ((conf_rx & CONF_RTR) != FALSE)
  {
    CANIDT4 |= (SET<<RTRTAG);      /* set RTRTAG bit in CANIDT4.   */   
  }
  /* else already done */

//  if (msk_rtr == TRUE)
  if ((conf_rx & CONF_MSK_RTR) != FALSE)
  {
    CANIDM4 |= (SET<<RTRMSK);	 /* set RTRMSK bit in CANIDM4.   */ 
  }
  /* else already done */
  
//  if (msk_ide == TRUE)
  if ((conf_rx & CONF_MSK_IDE) != FALSE)
  {
    CANIDM4 |= (SET<<IDEMSK);      /* set IDEMSK bit in CANIDM4.   */
  }
  /* else already done */

//  if (buffer == TRUE)
  if ((conf_rx & CONF_BUFFER) != FALSE)
  {
   ENABLE_CHANNEL_BUFFER; /* Buffer Reception enabled.*/
  }
  else
  {
    ENABLE_CHANNEL_RX; /* Reception enabled.*/
  }

}


/*F***************************************************************************
* FUNCTION_NAME:  SendCanMsg                                                
*----------------------------------------------------------------------------
* FUNCTION_AUTHOR: BERTHY J.S.                                              
* FUNCTION_DATE  : 28/09/99                                                 
*   AVR RELEASE  : 23/05/03 - JT                                                 
*----------------------------------------------------------------------------
* FUNCTION_PURPOSE: Send message        
*        The identifier to send is declared in the globale variable can_id_tx,
*        data to transmit is declared in the globale variable pt_candata_tx  
*        The configuration is defined in globale variable conf_tx.                   
* FUNCTION_INPUTS : void                                                        
* FUNCTION_OUTPUTS: void                                                        
*----------------------------------------------------------------------------
* GLOBAL VARIABLES USED :
*	- conf_tx
*	- pt_candata_tx
*	- can_tx_id
******************************************************************************
* The variable conf_tx must contain information:                                   
*    - on ide:
*           standart(NOT_IDE) or 
*           extended (IDE)                     
*    - on rtr:
*           data frame(NO_RTR) or       
*           remote frame(RTR) 
*    - on dlc: size of data dlc_t.
*
* Exemple of declaration:                          
* ---------------------- 
*                                                                  
* conf_tx = IDE | NO_RTR | DLC_6 
*                                      
******************************************************************************                                                                            
* NOTE:                                                                      
*     The corresponding MOb (or channel) must be selected before calling
*     this function.
*     It's very important to make sure that this MOb (or channel) is free.                 
*     No verification are perform by this function.                          
*
* Exemple of use:
* ---------------
*
*  CAN_SET_CHANNEL(CHANNEL_7);
*  canid_tx.std = frame1.id.std;
*  conf_tx      = frame1.ctrl;
*  pt_candata_tx= frame1.pt_donne;
*  CANIE2 |= (1 << CHANNEL_7);
*  SendCanMsg();
******************************************************************************/
void SendCanMsg (void)
{
  Uchar  dlc2send;


CAN_IT_DISABLE;   /* Why? */
 
  CANSTMOB = 0x00;
  CANCDMOB = 0x00;
  
//  if (ide == TRUE)
  if ((conf_tx & CONF_IDE) != FALSE)
  {
    CANIDT1 = CAN_SET_EXT_ID_28_21 (can_tx_id.ext);
    CANIDT2 = CAN_SET_EXT_ID_20_13 (can_tx_id.ext);
    CANIDT3 = CAN_SET_EXT_ID_12_5  (can_tx_id.ext);
    CANIDT4 = CAN_SET_EXT_ID_4_0   (can_tx_id.ext);

    CANCDMOB |= (SET<<IDE);        /* set IDE bit in CANCDMOB */
  }
  else
  {
    CANIDT1 = CAN_SET_STD_ID_10_4 (can_tx_id.std);
    CANIDT2 = CAN_SET_STD_ID_3_0  (can_tx_id.std);
    //CANIDM3 = 0; /* optional */
    CANIDM4 = 0;                   /* Clear RTRMSK and IDEMSK */

    /* IDE=0 in CANCDMOB already done at the beginning */
  }

  dlc2send = (conf_tx & MSK_CTRL_DLC);  
  CANCDMOB |= dlc2send;        /* set DLC field in CANCDMOB */
  
  if ((conf_tx & CONF_RTR) != FALSE)
  {
    CANIDT4 |= (SET<<RTRTAG);      /* set RTRTAG bit in CANIDT4.   */   
  }

/* Load data to transmit */
  for (; dlc2send!=0; dlc2send--)
  {
    CANMSG = *pt_candata_tx++;
  }

  ENABLE_CHANNEL_TX;

CAN_IT_ENABLE;

}


/*F***************************************************************************
* FUNCTION_NAME: ReadCanMsg                                                 
*----------------------------------------------------------------------------
* FUNCTION_AUTHOR: BERTHY J.S.                                              
* FUNCTION_DATE  : 28/09/99                                                 
*   AVR RELEASE  : 23/05/03 - JT                                                 
*----------------------------------------------------------------------------
* FUNCTION_PURPOSE: Copy message received on num_channel in structure       
*                   of type st_can_msg_t, and configured the MOB (or channel)
*                   with the new configuration given by next_conf, past as
*                   parameter                                   
* FUNCTION_INPUTS : Uchar next_conf: 
*                        - CHANNEL_DISABLE    
*                        - CHANNEL_RX_ENABLE (re-enable the channel in reception)
*                        - CHANNEL_RXB_ENABLE(re-enable the channel 
*                                             in buffer mode)                                                 
* FUNCTION_OUTPUTS: void                                                         
*----------------------------------------------------------------------------
* GLOBAL VARIABLES USED :
*	- conf_rx
*	- pt_st_can_rx
******************************************************************************
* NOTE:                                                                      
******************************************************************************/
void ReadCanMsg (Uchar next_conf)
{
  Uchar * pt_local;
  Uchar dlc2read;


  pt_st_can_rx->ctrl = CANCDMOB;  /* Save control register */

  conf_rx = CANCDMOB;

//  if(ide)
  if ((conf_rx & CONF_IDE) != FALSE)
  {
    pt_st_can_rx->id.tab[3] = CANIDT1 >> 3;   
    pt_st_can_rx->id.tab[2] = (CANIDT1 << 5) | (CANIDT2 >> 3);    
    pt_st_can_rx->id.tab[1] = (CANIDT2 << 5) | (CANIDT3 >> 3);    
    pt_st_can_rx->id.tab[0] = (CANIDT3 << 5) | (CANIDT4 >> 3);   
  }
  else
  {
    pt_st_can_rx->id.std = (CANIDT1 << 3) | (CANIDT2 >> 5);
  }

  pt_local = pt_st_can_rx->pt_donne;
  dlc2read = (conf_rx & MSK_CTRL_DLC);  

  for (; dlc2read!=0; dlc2read--)
  {
    *pt_local++ = CANMSG;
  }


/*---------- New configuration for this channel. */

//  b_var_read = next_conf;
//  if (NEW_CONF_CH_DISABLE)
  if (next_conf == CHANNEL_DISABLE)
  {
    DISABLE_CHANNEL; /* (Reception) disable.*/
  }
//  else if(NEW_CONF_CH_RX_ENABLE) 
  else if (next_conf == CHANNEL_RX_ENABLE) 
  {
    CANCDMOB |=  (CH_RxENA  << CONMOB); /* Reception (re)enable.*/
  }
  else /* CHANNEL_RXB_ENABLE */
  {
    CANCDMOB |=  (CH_RxBENA << CONMOB); /* Buffer (re)enable.*/
  }

}


/*F***************************************************************************
* FUNCTION_NAME:  FindFirstChIt                                              
*----------------------------------------------------------------------------
* FUNCTION_AUTHOR: BERTHY J.S.                                              
* FUNCTION_DATE  :                                               
*   AVR RELEASE  : 23/05/03 - JT                                                 
*----------------------------------------------------------------------------
* FUNCTION_PURPOSE: This function return the first channel with an interrupt
*                   request. The big priority start with the lowest channel 
*                   value (CHANNEL_0). 
* FUNCTION_INPUTS : void                 
* FUNCTION_OUTPUTS: Uchar num_channel: return the channel number of the first 
*                   it find in CANSIT registers (c.f. CANHPMOB register).                                                         
*****************************************************************************
* NOTE:                                                                      
******************************************************************************/
Uchar FindFirstChIt (void)
{
  Uchar num_channel;


  num_channel = (CANHPMOB >> HPMOB);

  return ((num_channel <= LAST_CHANNEL_NB) ? num_channel : NO_CHANNEL);
}


/*F***************************************************************************
* FUNCTION_NAME: fct_can_it                                                
*-----------------------------------------------------------------------------
* FUNCTION_AUTHOR: BERTHY J.S.                                              
* FUNCTION_DATE  :                                                  
*   AVR RELEASE  : to be done! - JT                                                 
*-----------------------------------------------------------------------------
* FUNCTION_PURPOSE: function called on can interrupt
*		The first task of this function is to identified whish channel 
*           generate the interrupt, and the second is to identified whish kind
*           of interrupt is generated (rxok, txok, error, buffer full).                                  
* FUNCTION_INPUTS : void                                                        
* FUNCTION_OUTPUTS: void                                                        
******************************************************************************
* NOTE:                                                                      
******************************************************************************/
#pragma vector=CANIT_vect
__interrupt void CANIT_handler(void)

{
  Uchar channel;
  Uchar save_canpage;
  
  save_canpage = CANPAGE; /* Save the current page */

  channel = FindFirstChIt();

  if (channel != NO_CHANNEL)
  {
    CAN_SET_CHANNEL(channel);
    if ((CANSTMOB & (1<<RXOK)) != FALSE)
    {

#ifdef    USER_CAN_FCT_IT_RXOK

      can_fct_it_rxok();

#endif /* USER_CAN_FCT_IT_RXOK */
 
    }
    else if ((CANSTMOB & (1<<TXOK)) != FALSE)
    {

#ifdef    USER_CAN_FCT_IT_TXOK

      can_fct_it_txok();  

#endif /* USER_CAN_FCT_IT_TXOK */

    }

    if((CANSTMOB & ((1<<TXOK) | (1<<RXOK))) == FALSE)
    {

#ifdef    USER_CAN_FCT_IT_ERROR    

      can_fct_it_error();

#endif /* USER_CAN_FCT_IT_ERROR */

    }

   CANSTMOB = CANSTMOB & (~INT_MOB_msk); /* Reset all MOb Int, Keep WDLC */

  } 
  else /* No Channel matchs with the interrupt => General IT*/
  {

#ifdef    USER_CAN_FCT_IT_GEN

    can_fct_it_gen();

#endif /* USER_CAN_FCT_IT_GEN */

    CANGIT = INT_GEN_msk;    /* New for AVR */
  } 

  CANPAGE = save_canpage; /* Restore the previous page */
}


/*F***************************************************************************
* FUNCTION_NAME: fct_tim_ovf_it                                                
*----------------------------------------------------------------------------
* FUNCTION_AUTHOR: BERTHY J.S.                                              
* FUNCTION_DATE  :                                                  
*   AVR RELEASE  : 23/05/03 - JT                                                 
*----------------------------------------------------------------------------
* FUNCTION_PURPOSE: function called on ovf_tim                                  
* FUNCTION_INPUTS : void                                                        
* FUNCTION_OUTPUTS: void                                                        
******************************************************************************
* NOTE: Automatic IT clear in AVR (Equ to: CANGIT &= ~MSK_CANGIT_OVRTIM;)                                                                    
******************************************************************************/
#pragma vector=CANTOVF_vect
__interrupt void CANTOVF_handler(void)
  {

#ifdef   USER_CAN_FCT_IT_TIMOVF
 
  can_fct_it_timovf();

#endif /*USER_CAN_FCT_IT_TIMOVF */

  }




























⌨️ 快捷键说明

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