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

📄 candemo.c

📁 TQ公司的STK16x开发系统的源码
💻 C
字号:
/* File: Candemo.C */

/****************************************************************************
* 
* STK16X.MAIN.504
* ===============
*
* Main module for 167C CAN demo (for demo and testing only!).
*
* TQ-Systems GmbH
* ----------------                                                         
* Customer: TQ-Components
* Project : STK16XSW
* Tools   : uVision 2.05
*
* Rev: Date:     Name:            Modification:
* ----+---------+----------------+------------------------------------------
* 100  03.05.01  K. Zoepf        taken over from STK16X.506                
*****************************************************************************/

/****************************************************************************
*
*                             availability summary
*
* available for Starterkit: STK16X STK16XU
* conformed for Starterkit: STK16X 
* available for Modul     : TQM164   TQM165   TQM165U  TQM166  TQM167
*                           TQM167LC TQM167U  TQM167UE
* conformed for Modul     : TQM164   TQM165   TQM165U  TQM166  TQM167
*                           TQM167LC TQM167U  TQM167UE
*****************************************************************************/
                    
/*==========================================================================*
* include files (#INCLUDE):
*===========================================================================*/

/*--------------------------------------------------------------------------*
* standard include files: 
*---------------------------------------------------------------------------*/
#include <string.h>             /* string functions          */

/*--------------------------------------------------------------------------*
* project specific include files:
*---------------------------------------------------------------------------*/
#include "header.h"
#include "reg16x.h"             /* special function register */
#include "timer.h"
#include "lcd.h"
#include "led.h"
#include "can.h"

/*==========================================================================*
* module internal definitions (#DEFINE):
*===========================================================================*/

/*==========================================================================*
* module internal type declarations (TYPEDEF):
*===========================================================================*/

/*==========================================================================*
* module internal constants (CONST):
*===========================================================================*/

const char *tqmmodule[] = 
{
  "Module UNDEF",
  "Module UNDEF",
  "Module UNDEF",
  "Module UNDEF",
  "Module TQM164",
  "Module TQM165",
  "Module TQM166",
  "Module TQM167",
  "Module TQM167LC",
  "Module TQM167U",
  "Module TQM167UE",
  "Module TQM165U",
  "Module TQM167UL"
};

/*==========================================================================*
* extern available constants (CONST):
*===========================================================================*/

/*==========================================================================*
* modul internal variables:
*===========================================================================*/
ULONG idata can_msgcount;           /* counter for outgoing CAN messages */

/*==========================================================================*
* globale external available variables (EXTERN):
*===========================================================================*/

/*==========================================================================*
* modul internal functions:
*===========================================================================*/

/*--------------------------------------------------------------------------*
* int read_NMT()
*---------------------------------------------------------------------------*
* FT:
* EP: -
* RW: -
* GP: -
*---------------------------------------------------------------------------*/
int read_NMT()
{ CAN_DATA_T data;
  int        val;

  val = can_read(NMT, &data);

  if (val == FALSE) return (FALSE);

  return (TRUE);
}

/*--------------------------------------------------------------------------*
* void can_idle()
*---------------------------------------------------------------------------*
* FT:
* EP: -
* RW: -
* GP: -
*---------------------------------------------------------------------------*/
void can_idle()
{ static CAN_DATA_T pdo2_t;

  if (read_NMT() == TRUE)
  {
    pdo2_t.data[0] = 0;
    pdo2_t.data[1] = 0;
    pdo2_t.data[2] = 0;
    pdo2_t.data[3] = 0;
    pdo2_t.data[4] = (can_msgcount & 0xFF000000) >> 24;
    pdo2_t.data[5] = (can_msgcount & 0x00FF0000) >> 16;
    pdo2_t.data[6] = (can_msgcount & 0x0000FF00) >>  8;
    pdo2_t.data[7] =  can_msgcount & 0x000000FF;
    can_write(PDO2_tx, &pdo2_t);

    can_msgcount++;             //increment counter for outgoing CAN messages
  }
}

/*--------------------------------------------------------------------------*
* void can_test()
*---------------------------------------------------------------------------*
* FT: demo routine for testing CAN messages
* EP: -
* RW: -
* GP: -
*---------------------------------------------------------------------------*/ 
void can_test()
{ static CAN_DATA_T pdo1_t = {'P','D','O','1','t'};
  static CAN_DATA_T pdo2_t;
  static CAN_DATA_T sdo_t  = {'S','D','O',' ','t'};
  static CAN_DATA_T alarm  = {'A','L','A','R','M'};
  static CAN_DATA_T pdo1_r;
  static CAN_DATA_T pdo2_r;
  static CAN_DATA_T sdo_r;
  
  can_write(PDO1_tx, &pdo1_t);
  can_write(SDO_tx, &sdo_t);
  can_write(EMERGENCY, &alarm);

  pdo2_t.data[0] = 0x01;
  pdo2_t.data[1] = 0x02;
  pdo2_t.data[2] = 0x03;
  pdo2_t.data[3] = 0x04;
  pdo2_t.data[4] = 0x05;
  pdo2_t.data[5] = 0x06;
  pdo2_t.data[6] = 0x07;
  pdo2_t.data[7] = 0x08;
  can_write(PDO2_tx, &pdo2_t);
}

/*==========================================================================*
* extern available functions:
*===========================================================================*/

/*--------------------------------------------------------------------------*
* void main(void)
*---------------------------------------------------------------------------*
* FT: -
* EP: -
* RV: -
* GP: -
*---------------------------------------------------------------------------*/ 
void main()
{ int i;

  timer_init(NULL);
  lcd_init();
  led_init();
  //lcd_center(0, "TQM167 ");
  lcd_center(0, tqmmodule[TQMMOD]);  // Display Module type
  lcd_center(1, "CAN demo");

  /* start-up LED demo: */
  for (i=0; i<LED_MAX+1; i++)
  {
    led_set(i, LED_ON);
    timer_delay_10ms(8);
  }
  for (i=0; i<LED_MAX+1; i++)
  {
    led_set(i, LED_OFF);
    timer_delay_10ms(8);
  }

  can_msgcount = (ULONG) 0;     // reset counter for outgoing CAN messages
  can_init(0x00, BPS500);       // init CAN
  can_test();                   // perform CAN demo test

  /* endless main loop: */
  while(1)
  {
    can_idle();                 // answer incoming CAN messages
    timer_delay_10ms(5);
  }

}

⌨️ 快捷键说明

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