📄 can.c
字号:
/*2006-06-16*/
#include "sys2407.h"
#include "prot2407.h"
#include "ioreg.h"
void can_init(void);
void can_init(void)
{
UINT16 temp;
*( UINT16 *)MCRB=set_bit( *( UINT16 *)MCRB, MASK_6 | MASK_7); /*config the IOPC6 and IOPC7 to CANTX and CANRX*/
*CAN_IFR=0xFFFF; /*clear all the can int flag*/
*CANMCR=set_bit(*CANMCR, MASK_12); /*CCR=1*/
while ( (*GSR & MASK_4 ) !=MASK_4) continue; /*wait the CCE be 1*/
*BCR2=0x03; /*TQ=(BCR2+1)/I_clksystem */
*BCR1=set_bit( *BCR1,MASK_10 | MASK_9 | MASK_7 | MASK_5 | MASK_4 |MASK_3 | MASK_2 | MASK_0);
/*TSEG1=7 TSEG2=5 BuadRate=500K*/
*CANMCR=clr_bit(*CANMCR, MASK_12); /*CCR=0*/
while ( (*GSR & MASK_4 ) !=0) continue; /*wait the CCE be 1*/
*MDER= MASK_6; /*config MSB2 to Revieve; Unable the MSBx */
/*DATA byte order is 0 1 2 3 4 5 6 7 */
/*The module leaves the power-down mode when detecting any dominant value on the CAN bus.*/
/*.CDR=1*/
/*The mailbox to write is MBX2*/
*CANMCR= MASK_10 | MASK_9 |MASK_8 |MASK_1;
*LAM0_H=0x7FFF;
*LAM0_L=0x0FFFF;
*LAM1_H=0x7FFF;
*LAM1_L=0x0FFFF; /*the message the MSB0 1 2 3 recieved must be corpierate the identifue of MSBx*/
/*that is: the LAM register is unuseable*/
/*init the MBX2*/
/*standar identifier*/
/*No acceptence mask */
/*the identifier is 0x222 */
*MSGID2H=MASK_13 |(0x222<<2);
*MSGID2L=0xFFFF;
*MSGCTRL2=0x08; /*data length=8 */
*MBX2A=0X0000;
*MBX2B=0X0000;
*MBX2C=0X0000;
*MBX2D=0X0000;
*CANMCR=clr_bit(*CANMCR , MASK_8); /*.CDR=0*/
/*init the MBX4*/
*MSGID4H=MASK_13 |(0x444<<2);
*MSGID4L=0xFFFF;
*MSGCTRL4=0x08; /*data frame,data length=8 */
*MBX4A=0X0000;
*MBX4B=0X0000;
*MBX4C=0X0000;
*MBX4D=0X0000;
*MDER=*MDER | MASK_4 |MASK_2; /*enable the MBX2 and MBX4*/
/*low priority
enable the MIM2*/
*CAN_IMR=MASK_15 | MASK_10; /*init the int of the can*/
*CAN_IFR=0xFFFF;
}
void msgb4_send(UINT16 A,UINT16 B,UINT16 C,UINT16 D)
{
*MBX4A=A;
*MBX4B=B;
*MBX4C=C;
*MBX4D=D;
*TCR=*TCR | MASK_6;
while ( !(*TCR & MASK_14) );
*TCR=clr_bit( *TCR, MASK_14);
}
void interrupt GISR5()
{
if ( *PIVR==0x40 )
{
if ( get_bit(*CAN_IFR,MASK_10)==MASK_10 )
{
msgb4_send(*MBX2A, *MBX2B, *MBX2C, *MBX2D); /*send the data recived by MBX2*/
*RCR= set_bit(*RCR,MASK_6); /*clr the intrrupt flag*/
}
*CAN_IFR=0xFFFF;
}
else
{
ti_led_on(3);
wait_ms(20);
ti_led_off(3);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -