基于STM32的CAN总线的数据采集卡设计 联系客服

发布时间 : 星期六 文章基于STM32的CAN总线的数据采集卡设计更新完毕开始阅读

附录1

35

附录2

程序原代码:

#include \

typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus; vu32 ret; /* for return of the interrupt handling */ volatile TestStatus TestRx; ErrorStatus HSEStartUpStatus;

TestStatus CAN_Polling(void); TestStatus CAN_Interrupt(void);

void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; // LED

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure CAN pin: RX */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOB, &GPIO_InitStructure); /* Configure CAN pin: TX */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_PinRemapConfig(GPIO_Remap1_CAN1, ENABLE ); //IO脚到 PB8,PB9

}

36

重影射CAN

//系统中断管理

void NVIC_Configuration(void) { }

//配置系统时钟,使能各外设时钟 void RCC_Configuration(void) { }

//配置所有外设

SystemInit();

RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA

RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE); /* enabling interrupt */

NVIC_InitStructure.NVIC_IRQChannel=USB_LP_CAN1_RX0_IRQn;; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); #ifdef VECT_TAB_RAM

/* Set the Vector Table base location at 0x20000000 */ NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); #else /* VECT_TAB_FLASH */

/* Set the Vector Table base location at 0x08000000 */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); #endif

/* Configure the NVIC Preemption Priority Bits */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); NVIC_InitTypeDef NVIC_InitStructure;

|RCC_APB2Periph_GPIOB , ENABLE );

37

void Init_All_Periph(void) { }

int main(void) {

if (TestRx == FAILED) {

GPIO_SetBits(GPIOD, GPIO_Pin_11); } else {

GPIO_ResetBits(GPIOA, GPIO_Pin_1); } */

/* CAN transmit at 500Kb/s and receive by interrupt in loopback mode*/

TestRx = CAN_Interrupt();

/*

if (TestRx == FAILED) {

GPIO_ResetBits(GPIOA, GPIO_Pin_10); } else {

GPIO_ResetBits(GPIOA, GPIO_Pin_1); }

/* CAN transmit at 100Kb/s and receive by polling in loopback mode*/ TestRx = CAN_Polling(); Init_All_Periph(); RCC_Configuration(); GPIO_Configuration(); NVIC_Configuration();

38