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

📄 main.c

📁 RTX51 CAN Program for the Infineon C505C, This program demonstrates how to transmit and receive CAN
💻 C
字号:
/*-----------------------------------------------------------------------------
MAIN.C
MCB515 Main C Routines

These routines initialize the on-chip CAN controller on the Phytec kitCON-515C
or kitCON-505C evaluation board and test the controller.

Be sure that the TARGET_PROC and SENDER macros are set the way you need them!

1998-09-19  JCW  Initial Revision (for Keil MCB-517 board)
1999-02-08  GHC  Phytec kitCON-515C version
1999-02-08  GHC  Added TARGET_PROC macro & conditional compilation for 505C/515C
-----------------------------------------------------------------------------*/

#define TARGET_PROC 505 /* 505 or 515 */

#if TARGET_PROC == 505
sfr XPAGE = 0x91;
#endif

#include <rtx51.h>
#include <reg517.h>
#include <intrins.h>
#include <stdlib.h>

#define EXTEND 1
#include "rtxcan.h"
#include "mcb517.h"

#define SENDER			0
#define DEMO_MSG_ID		456
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
#define DELAY						\
		  {					\
		  volatile unsigned int data i;		\
		  for (i = 0; i < 20000; i++)		\
		    {					\
		    _nop_ ();				\
		    }					\
		  }

/*-----------------------------------------------------------------------------
This function displays a value on the LEDs.
0 bits turn the LED off.
1 bits turn the LED on.
Bits 6 and 7 are not output.

The LEDs on the Phytec kitCON-515C board are on port 4.  The CAN interface
uses bits 6 and 7 of port 4, so those bits must not be written while using CAN.
-----------------------------------------------------------------------------*/
void
output(
  unsigned char value)
{
#if TARGET_PROC == 515
value ^= 0xff;  /* The LEDs are wired so that 0==on,1==off, which is the
                   opposite of what we want.  Therefore, we invert all the
                   bits before output.
                */
P4 &= (value | 0xc0);
P4 |= (value & 0x3f);
#else
value += 0;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
#endif
}

/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void main (void)
{

/*-----------------------------------------------
Change the LEDs and delay waiting for
the power supply to stabilize.  Then, change the
LEDs again.
-----------------------------------------------*/
output(0x38);
DELAY;
output(0x07);

#if TARGET_PROC == 505
/* Put the CAN controller where the RTX CAN routines expect it. */
XPAGE = 0xF7;
#endif

/*-----------------------------------------------
Startup the RTOS.
-----------------------------------------------*/
os_start_system (TASK_STARTUP);

while (1)
  {
  /*** Do something here to indicate start-up failure ***/
  }
}

/*-----------------------------------------------------------------------------
System setup TASK.  This task initializes the RTOS and CAN stuff and then
kills itself.
-----------------------------------------------------------------------------*/
void startup_task (void) _task_ TASK_STARTUP
{
/*-----------------------------------------------
Startup the CAN routines.
-----------------------------------------------*/
switch (can_task_create ())
  {
  case C_OK:
    break;

  default:
    while (1)
      {
      /*** Do something here to indicate CAN start-up failure ***/
      }
    break;
  }

/*-----------------------------------------------
Initialize the CAN hardware for 1mbit @ 16MHz.
-----------------------------------------------*/
#if TARGET_PROC == 505
#define CANTIMING1 0x23
#endif
#if TARGET_PROC == 515
#define CANTIMING1 0x34
#endif
switch (can_hw_init (0x80, CANTIMING1, 0x00, 0x00, 0x00))
  {
  case C_OK:
    break;

  default:
    while (1)
      {
      /*** Do something here to indicate CAN hardware init failure ***/
      }
    break;
  }

/*-----------------------------------------------
Define all of the CAN objects we'll send and recv.
-----------------------------------------------*/
#if (SENDER != 0)
  can_def_obj (DEMO_MSG_ID, 1, D_SEND);
#else
  can_def_obj (DEMO_MSG_ID, 1, D_REC);
#endif

/*-----------------------------------------------
Clear LEDs and startup other CAN tasks.
-----------------------------------------------*/
output(0);

#if TARGET_PROC == 505
os_set_slice (2667);    /* 4 ms */
#define DECISECOND 25
#else
os_set_slice (1000);    /* 1 ms */
#define DECISECOND 100
#endif

can_start ();

#if (SENDER != 0)
  os_create_task (TASK_XMIT_CAN);
#else
  os_create_task (TASK_RECV_CAN);
#endif

//os_create_task (TASK_BUS_STATUS);
//os_create_task (TASK_RECV_CAN);

/*-----------------------------------------------
Kill the current task, but delay in case the
task didn't get killed.
-----------------------------------------------*/
os_delete_task (os_running_task_id ());

while (1)
  {
  os_wait (K_TMO, DECISECOND, NULL);
  }
}

/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void can_transmit_task (void) _task_ TASK_XMIT_CAN _priority_ 0
{
register data unsigned char val = 0;
struct can_message_struct xdata j;
unsigned char i;

/*-----------------------------------------------
Transmit message 456 and delay for 1 second.
-----------------------------------------------*/
while (1)
  {
  val += 1;
  output(val);

  j.identifier = DEMO_MSG_ID;
  for (i=0; i<8; i++) { j.c_data [i] = val; }

  can_send (&j);
  can_get_status ();

  os_wait (K_TMO, 2 * DECISECOND, NULL);
  os_wait (K_TMO, 2 * DECISECOND, NULL);
  os_wait (K_TMO, 2 * DECISECOND, NULL);
  os_wait (K_TMO, 2 * DECISECOND, NULL);
  os_wait (K_TMO, 2 * DECISECOND, NULL);
  }
}

/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void can_receive_task (void) _task_ TASK_RECV_CAN _priority_ 1
{
struct can_message_struct xdata j;

can_bind_obj (DEMO_MSG_ID);

while (1)
  {
  switch (can_wait (5, &j))
    {
    default:
      break;

    case C_OK:
      output(j.c_data [0]);
      break;
    }

  can_get_status ();
  }
}

/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/

⌨️ 快捷键说明

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