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

📄 usercode.c

📁 包含了NXP众多的MCU的例子程序
💻 C
📖 第 1 页 / 共 2 页
字号:

/*
*****************************************************************************
**
**      Project     : EW CAN Library for Philips LPC2129
**
**      Modulename  : Application code
**
**      Filename    : usercode.c
**
**      Abstract    : This file contain application source code for Philips
**                    LPC2129. The application uses the CAN module and
**                    send messages between the two CAN controllers CAN1
**                    and CAN2. The CAN library <ew_can.c> is used and
**                    the <user_can.h> contain application specific settings
**                    for the library.
**
**      Hardware    : IAR KickStart Kit for Philips LPC2129
**                    1. Power supply:
**                      The Kickstart kit needs to be supplied with an
**                      external power supply (7Volt has been used) if
**                      the external CAN bus shall be operable.
**
**                    2. Connection:
**                    Connect the CAN D-SUB connectors on the board:
**                        CAN1                CAN2
**                        pin2 ---- CANL ---- pin2
**                        pin7 ---- CANH ---- pin7
**
**      Software    : CAN module setting:
**                    These settings are made in the <user_can.h> file
**
**                    1. CAN pin usage - RD2, TD2, and RD1 are enabled
**                       (TD1 is by default enabled in LPC2129)
**
**                    2. CAN Mode is set as Priority depends on Tx
**                     priority field.
**
**                    3. CAN Interrupt
**                     CAN1 interrupt disabled (polled by application)
**                     CAN2 RX interrupt enabled
**
**                    4. CAN Bit timing
**                          Clock = 100 usec/bit
**                          SAM    =0 Bus is sampled once
**                          TSEG1 = 0xC=12
**                          TSEG2 = 1
**                          SJW   = 0
**                          BRP   = 0x16 =22
**
**                    5. Acceptance filter mode
**                    AcceptanceFilter Full CAN message is used.
**
**                    6. User code macros for CAN2 RX interrupts are defined
**
**      Overview    : 1. GPIO, and interrupt are initialized
**                    2. CAN module is initialized using EW_Init_CAN()
**                    3. Used CAN IDs are initialized using EW_AddFilter_CAN()
**                       The CAN filter uses the 5 different filter types
**                       A. Full standard ID messages
**                       B. Standard ID messages
**                       C. Standard Group ID messages
**                       D. Extended ID messages
**                       E. Extended Group ID messages
**                    4. CAN messages are sent between CAN1 and CAN2
**                       CAN1 is polled to detect received CAN message
**                       CAN2 uses receive interrupt to detect rx message
**                       The LEDs will toggle if CAN messages are running
**                       Button B1 can be used to stop CAN messages
**                       Button B2 can be used to start CAN messages
**
**      Functions   : main()
**
**      Date        : 2005-12-05
**
**      Version     : 1.00A
**
**      (c) Copyright IAR Systems 2005
**
**      THE SOFTWARE IS PROVIDED "AS IS"  WITHOUT ANY WARRANTY OF ANY KIND,
**      IAR SYSTEMS DOES NOT WARRANT THAT THE SOFTWARE WILL BE ERROR-FREE
**      OR THAT SUCH ERRORS WILL BE CORRECTED, AS A RESULT, IAR SYSTEMS
**      SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL
**      DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF SUCH
**      SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
**      CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
**
*****************************************************************************
*/

/*
**===========================================================================
**  1       GENERAL
**  1.1     Revisions
**===========================================================================
*/

/*
**===========================================================================
**  1.2     References
**===========================================================================
*/

/*
**===========================================================================
**  2.      INCLUDE FILES
**  2.1     Standard include files
**===========================================================================
*/

/*
**===========================================================================
**  2.2     Application include files
**===========================================================================
*/
#include "iolpc2129.h"      /* Defines Special function registers */
#include <intrinsics.h>     /* Intrinsic functions */
#include "ew_can.h"         /* EW CAN library header file */
#include "user_can_def.h"   /* Application specific CAN definitions */

/*
**===========================================================================
**  3.      DECLARATIONS
**  3.1     Internal constants
**===========================================================================
*/

/*
**===========================================================================
**  3.2     Internal macros
**===========================================================================
*/

