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

📄 main.c

📁 it is a automotive application of CAN protocol one microcontroller.
💻 C
字号:
/*-----------------------------------------------------------------------------
MAIN.C
MCB517 Main C Routines

These routines initialize the CAN controller (81C90) on the Keil MCB517
evaluation board and test the controller.

1998-09-19  JCW  Initial Revision
-----------------------------------------------------------------------------*/
#include <rtx51.h>
#include <reg517.h>
#include <intrins.h>
#include <stdlib.h>

#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_ ();				\
		    }					\
		  }



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

/*-----------------------------------------------
Toggle Port 4 (the LEDs) and delay waiting for
the power supply to stabalize.  Then, Toggle
Port 4 back the way it was.
-----------------------------------------------*/
P4 ^= 0xFF;
DELAY;
P4 ^= 0xFF;

/*-----------------------------------------------
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.
-----------------------------------------------*/
switch (can_hw_init (0x23, 0x42, 0xF8, 0x00, 0x04))
  {
  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 P4 and startup other CAN tasks.
-----------------------------------------------*/
P4 = 0x00;

os_set_slice (1000);

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, 100, NULL);
  }
}

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

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

  j.identifier = DEMO_MSG_ID;
  j.c_data [0] = val;

  can_send (&j);
  can_get_status ();

  os_wait (K_TMO, 200, NULL);
  os_wait (K_TMO, 200, NULL);
  os_wait (K_TMO, 200, NULL);
  os_wait (K_TMO, 200, NULL);
  os_wait (K_TMO, 200, 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:
      P4 = j.c_data [0];
      break;
    }

  can_get_status ();
  }
}

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

⌨️ 快捷键说明

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