本帖最后由 cx103931118cc 于 2022-5-12 14:02 编辑
void CAN_Mode_Init(void)
{
can_parameter_struct can_parameter;
can_filter_parameter_struct can_filter;
rcu_periph_clock_enable(RCU_GPIOA); //ʹÄÜPORTAʱÖÓ
rcu_periph_clock_enable(RCU_CAN0); //ʹÄÜCAN0ʱÖÓ
// rcu_periph_clock_enable(RCU_AF);
can_struct_para_init(CAN_INIT_STRUCT, &can_parameter); //Can³õʼ»¯²ÎÊý½á¹¹Ìå
can_struct_para_init(CAN_FILTER_STRUCT, &can_filter);
//
gpio_init(GPIOA, GPIO_MODE_AF_PP , GPIO_OSPEED_50MHZ, GPIO_PIN_12); //33=PA12=CAN0_TX,ÉèÖÃÍÆÍìÊä³ö
gpio_init(GPIOA, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, GPIO_PIN_11); //32=PA11=CAN0_RX,ÉèÖø¡¿ÕÊäÈë
// gpio_pin_remap_config(GPIO_CAN_PARTIAL_REMAP,DISABLE);
//Can¹ýÂËÆ÷²ÎÊý½á¹¹Ìå
//CANµ¥ÔªÉèÖÃ
can_deinit(CAN0);
can_parameter.time_triggered = DISABLE; //·Çʱ¼ä´¥·¢Í¨ÐÅģʽ
can_parameter.auto_bus_off_recovery = DISABLE; //·Ç×Ô¶¯ÀëÏ߻ָ´
can_parameter.auto_wake_up = DISABLE; //·Ç×Ô¶¯»½ÐÑ
can_parameter.no_auto_retrans = DISABLE; //·Ç×Ô¶¯ÖØ´«
can_parameter.rec_fifo_overwrite = DISABLE; //·Ç½ÓÊÕFIFOÂúʱ¸²¸Ç
can_parameter.trans_fifo_order = DISABLE; //·Ç·¢ËÍFIFO˳Ðò
can_parameter.working_mode = CAN_NORMAL_MODE; //ģʽÉèÖãºÆÕͨģʽ
//ÉèÖò¨ÌØÂÊBand=APB1(108M)/(1TQ+5TQ+3TQ)/Prescaler=108M/(1+5+3)/48=250Kpbs
can_parameter.resync_jump_width = CAN_BT_SJW_1TQ; //ÔÙͬ²½²¹³¥¿í¶È
can_parameter.time_segment_1 = CAN_BT_BS1_5TQ; //λ¶Î1
can_parameter.time_segment_2 = CAN_BT_BS2_3TQ; //λ¶Î2
can_parameter.prescaler = 48; //²¨ÌØÂÊ·ÖƵϵÊý
can_init(CAN0, &can_parameter);
//ÉèÖùýÂËÆ÷
can_filter.filter_number = 0; //¹ýÂËÆ÷0
can_filter.filter_mode = CAN_FILTERMODE_MASK; //ÆÁ±Îλģʽ
can_filter.filter_bits = CAN_FILTERBITS_32BIT; //32λ¿í
can_filter.filter_list_high = 0x0000; //32λID
can_filter.filter_list_low = 0x0000;
can_filter.filter_mask_high = 0x0000; //32λMASK
can_filter.filter_mask_low = 0x0000;
can_filter.filter_fifo_number = CAN_FIFO0; //¹ýÂËÆ÷0¹ØÁªµ½FIFO0
can_filter.filter_enable=ENABLE; //¼¤»î¹ýÂËÆ÷0
can_filter_init(&can_filter); //Â˲¨Æ÷³õʼ»¯
}
unsigned char Can_Send_Msg(unsigned char* msg,unsigned char len)
{
unsigned char mbox;
unsigned short int i=0;
can_trasnmit_message_struct TxMessage;
TxMessage.tx_sfid=0x12; //±ê×¼±êʶ·û
TxMessage.tx_efid=0x12; //ÉèÖÃÀ©Õ¹±êʾ·û
TxMessage.tx_ff=CAN_FF_STANDARD; //±ê×¼Ö¡
TxMessage.tx_ft=CAN_FT_DATA; //Êý¾ÝÖ¡
TxMessage.tx_dlen=len; //Òª·¢Ë͵ÄÊý¾Ý³¤¶È
for(i=0;i<len;i++)
TxMessage.tx_data=msg;
mbox=can_message_transmit(CAN0,&TxMessage);
i=0;
while((can_transmit_states(CAN0,mbox)==CAN_TRANSMIT_FAILED)&&(i<0XFFF))i++; //µÈ´ý·¢ËͽáÊø
if(i>=0XFFF)return 1;
return 0;
}
Res=Can_Send_Msg(Canbuf,8);
if(Res)
{
LED_D1_ON;
}
else
{
LED_D2_ON;
}
|