STM32F103芯片CAN總線應(yīng)用程序_第1頁
STM32F103芯片CAN總線應(yīng)用程序_第2頁
STM32F103芯片CAN總線應(yīng)用程序_第3頁
STM32F103芯片CAN總線應(yīng)用程序_第4頁
STM32F103芯片CAN總線應(yīng)用程序_第5頁
已閱讀5頁,還剩1頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、/* -Copyright(a)-作者:日期:修改記錄:描述: STM32共有14組過濾器,用以對接收到的幀進(jìn)行過濾。每組過濾器包括了2個(gè)可配置的32位寄存器:CAN_FxR0和CAN_FxR1。 對于過濾器組,可以將其配置成屏蔽位模式,這樣CAN_FxR0中保存的就是標(biāo)識(shí)符匹配值,CAN_FxR1中保存的是屏蔽碼, 即CAN_FxR1中如果某一位為1, 則CAN_FxR0中相應(yīng)的位必須與收到的幀的標(biāo)志符中的相應(yīng)位吻合才能通過過濾器;CAN_FxR1中為0的位表示CAN_FxR0中的相應(yīng)位可不必與收到的幀進(jìn)行匹配。 過濾器組還可以被配置成標(biāo)識(shí)符列表模式,此時(shí)CAN_FxR0和CAN_FxR1中

2、的都是要匹配的標(biāo)識(shí)符,收到的幀的標(biāo)識(shí)符必須與其中的一個(gè)吻合才能通過過濾。 例如: CAN_FilterInitStructure.CAN_FilterNumber=0; /設(shè)置第一組過濾器 CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask; /屏蔽位模式 CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit; /32位 CAN_FilterInitStructure.CAN_FilterIdHigh=(0x0635<<5); /接收ID為63

3、5的消息 11位幀ID 標(biāo)準(zhǔn)幀因此左移5 CAN_FilterInitStructure.CAN_FilterIdLow=0x0000; CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0xffff; / CAN_FilterInitStructure.CAN_FilterMaskIdLow=0xffff; CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0; /第一組過濾器指向FIFO0 CAN_FilterInitStructure.CAN_FilterActivation=ENAB

4、LE; CAN_FilterInit(&CAN_FilterInitStructure); 濾波器配置詳細(xì)如下: 1、對擴(kuò)展數(shù)據(jù)幀進(jìn)行過濾:(只接收擴(kuò)展數(shù)據(jù)幀) CAN_FilterInitStructure.CAN_FilterIdHigh = (u32)slave_id<<3)&0xFFFF0000)>>16; CAN_FilterInitStructure.CAN_FilterIdLo=(u32)slave_id<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF; CAN_FilterInitStructu

5、re.CAN_FilterMaskIdHigh = 0xFFFF; CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0xFFFF; (注:標(biāo)準(zhǔn)幀數(shù)據(jù)幀、標(biāo)準(zhǔn)遠(yuǎn)程幀和擴(kuò)展遠(yuǎn)程幀均被過濾) 2、對擴(kuò)展遠(yuǎn)程幀過濾:(只接收擴(kuò)展遠(yuǎn)程幀) CAN_FilterInitStructure.CAN_FilterIdHigh = (u32)slave_id<<3)&0xFFFF0000)>>16; CAN_FilterInitStructure.CAN_FilterIdLow = (u32)slave_id<<3)|CAN

6、_ID_EXT|CAN_RTR_REMOTE)&0xFFFF; CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0xFFFF; CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0xFFFF; 3、對標(biāo)準(zhǔn)遠(yuǎn)程幀過濾:(只接收標(biāo)準(zhǔn)遠(yuǎn)程幀) CAN_FilterInitStructure.CAN_FilterIdHigh = (u32)slave_id<<21)&0xffff0000)>>16; CAN_FilterInitStructure.CAN_FilterIdLo

7、w = (u32)slave_id<<21)|CAN_ID_STD|CAN_RTR_REMOTE)&0xffff; CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0xFFFF; CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0xFFFF; 4、對標(biāo)準(zhǔn)數(shù)據(jù)幀過濾:(只接收標(biāo)準(zhǔn)數(shù)據(jù)幀) CAN_FilterInitStructure.CAN_FilterIdHigh = (u32)slave_id<<21)&0xffff0000)>>16; CAN_Fi

8、lterInitStructure.CAN_FilterIdLow = (u32)slave_id<<21)|CAN_ID_STD|CAN_RTR_DATA)&0xffff; CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0xFFFF; CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0xFFFF; 5、對擴(kuò)展幀進(jìn)行過濾:(擴(kuò)展幀不會(huì)被過濾掉) CAN_FilterInitStructure.CAN_FilterIdHigh = (u32)slave_id<<3)&

9、;0xFFFF0000)>>16; CAN_FilterInitStructure.CAN_FilterIdLow = (u32)slave_id<<3)|CAN_ID_EXT)&0xFFFF; CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0xFFFF; CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0xFFFC; 6、對標(biāo)準(zhǔn)幀進(jìn)行過濾:(標(biāo)準(zhǔn)幀不會(huì)被過濾掉) CAN_FilterInitStructure.CAN_FilterIdHigh = (u32)slave_

10、id<<21)&0xffff0000)>>16; CAN_FilterInitStructure.CAN_FilterIdLow = (u32)slave_id<<21)|CAN_ID_STD)&0xffff; CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0xFFFF; CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0xFFFC; 注:slave_id為要過濾的id號(hào)。*/#include "CAN.h"/*/全局變量聲明/引用