/*
**===========================================================================
**  3.3     Internal type definitions
**===========================================================================
*/

/*
**===========================================================================
**  3.4     Global variables (declared as 'extern' in some header file)
**===========================================================================
*/

/*
**===========================================================================
**  3.5     Internal function prototypes (defined in Section 5)
**===========================================================================
*/
__irq __arm void irq_handler(void);

/*
**===========================================================================
**  3.6     Internal variables
**===========================================================================
*/
/*--- CAN receive and transmitte messages */
CAN_MSG RxMsg0;
CAN_MSG RxMsg1;
CAN_MSG RxMsg2;
CAN_MSG TxMsg;

/*--- Counters ---*/
U32 CANInterruptCount1;
U32 CANInterruptCount2;
U32 CAN1ii;
U32 CAN2ii;

U32 CANRunFlag;     /* Flag used to enable/disable CAN messages */


/*
**----------------------------------------------------------------------------------------
** CAN Filter table
** These tables are installed into the CAN filter table by calling the
** EW_AddFilter_CAN() function.
**
** It is not required that the items in the Filter table are sorted as EW_AddFilter_CAN()
** function installes new items into the CAN filter at a sorted location.
**----------------------------------------------------------------------------------------
*/
/*--- CAN1 Filters ---*/
CAN_Filter CAN1Filter[] = {
/* Filter         Controller, Disable,  ID,        ID       */
/*  Type           1  2        1  2     1          2        */
{ EW_CAN_FULL,     1, 1,       0, 0,    0x14,      0x15  },    /* Standard ID 0x14, 0x15 */
{ EW_CAN_FULL,     1, 1,       0, 0,    0x16,      0x17  },    /* Standard ID 0x16, 0x17 */
{ EW_CAN_FULL,     1, 1,       0, 0,    0x18,      0x1E  },    /* Standard ID 0x18, 0x1E */
{ EW_CAN_FULL,     1, 1,       0, 0,    0x20,      0x22  },    /* Standard ID 0x20, 0x22 */
{ EW_CAN_STD,      1, 1,       0, 0,    0x140,     0x142 },    /* Standard ID 0x140, 0x142 */
{ EW_CAN_STD_GRP,  1, 1,       0, 0,    0x203,     0x206 },    /* Standard ID 0x203-0x206 */
{ EW_CAN_STD_GRP,  1, 1,       0, 0,    0x500,     0x580 },    /* Standard ID 0x500-0x580 */
{ EW_CAN_EXT,      1, 1,       0, 0,    0x123456,  0 },        /* Extended ID 0x123456 */
{ EW_CAN_EXT,      1, 1,       0, 0,    0x222333,  0 },        /* Extended ID 0x222333 */
{ EW_CAN_EXT_GRP,  1, 1,       0, 0,    0x300000,  0x400000 }, /* Extended ID 0x300000-0x400000 */
{ 0,            0, 0,       0, 0,    0,         0 },        /* End of table */
};

/*--- CAN2 Filters ---*/
CAN_Filter CAN2Filter[] = {
/* Filter         Controller, Disable,  ID,        ID       */
/*  Type           1  2        1  2     1          2        */
{ EW_CAN_FULL,     2, 2,       0, 0,    0x10,      0x12  },    /* Standard ID 0x10, 0x12 */
{ EW_CAN_FULL,     2, 2,       0, 0,    0x23,      0x24  },    /* Standard ID 0x23, 0x24 */
{ EW_CAN_FULL,     2, 2,       0, 0,    0x26,      0x27  },    /* Standard ID 0x26, 0x27 */
{ EW_CAN_FULL,     2, 2,       0, 0,    0x29,      0x2A  },    /* Standard ID 0x29, 0x2A */
{ EW_CAN_STD,      2, 2,       0, 0,    0x150,     0x151 },    /* Standard ID 0x150, 0x151 */
{ EW_CAN_STD_GRP,  2, 2,       0, 0,    0x600,     0x620 },    /* Standard ID 0x600-0x620 */
{ EW_CAN_STD_GRP,  2, 2,       0, 0,    0x700,     0x780 },    /* Standard ID 0x700-0x780 */
{ EW_CAN_EXT,      2, 2,       0, 0,    0x334455,  0 },        /* Extended ID 0x334455 */
{ EW_CAN_EXT,      2, 2,       0, 0,    0x444666,  0 },        /* Extended ID 0x444555 */
{ EW_CAN_EXT_GRP,  2, 2,       0, 0,    0x500000,  0x600000 }, /* Extended ID 0x500000-0x600000 */
{ 0,            0, 0,       0, 0,    0,         0 },        /* End of table */
};


