|
本帖最后由 xiaohancf 于 2018-8-24 10:07 編輯
先貼程序main.c:
#include "main.h"
#include "LCD12864.h"
#include "HX711.h"
unsigned long HX711_Buffer = 0;
unsigned int Weight_Maopi = 0,Weight_Shiwu = 0;
//****************************************************
//主函數(shù)
//****************************************************
void main()
{
LCD12864_Reset(); //初始化液晶
LCD12864_HAIZI_SET(); //設(shè)置為普通模式
LCD12864_NoWaitIdle_COM_Write(0x80); //指針設(shè)置
LCD12864_write_word("※※※※※※※※");
LCD12864_NoWaitIdle_COM_Write(0x90); //指針設(shè)置
LCD12864_write_word("※※歡迎使用※※");
Delay_ms(1000); //延時1s,等待傳感器穩(wěn)定
Get_Maopi(); //稱毛皮重量
LCD12864_NoWaitIdle_COM_Write(0x01); //清空
while(1)
{
Get_Weight(); //稱重
if( Weight_Shiwu > 10000 )
{
LCD12864_NoWaitIdle_COM_Write(0x80); //指針設(shè)置
LCD12864_write_word("※※※超重※※※");
}
else if( Weight_Shiwu < 10000)
{
//顯示當(dāng)前重量
LCD12864_NoWaitIdle_COM_Write(0x80);
LCD12864_write_word("重量:");
if( Weight_Shiwu/10000 != 0)
{
LCD12864_Data_Write(Weight_Shiwu/10000 + 0x30);
}
else
{
LCD12864_Data_Write(' ');
}
LCD12864_Data_Write(Weight_Shiwu%10000/1000 + 0x30);
LCD12864_Data_Write('.');
LCD12864_Data_Write(Weight_Shiwu%1000/100 + 0x30);
LCD12864_Data_Write(Weight_Shiwu%100/10 + 0x30);
LCD12864_Data_Write(Weight_Shiwu%10 + 0x30);
LCD12864_write_word(" Kg");
LCD12864_NoWaitIdle_COM_Write(0x90);
LCD12864_write_word("重量:");
if( Weight_Shiwu*2/10000 != 0)
{
LCD12864_Data_Write(Weight_Shiwu*2/10000 + 0x30);
}
else
{
LCD12864_Data_Write(' ');
}
LCD12864_Data_Write(Weight_Shiwu*2%10000/1000 + 0x30);
LCD12864_Data_Write('.');
LCD12864_Data_Write(Weight_Shiwu*2%1000/100 + 0x30);
LCD12864_Data_Write(Weight_Shiwu*2%100/10 + 0x30);
LCD12864_Data_Write(Weight_Shiwu*2%10 + 0x30);
LCD12864_write_word(" ");
}
}
}
//****************************************************
//稱重
//****************************************************
void Get_Weight()
{
HX711_Buffer = HX711_Read();
HX711_Buffer = HX711_Buffer/100;
if(HX711_Buffer > Weight_Maopi)
{
Weight_Shiwu = HX711_Buffer;
Weight_Shiwu = Weight_Shiwu - Weight_Maopi; //獲取實物的AD采樣數(shù)值。
Weight_Shiwu = (unsigned int)((float)Weight_Shiwu/2.09+0.05); //計算實物的實際重量
//因為不同的傳感器特性曲線不一樣,因此,每一個傳感器需要矯正這里的2.15這個除數(shù)。
//當(dāng)發(fā)現(xiàn)測試出來的重量偏大時,增加該數(shù)值。
//如果測試出來的重量偏小時,減小改數(shù)值。
//該數(shù)值一般在2.15附近調(diào)整之間。因傳感器不同而定。
//+0.05是為了四舍五入百分位
Weight_Shiwu=Weight_Shiwu/2;
}
}
//****************************************************
//獲取毛皮重量
//****************************************************
void Get_Maopi()
{
unsigned char i = 0;
unsigned int Temp_Weight = 0;
Weight_Maopi = 0;
for( i = 0 ; i < 10 ; i++)
{
HX711_Buffer = HX711_Read();
Temp_Weight = HX711_Buffer/100;
if( Temp_Weight > Weight_Maopi)
{
Weight_Maopi = Temp_Weight;
}
}
}
//****************************************************
//MS延時函數(shù)(12M晶振下測試)
//****************************************************
void Delay_ms(unsigned int n)
{
unsigned int i,j;
for(i=0;i<n;i++)
for(j=0;j<123;j++);
}
main.h:
#ifndef __MAIN_H__
#define __MAIN_H__
#include <reg52.h>
//函數(shù)或者變量聲明
extern void Delay_ms(unsigned int n);
extern void Get_Maopi();
extern void Get_Weight();
#endif
LCD12864.h:
#ifndef __LCD12864_H__
#define __LCD12864_H__
#include <reg52.h>
#include <intrins.h>
//引腳定義
sbit LCD12864_RS_PORT = P1^0;
sbit LCD12864_RW_PORT = P1^1;
sbit LCD12864_E_PORT = P1^2;
#define LCD12864_DA_PORT P2
//函數(shù)或者變量聲明
extern void LCD12864_WaitIdle();
extern void LCD12864_COM_Write( unsigned char com_da);
extern void LCD12864_NoWaitIdle_COM_Write(unsigned char com_da);
extern void LCD12864_Data_Write(unsigned char da);
extern void lcd_delay_ms(unsigned char x);
extern void LCD12864_Reset();
extern void LCD12864_write_word(unsigned char *s);
//extern void LCD12864_PHOTO_SET();
extern void LCD12864_HAIZI_SET();
//extern void LCD12864_HAIZI_WRITE(unsigned char xpos,unsigned char ypos,unsigned char daH,unsigned char daL);
//extern void LCD12864_PHOTO_WRITE(unsigned char *img);
#endif
HX711.h:
#ifndef __HX711_H__
#define __HX711_H__
#include <reg52.h>
#include <intrins.h>
//IO設(shè)置
sbit HX711_DOUT=P3^4;
sbit HX711_SCK=P3^5;
//函數(shù)或者變量聲明
extern void Delay__hx711_us(void);
extern unsigned long HX711_Read(void);
#endif
LCD12864.c:
#include "LCD12864.h"
//********************************************************************
//LCD12864 忙 信號檢測
//********************************************************************
void LCD12864_WaitIdle()
{
unsigned char i;
LCD12864_DA_PORT = 0xff;
LCD12864_RS_PORT = 0;
LCD12864_RW_PORT = 1;
LCD12864_E_PORT = 1;
while((LCD12864_DA_PORT&0x80)==1); /*等待BF 不為1*/
LCD12864_E_PORT = 0;
for(i=0;i<5;i++);
}
//********************************************************************
//檢測忙信號寫入命令字 com_da 為待寫入的命令字
//********************************************************************
void LCD12864_COM_Write( unsigned char com_da)
{
LCD12864_WaitIdle();
LCD12864_RS_PORT = 0;
LCD12864_RW_PORT = 0;
LCD12864_DA_PORT = com_da;
LCD12864_E_PORT = 1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
LCD12864_E_PORT = 0;
}
//********************************************************************
//不檢測忙信號寫入命令字 com_da 為待寫入的命令字
//********************************************************************
void LCD12864_NoWaitIdle_COM_Write(unsigned char com_da)
{
LCD12864_RS_PORT = 0;
LCD12864_RW_PORT = 0;
LCD12864_DA_PORT = com_da;
LCD12864_E_PORT = 1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
LCD12864_E_PORT = 0;
}
//********************************************************************
//數(shù)據(jù)寫入 da 為待寫入的8位數(shù)據(jù)
//********************************************************************
void LCD12864_Data_Write(unsigned char da)
{
LCD12864_WaitIdle(); /*檢測忙信號*/
LCD12864_RS_PORT = 1;
LCD12864_RW_PORT = 0;
LCD12864_DA_PORT = da;
LCD12864_E_PORT = 1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
LCD12864_E_PORT = 0;
}
//*************************************************************************************
//寫連續(xù)字符函數(shù)
//*************************************************************************************
void LCD12864_write_word(unsigned char *s)
{
while(*s>0)
{
LCD12864_Data_Write(*s);
s++;
}
}
//********************************************************************
//1MS為單位的延時程序,不準確
//********************************************************************
void lcd_delay_ms(unsigned char x)
{
unsigned char j;
while(x--){
for(j=0;j<125;j++)
{;}
}
}
//********************************************************************
//LCD12864初始化
//********************************************************************
void LCD12864_Reset()
{
lcd_delay_ms(100); /*適當(dāng)延時待LCD自動復(fù)位完成*/
LCD12864_NoWaitIdle_COM_Write(0x30); /*使用8位并口通訊*/
lcd_delay_ms(10);
LCD12864_NoWaitIdle_COM_Write(0x30); /*使用8位并口通訊*/
lcd_delay_ms(10);
LCD12864_NoWaitIdle_COM_Write(0x0c); /*顯示開及光標(biāo)設(shè)置*/
lcd_delay_ms(10);
LCD12864_NoWaitIdle_COM_Write(0x01); /*顯示清屏*/
lcd_delay_ms(30);
LCD12864_NoWaitIdle_COM_Write(0x06); /*DDRAM的地址計數(shù)器(AC)加1*/
lcd_delay_ms(30);
}
//void LCD12864_PHOTO_SET()
//{
// LCD12864_COM_Write(0x36);
// lcd_delay_ms(10);
// LCD12864_COM_Write(0x36);
// lcd_delay_ms(10);
//}
//
void LCD12864_HAIZI_SET()
{
LCD12864_COM_Write(0x30);
lcd_delay_ms(10);
LCD12864_COM_Write(0x30);
lcd_delay_ms(10);
}
//
//
//void LCD12864_HAIZI_WRITE(unsigned char xpos,unsigned char ypos,unsigned char daH,unsigned char daL)
////ST7920 漢字字符寫入
////參數(shù)說明: xpos 待寫入的X位置
////ypos 待寫入的Y位置
////daH 待寫入的漢字的高八位 daL待寫入的漢字的低八位
//{
// unsigned char xy_pos;
// if((xpos>=8)||(ypos>=4) ) return; /*X位置超出顯示范圍退出*/
// if(ypos==0) xy_pos = 0x80 + xpos;
// else if(ypos==1) xy_pos = 0x90 + xpos; /*計算轉(zhuǎn)換地址*/
// else if(ypos==2) xy_pos = 0x88 + xpos;
// else if(ypos==3) xy_pos = 0x98 + xpos;
// LCD12864_COM_Write(xy_pos); /*寫地址*/
// lcd_delay_ms(1);
// LCD12864_Data_Write(daH); /*寫高八位數(shù)據(jù)*/
// lcd_delay_ms(1);
// LCD12864_Data_Write(daL); /*寫低八位數(shù)據(jù)*/
// lcd_delay_ms(1);
//}
//
//
//void LCD12864_PHOTO_WRITE(unsigned char *img)
//{
// unsigned char x,y,i,j;
// unsigned int k=0;
// y=0x80; /*設(shè)置起始 繪圖區(qū)的 Y地址坐標(biāo)*/
// x=0x80; /*設(shè)置起始 繪圖區(qū)的 X地址坐標(biāo)*/
// for(i=0;i<32;i++){ /*寫上半部*/
// LCD12864_COM_Write(y);
// LCD12864_COM_Write(x);
// for(j=0;j<16;j++){
// LCD12864_Data_Write(img[k]);
// k++;
// }
// y++;
// }
//
// y=0x80; /*設(shè)置起始 繪圖區(qū)的 Y地址坐標(biāo)*/
// x=0x88; /*設(shè)置起始 繪圖區(qū)的 X地址坐標(biāo)*/
// for(i=0;i<32;i++){ /*寫下半部*/
// LCD12864_COM_Write(y);
// LCD12864_COM_Write(x);
// for(j=0;j<16;j++){
// LCD12864_Data_Write(img[k]);
// k++;
// }
// y++;
// }
//
//}
HX711.c:
#include "HX711.h"
//****************************************************
//延時函數(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<24;i++)
{
HX711_SCK=1;
count=count<<1;
HX711_SCK=0;
if(HX711_DOUT)
count++;
}
HX711_SCK=1;
Delay__hx711_us();
HX711_SCK=0;
return(count);
}
|
-
電子秤.jpg
(51.16 KB, 下載次數(shù): 28)
下載附件
2018-8-24 10:04 上傳
電路圖
-
-
12864中文.doc
2018-8-24 10:07 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
2.61 MB, 下載次數(shù): 25, 下載積分: 黑幣 -5
附上12864說明
|