51 single chip simple electronic scale program

Publisher:WanderlustGazeLatest update time:2020-09-14 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

This is my single-chip microcomputer design, which is mainly made of pressure sensor, HX711 module and 51 single-chip microcomputer. The attachment contains the schematic diagram and source code of the electronic scale.
This simple electronic scale has three modes:
Mode 1: ordinary object measurement;
Mode 2: pricing mode;
Mode 3: cumulative measurement mode;
In addition, in addition to using buttons to switch modes, you can also switch modes by sending commands from the host computer.
This course design is mainly aimed at the application of the basic knowledge of 51 single-chip microcomputers, in which the following knowledge points are mainly used: matrix buttons, LED lights, buzzers, AD conversion, LCD12864,
timer 0, timer 1, external interrupt 0, external interrupt 1, serial port (timer 2 is used, so please use STC89C52 chip when burning), etc.

A simple electronic scale based on 51 single-chip microcomputer is briefly introduced
. It has simple performance and is divided into 3 modes. Mode 1 is used for measuring heavy objects, and mode 2 is mainly used for pricing of items. However, the unit price input can only be an integer. Since the weight of heavy objects is accurate to two decimal places, the price after unit pricing can be a decimal. Mode 3 is the cumulative measurement of heavy objects. At the same time, after processing the key to switch modes, the host computer can also send instructions to switch modes. When the host computer sends 1, the receiver starts mode 1 after receiving the instruction and sends "mode 1 start". Other modes are similar to this, but when the host computer sends other characters, the single-chip microcomputer will return words like "mode switching failed, please enter the correct serial number".
Mode Description
Mode 1
Normal measurement mode, only normal measurement
Mode 2
Pricing measurement mode, the price can be calculated according to the input price and the weight of the object being measured
Mode 3
Cumulative measurement mode, the weight of the object being measured can be accumulated after the key is pressed, and the total weight will be displayed after the end key is pressed
Key Description
Number Key
Price input key in mode 2, only valid in mode 2
Function key A
Press the A key to enter mode 1
Function key B
Press the B key to enter mode 2
Function key C
Press the C key to enter mode 3
Function key D
Press in mode 3 to confirm the weight of the object being measured. If the mode 3 end key is not pressed, continue to measure the next object being measured. Only valid in mode 3
Function key *
Price clear key in mode 2, after pressing, the price returns to 0 and the display is cleared. Only valid in mode 2
Function key #
Price confirmation key in mode 2, after pressing, the next is fixed, and the price is in the state of re-entering when the number key is pressed again. In mode 3, it is used as the mode 3 end key, and the total weight of the cumulative measurement is displayed after pressing the key.

Interface Introduction Mode 1 Interface

Mode 2 interface

Mode 3 interface

Mode 3 final interface

Main flow chart of the program

The flow chart of each module (omitted)
and the source program of the microcontroller are as follows:

#include "reg52.h"

#include "HX711.h"

#include "lcd12864.h"

#include "module.h"

#include "keyborad.h"


//*************************************************************************

// Timer 0 initialization function

//*************************************************************************

void timer0_init(void)//50ms

{

ET0 = 1; // Enable timer 0 interrupt

TMOD = 0x11; //Timer working mode selection

TL0 = 0xb0;

TH0 = 0x3c; //Assign initial value to the timer

TR0 = 1; //Start the timer

EA = 1;

}



//*************************************************************************

// Timer 0 interrupt service function

// Refresh 5 times per second

//*************************************************************************

bit weight_flag=0; //Flag of timer 0

uchar time0_count=0; //Timer 0 count flag

void timer0() interrupt 1

{

TL0 = 0xb0;

TH0 = 0x3c; //Assign initial value to the timer

time0_count++;

if(time0_count>=4){

time0_count = 0;

weight_flag = 1;

}

}


//*************************************************************************

// Timer 1 initialization function

//*************************************************************************

void timer1_init(void)        //50ms

{

ET1 = 1;

TL1 = 0xB0; //Set the initial timing value

TH1 = 0x3C; //Set the initial timing value

// TR1 = 1; //Timer 1 starts timing

}


//*************************************************************************

// Timer 0 interrupt service function

//Time for 3 seconds

//*************************************************************************

bit weight_flag1; //Flag of timer 1

uchar timer1_count; //Timer count 1 flag

void timer1(void) interrupt 3

