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

📄 main.c

📁 freescale can bus application code using freescale mcu and codewarrior platform
💻 C
字号:
/////////////////////////////////////////////////////////////////////////////////////////
//
// Sample for Freescale EVB9S08DZ60
//
// This program must be programmed in two EVB9S08DZ60 Demo Board connected
// through the CAN bus.
//
// This program sends the potentiometer value to the other board through 
// the CAN bus and displays the value received on the PTD[7..0] LEDs.
//
// A CAN cable must connect each node.
//
// Before to run this example, verify that all jumpers are in their default
// position. The "HS/SW SEL" jumper (J501) must be in HS position
// and the define ID_TX of the second board has the value of ID_RX
// of the first one and ID_RX of the second one has the value of ID_TX
// of the first one.
//
// See the "Summary of Jumper and Connector Settings" chapter in
// the user's manual.
//
// NOTE: This example serves the only purpose of setting the CAN peripheral
// correctly, and is not intended to be a starting point for a real-world 
// CAN application.
//
// --------------------------------------------------------------------------------------
//
// This project has been written for CodeWarrior 5.0 for HC(S)08
// and uses Registers Files MC9S08DZ60.H and MC9S08DZ60.C, version 2.87.006
//         												  
// --------------------------------------------------------------------------------------
// Copyright (c) 2006 SofTec Microsystems
// http://www.softecmicro.com/
//
/////////////////////////////////////////////////////////////////////////////////////////

#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#include "mscan.h"

#define ID_TX               0x0001
#define ID_RX               0x0002

Bool can_send_enable = FALSE;

/////////////////////////////////////////////////////////////////////////////////////////
// PeriphInit
// --------------------------------------------------------------------------------------
// Initializes various registers and peripherals
/////////////////////////////////////////////////////////////////////////////////////////
void PeriphInit(void)
{
										
  // Clear COP Watchdog timeout
  SOPT1 = 0x00;
  
  // Select FBE MCG mode (IREFS=0, CLKS=10)
  MCGC1 = 0xB8;
  // LP=0 and selects external high frequency crystal clock  		                
  MCGC2 = 0x36;
  // PLLS=0
  MCGC3 = 0x00;
  // Wait until the initialization cycle of the external crystal clock is completed 
  while(!(MCGSC&0x02))
    ;
  
  // Set TJA1040 CAN transceiver in normal mode operation (STB LOW) 
  PTED = 0x00;
  PTEDD = 0x20;   
  
  //Select fBUS/2 as ADC clock source, clock divide 2 and 8 bit conversion mode
  ADCFG = 0x21;
  
  // Configures PTD port as output and power off LEDs
  PTDD = 0xFF;  
  PTDDD = 0xFF;

  // AD2 pin I/O control disabled
  APCTL1 = 0x04;
  
  // Timer1 Overflow every 5ms
  TPM1MODH = 0x27;
  TPM1MODL = 0x10;
  
  // Enable Interrupt Overflow Timer1 and 
  // select bus clock as clock source. Prescaler Divisor = 1
  TPM1SC = 0x48;
  
  (void)MSCANInit();
  EnableInterrupts;
  
}

/////////////////////////////////////////////////////////////////////////////////////////
// MAIN
/////////////////////////////////////////////////////////////////////////////////////////
void main (void)
{
  
  unsigned char potentiometer_value;
  struct can_msg msg_send, msg_get;

  PeriphInit();

  for(;;) 
  {
    // Select ADC2(PTA2) channel and continuous conversion
    ADSC1 = 0x22;
    while (!(ADSC1 & 0x80))
      ;
    potentiometer_value = ADRL;

    if(can_send_enable) 
    {
      // Fills the message fields
      msg_send.id = ID_TX;
      msg_send.data[0] = (unsigned char)(potentiometer_value);
      msg_send.len = 1;
      msg_send.RTR = FALSE;
      msg_send.prty = 0;
      // Sends a data frame message
      (void)MSCANSendMsg(msg_send);
          
      can_send_enable = FALSE;
    }
    if(MSCANCheckRcvdMsg()) 
    {
      // Checks for new messages
      if(MSCANGetMsg(&msg_get))
        // Gets new messages
        if(msg_get.id == ID_RX && !msg_get.RTR)
          // Displays data received on port D[7..0]
          PTDD = ~msg_get.data[0];
    }
  }
  
}

/////////////////////////////////////////////////////////////////////////////////////////
// Timer1_overflow
// --------------------------------------------------------------------------------------
// TIMER1 Overflow Interrupt 
/////////////////////////////////////////////////////////////////////////////////////////
interrupt void Timer1_overflow (void)
{
  
  can_send_enable = TRUE;
  
  TPM1SC &=0x7F;
    
}

⌨️ 快捷键说明

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