当前位置:网站首页>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 */
}边栏推荐
- Pseudo class of a element
- RS-485总线通信应用
- Dynamic routing rip protocol experiment
- Codeforces Round #809 (Div. 2), problem: (C) Qpwoeirut And The City
- C语言——关系运算符和逻辑运算符、if语句、switch语句、分支结构的嵌套
- 2022 latest live broadcast monitoring 24-hour monitoring (III) analysis of barrage in live broadcast room
- TCP的三次握手与四次断开
- 7.8 Ruijie online written examination
- 最新C语言入门与进阶 -史上最全最详细的C语言教程!! 第一节-总览C语言概括
- HCIA动态路由OSPF实验
猜你喜欢

Experiment of OSPF in mGRE environment
![[explain C language in detail] takes you to play with the choice (Branch) structure](/img/ca/7ee9f62a2478785c97684c7a0cc749.png)
[explain C language in detail] takes you to play with the choice (Branch) structure

C语言——数据类型、基本数据类型的取值范围
![[详解C语言]一文带你玩转循环结构(for_while_do-while)](/img/d9/75053297873a5b5458514e7f557cdc.png)
[详解C语言]一文带你玩转循环结构(for_while_do-while)

静态路由基础配置(IP地址的规划、静态路由的配置),实现全网可达。

数字集成电路:MOS管器件章(一)

OSPF静态大实验

Test and open basic daily question brushing (continuous updating...)

微信小程序:用户微信登录流程(附:流程图+源码)

C语言——字符和字符串、算术运算符、类型转换
随机推荐
Educational Codeforces Round 132 (Rated for Div. 2), problem: (D) Rorororobot
WAN technology experiment
6.30 didi surface warp (one side + two sides)
Static comprehensive experiment (comprehensive exercise of static route, loopback interface, default route, empty interface, floating static)
C语言——数组、字符串处理函数、strlen、strcpy和strncpy、strcat和strncat、strcmp和strncmp
C语言——while语句、dowhile语句、for循环和循环结构、break语句和continue语句
JS 99 multiplication table
OSPF configuration in mGRE environment and LSA optimization - reduce the amount of LSA updates (summary, special areas)
[详解C语言]一文带你玩转选择(分支)结构
Text to image intensive reading of paper gr-gan: gradually refine text to image generation
初识网页设计
NB-IOT接入云平台
Dynamic routing ofps protocol configuration
动态路由ofps协议配置
OSPF的重发布及路由策略
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
NAT network address translation experiment
[explain C language in detail] takes you to play with functions
JS logical operator
C语言——关系运算符和逻辑运算符、if语句、switch语句、分支结构的嵌套