void gp(void)
{
GPIO_InitTypeDef GPIO_Struct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOF,ENABLE);
GPIO_Struct.GPIO_Mode=GPIO_Mode_IPU;
GPIO_Struct.GPIO_Pin=GPIO_Pin_11;
GPIO_Init(GPIOA,&GPIO_Struct);
GPIO_Struct.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Struct.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Struct.GPIO_Pin=GPIO_Pin_12;
GPIO_Init(GPIOA,&GPIO_Struct);
GPIO_Struct.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Struct.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Struct.GPIO_Pin=GPIO_Pin_6;
GPIO_Init(GPIOF,&GPIO_Struct);
GPIO_SetBits(GPIOF,GPIO_Pin_6);
}
void can(void)
{
CAN_InitTypeDef CAN_Struct;
CAN_FilterInitTypeDef CAN_FilterStruct;
CanTxMsg TxMessage;
CanRxMsg RxMessage;
u8 TransmitMailbox=0;
u8 i=0;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1,ENABLE);
CAN_Struct.CAN_TTCM=DISABLE;
CAN_Struct.CAN_ABOM=DISABLE;
CAN_Struct.CAN_AWUM=DISABLE;
CAN_Struct.CAN_NART=DISABLE;
CAN_Struct.CAN_RFLM=DISABLE;
CAN_Struct.CAN_TXFP=DISABLE;
CAN_Struct.CAN_Mode=CAN_Mode_LoopBack;
CAN_Struct.CAN_SJW=CAN_SJW_1tq;
CAN_Struct.CAN_BS1=CAN_BS1_8tq;
CAN_Struct.CAN_BS2=CAN_BS2_7tq;
CAN_Struct.CAN_Prescaler=5;
CAN_Init(CAN1,&CAN_Struct);
CAN_FilterStruct.CAN_FilterNumber=0;//
CAN_FilterStruct.CAN_FilterMode=CAN_FilterMode_IdMask;//
CAN_FilterStruct.CAN_FilterScale=CAN_FilterScale_32bit;/
CAN_FilterStruct.CAN_FilterIdHigh=0x0000;//IDµÄ¸ß16λ
CAN_FilterStruct.CAN_FilterIdLow=0x0000;//IDµÄµÍ16λ
CAN_FilterStruct.CAN_FilterMaskIdHigh=0x0000;//
CAN_FilterStruct.CAN_FilterMaskIdLow=0x0000;//
CAN_FilterStruct.CAN_FilterFIFOAssignment=0;//
CAN_FilterStruct.CAN_FilterAc
tivation=ENABLE;//¼¤»î¹ýÂËÆ÷
CAN_FilterInit(&CAN_FilterStruct);
TxMessage.StdId=0x11;//ÉèÖñêʶ·û
TxMessage.RTR=CAN_RTR_DATA;//Êý¾ÝÖ¡
TxMessage.IDE=CAN_ID_STD;//±ê×¼±êʶ·û
TxMessage.DLC=2;//·¢Ë͵Ä×Ö½ÚÊý
TxMessage.Data[0]=0x33;//µÚÒ»Ö¡Êý¾Ý
TxMessage.Data[1]=0xFE;// µÚ¶þÖ¡Êý¾Ý
TransmitMailbox=CAN_Transmit(CAN1,&TxMessage);//·¢ËÍÏûÏ¢
while((CAN_TransmitStatus(CAN1,TransmitMailbox)!=CANTXOK)&&(i!=0xff))
{
i++;
}
RxMessage.StdId=0x00;//³õʼ»¯½ÓÊܹýÂËÆ÷
RxMessage.IDE=CAN_ID_STD;
RxMessage.DLC=0x00;
RxMessage.Data[0]=0x00;
RxMessage.Data[1]=0x00;
CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
if(RxMessage.Data[0]==0x33)
{
GPIO_ResetBits(GPIOF,GPIO_Pin_6);
}
}
0