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

📄 tms470r1a256_can_02.c

📁 tmp470开发板的源程序
💻 C
字号:
//------------------------------------------------------------------------------
// tms470r1a256_CAN_02.c - CAN Demo (Interrupt Version)
//
// Description: This demo code demonstrates the use of the standard CAN
// controller (SCC) in an interrupt-driven version. Multiple CAN nodes running
// this code example can be wired together, and will all toggle their LEDs when
// the button of any node is pressed.
//
// On Button press, a message with the standard msg ID of 0x400 is send out and
// the LED is toggled. When a message with the ID of 0x400 is received,
// the LED status is set according to the data field of the incoming message.
// 11-bit standard identifier messages are used.
//
//            TMS470R1A256
//      /|\ +---------------+
//       |  |               |
//       +--|PLLDIS    GIOA2|<--- Button#1
//          |               |
//          |           HET0|---> LED
//          |               |
//          |       XIN/XOUT|<--- 12MHz crystal
//          |               |
//          |         CANSTX|---> CAN network, 125kbit/s
//          |         CANSRX|<---
//          |          GIOB1|---> CAN transceiver enable
//          |               |
//          +---------------+
//
// Andreas Dannenberg / John Mangino
// Texas Instruments Inc.
// April 12th 2005
// Built with IAR Embedded Workbench Version: 4.11A
//------------------------------------------------------------------------------

#include "intrinsic.h"
#include "iotms470r1a256.h"
#include "tms470r1a256_bit_definitions.h"

unsigned int LED_State;

void CAN_Init(void);
void SCCB_irq_handler(void);
void GIOB_irq_handler(void);

