|
我在使用hx711模塊配置成功后,每次讀取完重量拿走后都會(huì)顯示上一次測(cè)量結(jié)果的后幾個(gè)數(shù)字,怎么讓它實(shí)時(shí)刷新數(shù)據(jù)并且讓后面的數(shù)字消失掉啊
求解。。
讀取東西時(shí)
拿走東西后
/************************************************************************************
主程序
*************************************************************************************/
#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(); //設(shè)置NVIC中斷分組2:2位搶占優(yōu)先級(jí),2位響應(yīng)優(yōu)先級(jí)
// 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;
//校準(zhǔn)參數(shù)
//因?yàn)椴煌膫鞲衅魈匦郧不是很一致,因此,每一個(gè)傳感器需要矯正這里這個(gè)參數(shù)才能使測(cè)量值很準(zhǔn)確。
//當(dāng)發(fā)現(xiàn)測(cè)試出來(lái)的重量偏大時(shí),增加該數(shù)值。
//如果測(cè)試出來(lái)的重量偏小時(shí),減小改數(shù)值。
//該值可以為小數(shù)
#define GapValue 586
void Init_HX711pin(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); //使能PF端口時(shí)鐘
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); //根據(jù)設(shè)定參數(shù)初始化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); //初始化設(shè)置為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個(gè)脈沖下降沿來(lái)時(shí),轉(zhuǎn)換數(shù)據(jù)
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; //獲取實(shí)物的AD采樣數(shù)值。
Weight_Shiwu = (s32)((float)Weight_Shiwu/GapValue); //計(jì)算實(shí)物的實(shí)際重量
//因?yàn)椴煌膫鞲衅魈匦郧不一樣,因此,每一個(gè)傳感器需要矯正這里的GapValue這個(gè)除數(shù)。
//當(dāng)發(fā)現(xiàn)測(cè)試出來(lái)的重量偏大時(shí),增加該數(shù)值。
//如果測(cè)試出來(lái)的重量偏小時(shí),減小改數(shù)值。
}
}
|
|