/*--- CAN messages sent from CAN1 (to be received by CAN2 ---*/
CAN_MSG Can1Test[] = {
/* CTRL                                                   | ID         | Data1      | Data2       */
/* Priority,0-255, DLC bits, RTR,     Frame format        | ID         | Data1      | Data2       */
{   1            + (8<<16) +  0  +EW_CAN_CTRL_STANDARD_ID, 0x10,        0x11111111L, 0x22222222L },  // 0
{   2            + (8<<16) +  0  +EW_CAN_CTRL_STANDARD_ID, 0x12,        0x33333333L, 0x44444444L },  // 1
{   3            + (8<<16) +  0  +EW_CAN_CTRL_STANDARD_ID, 0x23,        0x11111111L, 0x22222222L },  // 2
{   4            + (8<<16) +  0  +EW_CAN_CTRL_STANDARD_ID, 0x24,        0x11111111L, 0x22222222L },  // 3
{   5            + (8<<16) +  0  +EW_CAN_CTRL_STANDARD_ID, 0x26,        0x33333333L, 0x44444444L },  // 4
{   6            + (8<<16) +  0  +EW_CAN_CTRL_STANDARD_ID, 0x27,        0x33333333L, 0x44444444L },  // 5
{   7            + (8<<16) +  0  +EW_CAN_CTRL_STANDARD_ID, 0x29,        0x33333333L, 0x44444444L },  // 6
{   0            + (8<<16) +  0  +EW_CAN_CTRL_STANDARD_ID, 0x2A,        0x33333333L, 0x44444444L },  // 7
{   8            + (8<<16) +  0  +EW_CAN_CTRL_STANDARD_ID, 0x150,       0x33333333L, 0x44444444L },  // 8
{   8            + (8<<16) +  0  +EW_CAN_CTRL_STANDARD_ID, 0x600,       0x33333333L, 0x44444444L },  // 9
{   8            + (8<<16) +  0  +EW_CAN_CTRL_STANDARD_ID, 0x620,       0x33333333L, 0x44444444L },  // 10
{   8            + (8<<16) +  0  +EW_CAN_CTRL_STANDARD_ID, 0x700,       0x33333333L, 0x44444444L },  // 11
{   8            + (8<<16) +  0  +EW_CAN_CTRL_STANDARD_ID, 0x780,       0x33333333L, 0x44444444L },  // 12
{   8            + (8<<16) +  0  +EW_CAN_CTRL_EXTENDED_ID, 0x334455L,   0x12345678L, 0x12345678L },  // 13
{   8            + (8<<16) +  0  +EW_CAN_CTRL_EXTENDED_ID, 0x444666L,   0x12345678L, 0x12345678L },  // 14
{   8            + (8<<16) +  0  +EW_CAN_CTRL_EXTENDED_ID, 0x500000L,   0x12345678L, 0x12345678L },  // 15
};


