当前位置:网站首页>Can bus communication application
Can bus communication application
2022-07-27 02:19:00 【Did Li fish today?】
Construction of environmental detection system for experimental production line
One 、 System composition

Two 、 The code analysis

//CAN Bus configuration
void CAN_User_Config(CAN_HandleTypeDef* hcan )
{
CAN_FilterTypeDef sFilterConfig;
HAL_StatusTypeDef HAL_Status;
sFilterConfig.FilterBank = 0; // filter 0
sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;// Mask bit mode
sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT; //32 A wide
sFilterConfig.FilterIdHigh = 0x0000; //32 position ID
sFilterConfig.FilterIdLow = 0x0000;
sFilterConfig.FilterMaskIdHigh = 0x0000; //32 position MASK
sFilterConfig.FilterMaskIdLow = 0x0000;
sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0; // The received message is put into FIFO0 in
sFilterConfig.FilterActivation = ENABLE; // Activate the filter
sFilterConfig.SlaveStartFilterBank = 0;
HAL_Status=HAL_CAN_ConfigFilter(hcan, &sFilterConfig);
HAL_Status=HAL_CAN_Start(hcan); // Turn on CAN
if(HAL_Status!=HAL_OK)
{
printf(" Turn on CAN Failure \r\n");
}
HAL_Status=HAL_CAN_ActivateNotification(hcan, CAN_IT_RX_FIFO0_MSG_PENDING);
if(HAL_Status!=HAL_OK)
{
printf(" Failed to enable pending middle section \r\n");
}
}
// start-up CAN Bus
void can_start(void)
{
HAL_CAN_Start(&hcan);
}
// stop it CAN Bus
void can_stop(void)
{
HAL_CAN_Stop(&hcan);
}
//CAN Data sending function
uint8_t Can_Send_Msg_StdId(uint16_t My_StdId,uint8_t len,uint8_t Type_Sensor)
{
CAN_TxHeaderTypeDef TxMeg;
ValueType ValueType_t;
uint8_t vol_H,vol_L;
uint16_t i=0;
uint8_t data[8];
TxMeg.StdId=My_StdId; // Standard Identifier
TxMeg.ExtId=0x00; // Set identifier extension
TxMeg.IDE=CAN_ID_STD; // Standard frame
TxMeg.RTR=CAN_RTR_DATA; // Data frame
TxMeg.DLC=len; // The length of data to be sent
for(i=0;i<len;i++)
{
data[i]=0;
}
data[0] = Sensor_Type_t;
data[3] = (uint8_t)My_StdId&0x00ff; // Take the lower two to do ID
data[4] = My_StdId>>8; // High position behind
printf("Can_Send_Msg_StdId >>My_StdId Standard frame ID= %x \r\n",My_StdId);
printf("Can_Send_Msg_StdId >>Sensor_Type_t %d \r\n",data[0]);
ValueType_t=ValueTypes(Type_Sensor);
printf("Can_Send_Msg_StdId >>ValueType_t %d \r\n",ValueType_t);
switch(ValueType_t)
{
case Value_ADC:
vol_H = (vol&0xff00)>>8;
vol_L = vol&0x00ff;
data[1]=vol_H;
data[2]=vol_L;
printf("Can_Send_Msg_StdId >> Value_ADC TxMessage.Data[1]=vol_L %d \r\n",data[1]);
printf("Can_Send_Msg_StdId >> Value_ADC TxMessage.Data[2]=vol_L %d \r\n",data[2]);
break;
case Value_Switch:
data[1]=switching;
data[2]=0;
break;
case Value_I2C:
data[1]=sensor_tem;
data[2]=sensor_hum;
printf("Can_Send_Msg_StdId >> Value_I2C TxMessage.Data[1]=vol_L %d \r\n",data[1]);
printf("Can_Send_Msg_StdId >> Value_I2C TxMessage.Data[2]=vol_L %d \r\n",data[2]);
break;
default:
break;
}
if (HAL_CAN_AddTxMessage(&hcan, &TxMeg, data, &TxMailbox) != HAL_OK)
{
printf("Can send data error\r\n");
}
else
{
printf("Can send data success\r\n");
}
return 0;
}
//CAN Interrupt callback function
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
CAN_RxHeaderTypeDef RxMeg;
uint8_t Data[8] = {0};
HAL_StatusTypeDef HAL_RetVal;
int i;
RxMeg.StdId=0x00;
RxMeg.ExtId=0x00;
RxMeg.IDE=0;
RxMeg.DLC=0;
HAL_RetVal=HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxMeg, Data);
if ( HAL_OK==HAL_RetVal)
{
for(i=0;i<RxMeg.DLC;i++)
{
Can_data[i]= Data[i];
printf("%02X ",Data[i]);
}
printf("\r\n");
flag_send_data=1;
}
}Gateway main function
while (1)
{
/* USER CODE END WHILE */
if(1)
{
Value_Type=ValueTypes(Sensor_Type_t);
switch(Value_Type)
{
case Value_ADC: // light air flame Combustible gas
sensor_number=1;
vol=Get_Voltage();
printf("vol=Get_Voltage ===== %x \r\n",vol);
break;
case Value_Switch: // human body infrared voice
sensor_number=1;
switching=Switching_Value();
printf("switching=Switching_Value== %d \r\n",switching);
break;
case Value_I2C:
sensor_number=2;
SHT1x_get_temperature(&sensor_tem); // temperature
SHT1x_get_relative_humidity(&sensor_hum); // humidity
printf("sensor_tem ===== %d :;sensor_hum===%d \r\n",(int)sensor_tem,(int)sensor_hum);
break;
default:
break;
}
// Send the sensing data of this board to the gateway
Master_To_Gateway(Can_STD_ID, Value_Type, vol, switching, sensor_hum, sensor_tem );
}
HAL_Delay(1500);
// Send from CAN The bus receives data from other nodes to the gateway
if(flag_send_data==1)
{
CAN_Master_To_Gateway( Can_data,9);
flag_send_data=0;
}
//USART1 adopt M3 The main control module configuration tool configures the type of acquisition sensor or perhaps CAN Send standard frame ID ( Note the standard frame cannot exceed 0 to 0x7FF)
if(!usart1_data_fifo_is_empty())
{
HAL_Delay(100);
process_up();
}
/* USER CODE BEGIN 3 */
}Node main function
while (1)
{
/* USER CODE END WHILE */
if(1)
{
Value_Type=ValueTypes(Sensor_Type_t);
switch(Value_Type)
{
case Value_ADC: // light air flame Combustible gas
sensor_number=1;
vol=Get_Voltage();
printf("vol=Get_Voltage ===== %x \r\n",vol);
break;
case Value_Switch: // human body infrared voice
sensor_number=1;
switching=Switching_Value();
printf("switching=Switching_Value== %d \r\n",switching);
break;
case Value_I2C:
sensor_number=2;
SHT1x_get_temperature(&sensor_tem); // temperature
SHT1x_get_relative_humidity(&sensor_hum); // humidity
printf("sensor_tem ===== %d :;sensor_hum===%d \r\n",(int)sensor_tem,(int)sensor_hum);
break;
default:
break;
}
// Send the sensing data of this board to the gateway
Master_To_Gateway(Can_STD_ID, Value_Type, vol, switching, sensor_hum, sensor_tem );
}
HAL_Delay(1500);
// Send from CAN The bus receives data from other nodes to the gateway
if(flag_send_data==1)
{
CAN_Master_To_Gateway( Can_data,9);
flag_send_data=0;
}
//USART1 adopt M3 The main control module configuration tool configures the type of acquisition sensor or perhaps CAN Send standard frame ID ( Note the standard frame cannot exceed 0 to 0x7FF)
if(!usart1_data_fifo_is_empty())
{
HAL_Delay(100);
process_up();
}
/* USER CODE BEGIN 3 */
}边栏推荐
- 7.16 written examination of Duoyi network
- 6.29 Zhong'an Summer Internship
- Gan's training skills: alchemist cultivation plan - generative confrontation network training, participation and improvement
- [MySQL] MySQL startup and shutdown commands and some error reports to solve problems
- Is index reproduction text generation image is score quantitative experiment whole process reproduction inception score quantitative evaluation experiment step on the pit and avoid the pit process
- 动态路由ofps协议配置
- Solution: various error reports and pit stepping and pit avoidance records encountered in the alchemist cultivation plan pytoch+deeplearning (I)
- HCIA(网络初级综合实验练习)
- Lora光照传感器节点数据采集
- CAN总线通信应用
猜你喜欢

STM32入门教程第一讲

动态路由rip协议实验

mgre的全连和星型拓扑实验
![[MySQL] MySQL startup and shutdown commands and some error reports to solve problems](/img/23/b4c90604eba796692fbe049d2679d1.png)
[MySQL] MySQL startup and shutdown commands and some error reports to solve problems

Experiment exercise of two-layer packaging technology (HDLC, ppp--pap\chap, GRE)

Nb-iot access to cloud platform

Timer interrupt experiment

【mysql】mysql启动关闭命令以及一些报错解决问题

NAT (network address translation protocol)

TCP's three handshakes and four waves (brief introduction)
随机推荐
ESP8266Wi-Fi接入云平台
C语言——数组、字符串处理函数、strlen、strcpy和strncpy、strcat和strncat、strcmp和strncmp
[explain C language in detail] takes you to play with functions
ensp中的简单静态路由
Beyond hidden display ellipsis
6.28 flush written test
Lora光照传感器节点数据采集
JS——初识JS、变量的命名规则,数据类型
NB-IOT接入云平台
HCIA动态路由RIP基础实验
Is index reproduction text generation image is score quantitative experiment whole process reproduction inception score quantitative evaluation experiment step on the pit and avoid the pit process
Introduction to STM32 lesson 1
Golang - sync包的使用 (WaitGroup, Once, Mutex, RWMutex, Cond, Pool, Map)
【数据库课程设计】SQLServer数据库课程设计(学生宿舍管理),课设报告+源码+数据库关系图
Golang中的错误处理
(前缀和/思维)Codeforces Round #806 (Div. 4)F. Yet Another Problem About Pairs Satisfying an Inequality
NAT network address translation experiment
Golang bufio Reader 源码详解
FID index reproduction step on the pit to avoid the pit text generation image FID quantitative experiment whole process reproduction (FR é Chet inception distance) quantitative evaluation experiment s
6.29 Zhong'an Summer Internship