野火stm32-CAN(Loopback)

发布时间:2024-11-12

CAN Ё 偠

E-Mail

QQ

firefirestm32@http:// STM32 ST3.0.0

偠 can 偠(Ё ) USART1 ッЁ

PB8-CAN-RX

PB9-CAN-TX

startup/start_stm32f10x_hd.c

CMSIS/core_cm3.c

CMSIS/system_stm32f10x.c

FWlib/stm32f10x_gpio.c

FWlib/stm32f10x_rcc.c

FWlib/stm32f10x_usart.c

FWlib/stm32f10x_can.c

FWlib/misc.c

: USER/main.c

USER/stm32f10x_it.c

USER/led.c

USER/usart.c

USER/can.c

CAN ->

CAN (Controller Area Network, CAN) BOSCH ISO118?8 П CAN CAN J1939 催

STM32 CPU(cpu STM32F103VET6) CAN I/O Н PB8-CAN-RX PB9-CAN-TX CAN

TJA1050 CAN CAN CAN Ё CAN CAN

STM32 ЁCAN

偠Ё CAN CAN CAN CAN TX RX

偠 ->

佪 Ё

FWlib/stm32f10x_gpio.c

FWlib/stm32f10x_rcc.c

FWlib/stm32f10x_usart.c

FWlib/stm32f10x_can.c

FWlib/misc.c

c stm32f10x_conf.hЁ 1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

14.

15.

16.

17.

18.

19.

20.

21.22.

23.

24./*Includes------------------------------------------------------------------*//*Uncommentthelinebelowtoenableperipheralheaderfileinclusion*//*#include"stm32f10x_adc.h"*//*#include"stm32f10x_bkp.h"*/#include"stm32f10x_can.h"/*#include"stm32f10x_crc.h"*//*#include"stm32f10x_dac.h"*//*#include"stm32f10x_dbgmcu.h"*//*#include"stm32f10x_dma.h"*//*#include"stm32f10x_exti.h"*//*#include"stm32f10x_flash.h"*//*#include"stm32f10x_fsmc.h"*/#include"stm32f10x_gpio.h"/*#include"stm32f10x_i2c.h"*//*#include"stm32f10x_iwdg.h"*//*#include"stm32f10x_pwr.h"*/#include"stm32f10x_rcc.h"/*#include"stm32f10x_rtc.h"*//*#include"stm32f10x_sdio.h"*//*#include"stm32f10x_spi.h"*//*#include"stm32f10x_tim.h"*/#include"stm32f10x_usart.h"/*#include"stm32f10x_wwdg.h"*/#include"misc.h"/*HighlevelfunctionsforNVICandSysTick(add-

ontoCMSISfunctions)*/

OK main

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

14.

15.

16.

17.

18.

19.

20.

21.

22.

23./***@briefMainprogram.*@paramNone*@retval:None*/intmain(void){/*configthesysclockto72M*/SystemInit();/*USART1config*/USART1_Config();/*LEDconfig*/LED_GPIO_Config();printf("\r\n CAN Ё ......\r\n");USER_CAN_Init();printf("\r\nCAN ......\r\n");USER_CAN_Test();printf("\r\nCAN ......\r\n");while(1){}}

佪 SystemInit(); 72MHZ USART1_Config(); LEDLED_GPIO_Config(); 偠Ё

LED ゴ ЁUSART1_Config(); LED_GPIO_Config();

USER_CAN_Init(); CAN I/O ッ Ё ( ㄝ CAN Ё ) can.cЁ 1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.}/** CAN_Init* * * * */voidUSER_CAN_Init(void){CAN_NVIC_Configuration();CAN_GPIO_Config(); CAN ッ Ё :

USER_CAN_Init(); CAN_NVIC_Configuration(); CAN_GPIO_Config(); Н М г can.cЁ 1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

14./*EnableCAN1RX0interruptIRQchannel*/NVIC_InitStructure.NVIC_IRQChannel=USB_LP_CAN1_RX0_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;// 0/** CAN_NVIC_Configuration * * * * */staticvoidCAN_NVIC_Configuration(void){NVIC_InitTypeDefNVIC_InitStructure; CANRX0Ё :

15.

16.

17.

18.}NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;NVIC_Init(&NVIC_InitStructure);// 0

