CAN函数
CAN相关底层API函数使用说明。
头文件的引入
#include "can.h"
CAN1初始化
//该函数用于CAN1初始化
void CAN1_Init(void);
CAN2初始化
//该函数用于CAN2初始化
void CAN2_Init(void);
CAN1数据发送
//该函数用于CAN1数据发送
//Stdid为can标识符,data为需要通过can发送的8个字节的数据包
void CAN1_Write_Data(int16_t Stdid,uint8_t * data);
CAN2数据发送
//该函数用于CAN2数据发送
//Stdid为can标识符,data为需要通过can发送的8个字节的数据包
void CAN2_Write_Data(int16_t Stdid,uint8_t * data);
CAN1数据获取
//该函数用于CAN1数据获取
///Stdid为can标识符,data为用于can接收的8字节的数组
void CAN1_Read_Data(int16_t stdid,uint8_t * data);
CAN2数据获取
//该函数用于CAN2数据获取
///Stdid为can标识符,data为用于can接收的8字节的数组
void CAN2_Read_Data(int16_t stdid,uint8_t * data);
CAN1电机控制发送
//该函数用于CAN1电机控制发送
//Stdid为电机控制标识符
//num1-num4为CAN发送的内容,根据电机ID确定哪个num给值,不用的num直接给0即可
void CAN1_Motor_Write(int16_t stdid,u16 num1,u16 num2,u16 num3,u16 num4);
CAN2电机控制发送
//该函数用于CAN2电机控制发送
//Stdid为电机控制标识符
//num1-num4为CAN发送的内容,根据电机ID确定哪个num给值,不用的num直接给0即可
void CAN2_Motor_Write(int16_t stdid,u16 num1,u16 num2,u16 num3,u16 num4);
CAN1电机数据获取
//该函数用于CAN1电机数据获取,需要输入一个变量和四个变量地址
///Stdid为电机反馈标识符,根据电机ID定
//angle为电机机械角度变量地址
//speed为电机转速变量地址
//current为电机实际转矩电流变量地址
//temperature为电机温度变量地址
void CAN1_Motor_Read(int16_t stdid,int* angle,int* speed,int* current,float* temperature)
CAN2电机数据获取
//该函数用于CAN2电机数据获取,需要输入一个变量和四个变量地址
///Stdid为电机反馈标识符,根据电机ID定
//angle为电机机械角度变量地址
//speed为电机转速变量地址
//current为电机实际转矩电流变量地址
//temperature为电机温度变量地址
void CAN2_Motor_Read(int16_t stdid,int* angle,int* speed,int* current,float* temperature)
CAN串口回调
//CAN串口通讯协议回调函数,参数对应串口通讯协议参数
void CAN_Serial_Callback(char *type, char *stdid, int motor, int control){}
CAN通讯示例
//通过can通讯传输信息
#include "can.h"//引入头文件 "can.h"
uint8_t data_send[8] = {100,100,100,100,100,100,100,100};//构建uint8_t类型数组data_send[8]用于发送can通讯信息
uint8_t data_get[8];//构建uint8_t类型数组data_get[8]用于接收can通讯信息
int data_stdid = 0x233;//构建变量data_stdid来确定can通讯标识符
void user1_main(void)
{
CAN2_Init();//初始化CAN2
while(1)
{
//通过can2发送标识符为data_stdid,发送数据包为data_send[8]
CAN2_Write_Data(data_stdid,(uint8_t*)data_send);
//通过can2接收标识符为data_stdid的数据包,用data_get[8]来接收
CAN2_Read_Data(data_stdid,data_get);
delay_ms(1);
}
}
CAN电机控制示例
//通过can通讯控制电机,并且获取电机数据
#include "can.h"//引入头文件 "can.h"
int motor_angle,motor_speed,motor_current;//构建int类型变量用于获取电机当前机械角度,速度,转矩电流
float motor_temperature;//构建float类型变量用于获取电机当前温度
void user1_main(void)
{
CAN1_Init();//初始化CAN1
while(1)
{
//通过can1控制stdid为0x200,ID为1的电机给1000的电压(电流)输出
CAN1_Motor_Write(0x200,1000,0,0,0);
//通过can1控制stdid为0x2FF,ID为1和ID为2的两个电机给1000的电压(电流)输出
CAN1_Motor_Write(0x2FF,0,1000,0,0);
//通过can1控制stdid为0x1FF,ID为1和ID为2和ID为3和ID为4的四个电机给1000的电压(电流)输出
CAN1_Motor_Write(0x1FF,1000,1000,1000,1000);
//通过输入四个全局变量的地址,来获取通过can1通讯的电机标识符为0x201的数据
CAN1_Motor_Read(0x201,&motor_angle,&motor_speed,&motor_current,&motor_temperature);
delay_ms(1);
}
}