?? tms470r1a256_can_01.c
字號:
//------------------------------------------------------------------------------
// tms470r1a256_CAN_01.c - CAN Demo (Polling Version)
//
// Description: This demo code demonstrates the use of the standard CAN
// controller (SCC) in polling mode. 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 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
CAN_Init(); // Init standard CAN controller
LED_State = 0; // Init LED status var
while (1) // Loop forever...
{
if (CAN1RMP & RMP0) // Msg pending in 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
if (!(GIODINA & X2)) // 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
}
}
}
//------------------------------------------------------------------------------
// 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. No interrupts are used.
//------------------------------------------------------------------------------
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 global interrupt mask
CAN1GIM = 0x00;
// 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);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -