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

📄 can1.c

📁 M16C290FC can
💻 C
📖 第 1 页 / 共 2 页
字号:
/*""FILE COMMENT""****************************************************
 *System Name : CAN DEMO PROGRAM1
 *File Name   : can1.c
 *Version     : 1.00
 *Contens     : 
 *Customer    : RSO
 *Model       : 
 *Order       : 
 *CPU         : 
 *Compiler    : NC30 Version 5.30 Release 02
 *OS          : 
 *Programmer  : 
 *Note        : 
 *            : 
 **********************************************************************
 * Copyright(C)2005, Renesas Technology Corp.
 * Copyright(C)2005, Renesas Solutions Corp.
 * All rights reserved.
 **********************************************************************
 *History        2005.07.1  Ver 1.00
 *""FILE COMMENT END""************************************************/

#include "sfr29.h"
#include "define1.h"
#include "declare1.h"

 /*""FUNC COMMENT""****************************************************
  *ID               :2.0
  *Description      :Initialize CAN
  *--------------------------------------------------------------------
  *Include          :"sfr29.h"
  *                 :"define1.h"
  *                 :"declare1.h"
  *--------------------------------------------------------------------
  *Declaration      :void can_initial(void)
  *--------------------------------------------------------------------
  *Function         :Initialize CAN
  *--------------------------------------------------------------------
  *Arguments        :Nothing
  *--------------------------------------------------------------------
  *Returns          :Nothing
  *--------------------------------------------------------------------
  *Input            :Nothing
  *Ountput          :unsigned char trm_id       ;transmit ID
  *                 :unsigned char rcv_id       ;receive ID
  *--------------------------------------------------------------------
  *Call functions   :config_can0()
  *                 :set_rec_std_dataframe_can0
  *--------------------------------------------------------------------
  *Note             :
  *--------------------------------------------------------------------
  *History          :
  *                 :
  *""FUNC COMMENT END""***********************************************/
void can_initial(void)
{
    config_can0();                  //Configurate can
    c0recic = CAN0_LVL;             //Interrupt lvl
    c01erric = CAN0_ERROR_LVL;      //Interrupt lvl
    
    set_rec_std_dataframe_can0(14,CAN_RCV_ID);  //Set can slot14
    set_rec_std_dataframe_can0(15,CAN_RCV_ID);  //Set can slot15
    
    trm_id = CAN_TRM_ID;            //Set transmit ID
    rcv_id = CAN_RCV_ID;            //Set receive ID
}

 /*""FUNC COMMENT""****************************************************
  *ID               :2.1
  *Description      :Set can slot receive
  *--------------------------------------------------------------------
  *Include          :"sfr29.h"
  *                 :"declare1.h"
  *--------------------------------------------------------------------
  *Declaration      :void set_rec_std_dataframe_can0
  *                     (unsigned short in_slot,unsigned short in_sid)
  *--------------------------------------------------------------------
  *Function         :Set can slot receive
  *--------------------------------------------------------------------
  *Arguments        :unsigned short in_slot
  *                 :unsigned short in_sid
  *--------------------------------------------------------------------
  *Returns          :Nothing
  *--------------------------------------------------------------------
  *Input            :Nothing
  *Ountput          :Nothing
  *--------------------------------------------------------------------
  *Call functions   :Nothing
  *--------------------------------------------------------------------
  *Note             :
  *--------------------------------------------------------------------
  *History          :
  *                 :
  *""FUNC COMMENT END""***********************************************/
