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

📄 master.c

📁 基于mc908gz60的LIN通信例程
💻 C
📖 第 1 页 / 共 2 页
字号:
    if ((PTE & 0x20) && (!sleep_mode))
    {
        GotoSleep();                      /* yes, send the goto sleep command */
        ChangeList(0);
        TBCR_TBIE = 0;                               /* disable TBM interrupt */
    }

    if (sleep_mode)                                 /* cluster in sleep mode? */
    {
        NewNode();
        LostNode();
    }
                              /* switch turned off and cluster in sleep mode? */
    if (sleep_mode && (!(PTE & 0x20)))
    {
        TBCR_TBIE = 1;                           /* yes, enable TBM interrupt */
        l_ifc_init_i1();                          /* Initialize the interface */
        l_ifc_connect_i1();                     /* Connect driver to I/O pins */
        l_sch_set_i1(sch_conflict_resolving, 0);  /* start conflict resolving */
        while (l_sch_tick_i1() != 1)                             /* one round */
        {
            BusyWaitUntilNextPeriod();
        }
        l_sch_set_i1 (normal_mode,0);           /* set the normal runing mode */
        sleep_mode = 0;
        ChangeList(1);                           /* reactive active node list */
    }
}


/*******************************************************************************
*                                                                              *
*    Function name: Process                                                    *
*    Function:      Process the received messages and change internal states   *
*                   as well as send out responds to the slave.                 *
*                                                                              *
*******************************************************************************/

void Process (void)
{
    static l_u16  periods_until_enter_low_power = PERIODS_PER_SECOND;

    l_sch_tick_i1 ();                          /* Give processing time to LIN */
  
    #ifdef PET_WATCHDOG
    PET_WATCHDOG();
    #endif
    
    LinActive();                                          /* check the switch */

    l_u8_wr_info(0xAA);                           /* send info to slave nodes */
    
    if (l_u8_rd_SlaveRespB0_demo_net() == 0x81)         /* new node detected? */
    {
        l_frame_SlaveResp_demo_net_buf[0] = 0x80;
        TBCR_TBIE = 0;                               /* disable TBM interrupt */
        NewNode();
        while (l_sch_tick_i1() != 1)                             /* one round */
        {
            BusyWaitUntilNextPeriod();
        }

        l_sch_set_i1 (sch_conflict_resolving, 0);     /* start schedule table */
        while (l_sch_tick_i1() != 1)                             /* one round */
        {
            BusyWaitUntilNextPeriod();
        }
        ChangeList(1);
        TBCR_TBIE = 1;                                /* enable TBM interrupt */
        l_sch_set_i1 (normal_mode, 0);          /* Set & start schedule table */
    }
  
    messages[0] = l_u8_rd_data_20();       /*                                 */
    messages[1] = l_u8_rd_data_18();       /* fill in the messages bufer with */
    messages[2] = l_u8_rd_data_10();       /* the data from the nodes         */
    messages[3] = l_u8_rd_data_28();       /*                                 */

    status = l_ifc_read_status_i1();
            /* l_ifc_read_status call without any frame transfer returns zero */
    if ((status != 0x0000) & !(status & 0x0002))/* no response from the node? */
    {
        status &= 0xFF00;                                    /* mask low byte */
        switch (status)
        {
            case 0x2000:
                        if (active_nodes[0])
                        {
                            TBCR_TBIE = 0;           /* disable TBM interrupt */
                            LostNode();
                            TBCR_TBIE = 1;            /* enable TBM interrupt */
                        }
                        active_nodes[0] = 0;     /* clear the active node flag*/
                        break;
            case 0xD800:
                        if (active_nodes[1])
                        {
                            TBCR_TBIE = 0;           /* disable TBM interrupt */
                            LostNode();
                            TBCR_TBIE = 1;            /* enable TBM interrupt */
                        }
                        active_nodes[1] = 0;     /* clear the active node flag*/
                        break;
            case 0x5000:
                        if (active_nodes[2])
                        {
                            TBCR_TBIE = 0;           /* disable TBM interrupt */
                            LostNode();
                            TBCR_TBIE = 1;            /* enable TBM interrupt */
                        }
                        active_nodes[2] = 0;     /* clear the active node flag*/
                        break;
            case 0xA800:
                        if (active_nodes[3])
                        {
                            TBCR_TBIE = 0;           /* disable TBM interrupt */
                            LostNode();
                            TBCR_TBIE = 1;            /* enable TBM interrupt */
                        }
                        active_nodes[3] = 0;     /* clear the active node flag*/
        }
    }
  
    if ((l_ifc_read_status_i1() & 0x0010) == L_WAKEUP_REQUEST)
    {
        l_ifc_init_i1();                          /* Initialize the interface */
        l_ifc_connect_i1();                     /* Connect driver to I/O pins */
        l_sch_set_i1(sch_conflict_resolving, 0);  /* start conflict resolving */
        while (l_sch_tick_i1() != 1)                             /* one round */
        {
            BusyWaitUntilNextPeriod();
        }
        l_sch_set_i1(normal_mode,0);  
    }
    BusyWaitUntilNextPeriod();             /* Wait until start of next period */
}


/*******************************************************************************
*                                                                              *
*    Function name: Check                                                      *
*    Function:      Check that the call return_value is success, or enter      *
*                   failure mode.                                              *
*                                                                              *
*******************************************************************************/

void Check (l_bool return_value)
{
    if (return_value)
    {
        NodeFailure();
    }
}


/*******************************************************************************
*                                                                              *
*    Function name: main                                                       *
*    Function:      Start the system and then enter the eternal operation loop.*
*                                                                              *
*******************************************************************************/

void main (void)
{
    l_bool result;
    
    #ifdef PET_WATCHDOG
    PET_WATCHDOG();
    #endif

    previous_start_time = l_get_us_counter();   /* used in busy wait function */

    CONFIG2_TMCLKSEL = 1;                         /* enable TBM extra divider */
    TBCR &= 0xAF;                               /* set TBM divider to 262,144 */
    TBCR_TBON = 1;                                             /* run the TBM */
    TBCR_TBIE = 1;                                    /* enable TBM interrupt */
 
    init_target();                             /* Initialize non-LIN hardware */
    result = l_sys_init();                 /* Initialize LIN system component */
    Check (result);
    l_ifc_init_i1();                              /* Initialize the interface */
    ENABLE_INTS();                                      /* Turn on interrupts */
    result = l_ifc_connect_i1();                /* Connect driver to I/O pins */
    Check(result);
    
    DDRF = 0xFF;                                     /* Set the PTF as output */
    DDRE_DDRE5 = 0;                                      /* Set PTE5 as input */

    l_bool_wr_resolving_done(1);      /* set the conflict resolving done flag */

    l_sch_set_i1(sch_conflict_resolving, 0);      /* start conflict resolving */
    
    while (l_sch_tick_i1() != 1)                                 /* one round */
    {
        BusyWaitUntilNextPeriod();
    }
    l_sch_set_i1(normal_mode, 0);               /* Set & start schedule table */
      
    for EVER  /* forever */
    {
        Process();                                   /* Do all the processing */
    }
}

⌨️ 快捷键说明

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