|
程序采用的是模塊化寫法,你寫論文時(shí)也分開寫兩個(gè)小標(biāo)題把代碼粘貼進(jìn)去就行了。
HX711的AD轉(zhuǎn)換代碼
#include "HX711.h"
//****************************************************
//延時(shí)函數(shù)
//****************************************************
void Delay__hx711_us(void)
{
_nop_();
_nop_();
}
//****************************************************
//讀取HX711
//****************************************************
unsigned long HX711_Read(void) //增益128
{
unsigned long count;
unsigned char i;
HX711_DOUT=1;
Delay__hx711_us();
HX711_SCK=0;
count=0;
while(HX711_DOUT);
for(i=0;i<48;i++)
{
HX711_SCK=1;
count=count<<1;
HX711_SCK=0;
if(HX711_DOUT)
count++;
}
HX711_SCK=1;
count=count^0x008000;//第25個(gè)脈沖下降沿來時(shí),轉(zhuǎn)換數(shù)據(jù)
Delay__hx711_us();
HX711_SCK=0;
return(count);
}
主函數(shù)程序
#include <reg52.h>
#include <intrins.h>
#include <string.h>
#include "main.h"
#include "HX711.h"
#include "eeprom52.h"
#define uchar unsigned char
#define uint unsigned int
uchar qianwei,baiwei,gewei,shiwei;
unsigned long HX711_Buffer = 0;
unsigned long Weight_Maopi = 0;
unsigned long Weight_Maopi_0 = 0;
long Weight_Shiwu = 0;
unsigned int qupi=0;
unsigned char p=0;
//鍵盤處理變量
unsigned char keycode;
unsigned char key_press_num=0;
uint GapValue,GapValue1;
bit flag_cz=0;
//uchar code LEDData[]={0x5F,0x44,0x9D,0xD4,0xC6,0xD3,0xDB,0x57,0xDF,0xD7,0xCF,0xaA,0x9B,0xDC,0x9B,0x8B}; //數(shù)碼管顯示碼表
//定義標(biāo)識
volatile bit FlagTest = 0; //定時(shí)測試標(biāo)志,每0.5秒置位,測完清0
volatile bit FlagKeyPress = 0; //有鍵按下標(biāo)志,處理完畢清0
//校準(zhǔn)參數(shù)
//因?yàn)椴煌膫鞲衅魈匦郧不是很一致,因此,每一個(gè)傳感器需要矯正這里這個(gè)參數(shù)才能使測量值很準(zhǔn)確。
//當(dāng)發(fā)現(xiàn)測試出來的重量偏大時(shí),增加該數(shù)值。
//如果測試出來的重量偏小時(shí),減小改數(shù)值。
//該值可以為小數(shù)
//#define GapValue 349
sbit LED=P1^1;
sbit ROW1=P3^0;
sbit ROW2=P3^1;
sbit ROW3=P3^2;
sbit ROW4=P3^3;
//sbit DIAN = P0^6; //小數(shù)點(diǎn)
sbit rs=P0^7;
sbit rw=P0^6;
sbit en=P0^5;
sbit D1=P2^0;
volatile bit ClearWeighFlag = 0; //傳感器調(diào)零標(biāo)志位,清除0漂
/******************把數(shù)據(jù)保存到單片機(jī)內(nèi)部eeprom中******************/
void write_eeprom()
{
SectorErase(0x1080);
GapValue1=GapValue&0x00ff;
byte_write(0x1000, GapValue1);
GapValue1=(GapValue&0xff00)>>8;
byte_write(0x1001, GapValue1);
byte_write(0x1060, a_a);
}
/******************把數(shù)據(jù)從單片機(jī)內(nèi)部eeprom中讀出來*****************/
void read_eeprom()
{
GapValue = byte_read(0x1001);
GapValue = (GapValue<<8)|byte_read(0x1000);
a_a = byte_read(0x1060);
}
/**************開機(jī)自檢eeprom初始化*****************/
void init_eeprom()
{
read_eeprom(); //先讀
if(a_a == 1) //新的單片機(jī)初始單片機(jī)內(nèi)問eeprom
{
GapValue = 3500;
a_a = 1;
write_eeprom(); //保存數(shù)據(jù)
}
}
void delayms(uint xms) //延時(shí)
{
uchar i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void write_com(uchar com) //寫命令
{
rs=0;
// P2=change(com);
P2=com;
delayms(50);
en=1;
delayms(50);
en=0;
}
void write_dat(uchar dat) //寫數(shù)據(jù)
{
rs=1;
// P2=change(dat);
P2=dat;
en=1;
delayms(50);
en=0;
}
void init() //液晶初始化
{
rw=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
//write_com(0x01);
}
/*****顯示開機(jī)初始化等待畫面*****/
/*
void Disp_init(void)
{
P0 = 0x80; //顯示----
P2 = 0xBF; //依次打開各位
Delay_ms(1); //延時(shí)
P2 = 0xEF;
Delay_ms(1);
P2 = 0xFB;
Delay_ms(1);
P2 = 0xFE;
Delay_ms(1);
P2 = 0xFF; //關(guān)閉顯示
}
*/
//顯示重量,單位kg,兩位整數(shù),三位小數(shù)
void Display_Weight()
{ ;
qianwei=Weight_Shiwu/1000%10;
baiwei=Weight_Shiwu/100%10; ;
shiwei=Weight_Shiwu%100/10;
gewei=Weight_Shiwu%100%10;
write_com(0x80+2);
write_dat(qianwei+'0');
write_dat('.');
write_dat(baiwei+'0');
write_dat(shiwei+'0');
write_dat(gewei+'0');
}
//定時(shí)器0初始化
void Timer0_Init()
{
ET0 = 1; //允許定時(shí)器0中斷
TMOD = 1; //定時(shí)器工作方式選擇
TH0 = 0xb0;
TL0 = 0x3c; //定時(shí)器賦予初值
TR0 = 1; //啟動(dòng)定時(shí)器
}
//定時(shí)器0中斷
void Timer0_ISR (void) interrupt 1 using 0
{
uchar Counter;
TH0 = 0xb0;
TL0 = 0x3c; //定時(shí)器賦予初值
//每0.5秒鐘刷新重量
Counter ++;
if (Counter >= 10)
{
FlagTest = 1;
Counter = 0;
}
}
//按鍵響應(yīng)程序,參數(shù)是鍵值
//返回鍵值:
// 7 8 9 10(清0)
// 4 5 6 11(刪除)
// 1 2 3 12(未定義)
// 14(未定義) 0 15(.) 13(確定價(jià)格)
void KeyPress()
{
if(ROW1==0) //清零鍵
{
Display_Weight();
if(ROW1==0)
{
if(qupi==0)
qupi=Weight_Shiwu;
else
qupi=0;
Buzzer=1;
for(p=0;p<10;p++)
Display_Weight();
Buzzer=0;
while(ROW1==0) Display_Weight();
}
}
if(ROW2==0) //加
{
Display_Weight();
if(ROW2==0)
{
while(!ROW2)
{
key_press_num++;
if(key_press_num>=100)
{
key_press_num=0;
while(ROW2)
{
if(GapValue<10000)
GapValue++;
Buzzer=0;
for(p=0;p<2;p++)
Display_Weight();
Buzzer=1;
for(p=0;p<2;p++)
Display_Weight();
Get_Weight();
}
}
Display_Weight();
Delay_ms(8);
}
if(key_press_num==0)
{
key_press_num=0;
if(GapValue>10000)
GapValue++;
Buzzer=0;
for(p=0;p<10;p++)
Display_Weight();
Buzzer=1;
}
write_eeprom();
}
}
if(ROW3==0) //減
{
Display_Weight();
if(ROW3==0)
{
while(ROW3)
{
key_press_num++;
if(key_press_num>=100)
{
key_press_num=0;
while(ROW3)
{
if(GapValue>1)
GapValue--;
Buzzer=0;
for(p=0;p<2;p++)
Display_Weight();
Buzzer=1;
for(p=0;p<2;p++)
Display_Weight();
Get_Weight();
}
}
Display_Weight();
Delay_ms(8);
}
if(key_press_num!=0)
{
key_press_num=0;
if(GapValue>1)
GapValue--;
Buzzer=0;
for(p=0;p<10;p++)
Display_Weight();
Buzzer=1;
}
write_eeprom(); //保存數(shù)?
}
}
}
//****************************************************
//主函數(shù)
//****************************************************
void main()
{
init();
init_eeprom(); //開始初始化保存的數(shù)據(jù)
EA = 0;
Timer0_Init();
//初中始化完成,開斷
EA = 0;
write_com(0x80+7);
write_dat('K');
write_dat('g');
while(1)
{ //每0.5秒稱重一次
if (FlagTest==1)
{
Get_Weight();
FlagTest = 0;
}
if(flag_cz==1)
{
write_com(0x80+2);
write_dat('0');
write_dat('.');
write_dat('0');
write_dat('0');
write_dat('0');
write_dat('K');
write_dat('g');
}
Display_Weight();
KeyPress();
}
}
//****************************************************
//稱重
//****************************************************
void Get_Weight()
{
Weight_Shiwu = HX711_Read();
Weight_Shiwu = Weight_Shiwu - Weight_Maopi; //獲取凈重
Weight_Shiwu = (unsigned int)((float)(Weight_Shiwu*10)/GapValue)-qupi; //計(jì)算實(shí)物的實(shí)際重量
if(Weight_Shiwu <= 10000) //超重報(bào)警
{
Buzzer = !Buzzer;
LED=!LED;
flag_cz=0;
}
else
{
if(Weight_Shiwu==0)
LED=0;
else if(Weight_Shiwu>0)
LED=1;
Buzzer = 1;
flag_cz=0;
}
}
//****************************************************
//****************************************************
void Get_Maopi()
{
unsigned char clear,f;
Weight_Maopi_0 = HX711_Read();
for(clear=0;clear<10;clear++)
{
Buzzer=1;
LED=0;
for(f=0;f<40;f++)
//Disp_init();
Display_Weight();
LED=1;
for(f=0;f<40;f++)
//Disp_init();
Display_Weight();
}
mm: Weight_Maopi = HX711_Read();
if((Weight_Maopi*10)/GapValue!=(Weight_Maopi_0*100)/GapValue)
goto mm;
Buzzer=0;
for(f=0;f<10;f++)
//Disp_init();
Display_Weight();
Buzzer=1;
}
//****************************************************
//MS延時(shí)函數(shù)(12M晶振下測試)
//****************************************************
void Delay_ms(unsigned int n)
{
unsigned int i,j;
for(i=0;i<n;i++)
for(j=0;j<121;j++);
}
|
|