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
}
}
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
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- The silk screen is AA6B SOT23-6 package with 2.2UH inductor 3.7~4.2V boost to 5V
- Add uart6 driver
- How fast can electric vehicles be “fast charged”?
- 5G millimeter wave RF algorithm technology
- LC filter design issues in DCDC step-down circuits
- EEWORLD University Hall----RMB settlement, VAT invoice issuance and various payment methods
- [CB5654 Intelligent Voice Development Board Review] Comparison of Voice Recognition Development Boards
- C2000 Delfino MCU F28379D LaunchPad Development Kit
- [HPM-DIY] HPM SDK updated to 0.13 SD card reading and writing performance greatly improved
- Radio communication equipment manufacturing