STM32 CAN总线应用程序,调试通过仅供参考(2)
- UID
- 1029342
- 性别
- 男
|
STM32 CAN总线应用程序,调试通过仅供参考(2)
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])
{
CanTxMsgTxMessage; //定义数据结构类型变量
u8TransmitMailbox = 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,u8TQ_A_flag,u8 TQ_B_flag)
{
u16Send_add,num;
u8 Send_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; |
|
|
|
|
|