经过can协议分析仪调试过的stm32的can模块收发程序

不多说了,直接上代码

主函数:main.c
#include "main.h"

int main(void)
{
 SystemInit();
 can_init();
 
 while(1)
 {

 }

}

can模块配置的子函数:can.c

#include "can.h"

void can_init(void)
{
 GPIO_InitTypeDef GPIO_InitStructure;
 NVIC_InitTypeDef NVIC_InitStructure;
 CAN_InitTypeDef CAN_InitStructure;
 CAN_FilterInitTypeDef CAN_FilterInitStructure;

 //NVIC_Configuration
 NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;     // 主优先级为0
 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;            // 次优先级为0
 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
 NVIC_Init(&NVIC_InitStructure);

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);                                   
 RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
 
 //Configure CAN pin: RX
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
 GPIO_Init(GPIOA, &GPIO_InitStructure);
 
 //Configure CAN pin: TX
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_Init(GPIOA, &GPIO_InitStructure); 

 CAN_DeInit(CAN1);
 CAN_StructInit(&CAN_InitStructure); 
 
 CAN_InitStructure.CAN_TTCM=DISABLE;           // 时间触发通信禁止
 CAN_InitStructure.CAN_ABOM=DISABLE;           // 离线退出是在中断置位清0后退出
 CAN_InitStructure.CAN_AWUM=DISABLE;           // 自动唤醒模式:清零sleep
 CAN_InitStructure.CAN_NART=DISABLE;           // 自动重新传送豹纹,知道发送成功
 CAN_InitStructure.CAN_RFLM=DISABLE;           // FIFO没有锁定,新报文覆盖旧报文
 CAN_InitStructure.CAN_TXFP=DISABLE;           // 发送报文优先级确定:标志符
 CAN_InitStructure.CAN_Mode=CAN_Mode_Normal;   // 模式: CAN_Mode_Normal / CAN_Mode_LoopBack
 CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;        // 1tq、BS1、BS2的值跟波特率有关
 CAN_InitStructure.CAN_BS1=CAN_BS1_8tq;
 CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;
 CAN_InitStructure.CAN_Prescaler=9;       // 分频系数为1
 CAN_Init(CAN1, &CAN_InitStructure);      // 初始化CAN1 
 
 CAN_FilterInitStructure.CAN_FilterNumber=0;
 CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdList;
 CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;
 CAN_FilterInitStructure.CAN_FilterIdHigh=0x014 << 5;
 CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
 CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;
 CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
 CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0;
 CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
 CAN_FilterInit(&CAN_FilterInitStructure);

 CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE); //中断使能 

void USB_LP_CAN1_RX0_IRQHandler(void)
{
 CanRxMsg RxMessage;
 
 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); 

主函数的头文件:main.h

#ifndef  _MAIN_H
#define  _MAIN_H

#include "stm32f10x.h"
#include "can.h"

#endif 

子函数的头文件:can.h

#ifndef _CAN_H
#define _CAN_H

#include "main.h" 

void can_init(void);

#endif

不明白的地方欢迎提问

永不止步步 发表于11-26 09:49 浏览65535次
分享到:

已有0条评论

暂时还没有回复哟,快来抢沙发吧

添加一条新评论

只有登录用户才能评论,请先登录注册哦!

话题作者

永不止步步
金币:67417个|学分:363741个
立即注册
畅学电子网,带你进入电子开发学习世界
专业电子工程技术学习交流社区,加入畅学一起充电加油吧!

x

畅学电子网订阅号