📄 b_canx1.c
字号:
/******************************************************************************
C167CR CAN Ap. Note project
Main - b_canx1
This is an example program showing how to use the CAN interface on the Keil
MCB167 evaluation board.
Copyright (c) 1997 Keil Software
******************************************************************************/
#include <stdio.h>
#include <intrins.h>
#include "can_ifc.h"
#include "can_msgs.h"
#define TIME_MO 1 /* message object number for time message */
/******************************************************************************
Setup CAN controller
Returns: nothing
-----------------------------------------------------------------------------*/
void
setup_can(void)
{
begin_can_init();
/* These mask values tell the CAN controller that all bits in a
message id. are significant when comparing the id of a received message
to the id's in the arbitration registers of the message objects.
*/
CAN_MASK_SHORT = 0xffff;
CAN_UMASK_LONG = 0xffff;
CAN_LMASK_LONG = 0xf8ff;
/* Since this program doesn't use message object 15, it is unnecessary
to initialize the 'mask of last message' registers (CAN_UMASK_LAST and
CAN_LMASK_LAST).
*/
CAN_MSGOBJ[TIME_MO].msg_ctl = MSGVAL_CLR;
CAN_MSGOBJ[TIME_MO].arbitr = ARBITR(CAN_TIME_MSG);
CAN_MSGOBJ[TIME_MO].msg_cfg = MSG_CFG(LEN_CAN_TIME_MSG, CANDIR_RECEIVE, 0);
CAN_MSGOBJ[TIME_MO].msg_ctl =
/* clear bits set bits */
INTPND_CLR &
RXIE_CLR &
TXIE_CLR &
MSGVAL_SET &
NEWDAT_CLR &
MSGLST_CLR &
TXRQ_CLR &
RMTPND_CLR;
/* CAN_IE_ must be set for any CAN interrupts to occur.
CAN_EIE_ must be set for CAN error status change interrupts to occur.
*/
end_can_init(CAN_IE_ | CAN_EIE_);
}
/******************************************************************************
main
What this program does:
Setup the CAN interface, with a message object for receiving time message.
Loop forever {
Transmit time message remote frame.
Wait for time message to be received.
Process time message.
}
Returns: never
-----------------------------------------------------------------------------*/
void
main(void)
{
unsigned long time_value;
unsigned long last_printed_time_value = 0ul - 1000ul;
printf ("Program B start\n");
/* Set up CAN interface */
setup_can();
/* Main loop */
while (1) {
/* Request time message.
We're going to detect receipt of the message by examining NEWDAT
(there are other ways), so also clear NEWDAT to make sure we process
a new time message, not an old one that has been waiting around
unprocessed.
*/
CAN_MSGOBJ[TIME_MO].msg_ctl = TXRQ_SET & NEWDAT_CLR;
/* Wait for updated time message
Note: Doing it this way, the program will hang here forever if the
message is not received.
*/
while (!(CAN_MSGOBJ[TIME_MO].msg_ctl & NEWDAT_)) ;
/* Process the time message */
copy_received_can_message(TIME_MO, &time_value);
/* If this time message is at least one second later than the last one
that was output then output it.
*/
if (time_value - last_printed_time_value >= 1000) {
printf("%lu\n", time_value);
last_printed_time_value = time_value;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -