/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file yunhorn_sts_weight_scale.c * * @author Yunhorn (r) Technology Limited Application Team * * @brief Yunhorn (r) SmarToilets (r) Product configuration file. * ****************************************************************************** * @attention * * Copyright (c) 2024 Yunhorn Technology Limited. * Copyright (c) 2024 Shenzhen Yunhorn Technology Co., Ltd. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ #include "sts_weight_scale.h" uint32_t HX711_Buffer; volatile uint32_t gross_weight; int32_t net_weight; uint8_t Flag_Error; volatile uint32_t last_net_weight=0; volatile sts_sensor_t sts_sensor_data; void sts_weight_scale_init(void) { HX711_Init(); Get_GrossWeight(); HAL_Delay(500); Get_GrossWeight(); } void sts_weight_scale(sts_sensor_t *sts_sensor_data) { //while(1) { Get_NetWeight(); APP_LOG(TS_OFF, VLEVEL_M, "Net Weight = %d g \r\n", net_weight); sts_sensor_data->weight_scale_value=net_weight; last_net_weight = net_weight; if (((last_net_weight - net_weight) > 100) && (net_weight > 100)) // re-calibrate { Get_GrossWeight(); HAL_Delay(100); Get_GrossWeight(); //HAL_Delay(2000); } //HAL_Delay(2000); } } /************************************************************************************ *************************************************************************************/ uint8_t Flag_Error = 0; //校准参数 //因为不同的传感器特性曲线不是很一致,因此,每一个传感器需要矫正这里这个参数才能使测量值很准确。 //当发现测试出来的重量偏大时,增加该数值。 //如果测试出来的重量偏小时,减小改数值。 //该值可以为小数 //#define GapValue 106.5 #define GapValue 903.2 // for 1kg bar void HX711_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; __HAL_RCC_GPIOA_CLK_ENABLE(); GPIO_InitStruct.Pin = HX711_SCK_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; HAL_GPIO_Init(HX711_SCK_PORT, &GPIO_InitStruct); //HX711_DOUT GPIO_InitStruct.Pin = HX711_DOUT_PIN; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; HAL_GPIO_Init(HX711_DOUT_PORT, &GPIO_InitStruct); HAL_GPIO_WritePin(HX711_SCK_PORT, HX711_SCK_PIN, GPIO_PIN_RESET); } //**************************************************** //读取HX711 //**************************************************** uint32_t HX711_Read(void) //增益128 { uint32_t count; uint8_t i; //HX711_DOUT=1; HAL_GPIO_WritePin(HX711_DOUT_PORT, HX711_DOUT_PIN, GPIO_PIN_SET); delay_us(1); //HX711_SCK=0; HAL_GPIO_WritePin(HX711_SCK_PORT, HX711_SCK_PIN, GPIO_PIN_RESET); count=0; //while(HX711_DOUT); while(HAL_GPIO_ReadPin(HX711_DOUT_PORT, HX711_DOUT_PIN)==GPIO_PIN_SET); for(i=0;i<24;i++) { //HX711_SCK=1; HAL_GPIO_WritePin(HX711_SCK_PORT, HX711_SCK_PIN, GPIO_PIN_SET); count=count<<1; delay_us(1); //HX711_SCK=0; HAL_GPIO_WritePin(HX711_SCK_PORT, HX711_SCK_PIN, GPIO_PIN_RESET); //if(HX711_DOUT) if (HAL_GPIO_ReadPin(HX711_DOUT_PORT, HX711_DOUT_PIN)==GPIO_PIN_SET) count++; delay_us(1); } //HX711_SCK=1; HAL_GPIO_WritePin(HX711_SCK_PORT, HX711_SCK_PIN, GPIO_PIN_SET); count=count^0x800000;//第25个脉冲下降沿来时,转换数据 delay_us(1); //HX711_SCK=0; HAL_GPIO_WritePin(HX711_SCK_PORT, HX711_SCK_PIN, GPIO_PIN_RESET); return(count); } //**************************************************** //Get Gross Weight //**************************************************** void Get_GrossWeight(void) { gross_weight = HX711_Read(); APP_LOG(TS_OFF, VLEVEL_M, "\r\n ##### Gross Weight =%d <<<<<< \r\n",gross_weight); } //**************************************************** //Weight Scale //**************************************************** void Get_NetWeight(void) { HX711_Buffer = HX711_Read(); if(HX711_Buffer > gross_weight) { net_weight = HX711_Buffer; net_weight = net_weight - gross_weight; //获取实物的AD采样数值。 net_weight = (int32_t)((float)net_weight/GapValue); //计算实物的实际重量 //因为不同的传感器特性曲线不一样,因此,每一个传感器需要矫正这里的GapValue这个除数。 //当发现测试出来的重量偏大时,增加该数值。 APP_LOG(TS_OFF, VLEVEL_M, "\r\n ##### Net Weight =%d \r\n", net_weight);//如果测试出来的重量偏小时,减小改数值。 } } //延时nus //nus为要延时的us数. static uint8_t fac_us=0;//us延时倍乘数 static uint16_t fac_ms=0;//ms延时倍乘数 void delay_us(uint32_t nus) { fac_us=SystemCoreClock/8000000; //为系统时钟的1/8 uint32_t temp; SysTick->LOAD=nus*fac_us; //时间加载 SysTick->VAL=0x00; //清空计数器 SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ; //开始倒数 do { temp=SysTick->CTRL; } while(temp&0x01&&!(temp&(1<<16)));//等待时间到达 //bit16置一说明时间到,跳出循环 SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //关闭计数器 SysTick->VAL =0X00; //清空计数器 } //延时nms //注意nms的范围 //SysTick->LOAD为24位寄存器,所以,最大延时为: //nms<=0xffffff*8*1000/SYSCLK //SYSCLK单位为Hz,nms单位为ms //对72M条件下,nms<=1864 void delay_ms(uint16_t nms) { fac_us=SystemCoreClock/8000000; //为系统时钟的1/8 fac_ms=(uint16_t)fac_us*1000;//非ucos下,代表每个ms需要的systick时钟数 uint32_t temp; SysTick->LOAD=(uint32_t)nms*fac_ms;//时间加载(SysTick->LOAD为24bit) SysTick->VAL =0x00; //清空计数器 SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ; //开始倒数 do { temp=SysTick->CTRL; } while(temp&0x01&&!(temp&(1<<16)));//等待时间到达 SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //关闭计数器 SysTick->VAL =0X00; //清空计数器 }