#include "main.h"
#include "HX711.h"
unsigned long HX711_Buffer = 0;
unsigned long Weight_Maopi = 0;
long Weight_Shiwu = 0;
unsigned int Temp_Buffer = 0;
//?????????
uchar code table[] = { 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0X00,0XFF,0x40}; //0,1,2,3,4,5,6,7,8,9,????????,g,e,r,o,h,l //????
//?????λ???
uchar code P0[] = {0xfe,0xfd,0xfb,0xf7};
unsigned char flag = 0;
unsigned char timer=0;
bit Flag_ERROR = 0;
bit Flag_yemian = 0;
sbit beep = P2^3;
sbit CS = P3^7;
sbit DCLK = P2^1;
sbit DIN = P2^0;
sbit DOUT = P2^5;
sbit DU = P2^6; //???????
sbit WE = P2^7; //?????λ?
unsigned char q = 0,b = 0,s = 0,g = 0;
unsigned int GapValue=https://bbs.csdn.net/topics/ 430; //У?????
unsigned char num1,num2,num3; //num1 ?? num2 ??洢??????eeprom?????? num3????±???
#include "eeprom52.h"
/******************????????浽????????eeprom??******************/
void write_eeprom()
{
num1=GapValue/256;
num2=GapValue%256;
SectorErase(0x2000);
byte_write(0x2001, num1);
byte_write(0x2002, num2);
byte_write(0x2060, a_a);
}
/******************??????????????eeprom?ж?????*****************/
void read_eeprom()
{
num1 = byte_read(0x2001);
num2 = byte_read(0x2002);
a_a = byte_read(0x2060);
GapValue=https://bbs.csdn.net/topics/ num1*256+num2;
}
/**************???????eeprom?????*****************/
void init_eeprom()
{
read_eeprom(); //???
if(a_a != 3) //?μ?????????????????eeprom
{
GapValue=https://bbs.csdn.net/topics/ 430;
a_a = 3;
write_eeprom();
}
}
//??譴??
void Scan_Key()
{
if(KEY1 == 0)
{
Delay_ms(5);
if(KEY1 == 0)
{
while(KEY1 == 0);
Get_Maopi(); //??
}
}
if(KEY2 == 0)
{
Delay_ms(25);
if(KEY2 == 0)
{
// while(KEY2 == 0);
Flag_yemian=1; num3=0;
if(GapValue<999) { GapValue++; }
write_eeprom(); //????????
q = 12;
b = GapValue/100;
s = GapValue%100/10;
g = GapValue%10;
}
}
if(KEY3 == 0)
{
Delay_ms(25);
if(KEY3 == 0)
{
Flag_yemian=1; num3=0;
//while(KEY3 == 0);
if(GapValue>1) { GapValue--; }
write_eeprom(); //????????
q = 12;
b = GapValue/100;
s = GapValue%100/10;
g = GapValue%10;
}
}
}
//****************************************************
//??????
//****************************************************
void main()
{
init_eeprom();
Init_Timer0();
q = 12;
b = 12;
s = 12;
g = 12;
Delay_ms(1000); //???,????????????
Get_Maopi(); //????????
while(1)
{
EA = 0;
Get_Weight(); //????
EA = 1;
Scan_Key();
num3++;
if(num3>=10) {num3=0; Flag_yemian=0; }
if(Flag_yemian==0)
{
//??????????
if( Flag_ERROR == 1)
{
beep =0; //??????????
}
else
{
beep =1;
q = Weight_Shiwu/1000;
b = Weight_Shiwu%1000/100;
s = Weight_Shiwu%100/10;
g = Weight_Shiwu%10;
}
Delay_ms(100);
}
}
}
//****************************************************
//????
//****************************************************
void Get_Weight()
{
Weight_Shiwu = HX711_Read();
Weight_Shiwu = Weight_Shiwu - Weight_Maopi; //???????
if(Weight_Shiwu > 0)
{
Weight_Shiwu = (unsigned int)((float)Weight_Shiwu/GapValue); //???????????????
if(Weight_Shiwu > 5000) //???????
{
Flag_ERROR = 1;
q = 5; //
b = 0; //
s = 0; //
g = 0; //
}
else
{
Flag_ERROR = 0;
}
}
else
{
Weight_Shiwu = 0;
// Flag_ERROR = 1; //???????
}
}
//****************************************************
//?????????
//****************************************************
void Get_Maopi()
{
Weight_Maopi = HX711_Read();
}
//****************************************************
//??????????0
//****************************************************
void Init_Timer0()
{
TMOD = 0X01; //T0, ??????1
TH0 = (65536 - 1000)/256;
TL0 = (65536 - 1000)%256;
TR0 = 1; //????????
ET0 = 1; //?????????ж?
EA = 1; //???????ж?
}
//?ж????
//****************************************************
void Timer0() interrupt 1
{
TH0 = (65536 - 1000)/256;
TL0 = (65536 - 1000)%256;
flag++;
if(flag >= 4)
{
flag = 0;
}
switch(flag)
{
case 0:
DCLK=0 DIN=1 DU=1 WU=1 ;
P0 = table[q];
DU=0 WU=0;
DCLK=1;
break;
case 1:
DCLK=0 DIN=1 DU=1 WU=1;
P0 = table[b]; //??????λ?
DU=0 WU=1;
DCLK=1;
break;
case 2:
DCLK=0 DIN=0 DU=1 WU=1;
P0 = table[s]; ////???????λ?
DU=0 WU=0;
DCLK=1;
break;
case 3:
DCLK=1 DIN=1 DU=1 WU=0;
P0 = table[g]; //???????λ?
DU=0 WU=0;
DCLK=1;
break;
}
}
//****************************************************
//MS???????(12M?????2???)
//****************************************************
void Delay_ms(unsigned int n)
{
unsigned int i,j;
for(i=0;i<n;i++)
for(j=0;j<123;j++);
}
uj5u.com熱心網友回復:
總是報錯,但我自己找不到哪里錯了uj5u.com熱心網友回復:
問這種問題最好把錯誤資訊貼出來,不然沒人看的。uj5u.com熱心網友回復:
DCLK=0 DIN=1 DU=1 WU=1 ;每一句都要有分號
DCLK=0; DIN=1; DU=1; WU=1 ;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/70331.html
標籤:單片機/工控