void main(void)
{
  PCR = CLKDIV_1;                               // ICLK = SYSCLK = 12MHz
  PCR |= PENABLE;                               // Enable peripherals

  HETDIR = 0x813c3dd5;                          // Set HET as GIO outputs
  HETDOUT = 0x813c3dd5;                         // All LEDs off

  GIODIRB = X1;                                 // Set GIOB1 to output
  GIODCLRB = X1;                                // Output 0 on GIOB1 to enable
                                                // external CAN transceiver

  GIOENA1 = A2;                                 // Enable int for button 1
  GIOPOL1 = 0x00;                               // Rising polarity
  GIOPRY1 = 0x00;                               // Set to low priority (GIOB)
  GIOFLG1 = 0x00;                               // Clear GIO int flag register

  CAN_Init();                                   // Init standard CAN controller

  LED_State = 0;                                // Init LED status var

  REQMASK = (1 << CIM_GIOB) + (1 << CIM_SCCB);  // Enable GIOB/SCCB int mask
  __enable_interrupt();                         // Enable interrupts

  while (1)                                     // Loop forever...
  {
  }
}
//------------------------------------------------------------------------------
// Standard CAN controller initialization
//
// Sets up the CAN controller for operation at 125kbit/s. Two mailboxes are
// initialized for receiving (mailbox 0) and transmitting (mailbox 1) messages
// with a standard 11-bit ID of 0x400. Interrupts are enabled to indicate
// incoming messages.
//------------------------------------------------------------------------------
void CAN_Init(void)
{
  // Use CANTX pin for the CAN transmit functions
  CAN1TIOC = TXFUNC;

  // Use CANRX pin for the CAN receive functions
  CAN1RIOC = RXFUNC;

  // Set SCC interrupt configuration. Mailbox 0 (receive mailbox) generates
  // low-priority interrupts (int line 1 --> CIM_SCCB).
  CAN1MIL = MIL0;                               // Use int line 1 for mbox 0
  CAN1MIM = MIM0;                               // Enable int for mbox 0
  CAN1GIM = I1EN;                               // Enable int line 1

  // Setup master control register
  // Enable configuration mode, activate auto bus on after bus off condition
  CAN1MC = CCR + ABO;

  // Wait until CPU has access to CAN configuration registers
  while (!(CAN1ES & CCE));

  // Setup CAN bit timing for 125kbit/s according to CiA specifications:
  // 8us nominal bit time w/ 16TQs/bit, sample point is located at 7us (14TQ)
  // BRP = 5 (Prescaler 6, w/ ICLK = 12MHz)
  // Sample 3x, TSEG1 = 13, TSEG2 = 2, SJW = 1
  CAN1BTC = (5 << 16) + SAM + TSEG1_13 + TSEG2_2 + SJW_1;

  // Setup local acceptance mask LAM0
  // Treat all incoming MID bits as significant
  CAN1LAM0 = 0x00 << 18;

  // Configure mailbox 0 for receive
  CAN1MID0 = AME + 0x400 << 18;                 // Set ID for messages to RX,
  CAN1MCF0 = 0x00;                              // use acceptance mask LAM0
  CAN1MDL0 = 0x00;
  CAN1MDH0 = 0x00;

  // Configure mailbox 1 for transmit
  CAN1MID1 = 0x400 << 18;                       // Set ID for msgs to transmit
  CAN1MCF1 = DLC_1;                             // Send one byte
  CAN1MDL1 = 0x00;
  CAN1MDH1 = 0x00;

  CAN1MD = MD0;                                 // Use mbox 0 for RX, 1 for TX
  CAN1OPC = OPC0;                               // Protect against overwrite
  CAN1ME = ME1 + ME0;                           // Enable mailboxes 1 and 0

  // Start CAN module
  CAN1MC &= ~CCR;

  // Wait until CAN module is started
  while (CAN1ES & CCE);
}
//------------------------------------------------------------------------------
// SCCB Interrupt Handler
//
// Checks if a new CAN message was received into mailbox 0, and adjusts the
// LED status variable and the actual LED according to the message contents.
//------------------------------------------------------------------------------
void SCCB_irq_handler(void)
{
  if (CAN1GIF1 & GMIF1)                         // Msg received?
    if ((CAN1GIF1 & 0xf) == MIV1_0)             // Mailbox 0?
    {
      LED_State = CAN1MDL0 >> 24;               // Read new state from CAN msg

      if (LED_State)                            // Update LED
        HETDCLR = 0x01;                         // LED on
      else
        HETDSET = 0x01;                         // LED off

      CAN1RMP = RMP0;                           // Clear flag, new msg can be
    }                                           // received now
}
//------------------------------------------------------------------------------
// GIOB Interrupt Handler
//
// Checks if button 1 has caused the interrupt, toggles the LED status
// variable, and sends a CAN message out of mailbox 1 containing the new
// LED status. Then, the LED on the local board is updated.
//------------------------------------------------------------------------------
void GIOB_irq_handler(void)
{
  if (GIOFLG1 & A2)                             // Was button 1 pressed?
  {
    LED_State ^= 0x01;                          // Toggle status var

    CAN1ME &= ~ME1;                             // Disable mailbox 1
    CAN1MDL1 = LED_State << 24;                 // Update msg data byte D0
    CAN1ME |= ME1;                              // Re-enable mailbox 1

    CAN1TRS = TRS1;                             // Send message 1
    while (!(CAN1TA & TA1));                    // Wait for transmission end

    if (LED_State)                              // Update LED
      HETDCLR = 0x01;                           // LED on
    else
      HETDSET = 0x01;                           // LED off

    CAN1TA = TA1;                               // Clear TX ACK flag
    while (CAN1TA & TA1);                       // Wait for flag to be cleared

    for (volatile unsigned int i = 0; i < 100000; i++); // Button debounce

    GIOFLG1 = A2;                               // Clear int flag
  }
}
//------------------------------------------------------------------------------
// TMS470R1A256 Standard Interrupt Handler
//------------------------------------------------------------------------------
#pragma vector = IRQV
__irq __arm void irq_handler(void)
{
  switch ((IRQIVEC & 0xff) - 1)                 // Convert IVEC index to
  {                                             // CIM interrupt channel
    case CIM_SCCB :                             // SCC interrupt B
      SCCB_irq_handler();
      break;
    case CIM_GIOB :                             // GIO interrupt B
      GIOB_irq_handler();
      break;
  }
}

⌨️ 快捷键说明

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