{

TL1 = 0xB0; //Set the initial timing value

TH1 = 0x3C; //Set the initial timing value

timer1_count++;

if (timer1_count == 60) {

TR1 = 0;

timer1_count = 0;

weight_flag1 = 1;

}

}


//*************************************************************************

// Initialize external interrupt 0

// Used to adjust the overweight alarm value

//*************************************************************************

head weight_alarm=200000;

void INIT0_int()

{

EX0 = 1;

IT0 = 1;

}

//*************************************************************************

// External interrupt 0 service function

//*************************************************************************

void INIT0_deal() interrupt 0

{

weight_alarm-=50000;if(weight_alarm<=100000)weight_alarm=450000;

}


//*************************************************************************

// Initialize external interrupt 1

// Used to adjust the overweight alarm value

//*************************************************************************

void INIT1_int()

{

EX1 = 1;

IT1 = 1;

}

//*************************************************************************

// External interrupt 1 service function

//*************************************************************************

void INIT1_deal() interrupt 2

{

weight_alarm+=50000;if(weight_alarm>=450000)weight_alarm=200000;

}


//*************************************************************************

//Serial port sending data function

//*************************************************************************

void SendData(unsigned char *s)

{

while(*s>0)

{

SBUF = *s;

while(!IF);

IF = 0;

s++;

}

}

//*************************************************************************

//Serial port initialization function

//*************************************************************************

sfr T2MOD = 0x9C;

void USTAR_init()

{

PS = 1;

SCON = 0x50;

T2MOD = 0x01;

T2CON = 0x30;

TH2 = 0xFF;

TL2 = 0xDC;        

RCAP2H = 0XFF;

RCAP2L = 0xDC; //Baud rate 9600

TR2 = 1;

EN = 1;

EA = 1;

}

//*************************************************************************

//Serial port interrupt service function

//*************************************************************************

uchar ReceiveData=0;

void USTAR() interrupt 4

{

ReceiveData = SBUF;

while(!RI);

RI = 0;

if(ReceiveData=='1')

{

SendData("Mode 1 start n");

}

else if(ReceiveData=='2')

{

SendData("Mode 2 start n");

}

else if(ReceiveData=='3')

{

SendData("Mode 3 start n");        

}

else

{

SendData("Switch failed,");

SendData("non-belonging mode n");

}

}

//*************************************************************************

//Serial port mode switching interface function

//*************************************************************************

void BoundaryChange()

{

if(ReceiveData==0)return;

if(ReceiveData=='1')

{

module1_flag=1;

module2_flag=0;

module3_flag=0;

ReceiveData=0;

module1_init();

}

else if(ReceiveData=='2')

{

module1_flag=0;

module2_flag=1;

module3_flag=0;

ReceiveData=0;

module2_init();

}

else if(ReceiveData=='3')

{

module1_flag=0;

module2_flag=0;

module3_flag=1;

ReceiveData=0;

module3_init();        

}

}


//*************************************************************************

// Alarm function

//*************************************************************************

sbit beep = P2^7;

sbit led = P0^0;

void beep_alarm()

{

uint i=500;

while(i--){

beep = ~beep;

if(i==1)led =~led;

delay(1);}

}

//*************************************************************************

// Main function

//*************************************************************************

void main()

{

lcd_init(); //LCD12864 initialization

USTAR_init(); //Serial port interrupt initialization

timer0_init(); //Timer 0 initialization

timer1_init(); //Timer 1 initialization

INIT0_int(); //External interrupt 0 initialization

INIT1_int(); //External interrupt 1 initialization

Get_MaoPi();        

Get_MaoPi();

delay(1000);

Get_MaoPi();

Get_MaoPi(); //Get the fur weight

while(1)

{

BoundaryChange();

if(weight_flag==1){

Get_Weight();

weight_flag=0;} //Refresh every time the flag is 1

Key_Deal();        

if(module1_flag==1) module1();

else if(module2_flag==1)module2();

else if(module3_flag==1)module3();

if(weight >= weight_alarm){beep_alarm();}else{led=1;}//overweight alarm

if(module3_flag != 1){thing_count=1;Totle_weight=0;}//All counts in non-mode 3 are cleared

}        

}





Reference address:51 single chip simple electronic scale program

Previous article:Speed ​​measurement based on 51 single chip PWM speed regulation digital tube display
Next article:Single chip microcomputer + PCF8591 to realize digital voltmeter

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号