?? main01.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 1
#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
{
/*-----------------------------------------------
Set time slice and startup the CAN routines.
-----------------------------------------------*/
os_set_slice (1000);
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 object we'll send and recv.
-----------------------------------------------*/
#if (SENDER != 0)
can_def_obj (DEMO_MSG_ID, 8, D_SEND);
#else
can_def_obj (DEMO_MSG_ID, 8, D_REC);
#endif
/*-----------------------------------------------
Clear P4 and startup other CAN tasks.
-----------------------------------------------*/
P4 = 0x00;
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
{
unsigned char data 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, 250, NULL);
os_wait (K_TMO, 250, NULL);
os_wait (K_TMO, 250, NULL);
os_wait (K_TMO, 250, NULL);
}
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void can_receive_task (void) _task_ TASK_RECV_CAN _priority_ 0
{
struct can_message_struct xdata j;
while (1)
{
switch (can_receive (10, &j))
{
default:
break;
case C_OK:
if (j.identifier != DEMO_MSG_ID)
break;
P4 = j.c_data [0];
break;
}
can_get_status ();
}
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void status_task (void) _task_ TASK_BUS_STATUS _priority_ 2
{
while (1)
{
switch (can_get_status ())
{
default:
break;
case C_ERR_ACTIVE:
P4 ^= 0x20;
P4 &= 0x20;
break;
case C_ERR_PASSIVE:
P4 ^= 0x40;
P4 &= 0x40;
break;
case C_BUS_OFF:
P4 ^= 0x80;
P4 &= 0x80;
break;
}
os_wait (K_IVL, 250, NULL);
}
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -