/*************************************************************************
CAN1_Configuration
Ãèêö£o3õê¼»ˉCAN1ÅäÖÃÎa1M2¨ìØÂê
*************************************************************************/
void CAN1_Configuration(void)
{
CAN_InitTypeDef can;
CAN_FilterInitTypeDef can_filter;
GPIO_InitTypeDef gpio;
NVIC_InitTypeDef nvic;
//ê1ÄüÏà1Øê±Öó
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//ê1ÄüPORTAê±Öó
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);//ê1ÄüCAN1ê±Öó
//3õê¼»ˉGPIO
gpio.GPIO_Pin = GPIO_Pin_11| GPIO_Pin_12;
gpio.GPIO_Mode = GPIO_Mode_AF;//¸′óÃ1|Äü
gpio.GPIO_OType = GPIO_OType_PP;//íÆíìêä3ö
gpio.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
gpio.GPIO_PuPd = GPIO_PuPd_UP;//éÏà-
GPIO_Init(GPIOA, &gpio);//3õê¼»ˉPA11,PA12
//òy½Å¸′óÃó3éäÅäÖÃ
GPIO_PinAFConfig(GPIOD,GPIO_PinSource11,GPIO_AF_CAN1); //GPIOA11¸′óÃÎaCAN1
GPIO_PinAFConfig(GPIOD,GPIO_PinSource12,GPIO_AF_CAN1); //GPIOA12¸′óÃÎaCAN1
nvic.NVIC_IRQChannel = CAN1_RX0_IRQn;
nvic.NVIC_IRQChannelPreemptionPriority = 2;
nvic.NVIC_IRQChannelSubPriority = 1;
nvic.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvic);
nvic.NVIC_IRQChannel = CAN1_TX_IRQn;
nvic.NVIC_IRQChannelPreemptionPriority = 1;
nvic.NVIC_IRQChannelSubPriority = 1;
nvic.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvic);
CAN_DeInit(CAN1);
CAN_StructInit(&can);
can.CAN_TTCM=DISABLE; //·Çê±¼ä′¥·¢í¨DÅÄ£ê½
can.CAN_ABOM=DISABLE; //èí¼t×Ô¶ˉàëÏß1üàí
can.CAN_AWUM=DISABLE;//ËˉÃßÄ£ê½í¨1yèí¼t»½DÑ(Çå3yCAN->MCRμÄSLEEPλ)
can.CAN_NART=DISABLE; //½ûÖ1±¨ÎÄ×Ô¶ˉ′«Ëí
can.CAN_RFLM=DISABLE; //±¨ÎÄ2»Ëø¶¨,DÂμĸ2¸Ç¾éμÄ
can.CAN_TXFP=ENABLE; //óÅÏ輶ó鱨Îıê궷û¾ö¶¨
can.CAN_Mode = CAN_Mode_Normal;//Ä£ê½éèÖÃ
can.CAN_SJW = CAN_SJW_1tq; //ÖØDÂí¬2½ìøÔ¾¿í¶è(Tsjw)Îatsjw+1¸öê±¼äμ¥λ CAN
can.CAN_BS1 = CAN_BS1_9tq;//Tbs1·¶Î§CAN_BS1_1tq ~CAN_BS1_16tq
can.CAN_BS2 = CAN_BS2_4tq;//Tbs2·¶Î§CAN_BS2_1tq ~ CAN_BS2_8tq
can.CAN_Prescaler = 3; //////CAN BaudRate 42/(1+9+4)/3=1Mbps
CAN_Init(CAN1, &can); // 3õê¼»ˉCAN1
can_filter.CAN_FilterNumber = 0;//1yÂËÆ÷0
can_filter.CAN_FilterMode = CAN_FilterMode_IdMask;
can_filter.CAN_FilterScale = CAN_FilterScale_32bit;//32λ
can_filter.CAN_FilterIdHigh = 0x0000;////32λID
can_filter.CAN_FilterIdLow = 0x0000;
can_filter.CAN_FilterMaskIdHigh = 0x0000;//32λMASK
can_filter.CAN_FilterMaskIdLow = 0x0000;
can_filter.CAN_FilterFIFOAssignment = 0;
can_filter.CAN_FilterActivation=ENABLE;//¼¤»î1yÂËÆ÷0
CAN_FilterInit(&can_filter);//ÂË2¨Æ÷3õê¼»ˉ
CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);
CAN_ITConfig(CAN1,CAN_IT_TME,ENABLE);
}
unsigned char can_tx_success_flag = 0;
这个程序是通过外接CAN 模块 发送数据 驱动伺服电机的, 电机没有转动,, 用F103的板载CAN模块, 和103的配套程序,就可以动
一周热门 更多>