/*--- CAN messages sent from CAN2 (to be received by CAN1 ---*/
CAN_MSG Can2Test[] = {
/* CTRL                                               | ID         | Data1      | Data2       */
/* Priority,0-255, DLC bits, RTR, Frame format        | ID         | Data1      | Data2       */
{   0            + (8<<16) +  0  +EW_CAN_CTRL_STANDARD_ID, 0x20,        0x55555555L, 0x66666666L },  // 0
{   1            + (8<<16) +  0  +EW_CAN_CTRL_STANDARD_ID, 0x22,        0x77777777L, 0x88888888L },  // 1
{   2            + (8<<16) +  0  +EW_CAN_CTRL_STANDARD_ID, 0x14,        0x77777777L, 0x88888888L },  // 2
{   3            + (8<<16) +  0  +EW_CAN_CTRL_STANDARD_ID, 0x15,        0x77777777L, 0x88888888L },  // 3
{   4            + (8<<16) +  0  +EW_CAN_CTRL_STANDARD_ID, 0x16,        0x77777777L, 0x88888888L },  // 4
{   5            + (8<<16) +  0  +EW_CAN_CTRL_STANDARD_ID, 0x17,        0x77777777L, 0x88888888L },  // 5
{   6            + (8<<16) +  0  +EW_CAN_CTRL_STANDARD_ID, 0x18,        0x77777777L, 0x88888888L },  // 6
{   7            + (8<<16) +  0  +EW_CAN_CTRL_STANDARD_ID, 0x1E,        0x77777777L, 0x88888888L },  // 7
{   8            + (8<<16) +  0  +EW_CAN_CTRL_STANDARD_ID, 0x140,       0x77777777L, 0x88888888L },  // 8
{   8            + (8<<16) +  0  +EW_CAN_CTRL_STANDARD_ID, 0x142,       0x77777777L, 0x88888888L },  // 9
{   8            + (8<<16) +  0  +EW_CAN_CTRL_STANDARD_ID, 0x204,       0x77777777L, 0x88888888L },  // 10
{   8            + (8<<16) +  0  +EW_CAN_CTRL_STANDARD_ID, 0x500,       0x77777777L, 0x88888888L },  // 11
{   8            + (8<<16) +  0  +EW_CAN_CTRL_EXTENDED_ID, 0x123456L,   0x12345678L, 0x12345678L },  // 12
{   8            + (8<<16) +  0  +EW_CAN_CTRL_EXTENDED_ID, 0x222333L,   0x12345678L, 0x12345678L },  // 13
{   8            + (8<<16) +  0  +EW_CAN_CTRL_EXTENDED_ID, 0x300000L,   0x77777777L, 0x88888888L },  // 14
{   8            + (8<<16) +  0  +EW_CAN_CTRL_EXTENDED_ID, 0x400000L,   0x77777777L, 0x88888888L },  // 15
};


/*
**===========================================================================
**  4.      GLOBAL FUNCTIONS (declared as 'extern' in some header file)
**===========================================================================
*/

void feed (void)
/*
**---------------------------------------------------------------------------
**
**  Abstract:
**      Feed sequence for the PLL
**
**  Parameters:
**      None
**
**  Returns:
**      None
**
**---------------------------------------------------------------------------
*/
{
  PLLFEED=0xAA;
  PLLFEED=0x55;
}

void main( void )
/*
**---------------------------------------------------------------------------
**
**  Abstract:
**      User application
**
**  Parameters:
**      None
**
**  Returns:
**      None
**
**---------------------------------------------------------------------------
*/
{
    U32 ii, jj;
    U32 CAN1msg;
    U32 CAN2msg;
    U32 volatile *pAddrReg;
    U16 Status;

    // The MAMTIM value would depend upon the system clock desired
    // MAM is fully enabled below
    MAMCR_bit.MODECTRL=0x0;   // Disabel MAM
    MAMTIM_bit.CYCLES =0x1;   // Change MAM clock divider
    MAMCR_bit.MODECTRL=0x2;   // Enable MAM

    // Setup the system clock to the desired clock frequency
    // The PLLCFG value is provided for input frequency of 14.7456 MHz
    // Disabe PLL
    PLLCON = 0;
    feed();

    // Set the peripheral clock. Here we set it to 14.7456MHz/4
    VPBDIV=0x0;

    /*
    **----------------
    ** Initialise CAN
    **----------------
    */
    EW_Init_CAN();

    /*
    **------------------------------------
    ** Initialise GPIO - Buttons and LEDs
    **------------------------------------
    */
    /*--- Buttons on P0.15 + P0.16, input pins ---*/
    PINSEL0_bit.P0_15 = 0;
    PINSEL1_bit.P0_16 = 0;

    IO0DIR_bit.P0_15 = 0;   /* B1 */
    IO0DIR_bit.P0_16 = 0;   /* B2 */

    /*--- LEDs on P0.12 + P0.13, output pins ---*/
    PINSEL0_bit.P0_12 = 0;
    PINSEL0_bit.P0_13 = 0;

    IO0DIR_bit.P0_12 = 1;
    IO0DIR_bit.P0_13 = 1;

⌨️ 快捷键说明

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