java如何发送指令到单片机_STM32F103ZE单片机的CAN发送程序代码

使用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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值