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

📄 main.c

📁 MC68HC908GR60A LIN-BUS源码(编译环境:CodeWarriorV3.1)
💻 C
字号:
/*******************************************************************************
*                (c) Freescale Inc. 2004  all rights reserved.                 *
*                                                                              *
*                                                                              *
*     An example application for AN2767 based on MC68HC908GR60A, slave node.   *
*     =====================================================================    *
*                                                                              *
*    $File Name     : main.c$                                                  *
*    $Author        : re004c$                                                  *
*    $Date          : Nov-5-2004$                                             *
*    $Version       : 1.1.10.0$                                                 *
*    Function:                                                                 *
*                                                                              *
* ============================================================================ *
* THIS SOFTWARE IS PROVIDED BY FREESCALE SEMICONDUCTOR "AS IS" AND ANY         *
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  *
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE       *
* DISCLAIMED.  IN NO EVENT SHALL FREESCALE SEMICONDUCTOR OR ITS CONTRIBUTORS   *
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR       *
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF         *
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS     *
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN      *
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)      *
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE   *
* POSSIBILITY OF SUCH DAMAGE.                                                  *
*******************************************************************************/

#include <lin.h>
#include "target.h"
#include "board.h"

#define DEBOUNCE 1                                 /* 32.8 ms x (1+1) = 66ms  */
#define LOW_POWER 122                              /* 32.8 ms x (122+1) = 4s  */

unsigned char data = 1;
unsigned char id = 0;
unsigned char key_last;
unsigned char count;
int keycount, low_power_delay;

#define EVER (;;)                                 /* To avoid warning message */

extern void adjust_clock(void);

l_u8 LD_SERIAL[] = {0x01, 0x02, 0x03, 0x04};


/*******************************************************************************
*                                                                              *
*    Function name: NodeFailure                                                *
*    Function:      Do the processing necessary to re-start or enter           *
*                   limp-home mode. In this example we enter an endless loop.  *
*                                                                              *
*******************************************************************************/

void NodeFailure (void)
{
    for EVER                                      /* To avoid warning message */
    {
        /* forever */
    }
}


/*******************************************************************************
*                                                                              *
*    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: ReadButton                                                 *
*    Function:      The port line is read and its level compared with what     *
*                   it was the previous time through the loop. If it is the    *
*                   same, the counter "keycount" is used for debounce (80ms)   *
*                   If the switch status changes the counter is reset.         *
*                                                                              *
*******************************************************************************/

void ReadButton (void)
{
    unsigned char key;

    key = BUTTON_PIN;                            /* read button on button pin */
    if (key == key_last)                               /* same as last time ? */
    {
        if (keycount == DEBOUNCE)                  /* yes, (debounce + 2)th ? */
        {
            if (key == 0)                                /* yes, key pressed ?*/
            {
                data++;                                     /* increment data */
                if (data == 16) data = 1;            /* wrapping from 15 to 1 */
            }
            keycount ++;                                 /* prevents re-entry */
        }        
        else
        {
            keycount ++;
        }
    }
    else
    {   
        keycount = 0;                              /* no, different, so reset */
        key_last = key;                              /* count and save status */
    }
}


/*******************************************************************************
*                                                                              *
*    Function name: LedDisplay                                                 *
*    Function:      According to mode the LEDs display the 4-bit data field    *
*                   or the flashing ID.                                        *
*                                                                              *
*******************************************************************************/

void LedDisplay (void)
{
    unsigned char led;

    led = data << LED_OFFSET;                         /* drive LEDs with data */
    LED_PORT = (LED_PORT & ~LED_MASK) | (led & LED_MASK);
}


/*******************************************************************************
*                                                                              *
*    Function name: LinResponse                                                *
*    Function:      According to ID, the response field is sent using "data".  *
*                                                                              *
*******************************************************************************/

void LinResponse (void)
{
    l_u8_wr_data_28(data);                                    /* LIN response */
}


/*******************************************************************************
*                                                                              *
*    Function name: CheckLowPower                                              *
*    Function:      Check if the goto_sleep commnad has been received or there *
*                   is no activity on LIN bus for 4 sec.                       *
*                                                                              *
*******************************************************************************/

void CheckLowPower(void)
{
    l_u16 status;

    status = l_ifc_read_status_i1();
                                                   /* Sleep command received? */
    if ((status & 0x0008)== L_SLEEP_REQUEST)
    {
        LIN_EN_PIN = 0;      /* yes, disable MC33399 LIN interface turn off */
                             /* LT1121 to reache low power consumption */
    }

    if ((status & 0x00FF) == 0x00)                     /* No activity on LIN? */
    {
        low_power_delay--;                      /* yes, decrement the counter */
    }
    else
    {
        low_power_delay = LOW_POWER;
    }
    if (low_power_delay == 0)            /* no activity on LIN for 4 seconds? */
    {
        LIN_EN_PIN = 0;        /* yes, disable MC33399 LIN interface turn off */
                               /* LT1121 to reache low power consumption */
    }
}


/*******************************************************************************
*                                                                              *
*    Function name: main                                                       *
*    Function:      Initialize the network and start processing signals in a   *
*                   forever running loop.                                      *
*                                                                              *
*******************************************************************************/
 
void main (void)
{
    l_u8   button_request_id = 0;
    l_bool result;
    
    low_power_delay = LOW_POWER;          /* initialize the low power counter */
    BUTTON_DDR = 0;                            /* set the button pin as input */
    
    LED_DDR |= LED_MASK;                            /* set LED port as output */
    LED_PORT &= ~LED_MASK;                                /* turn the LED off */
  
    LIN_EN_DDR = 1;                           /* set LIN enable pin as output */
    LIN_EN_PIN = 1;                             /* enable 33399 LIN interface */
    
    TBCR = 0x20;                                      /* /262144 for 30.5Hz @ */
    TBCR = 0x22;                                    /* 8.000MHz & enable  TBM */

    CONFIG1 |= 0x02;                                      /* enable STOP mode */
    CONFIG2_TMCLKSEL = 1;                         /* extra TBM divider enable */
      
    init_target();
    result = l_sys_init();
    Check(result);
    l_ifc_init_i1();
    ENABLE_INTS();                                      /* turn on interrupts */
    result = l_ifc_connect_i1();
    Check(result);
    
    l_u8_wr_SlaveRespB0_demo_net(0x81);             /* set the slave response */ 
    
    for EVER                                    /*   to avoid warning message */
    {
        PET_WATCHDOG();
                  
        if (l_flg_tst_res_done())        /* conflict resolving done flag set? */
        {
                                             /* yes, clear the slave response */
            l_u8_wr_SlaveRespB0_demo_net(0x80);  
            l_flg_clr_res_done();                           /* clear the flag */
        } 
           
        if (TBCR & 0x80)                                  /* is TBM flag set? */
        {
            TBCR_TACK = 1;                                   /* yes, clear it */
            count++;                                 /* used for LED flashing */
            ReadButton();                       /* read button on button port */
            LedDisplay();                          /* update LEDs on LED port */
            LinResponse();                            /* provide LIN response */
            CheckLowPower();                      /* check the low power mode */
        }
    }
}

⌨️ 快捷键说明

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