11、處extern volatile unsigned char Can_Flag; /CAN發(fā)送數(shù)據(jù)完成flag 標(biāo)志/*作者:描述:CAN 初始化、CAN波特率=PCLK1/(BRP+1)*(TS1+TS2+3)*/void Init_CAN(void) NVIC_InitTypeDef NVIC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; CAN_InitTypeDef CAN_InitStructure; CAN_FilterInitTypeDef CAN_FilterInitStructure; RCC_APB1PeriphCl

12、ockCmd(RCC_APB1Periph_CAN1, ENABLE); / CAN 口時(shí)鐘使能 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); / I/O 口時(shí)鐘使能 /* Configure CAN pin: RX PA11 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure CAN pin

13、: TX PA12*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); /* CAN register init */ CAN_DeInit(CAN1); CAN_StructInit(&CAN_InitStructure); /* CAN cell init */ CAN_InitStructure.CAN_TTCM=DISABLE; /禁止時(shí)間觸發(fā)通信模式 CAN_

14、InitStructure.CAN_ABOM=DISABLE; CAN_InitStructure.CAN_AWUM=DISABLE; /睡眠模式通過清除sleep位來喚醒 CAN_InitStructure.CAN_NART=DISABLE; /ENABLE;報(bào)文自動(dòng)重傳 CAN_InitStructure.CAN_RFLM=DISABLE; /接收溢出時(shí),F(xiàn)IFO未鎖定 CAN_InitStructure.CAN_TXFP=DISABLE; /發(fā)送的優(yōu)先級由標(biāo)示符的大小決定 CAN_InitStructure.CAN_Mode=CAN_Mode_Normal; /CAN_Mode_Loop

15、Back;/ CAN_InitStructure.CAN_SJW=CAN_SJW_1tq; /SJW=0 /波特率計(jì)算,PCLK1=36MHZ,CAN波特率=36MHZ/(2+1)*(15+6+3)=500Kbs CAN_InitStructure.CAN_BS1=CAN_BS1_16tq; /TS1=15 /波特率計(jì)算,PCLK1=36MHZ,CAN波特率=PCLK1/(BRP+1)*(TS1+TS2+3) CAN_InitStructure.CAN_BS2=CAN_BS2_7tq; /TS2=6 CAN_InitStructure.CAN_Prescaler=3; /BRP=2 9/ 45

16、=50K/ CAN_Init(CAN1,&CAN_InitStructure); /* CAN filter init */ CAN_FilterInitStructure.CAN_FilterNumber=0; /第1組過濾器 CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask; /屏蔽位模式 CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit; /32位 CAN_FilterInitStructure.CAN_FilterIdHigh=0x0

17、000; CAN_FilterInitStructure.CAN_FilterIdLow=0x0000; CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000; CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000; CAN_FilterInitStructure.CAN_FilterFIFOAssignment=0; /第一組過濾器指向FIFO0 CAN_FilterInitStructure.CAN_FilterActivation=ENABLE; CAN_FilterInit(&CA

18、N_FilterInitStructure); /* Enable CAN RX0 interrupt IRQ channel*/ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); /中斷優(yōu)先級組選擇0組使用 NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn; /USB_HP_CAN1_TX_IRQn 接收中斷使能 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; /搶占優(yōu)先級 NVIC_InitStructure.N

19、VIC_IRQChannelSubPriority = 2; /響應(yīng)優(yōu)先級 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* CAN FIFO0 message pending interrupt enable */ CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE); /使能過濾器中斷 /*作者:描述:CAN 發(fā)送 一標(biāo)準(zhǔn)幀數(shù)據(jù)*/void CAN_Send(unsigned short int SID,unsigned char data1,

20、unsigned char data2,unsigned char data3,unsigned char data4,unsigned char data5,unsigned char data6,unsigned char data7) /CAN 發(fā)送 一標(biāo)準(zhǔn)幀數(shù)據(jù) CanTxMsg TxMessage; CanRxMsg RxMessage; unsigned char TransmitMailbox; Can_Flag = 0; /* transmit 1 message */ TxMessage.StdId=SID;/0x101; /SID;/0x00; ID標(biāo)示符/ TxMessa

21、ge.ExtId=0x1234; TxMessage.RTR=CAN_RTR_DATA; /選擇數(shù)據(jù)幀 TxMessage.IDE=CAN_ID_STD; /CAN_ID_EXT;/ /選擇標(biāo)準(zhǔn)幀 TxMessage.DLC=7; TxMessage.Data0=data1; /data1; TxMessage.Data1=data2; /data2; TxMessage.Data2=data3; /data3; TxMessage.Data3=data4; /data4; TxMessage.Data4=data5; /data5; TxMessage.Data5=data6; /data6; TxMessage.Data6=data7; /data7;/ TxMessage.Data7=0x00; /data8;TransmitMailbox = CAN_Transmit(CAN1,&TxMessage); while(CAN_TransmitStatus(CAN1,TransmitMailbox) != CANTXOK); Can_Flag = 1; /* receive*/ RxMessage.StdId=0x00; RxMessage.IDE=CAN_ID_

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論