原创 STM32 CAN总线应用程序,调试通过仅供参考

2019-10-15 08:37 1557 7 2 分类: MCU/ 嵌入式 文集: 测试测量

1、打开STM32 CAN系统工作时钟


RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);

2、开放系统中断,根据实际情况选择


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);

//NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX1_IRQn;
//NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 5;
//NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
//NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//NVIC_Init(&NVIC_InitStructure);

//NVIC_InitStructure.NVIC_IRQChannel = CAN1_SCE_IRQn;
//NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
//NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
//NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//NVIC_Init(&NVIC_InitStructure);

//NVIC_InitStructure.NVIC_IRQChannel = USB_HP_CAN1_TX_IRQn;
//NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 4;
//NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
//NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//NVIC_Init(&NVIC_InitStructure);

3、端口配置

//初始化MCU_CAN_RXD---PA11
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);

//初始化MCU_CAN_TXD---PA12
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);

4、CAN初始化


void CAN_Configuration(void)
{
CAN_InitTypeDefCAN_InitStructure;
CAN_FilterInitTypeDefCAN_FilterInitStructure;


CAN_DeInit(CAN1);
CAN_StructInit(&CAN_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;
CAN_InitStructure.CAN_TXFP = ENABLE;
//CAN_InitStructure.CAN_Mode = CAN_Mode_LoopBack;
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_9tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
CAN_InitStructure.CAN_Prescaler = 200;


//此处Tseg1+1 = CAN_BS1_8tp

if (CAN_Init(CAN1,&CAN_InitStructure) == CANINITFAILED) 
{


}











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);


CAN_FilterInitStructure.CAN_FilterNumber=1;
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=1;
//CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);

CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE);
CAN_ITConfig(CAN2, CAN_IT_TME, ENABLE);//打开发送中断
CAN_ITConfig(CAN2, CAN_IT_FMP1, ENABLE);//打开接收中断
CAN_ITConfig(CAN2, CAN_IT_EWG|CAN_IT_EPV|CAN_IT_BOF|CAN_IT_LEC|CAN_IT_ERR, ENABLE);//打开错误中断

//CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE);//FIFO0 消息挂号中断屏蔽
//CAN_ITConfig(CAN1,CAN_IT_TME, ENABLE);//发送邮箱空中断屏蔽
//CAN_ITConfig(CAN1,CAN_IT_BOF, ENABLE);//离线中断允许
}

5、发送数据函数


void CanWriteData(uint16_t CanAddress,uint8_t SendData[8])
{

CanTxMsg TxMessage; //定义数据结构类型变量
u8 TransmitMailbox = 0;
u16 i = 0;
TxMessage.StdId = CanAddress;
TxMessage.ExtId=0x00;//tempId 为接受板地址,应用过程中要左移13位
TxMessage.IDE=CAN_ID_STD;//使用标准id
TxMessage.RTR=CAN_RTR_DATA;//发送数据帧
TxMessage.DLC= 8; //设置发送数据的长度
TxMessage.Data[0] = SendData[0];//赋值待发送
TxMessage.Data[1] = SendData[1];//赋值待发送
TxMessage.Data[2] = SendData[2];//赋值待发送
TxMessage.Data[3] = SendData[3];//赋值待发送
TxMessage.Data[4] = SendData[4];//赋值待发送
TxMessage.Data[5] = SendData[5];//赋值待发送
TxMessage.Data[6] = SendData[6];//赋值待发送
TxMessage.Data[7] = SendData[7];//赋值待发送
TransmitMailbox = CAN_Transmit(CAN1,&TxMessage);//开始发送数据
i = 0;
//通过检查CANTXOK位来确认发送是否成功
while((CAN_TransmitStatus(CAN1,TransmitMailbox) != CANTXOK) && (i != 0xFF))
{
i++;
}
i = 0;
while((CAN_MessagePending(CAN1,CAN_FIFO0) < 1) && (i != 0xFF))//返回被挂起的报文量是否为0
{
i++;
}
}

