使用Mikro C for ARM开发工具
//PLL 72M APB CLK36M
sbit KKEY1 at GPIOE_IDR.B0;
sbit KKEY2 at GPIOE_IDR.B2;
sbit KOUT1 at GPIOE_ODR.B4;
unsigned char flag1,flag2;
unsigned long Can_Init_Flags;
unsigned char Can_Send_Flags, Can_Rcv_Flags; // can flags
unsigned char tx_flags;
unsigned char Rx_Data_Len; // received data length in bytes
volatile char RxTx_Data[8]; // can rx/tx data buffer
volatile char RxTx_Data1[8];
char Msg_Rcvd; // reception flag
long ID_1st = 12111, ID_2nd = 3; // node IDs
long Rx_ID;
const unsigned int SJW = 2;
const unsigned int BRP = 4;
const unsigned int PHSEG1 = 7;
const unsigned int PHSEG2 = 2;
const unsigned int PROPSEG = 8;
const unsigned int CAN_CONFIG_FLAGS =
_CAN_CONFIG_AUTOMATIC_RETRANSMISSION &
_CAN_CONFIG_RX_FIFO_NOT_LOCKED_ON_OVERRUN &
_CAN_CONFIG_TIME_TRIGGERED_MODE_DISABLED &
_CAN_CONFIG_TX_FIFO_PRIORITY_BY_IDINTIFIER &
_CAN_CONFIG_WAKE_UP;
void can_write(void)
{
CAN1Write(ID_1st, RxTx_Data, 8, Can_Send_Flags);
}
void init_all(void)
{
CAN1InitializeAdvanced(SJW, BRP, PHSEG1, PHSEG2, PROPSEG, CAN_CONFIG_FLAGS, &_GPIO_MODULE_CAN1_PA11_12);
CAN1SetOperationMode(_CAN_OperatingMode_Initialization); // set CONFIGURATION mode
CANSetFilterScale32(0, _CAN_FILTER_ENABLED & _CAN_FILTER_ID_MASK_MODE & _CAN_FILTER_STD_MSG, ID_1st, -1);
CAN1SetOperationMode(_CAN_OperatingMode_Normal); // set NORMAL mode
Can_Send_Flags = _CAN_TX_STD_FRAME & // with CANWrite
_CAN_TX_NO_RTR_FRAME;
}
void can_write1(void)
{
CAN1Write(ID_1st, RxTx_Data, 8, Can_Send_Flags);
}
void main() {
GPIO_Digital_Input(&GPIOE_BASE, _GPIO_PINMASK_0);
GPIO_Digital_Input(&GPIOE_BASE, _GPIO_PINMASK_2);
GPIO_Digital_Output(&GPIOE_BASE, _GPIO_PINMASK_4);
flag1=1;
flag2=1;
init_all();
while (1) {
if(KKEY1==0)
{
KOUT1=0;
RxTx_Data[0]=0x39;
RxTx_Data[1]=0x39;
RxTx_Data[2]=0x39;
RxTx_Data[3]=0x39;
RxTx_Data[4]=0x39;
RxTx_Data[5]=0x39;
RxTx_Data[6]=0x39;
RxTx_Data[7]=0x39;
}
if(KKEY1==1)
{
KOUT1=1;
RxTx_Data[0]=0x30;
RxTx_Data[1]=0x30;
RxTx_Data[2]=0x30;
RxTx_Data[3]=0x30;
RxTx_Data[4]=0x30;
RxTx_Data[5]=0x30;
RxTx_Data[6]=0x30;
RxTx_Data[7]=0x30;
}
//can_write();
can_write1();
}
}