[Small project] luggage (unlock and weigh Bluetooth), luggage weighing

Source: Internet
Author: User

[Small project] luggage (unlock and weigh Bluetooth), luggage weighing

  1 #include <reg52.h>  2 #include "hx711.h"  3   4 sbit Led0=P0^0;       //ÓÃÒ»¿ÅСµÆµÄÁÁÃð´ú±íËøµÄ¿ª¹Ø  5 sbit Led1=P1^0;       //ÓÃСµÆ±íʾÐÐÀîµÄÖØÁ¿£¬Ã¿Ò»¸öСµÆ´ú±í300g£¬¹²Îå¿ÅСµÆ  6 sbit Led2=P1^1;  7 sbit Led3=P1^2;  8 sbit Led4=P1^3;  9 sbit Led5=P1^4; 10  11 unsigned long HX711_Buffer = 0; 12 unsigned int Weight_Maopi = 0,Weight_Shiwu = 0; 13 unsigned int i=0; 14  15 void init_led() 16 { 17    Led0=0; 18      Led1=0; 19      Led2=0; 20      Led3=0; 21      Led4=0; 22      Led5=0; 23 } 24 //**************************************************** 25 //³ÆÖØ 26 //**************************************************** 27 void Get_Weight() 28 { 29     HX711_Buffer = ReadCount(); 30     HX711_Buffer = HX711_Buffer/100; 31     if(HX711_Buffer > Weight_Maopi)             32     { 33         Weight_Shiwu = HX711_Buffer; 34         Weight_Shiwu = Weight_Shiwu - Weight_Maopi;                35         Weight_Shiwu = (unsigned int)((float)Weight_Shiwu/2.08+0.05);       36         Weight_Shiwu=Weight_Shiwu/2;     37      38     } 39 } 40  41 void Get_Maopi() 42 { 43     unsigned char i = 0; 44     unsigned int Temp_Weight = 0; 45  46     Weight_Maopi = 0; 47  48     for( i = 0 ; i < 10 ; i++)                 //È¡10×éÊý¾Ý£¬ÒÔ×î´óµÄÒ»¸öÊý¾ÝΪëƤÖØÁ¿ 49     { 50         HX711_Buffer = ReadCount(); 51         Temp_Weight = HX711_Buffer/100; 52  53         if( Temp_Weight > Weight_Maopi) 54         { 55             Weight_Maopi = Temp_Weight;      56         } 57     }         58 }  59  60 void alarm() 61 { 62      if(Weight_Shiwu>=300&&Weight_Shiwu<600) 63          { 64             Led1=1; 65               Led2=0; 66               Led3=0; 67               Led4=0; 68               Led5=0; 69          } 70          if(Weight_Shiwu>=600&&Weight_Shiwu<900) 71          { 72             Led1=1; 73               Led2=1; 74               Led3=0; 75               Led4=0; 76               Led5=0; 77          } 78          if(Weight_Shiwu>=900&&Weight_Shiwu<1200) 79          { 80             Led1=1; 81               Led2=1; 82               Led3=1; 83               Led4=0; 84               Led5=0; 85          } 86          if(Weight_Shiwu>=1200&&Weight_Shiwu<1500) 87          { 88             Led1=1; 89               Led2=1; 90               Led3=1; 91               Led4=1; 92               Led5=0; 93          } 94          if(Weight_Shiwu>=1500) 95          { 96             Led1=1; 97               Led2=1; 98               Led3=1; 99               Led4=1;100               Led5=1;101          }102      if(Weight_Shiwu<300)103          {104             Led1=0;105               Led2=0;106               Led3=0;107               Led4=0;108               Led5=0;109          } 110 }111 void main()112 {113     unsigned char ldat;114     init_led();115     TMOD=0X21;                //ÉèÖö¨Ê±Æ÷1Ϊģʽ2£¬×öΪ²¨ÌØÂÊ·¢ÉúÆ116     SCON=0x50;                //ÉèÖô¿Ú·½Ê½2117     TL1=0xFd;                //2400²¨ÌØÂʵijõÖµ,°å×ÓʹÓÃ12M¾§Õñ£¬³õÖµ=256-12000000/32/12/2400118     TH1=0xFd;                //119     TR1=1;                    //Æô¶¯²¨ÌØÂÊ·¢Éú                                                                                                            120     TH0=0xf8;      //¶¨Ê±2ms121     TL0=0x30;122     TR0=1;123     EA=1;124     ES=1;125     ET0=1;126     Get_Maopi();127     while(1)128     {129 130         if(RI)        //²éѯÊÇ·ñÓÐÊý¾Ý½ÓÊÕ131         {                  132             ldat=SBUF;        //Êý¾ÝÔÝ´æÓÚ±äÁ¿ÖÐ133             RI=0;                    //ÖØÐÂÇå0µÈ´ý½ÓÊÕ134             SBUF=ldat;        //½«½ÓÊÕµ½µÄÊý¾Ý·¢ËͳöÈ¥135             while(!TI);        //µÈ´ý·¢ËÍÍê³É136             TI=0;137             if(ldat=='A')138                 Led0=1;139             if(ldat=='B')140                 Led0=0;141       }142     alarm();143     }144 }145 146 void int0()interrupt 1147 {148    i++;149      if(i==400)150      {151        Get_Weight();152          i=0;153      }154      TH0=0xf8;155      TL0=0x30;156 }
Main
 1 #ifndef __HX711_H__ 2 #define __HX711_H__ 3  4  5 #include <reg52.h> 6 #include <intrins.h> 7  8 sbit ADDO=P3^4;  9 sbit ADSK=P3^5; 10 11 unsigned long ReadCount(void);12 13 #endif
HX711.H
 1 #include "HX711.h" 2  3 unsigned long ReadCount(void) 4 { 5 unsigned long Count; 6 unsigned char i; 7 ADSK=0;  8 Count=0; 9 while(ADDO); 10 for (i=0;i<24;i++)11 {12 ADSK=1; 13 Count=Count<<1; 14 ADSK=0; 15 if(ADDO) Count++;16 }17 ADSK=1;18 Count=Count^0x800000;19 ADSK=0;20 return(Count);21 }
HX711.c

Due to the limited conditions of raw materials and relatively lazy individuals, digital tubes are not used to display the weight, nor mechanical locks, but LED is used to represent the corresponding part.

Several problems occurred in this project:

1. When I first used a digital tube to display the weight, the four digital tubes should be displayed at the same time, but the results will be displayed cyclically.

2. The quality is sometimes changed from 0 to more than 5900.

3. When the code is confirmed to be normal, the mobile phone can connect to the bluetooth module during debugging, but the development board cannot respond according to the normal instructions.

 

Solution:

1. the analysis may be due to the excessively large value passed by the HX711 AD conversion module to be processed by the CPU, which consumes a lot of CPU resources. Because the processing function and display function are both in the main function, this is the case. Finally, put the AD value passed through HX711 into the timer interrupt for processing, and process it every 800 milliseconds. The digital tube is properly displayed.

2. The analysis may result in HX711_Buffer <Weight_Maopi in the Get_Weight () function due to hardware reasons (this must be changed here)

3. Check the code correctly. Later, use the core board to test the bluetooth module and find that the function is running normally. Most of the time when you use the Development Board to build a circuit, if the code is correct, if the function cannot be implemented normally, you should replace the core board for functional testing. Because the Development Board is connected to a certain number of peripherals, it will have a certain impact on the pin, thus affecting the normal function.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.