void set_rec_std_dataframe_can0(
    unsigned short in_slot,
    unsigned short in_sid)
{
    c0icr |= (0x0001<<in_slot);
    c0idr &= ~(0x0001<<in_slot);
    
    c0slot[in_slot].ba.sidh = (in_sid>>6) & 0x1f;   //SID10-6
    c0slot[in_slot].ba.sidl = in_sid & 0x3f;        //SID5-0
    
    while(c0mctl[in_slot].byte != 0x00){
        c0mctl[in_slot].byte = 0x00;
    }
    c0mctl[in_slot].byte = 0x40;
}


 /*""FUNC COMMENT""****************************************************
  *ID               :2.0.2
  *Description      :Set can bitrate
  *--------------------------------------------------------------------
  *Include          :"sfr29.h"
  *                 :"declare1.h"
  *--------------------------------------------------------------------
  *Declaration      :void set_bitrate_can0(void)
  *--------------------------------------------------------------------
  *Function         :Set can bitrate
  *--------------------------------------------------------------------
  *Arguments        :Nothing
  *--------------------------------------------------------------------
  *Returns          :Nothing
  *--------------------------------------------------------------------
  *Input            :Nothing
  *Ountput          :Nothing
  *--------------------------------------------------------------------
  *Call functions   :Nothing
  *--------------------------------------------------------------------
  *Note             :
  *--------------------------------------------------------------------
  *History          :
  *                 :
  *""FUNC COMMENT END""***********************************************/
void set_bitrate_can0(void)
{
    prc0 = 1;
    cclkr = 0x01;       // CAN0 clock = f1/2
    prc0 = 0;
    
    /* 500kbps 10Tq */
    brp_c0conr = 0;     // fcan(10MHz)
    sam_c0conr = 0;     //
    pts_c0conr = 2;     // PTS = 3Tq
    pbs1_c0conr = 2;    // PBS1 = 3 Tq
    pbs2_c0conr = 2;    // PBS2 = 3 Tq
    sjw_c0conr = 0;     // SJW = 1 Tq
}


 /*""FUNC COMMENT""****************************************************
  *ID               :2.0.1
  *Description      :CAN configuration
  *--------------------------------------------------------------------
  *Include          :"sfr29.h"
  *                 :"define1.h"
  *                 :"declare1.h"
  *--------------------------------------------------------------------
  *Declaration      :void config_can0()         ;
  *--------------------------------------------------------------------
  *Function         :Set can slot receive
  *--------------------------------------------------------------------
  *Arguments        :Nothing
  *--------------------------------------------------------------------
  *Returns          :Nothing
  *--------------------------------------------------------------------
  *Input            :Nothing
  *Ountput          :Nothing
  *--------------------------------------------------------------------
  *Call functions   :set_bitrate_can0()
  *                 :set_mask_can0()
  *--------------------------------------------------------------------
  *Note             :
  *--------------------------------------------------------------------
  *History          :
  *                 :
  *""FUNC COMMENT END""***********************************************/
void config_can0(void)
{
    reset_c0ctlr = 1;               /* configration */
    sleep_c0ctlr = 0;
    while(! state_reset_c0str){}
    
    porten_c0ctlr = 1;              /* CAN port enable */
    loopback_c0ctlr = 0;            /* normal */
    msgorder_c0ctlr = 1;            /* byte access */
    basiccan_c0ctlr = 1;            /* 0:normal mode 1:BasicCAN mode */
    buserren_c0ctlr = 0;            /* nus error interrupt disable */
    tsprescale_c0ctlr = 0;
    
    set_bitrate_can0();             /* set bit rate */
    set_mask_can0();                /* mask */
    
    reset_c0ctlr = 0;
    while(state_reset_c0str){}
}


 /*""FUNC COMMENT""****************************************************
  *ID               :2.0.3
  *Description      :Set can mask
  *--------------------------------------------------------------------
  *Include          :"sfr29.h"
  *                 :"define1.h"
  *                 :"declare1.h"
  *--------------------------------------------------------------------
  *Declaration      :void set_mask_can0(void)
  *--------------------------------------------------------------------
  *Function         :Set can mask
  *--------------------------------------------------------------------
  *Arguments        :Nothing
  *--------------------------------------------------------------------
  *Returns          :Nothing
  *--------------------------------------------------------------------
  *Input            :Nothing
  *Ountput          :Nothing

⌨️ 快捷键说明

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