初级会员
- 积分
- 79
- 金钱
- 79
- 注册时间
- 2019-9-13
- 在线时间
- 8 小时
|
10金钱
用hx711和STC8a8k单片机,做一个称重,我并不是要做电子秤,就自己写了一个程序,写完了以后调试了一整天,但就是用不了,请求各位大神帮我看看,这个程序哪里问题,这些乱码在keil上好像是可以重新显示出字,
/*******ÔúêyÂë1üéÏÏÔê¾,óÃcd4511Çy¶ˉ12òõêyÂë1ü*******/
#include<stc8.h>
#define u8 unsigned char
#define u16 unsigned int
sbit scl=P1^0; //hx711ὸö¶Ë¿úμĶ¨òå
sbit sda=P1^1;
sbit DUAN5=P0^5; //êyÂë1ü¶ÎÑ¡¶¨òå
sbit DUAN4=P0^4;
sbit DUAN3=P0^3;
sbit DUAN2=P0^2;
sbit DUAN1=P0^1;
sbit DUAN0=P0^0;
void Init_Timer0(); //¶¨ê±Æ÷03õê¼»ˉ
unsigned long HX711_Read(void); //¶á′«¸DÆ÷êy¾Y
void xianshi(u16 ,u16 ); //ÔúêyÂë1üéÏÏÔê¾,óÃcd4511
void delay(u16); //ὸöÑóê±
void Delay10us();
u16 flag = 0; //¶¨ê±Æ÷±ê־λ
unsigned int ds ;
u16 di[6];
void main(void)
{
delay(300);
Init_Timer0();
while(1)
{
if(flag==500)
{
ds = (unsigned int)((float)HX711_Read()/445); //¼ÆËãÖØá¿
flag=0;
}
/**¡¡2e·ÖóÃóúêyÂë1üÏÔê¾**/
di[5]=ds/100000;
di[4]=ds/10000%10;
di[3]=ds/1000%10;
di[2]=ds/100%10;
di[1]=ds/10%10;
di[0]=ds%10;
xianshi(di[5] ,5 );
delay(2);
xianshi(di[4] ,4 );
delay(2);
xianshi(di[3] ,3 );
delay(2);
xianshi(di[2] ,2 );
delay(2);
xianshi(di[1] ,1 );
delay(2);
xianshi(di[0] ,0 );
delay(2);
}
}
void Init_Timer0()
{
AUXR |= 0x80;
TMOD &= 0xF0;
TMOD |= 0x01;
TL0 = 0xcd; //1ms
TH0 = 0xd4;
TF0 = 0;
ET0 = 1; //¿aÆô¶¨ê±Æ÷ÖD¶Ï
EA = 1; //¿aÆô×üÖD¶Ï
TR0 = 1;
}
void Timer0() interrupt 1
{
TL0 = 0xcd;
TH0 = 0xd4;
flag++;
}
unsigned long HX711_Read(void) //Ôöòæ128
{
unsigned long count;
unsigned char i;
sda=1;
Delay10us();
scl=0;
count=0;
EA = 1;
while(sda);
EA = 0;
for(i=0;i<24;i++)
{
scl=1;
count=count<<1;
scl=0;
if(sda)
count++;
}
scl=1;
count=count^0x800000;//μú25¸öÂö3åϽμÑØà′ê±£¬×a»»êy¾Y
Delay10us();
scl=0;
return(count);
}
void Delay10us()
{
unsigned char i;
i = 25;
while (--i);
}
void delay(u16 a)
{
u16 i, j;
for(i=0;i<a;i++)
{
for(j=1000;j>0;j--);
}
}
void xianshi(u16 dat,u16 duan)
{
DUAN0=1;
DUAN5=1;
DUAN4=1;
DUAN3=1;
DUAN2=1;
DUAN1=1;
switch (duan)
{
case 0:
DUAN0=0;
P2=dat;
break;
case 1:
DUAN1=0;
P2=dat;
break;
case 2:
DUAN2=0;
P2=dat;
break;
case 3:
DUAN3=0;
P2=dat;
break;
case 4:
DUAN4=0;
P2=dat;
break;
case 5:
DUAN5=0;
P2=dat;
break;
}
}
|
|