This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:examples:can:arm [2010/03/14 14:11] – mikk.leini | en:examples:can:arm [2010/03/15 08:40] (current) – removed mikk.leini | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Simple CAN ====== | ||
| - | ===== Practice ===== | ||
| - | |||
| - | <code c> | ||
| - | // | ||
| - | // | ||
| - | // CAN ping-pong demo | ||
| - | // | ||
| - | // This program does not use CAN interrupts. | ||
| - | // | ||
| - | // Copyright (c) 2009 TUT Department of Mechatronics | ||
| - | // | ||
| - | // | ||
| - | |||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | |||
| - | // | ||
| - | // | ||
| - | // Configuration. | ||
| - | // | ||
| - | // | ||
| - | #define CAN_BITRATE | ||
| - | #define CAN_MSG_ID | ||
| - | #define CAN_MSG_OBJ_RX | ||
| - | #define CAN_MSG_OBJ_TX | ||
| - | #define PING_START_COUNT | ||
| - | #define PING_PERIOD | ||
| - | |||
| - | // | ||
| - | // | ||
| - | // Ping command. Send CAN TX frame. | ||
| - | // | ||
| - | // | ||
| - | void Ping(unsigned long ulCount) | ||
| - | { | ||
| - | tCANMsgObject psPingMsg; | ||
| - | |||
| - | // | ||
| - | // Setup ping message object. | ||
| - | // | ||
| - | psPingMsg.ulMsgID | ||
| - | psPingMsg.ulFlags | ||
| - | psPingMsg.ulMsgLen | ||
| - | psPingMsg.pucMsgData = (unsigned char *)& | ||
| - | |||
| - | // | ||
| - | // Set message. | ||
| - | // | ||
| - | CANMessageSet(CAN0_BASE, | ||
| - | } | ||
| - | |||
| - | // | ||
| - | // | ||
| - | // Main function. | ||
| - | // | ||
| - | // | ||
| - | int main(void) | ||
| - | { | ||
| - | tCANMsgObject psEchoMsg; | ||
| - | tBoolean bButtonState, | ||
| - | unsigned char pucRxData[8]; | ||
| - | unsigned long ulPingCount = 0; | ||
| - | |||
| - | // | ||
| - | // Set the clocking to run from the PLL at 50MHz | ||
| - | // | ||
| - | SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | | ||
| - | | ||
| - | |||
| - | // | ||
| - | // Enable peripherals. | ||
| - | // | ||
| - | SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); | ||
| - | SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC); | ||
| - | SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); | ||
| - | SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); | ||
| - | SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0); | ||
| - | |||
| - | // | ||
| - | // Configuring. | ||
| - | // | ||
| - | DeviceConfigure(); | ||
| - | RGBLEDConfigure(); | ||
| - | |||
| - | // | ||
| - | // Configure CAN pins. | ||
| - | // | ||
| - | GPIOPinTypeCAN(GPIO_PORTD_BASE, | ||
| - | |||
| - | // | ||
| - | // Enable the CAN controller. | ||
| - | // | ||
| - | CANInit(CAN0_BASE); | ||
| - | CANBitRateSet(CAN0_BASE, | ||
| - | CANEnable(CAN0_BASE); | ||
| - | |||
| - | // | ||
| - | // Create echo message object. | ||
| - | // | ||
| - | psEchoMsg.ulMsgID | ||
| - | psEchoMsg.ulMsgIDMask = 0xFF; | ||
| - | psEchoMsg.ulFlags | ||
| - | psEchoMsg.pucMsgData | ||
| - | |||
| - | // | ||
| - | // Set message object. | ||
| - | // | ||
| - | CANMessageSet(CAN0_BASE, | ||
| - | |||
| - | // | ||
| - | // Enable processor interrupts. | ||
| - | // | ||
| - | IntMasterEnable(); | ||
| - | |||
| - | // | ||
| - | // Loop forever. | ||
| - | // | ||
| - | while (1) | ||
| - | { | ||
| - | // | ||
| - | // Get button state. | ||
| - | // | ||
| - | bButtonState = ButtonIsPressed(); | ||
| - | |||
| - | // | ||
| - | // Button has been pressed down ? | ||
| - | // | ||
| - | if (bButtonState && !bButtonOldState) | ||
| - | { | ||
| - | // | ||
| - | // First ping. | ||
| - | // | ||
| - | Ping(PING_START_COUNT); | ||
| - | |||
| - | // | ||
| - | // Reset countdown. | ||
| - | // | ||
| - | CountDownReset(); | ||
| - | } | ||
| - | |||
| - | // | ||
| - | // Remember last button state. | ||
| - | // | ||
| - | bButtonOldState = bButtonState; | ||
| - | |||
| - | // | ||
| - | // CAN message received ? | ||
| - | // | ||
| - | if (CANStatusGet(CAN0_BASE, | ||
| - | { | ||
| - | // | ||
| - | // Get the message. | ||
| - | // | ||
| - | CANMessageGet(CAN0_BASE, | ||
| - | |||
| - | // | ||
| - | // Check message length. | ||
| - | // | ||
| - | if (psEchoMsg.ulMsgLen == 4) | ||
| - | { | ||
| - | // | ||
| - | // Get ping count and decrease it. | ||
| - | // | ||
| - | ulPingCount = *((unsigned long *)psEchoMsg.pucMsgData); | ||
| - | |||
| - | // | ||
| - | // Light up RGB LED. | ||
| - | // | ||
| - | RGBLEDSetRed(ulPingCount * 255 / PING_START_COUNT); | ||
| - | |||
| - | // | ||
| - | // Start countdown timer. | ||
| - | // | ||
| - | CountDownStartMS(PING_PERIOD); | ||
| - | } | ||
| - | } | ||
| - | |||
| - | // | ||
| - | // Countdown ended ? | ||
| - | // | ||
| - | if (CountDownHasEnded()) | ||
| - | { | ||
| - | // | ||
| - | // Turn off the LED. | ||
| - | // | ||
| - | RGBLEDSetRed(0); | ||
| - | |||
| - | // | ||
| - | // Send ping if any bounces left. | ||
| - | // | ||
| - | if (ulPingCount > 0) | ||
| - | { | ||
| - | // | ||
| - | // Ping with decreased count. | ||
| - | // | ||
| - | Ping(--ulPingCount); | ||
| - | } | ||
| - | |||
| - | // | ||
| - | // Reset countdown. | ||
| - | // | ||
| - | CountDownReset(); | ||
| - | } | ||
| - | } | ||
| - | } | ||
| - | </ | ||