void SEND_CAN_TQ_COMMAND(u8 device_add,u8 GFB_flag,u8 TQ_A_flag,u8 TQ_B_flag)
{
u16 Send_add,num;
u8Send_Data[8];
if(device_add == 0)
{
switch(GFB_flag)
{
case 90:
if(TQ_A_flag == 0x22)
{
TQ_CON_A = 1;
TQ_CON_B = 1;
TQ_CON_C = 1;
JDQ_A_Flag = 0x07;
Net_Node_CapA[0] = Net_Node_CapA[0]|0x07;
}
else if(TQ_A_flag == 0x11)
{
TQ_CON_A = 0;
TQ_CON_B = 0;
TQ_CON_C = 0;
JDQ_A_Flag = 0x00;
Net_Node_CapA[0] = Net_Node_CapA[0]&0x00;
}
if(TQ_B_flag == 0x22)
{
TQ_CON_2A = 1;
TQ_CON_2B = 1;
TQ_CON_2C = 1;
JDQ_B_Flag = 0x07;
Net_Node_CapB[0] = Net_Node_CapB[0]|0x07;
}
else if(TQ_B_flag == 0x11)
{
TQ_CON_2A = 0;
TQ_CON_2B = 0;
TQ_CON_2C = 0;
JDQ_B_Flag = 0x00;
Net_Node_CapB[0] = Net_Node_CapB[0]&0x00;
}
break;
case 91:
if(TQ_A_flag == 0x22)
{
TQ_CON_A = 1;
JDQ_A_Flag |= 0x04;
Net_Node_CapA[0] = Net_Node_CapA[0]|0x04;
}
else if(TQ_A_flag == 0x11)
{
TQ_CON_A = 0;
JDQ_A_Flag &= 0xFB;
Net_Node_CapA[0] = Net_Node_CapA[0]&0xFB;
}
break;
case 92:
if(TQ_A_flag == 0x22)
{
TQ_CON_A = 1;
JDQ_A_Flag |= 0x02;
Net_Node_CapA[0] = Net_Node_CapA[0]|0x02;
}
else if(TQ_A_flag == 0x11)
{
TQ_CON_A = 0;
JDQ_A_Flag &= 0xFD;
Net_Node_CapA[0] = Net_Node_CapA[0]&0xFD;
}
break;
case 93:
if(TQ_A_flag == 0x22)
{
TQ_CON_A = 1;
JDQ_A_Flag |= 0x01;
Net_Node_CapA[0] = Net_Node_CapA[0]|0x01;
}
else if(TQ_A_flag == 0x11)
{
TQ_CON_A = 0;
JDQ_A_Flag &= 0xFE;
Net_Node_CapA[0] = Net_Node_CapA[0]&0xFE;
}
break;
}
}
else
{
Send_add = 0x100 + device_add;
Send_Data[0] = 0x55;
Send_Data[1] = 0x01;
Send_Data[2] = device_add;//从机地址
Send_Data[3] = GFB_flag;//共补投入控制指令
Send_Data[4] = TQ_A_flag;
Send_Data[5] = TQ_B_flag;
num = Send_Data[1] + Send_Data[2] + Send_Data[3] + Send_Data[4] + Send_Data[5];
Send_Data[6] = num&0xFF;
Send_Data[7] = 0xAA;
CanWriteData(Send_add,Send_Data);
switch(GFB_flag)
{
case 90:
if(TQ_A_flag == 0x22)
{
Net_Node_CapA[device_add] = Net_Node_CapA[device_add]|0x07;
}
else if(TQ_A_flag == 0x11)
{
Net_Node_CapA[device_add] = Net_Node_CapA[device_add]&0x00;
}
if(TQ_B_flag == 0x22)
{
Net_Node_CapB[device_add] = Net_Node_CapB[device_add]|0x07;
}
else if(TQ_B_flag == 0x11)
{
Net_Node_CapB[device_add] = Net_Node_CapB[device_add]&0x00;
}
break;
case 91:
if(TQ_A_flag == 0x22)
{
Net_Node_CapA[device_add] = Net_Node_CapA[device_add]|0x04;
}
else if(TQ_A_flag == 0x11)
{
Net_Node_CapA[device_add] = Net_Node_CapA[device_add]&0xFB;
}
break;
case 92:
if(TQ_A_flag == 0x22)
{
Net_Node_CapA[device_add] = Net_Node_CapA[device_add]|0x02;
}
else if(TQ_A_flag == 0x11)
{
Net_Node_CapA[device_add] = Net_Node_CapA[device_add]&0xFD;
}
break;
case 93:
if(TQ_A_flag == 0x22)
{
Net_Node_CapA[device_add] = Net_Node_CapA[device_add]|0x01;
}
else if(TQ_A_flag == 0x11)
{
Net_Node_CapA[device_add] = Net_Node_CapA[device_add]&0xFE;
}
break;
}
}
}

PARTNER CONTENT

文章评论1条评论)

登录后参与讨论

curton 2019-10-16 21:06

学习了
相关推荐阅读
minicaihong 2020-10-22 16:44
场效应管放大电路的直流偏置电路详解
什么是偏置电路晶体管构成的放大器要做到不失真地将信号电压放大,就必须保证晶体管的发射结正偏、集电结反偏。即应该设置它的工作点。所谓工作点就是通过外部电路的设置使晶体管的基极、发射极和集电极处于所要求的...
minicaihong 2020-10-22 16:37
场效应管偏置电路-场效应管偏置电路的工作原理及作用分析
什么是偏置电路晶体管构成的放大器要做到不失真地将信号电压放大,就必须保证晶体管的发射结正偏、集电结反偏。即应该设置它的工作点。所谓工作点就是通过外部电路的设置使晶体管的基极、发射极和集电极处于所要求的...
minicaihong 2020-10-22 16:27
MOS管偏置电路-MOS管直流、恒流等偏置电路图文及方程详解
MOS管偏置电路MOS管直流偏置电路MOS管的单电源直流偏置电路有两种:1、只能用于结型和耗尽型MOS管的自给偏置电路。2、可用于各种MOS管的分压式偏置电路自给MOS管偏置电路(1)图解法根据图中电...
minicaihong 2020-05-27 09:50
sprintf 格式化字符串
sprintf 格式化字符串好久没写博客了,又遇到自己觉得很傻的问题,格式化字符串还要找下定义和用法sprintf() 函数把格式化的字符串写入变量中。arg1、arg2、++ 参数将被插入...
minicaihong 2020-05-25 15:07
12T和1T的单片机
标准51是12T的,就是说12个时钟周期(晶振周期,例如12M的,周期是1/12M,单位秒),机器做一个指令度周期,刚好就是1/12M*12=1uS,常见指令例如nop就是一个周期,刚好1uS,其他的...
minicaihong 2020-05-23 16:22
4HC595驱动8*8点阵屏
74HC595驱动8*8点阵屏置顶 菜袅1号 2020-05-19 10:00:03   52   收藏展开该项目使用国产M0核单片机,驱动方式类似于stm32平台;点阵驱动芯片:...
EE直播间
更多
我要评论
1
7
关闭 站长推荐上一条 /3 下一条