各位师傅好!我用STC12LE5A60S2单片机和HX711编写了一个电子秤程序,我想通过一只按键按一次锁定数码管显示的重量,再按一次解除锁定,进入正常显示。我试了下,断开hx711的DOUT或者PD_SCK或者同时断开均可达到目的,但不知C程序如何编写,试了很多方法均不行,敬请个位师傅给点思路,谢谢!!下面是程序:请大家帮忙看看!谢谢指教! #include<reg52.h>#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uint Lheight;
uint compensate;
uchar discode[4]={0,0,0,0};
extern void LedDisplay(void);
sbit P10= P1^0; //按键
sbit hx711_dout=P3^6;
sbit hx711_sck=P3^7;
extern unsigned char discode[4];
uchar code Dis_7[10]= {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90 };//0,1,2,3,4,5,6,7,8,9
uchar code scan_con[4]= {0xfe,0xfd,0xfb,0xf7 };
unsigned int ReadCount(void);
// 延时 MS
void delay(uint x)
{
uchar j;
while(x--)
{
for(j=0;j<125;j++)
{;}
}
}
//延时 500US
void delay500us(void)
{
unsigned char a,b;
for(b=7l;b>0;b--)
for(a=2;a>0;a--);
}
//hx711 us延时
void delay_hx711_us(void)
{
_nop_();
_nop_();
}
//数码管显示
void LedDisplay(void)
{
uchar k;
for(k=0;k<4;k++)
{
P0=Dis_7[discode[k]];
P2=scan_con[k];
delay500us();
P2=0xff;
}
}
// 按钮
void ButtonScan(void)
{
if(!P10)
{
delay(10);
}
if(!P10)
{
compensate=ReadCount();//
}
}
//读重量值,并A/D转换,返回 (hx711驱动程序)
unsigned int ReadCount(void)
{
unsigned long Count;
uint ADval;
uchar i;
hx711_dout=1;
delay_hx711_us();
hx711_sck=0;
Count=0;
while(hx711_dout)
LedDisplay();
for(i=0;i<24;i++)
{
hx711_sck=1;
Count=Count<<1;
hx711_sck=0;
if(hx711_dout)
Count++;
}
hx711_sck=1;
delay_hx711_us();
Count=Count^0x800000; //第25个脉冲下降沿来时转换数据
hx711_sck=0;
ADval=(float)(Count>>8);
return(ADval);
}
//分解
void discode0(void)
{
discode[0]=Lheight%10;
discode[1]=(Lheight%100)/10;
discode[2]=(Lheight%1000)/100;
discode[3]=(Lheight%10000)/1000;
}
//读取重量值
void WeighWork(void)
{
float Tempheight;
uint a;
a=ReadCount();
if(a<=compensate)
Lheight=0; //校0
else Lheight=a-compensate;
discode0(); //调用数值分解函数
Lheight=discode[3]*1000+discode[2]*100+discode[1]*10+discode[0];
Tempheight=(float)(Lheight/0.822);
Lheight=(float)(Tempheight); // 将Tempheight转浮点型
discode0(); //调用数值分解函数
}
void main(void)
{
P10=1;
delay(1000);
compensate=ReadCount()+1;
while(1)
{
// ReadCount();
WeighWork();
LedDisplay();
ButtonScan();
}
}
|