- #include "STM32f10x.h"
- u8 CAN_Mode_Init(u8 tsjw,u8 tbs2,u8 tbs1,u16 brp,u8 mode)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- CAN_InitTypeDef CAN_InitStructure;
- CAN_FilterInitTypeDef CAN_FilterInitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO |RCC_APB2Periph_GPIOB, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
- GPIO_PinRemapConfig(GPIO_Remap1_CAN1, ENABLE);
-
- /* Configure CAN pin: RX */ // PB8
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // ÉÏÀÊäÈë
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- /* Configure CAN pin: TX */ // PB9
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // ¸´ÓÃÍÆÍìÊä³ö
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- CAN_InitStructure.CAN_TTCM=DISABLE;//·Çʱ¼ä´¥·¢Í¨ÐÅģʽ
- CAN_InitStructure.CAN_ABOM=DISABLE;//Èí¼þ×Ô¶¯ÀëÏß¹ÜÀí
- CAN_InitStructure.CAN_AWUM=DISABLE;//˯Ãßģʽ¿Éͨ¹ýÈí¼þ»½ÐÑ
- CAN_InitStructure.CAN_NART=ENABLE;//½ûÖ¹±¨ÎÄ×Ô¶¯·¢ËÍ
- CAN_InitStructure.CAN_RFLM=DISABLE;//±¨ÎIJ»Ëø¶¨£¬Ðµĸ²¸Ç¾ÉµÄ
- CAN_InitStructure.CAN_TXFP=DISABLE;//ÓÅÏȼ¶Óɱ¨Îıêʾ·û¾ö¶¨
- CAN_InitStructure.CAN_Mode=CAN_Mode_Normal ;//ģʽÉèÖãºmode 0ÆÕͨģʽ 1 »Ø»·Ä£Ê½
-
- //²¨ÌØÂÊÉèÖÃ
- CAN_InitStructure.CAN_SJW=tsjw;//ÖØÐÂͬ²½ÌøÔ¾¿í¶È
- CAN_InitStructure.CAN_BS1=tbs1;
- CAN_InitStructure.CAN_BS2=tbs2; //Tbs2=tbs2+1¸öʱ¼äµ¥Î»CAN_BS2_1tq ~ CAN_BS2_8tq
- CAN_InitStructure.CAN_Prescaler=brp; //·ÖƵϵÊý(Fdiv)Ϊbrp+1
- CAN_Init(CAN1, &CAN_InitStructure); //³õʼ»¯CAN1
- CAN_FilterInitStructure.CAN_FilterNumber=0; //¹ýÂËÆ÷0
- CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask; //ÆÁ±Îλģʽ
- CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit; //32λ¿í
- CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000; //ÓÃÀ´É趨¹ýÂËÆ÷±êʾ·û£¬32λλ¿íʱΪÆä¸ß¶Î룬16λλ¿íʱλµÚһλ
- CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;//ÓÃÀ´É趨¹ýÂËÆ÷±êʾ·û£¬32λλ¿íʱΪÆäµÍ¶Î룬16λλ¿íʱλµÚ¶þλ
- CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;//¹ýÂËÆ÷¸ß16λÿλÎÞÐè¹ýÂË£¬
- CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;//¹ýÂËÆ÷µÍ16λÿλÎÞÐè¹ýÂË
- CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0;//¹ýÂËÆ÷0¹ØÁªµ½FIFO0
- CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;//¼¤»î¹ýÂËÆ÷0[code]u8 CAN_Send_Msg(u8* msg,u8 len)
- {
- u8 mbox;
- u16 j;
- CanTxMsg TxMessage; //can·¢ËͽṹÌå
- TxMessage.StdId=0x12; // ±ê×¼±êʶ·û
- // TxMessage.ExtId=0x00; // ÉèÖÃÀ©Õ¹±êʾ·û
- TxMessage.IDE=CAN_Id_Standard; // ±ê×¼Ö¡
- TxMessage.RTR=CAN_RTR_Data; // Êý¾ÝÖ¡
- TxMessage.DLC=len; // Òª·¢Ë͵ÄÊý¾Ý³¤¶È
- for(j=0;j<len;j++)
- TxMessage.Data[j]=msg[j];//дÈëÊý¾Ý
- //CAN_Transmit(CAN1, &TxMessage);
- mbox=CAN_Transmit(CAN1, &TxMessage);
- j=0;
- while((CAN_TransmitStatus(CAN1,mbox)==CAN_TxStatus_Failed)&&(j<0XFFF))//µÈ´ý·¢ËͽáÊø
- j++;
- if(j>0XFFF)
- return 1;
- return 0;
- }
- u8 CAN_Receive_Msg(u8 *buf)
- {
- u32 i;
- CanRxMsg RxMessage;
- if(CAN_MessagePending(CAN1,CAN_FIFO0)==0)
- return 0;//ûÓнÓÊÕµ½Êý¾Ý
- CAN_Receive(CAN1,CAN_FIFO0,&RxMessage);//¶ÁÈ¡Êý¾Ý
- for(i=0;i<8;i++)//°Ñ8¸öÊý¾Ý·ÅÔÚ²ÎÊýÊý×éÀï
- {
- buf[i]=RxMessage.Data[i];
- }
- return RxMessage.DLC;//·µ»ØÊý¾ÝÀàÐÍ
-
- }
- //ÖжϽÓÊÕº¯Êý
- void USB_LP_CAN1_RX0_IRQHandler(void)
- {
- u8 box;
- CanRxMsg RxMessage;
- if(CAN_GetITStatus(CAN1,CAN_IT_FMP0)!=RESET)
- {
- CAN_ClearITPendingBit(CAN1,CAN_IT_FMP0);//Çå³ýÖжϱê־λ
- CAN_Receive(CAN1,CAN_FIFO0,&RxMessage);
- }
- }
- int main(void)
- {
- u8 i;
- u8 cnt=4;
- u8 canbuf[8];
- SystemInit();
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//ÉèÖÃÖжÏÓÅÏȼ¶·Ö×éΪ×é2£º2λÇÀÕ¼ÓÅÏȼ¶£¬2λÏìÓ¦ÓÅÏȼ¶
- CAN_Mode_Init(CAN_SJW_1tq,CAN_BS2_8tq,CAN_BS1_9tq,4,CAN_Mode_Normal );//CAN³õʼ»¯»·»Øģʽ,²¨ÌØÂÊ500Kbps
- //²¨ÌØÂʵļÆË㣺 Ò»¹²1+8+9=18¸öʱ¼äµ¥Ôª£¬canʱÖÓƵÂÊΪ36mhz£¬4·ÖƵ
- //ËùÒÔ:²¨ÌØÂÊ=36/4/18=500kbps;
- for(i=0;i<8;i++)
- canbuf[i]=cnt+i;
- while(1)
- {
-
- CAN_Send_Msg(canbuf,8);
-
- }
-
- }
复制代码
CAN_FilterInit(&CAN_FilterInitStructure); //Â˲¨Æ÷³õʼ»¯
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // Ö÷ÓÅÏȼ¶Îª1(ÇÀÕ¼ÓÅÏȼ¶)
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // ´ÎÓÅÏȼ¶Îª0(ÏìÓ¦ÓÅÏȼ¶)
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE); //FIFO0ÏûÏ¢¹ÒºÅÖжÏÔÊÐí.
return 0;
}
[/code]
上传上去注释都乱码了。。请各位帮我看下,问题出在哪里了,我实在找不出来,多谢!
一周热门 更多>