找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2286|回复: 4
收起左侧

stm32单片机使用hx711称重传感器数据刷新问题 附程序

[复制链接]
ID:904460 发表于 2022-4-1 21:24 | 显示全部楼层 |阅读模式
我在使用hx711模块配置成功后,每次读取完重量拿走后都会显示上一次测量结果的后几个数字,怎么让它实时刷新数据并且让后面的数字消失掉啊
求解!!!

读取东西时

读取东西时

拿走东西后

拿走东西后

/************************************************************************************
                主程序                                
*************************************************************************************/
#include "stm32f10x.h"
#include "delay.h"
#include "HX711.h"
#include "sys.h"
#include "iic.h"
#include "oled.h"
#include "word.h"
#include "usart.h"
#include "hx711.h"
u8 outstr[32];

int main(void)
{               
        Init_HX711pin();
        delay_init();
        
        NVIC_Configuration();          //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
//        uart_init(9600);         //串口初始化为9600
        
        Get_Maopi();                                //称毛皮重量
        delay_ms(1000);
        Get_Maopi();                                //重新获取毛皮重量


        OLED_Init();  //OLED显示屏初始化

        while(1)
        {
                Get_Weight();

                sprintf((char*)outstr,"重量:%dg",Weight_Shiwu);//重量显示模块
                OLED_DisplayString(0,0,16,16,outstr);  
                delay_ms(1000);
//                OLED_Refresh_Screen();

        }
}


/************************************************************************************
                HX711模块程序                                
*************************************************************************************/
#include "HX711.h"
#include "delay.h"

u32 HX711_Buffer;
u32 Weight_Maopi;
s32 Weight_Shiwu;
u8 Flag_Error = 0;

//校准参数
//因为不同的传感器特性曲线不是很一致,因此,每一个传感器需要矫正这里这个参数才能使测量值很准确。
//当发现测试出来的重量偏大时,增加该数值。
//如果测试出来的重量偏小时,减小改数值。
//该值可以为小数
#define GapValue 586


void Init_HX711pin(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;

//        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);         //使能PF端口时钟
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB , ENABLE);
        GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
        
        //HX711_SCK   PB3
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;         // 端口配置
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;                  //推挽输出
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;                 //IO口速度为50MHz
        GPIO_Init(GPIOB, &GPIO_InitStructure);                                         //根据设定参数初始化GPIOB
         
        //HX711_DOUT  PA4
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//输入上拉
    GPIO_Init(GPIOA, &GPIO_InitStructure);  
        
        GPIO_SetBits(GPIOB,GPIO_Pin_3);                                        //初始化设置为0
}



//****************************************************
//读取HX711
//****************************************************
u32 HX711_Read(void)        //增益128
{
        unsigned long count;
        unsigned char i;
          HX711_DOUT=1;
        delay_us(1);
          HX711_SCK=0;
          count=0;
          while(HX711_DOUT);
          for(i=0;i<24;i++)
        {
                  HX711_SCK=1;
                  count=count<<1;
                delay_us(1);
                HX711_SCK=0;
                  if(HX711_DOUT)
                        count++;
                delay_us(1);
        }
         HX711_SCK=1;
    count=count^0x800000;//第25个脉冲下降沿来时,转换数据
        delay_us(1);
        HX711_SCK=0;  
        return(count);
}

//****************************************************
//获取毛皮重量
//****************************************************
void Get_Maopi(void)
{
        Weight_Maopi = HX711_Read();        
}

//****************************************************
//称重
//****************************************************
void Get_Weight(void)
{
        
        HX711_Buffer = HX711_Read();
        if(HX711_Buffer > Weight_Maopi)                        
        {
                Weight_Shiwu = HX711_Buffer;
                Weight_Shiwu = Weight_Shiwu - Weight_Maopi;                                //获取实物的AD采样数值。
        
                Weight_Shiwu = (s32)((float)Weight_Shiwu/GapValue);         //计算实物的实际重量
                                                                                                                                                //因为不同的传感器特性曲线不一样,因此,每一个传感器需要矫正这里的GapValue这个除数。
                                                                                                                                                //当发现测试出来的重量偏大时,增加该数值。
                                                                                                                                                //如果测试出来的重量偏小时,减小改数值。
        }

        
}



回复

使用道具 举报

ID:904460 发表于 2022-4-1 21:29 | 显示全部楼层
之前有用到OLED_Refresh_Screen()刷新整个屏幕,但是有肉眼可见的屏幕闪烁,求其他方法
回复

使用道具 举报

ID:904460 发表于 2022-4-2 20:54 | 显示全部楼层
已解决
显示修改为sprintf((char*)outstr,"重量:%4dg",Weight_Shiwu);//重量显示模块,这样就不会出现上述情况
回复

使用道具 举报

ID:904460 发表于 2022-4-2 20:55 | 显示全部楼层
已解决,将显示重量一行改为 sprintf((char*)outstr,"重量:%4dg",Weight_Shiwu);//重量显示模块,这样在显示4位数时就不会出现上述情况
回复

使用道具 举报

ID:469589 发表于 2023-8-26 17:01 | 显示全部楼层
也可以先清屏(或清空本行)然后显示,就没有这个问题了!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表