CAN_GPIO_Config(void) Ё CAN RX TX I/O

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11./** CAN_GPIO_Config * * * * */staticvoidCAN_GPIO_Config(void){GPIO_InitTypeDefGPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOB,ENABLE); CANGPIO :

12.

13.

14.

15.

16.

17.

18.

19.

20.

21.

22.

23.

24.

25.

26.

27.}/*CAN1Periphclockenable*/RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1,ENABLE);/*ConfigureCANpin:RX*/GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;GPIO_Init(GPIOB,&GPIO_InitStructure);//PB8// /*ConfigureCANpin:TX*/GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;GPIO_Init(GPIOB,&GPIO_InitStructure);//PB9// //#defineGPIO_Remap_CANGPIO_Remap1_CAN1 偠 I/OGPIO_PinRemapConfig(GPIO_Remap1_CAN1,ENABLE);

CAN П USER_CAN_Test(); CAN Ё can.cЁ 1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

14.

15.

16.

17.

18.

19.

20.

21.

22.

23.

24.

25.

26.27.

28.

29.

30./** CAN_Test* CAN Ё * * : * */voidUSER_CAN_Test(void){/*CANtransmitat100Kb/sandreceivebypollinginloopbackmode*/TestRx=CAN_Polling();if(TestRx==FAILED){LED1(OFF);//LED1OFF}else{LED1(ON);//LED1ON;}/*CANtransmitat500Kb/sandreceivebyinterruptinloopbackmode*/TestRx=CAN_Interrupt();if(TestRx==FAILED){LED2(OFF);//LED2OFF;}else{LED2(ON);//LED2ON;}}

Ё LED Ё Ё CAN_Polling(); 100Kb/s Ё CAN_Interrupt(); 500Kb/s

CAN_Polling(); CAN_Interrupt(); г can.cЁ

1./*

2.* CAN_Polling

3.* CAN

4.*

5.* :-PASSED

6.*-FAILED

7.*

8.*/

9.TestStatusCAN_Polling(void)

10.{

11.CAN_InitTypeDefCAN_InitStructure;

12.CAN_FilterInitTypeDefCAN_FilterInitStructure;

13.CanTxMsgTxMessage;

14.CanRxMsgRxMessage;

15.uint32_ti=0;

16.uint8_tTransmitMailbox=0;

17.

18./*CANregisterinit*/

19.CAN_DeInit(CAN1);

20.CAN_StructInit(&CAN_InitStructure);

21.

22./*CANcellinit*/

23.CAN_InitStructure.CAN_TTCM=DISABLE;

24.CAN_InitStructure.CAN_ABOM=DISABLE;

25.CAN_InitStructure.CAN_AWUM=DISABLE;

26.CAN_InitStructure.CAN_NART=DISABLE;

27.CAN_InitStructure.CAN_RFLM=DISABLE;

28.

29.

30.

31.

32.

33.

34.

35.

36.

37.

38.

39.

40.

41.

42.

43.

44.

45.

46.

47.

48.

49.

50.

51.

52.

53.

54.

55.

56.

57.

58.

59.

60.

61.

62.

63.

64.

65.

66.

67.

68.

69.

70.

71.

72.

73.

74.

75.

76.

77.

78.

79.

80.

81.

82.

83.

84.

85.

86.

87.

88.

89.

90.

91.

92.

93.

94.

95.

96.}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);// // 乥 5// CAN/*CANfilterinit*/CAN_FilterInitStructure.CAN_FilterNumber=0;CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;CAN_FilterInitStructure.CAN_FilterFIFOAssignment=0;CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;CAN_FilterInit(&CAN_FilterInitStructure);/*transmit*/TxMessage.StdId=0x11;TxMessage.RTR=CAN_RTR_DATA;TxMessage.IDE=CAN_ID_STD;TxMessage.DLC=2;TxMessage.Data[0]=0xCA;TxMessage.Data[1]=0xFE;//////////// 11 29 偠 8 TransmitMailbox=CAN_Transmit(CAN1,&TxMessage);i=0;// while((CAN_TransmitStatus(CAN1,TransmitMailbox)!=CANTXOK)&&(i!=0xFF)){i++;}i=0;// while((CAN_MessagePending(CAN1,CAN_FIFO0)<1)&&(i!=0xFF)){i++;}/*receive*/RxMessage.StdId=0x00;RxMessage.IDE=CAN_ID_STD;RxMessage.DLC=0;RxMessage.Data[0]=0x00;RxMessage.Data[1]=0x00;CAN_Receive(CAN1,CAN_FIFO0,&RxMessage);if(RxMessage.StdId!=0x11){returnFAILED;}if(RxMessage.IDE!=CAN_ID_STD){returnFAILED;}if(RxMessage.DLC!=2){returnFAILED;}if((RxMessage.Data[0]<<8|RxMessage.Data[1])!=0xCAFE){returnFAILED;}//printf("receivedata:0X%X,0X%X",RxMessage.Data[0],RxMessage.Data[1]);returnPASSED;/*TestPassed*/

П STM32

Ё

Ё Control / Sdatus / Configuration Ё

1-> Tx Mailboxes( )

STM32 CANЁ 3

2->Accepttance Filters( )

STM32 CANЁ 14 / Ё 3-> Receive FIFO( FIFO )

STM32 CANЁ 2 FIFO FIFO 3 CAN 偠Ё Tx

MailboxesЁ Accepttance Filters Receive FIFO

Ё 偠 偠 偠 -> -> -> CAN_Polling(void) Ё М

1->CAN 咬

2->CAN cell ⑤

3->CAN

4-> Ё

Ё Мㄝ ⑤ 5-> Ё

ㄝㄝ ㄝ ⑤

6-> TestStatus PASSED FAILED

STM32 Ё 21ゴ< bxcan> г М 偠 М д 乬 乜 乬 г д

CAN Ё stm32f10x_it.cЁ CAN Ё 1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

14.

15.

16.

17.

18.

19.

20.

21.

22.

23.

24.

25.

26.

27.

28.

29.

30.

31./** USB_LP_CAN1_RX0_IRQHandler * USBЁ CAN Ё USB CAN I/O CAN Ё * * : * */voidUSB_LP_CAN1_RX0_IRQHandler(void){CanRxMsgRxMessage;RxMessage.StdId=0x00;RxMessage.ExtId=0x00;RxMessage.IDE=0;RxMessage.DLC=0;RxMessage.FMI=0;RxMessage.Data[0]=0x00;RxMessage.Data[1]=0x00;CAN_Receive(CAN1,CAN_FIFO0,&RxMessage);if((RxMessage.ExtId==0x1234)&&(RxMessage.IDE==CAN_ID_EXT)&&(RxMessage.DLC==2)&&((RxMessage.Data[1]|RxMessage.Data[0]<<8)==0xDECA)){ret=1;}else{ret=0;}}

偠 ->

STM32 (DC5V) JLINK ( ) ッ ッ 115200 8-N-1 LED1 LED2

偠 д ^_^

野火stm32-CAN(Loopback).doc 将本文的Word文档下载到电脑

    精彩图片

    热门精选

    大家正在看

    × 游客快捷下载通道(下载后可以自由复制和排版)

    限时特价:7 元/份 原价:20元

    支付方式:

    开通VIP包月会员 特价:29元/月

    注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
    微信:fanwen365 QQ:370150219