void RCC_Config(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1,ENABLE)
}
void GPIO_for_can_and_uart_Config(void)
{
/*定义一个GPIO_InitTypeDef类型的结构体*/
GPIO_InitTypeDef GPIO_InitStructure;
/*设置can的RX--pa.11引脚*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*设置can的TX--pa.12引脚*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*设置usart1 的RX 脚 -PA.10为父浮空输入脚*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void Can_Config(void)
{
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
CAN_DeInit(CAN1);
CAN_StructInit(&CAN_InitStructure);
CAN_InitStructure.CAN_TTCM=DISABLE;
CAN_InitStructure.CAN_ABOM=DISABLE;
CAN_InitStructure.CAN_AWUM=DISABLE;
CAN_InitStructure.CAN_NART=DISABLE;
CAN_InitStructure.CAN_RFIM=DISABLE;
CAN_InitStructure.CAN_TXFP=DISABLE;
CAN_InitStructure.CAN_Mode=CAN_Mode_LoopBack;
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1=CAN_BS1_8tq;
CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;
CAN_InitStructure.CAN_Prescaler=5;
CAN_Init(CAN1,&CAN_InitStructure);
CAN_FilterInitStructure.CAN_FilterNumber=0;
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh=0x00AA<<3;
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x00FF<<3;
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=0;
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
}
void main(void)
{
u8 TransmitMailbox=0;
CanTxMsg TxMessage;
CanRxMsg RxMessage;
RCC_Config();
GPIO_for_can_and_uart_Config();
USART_Config();
Can_Config();
TxMessage.ExtId=0x00aa0000;
TxMessage.RTR=CAN_RTR_Data;
TxMessage.IDE=CAN_ID_EXT;
TxMessage.DLC=8;
TxMessage.Data[0]=0x00;
TxMessage.Data[1]=0x12;
TxMessage.Data[2]=0x34;
TxMessage.Data[3]=0x56;
TxMessage.Data[4]=0x78;
TxMessage.Data[5]=0xab;
TxMessage.Data[6]=0xcd;
TxMessage.Data[7]=0xef;
TransmitMailbox=CAN_Transmit(CAN1,&TxMessage);
while((CAN_TransmitStatus(CAN1,TransmitMailbox))!=CANTXOK);
printf("\r\nThe CAN has send data :0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,\r\n",
TxMessage.Data[0],TxMessage.Data[1],TxMessage.Data[2],TxMessage.Data[3],
TxMessage.Data[4],TxMessage.Data[5],TxMessage.Data[6],TxMessage.Data[7],);
while((CAN_MessagePending(CAN1,CAN_FIFO0)==0));
RxMessage.StdId=0x00;
RxMessage.IDE=CAN_ID_EXT;
RxMessage.DLC=0;
RxMessage.Data[0]=0x00;
RxMessage.Data[1]=0x12;
RxMessage.Data[2]=0x34;
RxMessage.Data[3]=0x56;
RxMessage.Data[4]=0x78;
RxMessage.Data[5]=0xab;
RxMessage.Data[6]=0xcd;
RxMessage.Data[7]=0xef;
CAN_Receive(CAN1,CAN_FIFO0,&RxMessage);
printf("\r\nThe CAN has received data :0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,\r\n",
RxMessage.Data[0],RxMessage.Data[1],RxMessage.Data[2],RxMessage.Data[3],
RxMessage.Data[4],RxMessage.Data[5],RxMessage.Data[6],RxMessage.Data[7],);
while(1);
}