15单片机重量传感器+HX711 AD模块;使用串口一输出
单片机源程序如下:
- #include "stc15.h"
- #include "delay.h"
- #include "uart.h"
- #include "stdio.h"
- #include "HX711.h"
- sbit LED1 = P2^6;
- sbit LED2 = P2^7;
- sbit LED3 = P4^5;
- sbit LED4 = P4^6;
- //sbit ren=P0^3;
- void Get_Weight();
- void Get_Maopi();
- char putchar(char c);
- unsigned long HX711_Buffer = 0;
- unsigned long Weight_Maopi = 0;
- unsigned long Weight_Shiwu = 0;
- #define GapValue 400
- bit Flag_ERROR = 0;
- void GPIO() //串口初始化
- {
- P0M0=0X00;P0M1=0X00;
- P1M0=0X00;P1M1=0X00;
- P2M0=0X00;P2M1=0X00;
- P3M0=0X00;P3M1=0X00;
- P4M0=0X00;P4M1=0X00;
- P5M0=0X00;P5M1=0X00;
- }
- void main()
- {
- GPIO();
-
- Uart1Init();
- Delay_x_ms(1000);
- Get_Maopi();
- while(1)
- {
- LED1=~LED1;
- Delay_x_ms(2000);
- EA = 0;
- Get_Weight(); //称重
- EA = 1;
- //Uart_1_Sendbit(Weight_Shiwu);
- printf("距离= %ldCM\r\n",Weight_Shiwu);
- }
- }
- void Get_Weight()
- {
- Weight_Shiwu = HX711_Read();
- Weight_Shiwu = Weight_Shiwu - Weight_Maopi; //获取净重
- if(Weight_Shiwu > 0)
- {
- Weight_Shiwu = (unsigned int)((float)Weight_Shiwu/GapValue); //计算实物的实际重量
- if(Weight_Shiwu > 5000) //超重报警
- {
- Uart_1_Sendbits("错误");
- }
- else
- {
- Uart_1_Sendbits("重量");
- }
- }
- else
- {
- Weight_Shiwu = 0;
- // Flag_ERROR = 1; //负重报警
- }
-
- }
- void Get_Maopi()
- {
- Weight_Maopi = HX711_Read();
- }
- char putchar(char C)
- {
- SBUF= C;
- while(TI==0);
- TI=0;
- return C;
- }
复制代码
所有代码51hei提供下载:
15单片机重量传感器.zip
(53.12 KB, 下载次数: 30)
|