本帖最后由 ErniuSong 于 2021-9-10 18:27 編輯
求助 想用稱重傳感器實時重量 來控制直流電機啟停,目前電機在單片機打開后會不停轉,稱重顯示也對,但是不會根據實時重量啟停
源碼如下 我是哪里的問題呀?
9.10更新:目前加了發送數據函數及中斷之后 LCD會顯示 WIGHT = 0.0000KG但是壓稱重傳感器 LCD顯示還是0沒有變化 電機在一直轉。
- #include "main.h"
- #include <reg52.h>
- #include "LCD1602.h"
- #include "HX711.h"
- #include "EEPROM.H"
- #include "motor4.H"
- #include <stdio.h>
- #include <intrins.h>
- //定義變量
- #define uchar unsigned char
- #define uint unsigned int
- unsigned char KEY_NUM = 0; //用來存放按鍵按下的鍵值
- unsigned long HX711_Buffer = 0; //用來存放HX711讀取出來的數據
- unsigned long Weight_Maopi = 0; //用來存放毛皮數據
- long Weight_Shiwu = 0; //用來存放實物重量
- long Max_Value = 0; //用來存放設置最大值
- char maxValueTable[4] = {1,0,0,0};
- unsigned char state = 0; //用來存放設置狀態
- unsigned char Blink_Speed = 0;
- typedef unsigned char u8; //定義取值范圍為0-255的數
- #define Blink_Speed_Max 6 //該值可以改變設置指針閃爍頻率
- #define delayNOP(); {_nop_();_nop_();_nop_();_nop_();};
- uchar data RXDdata[ ] = {0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
- 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20 };
- uchar temp,buf,m,count,KZW;
- bit playflag=0;
- void init_main()
- {
- TMOD=0x20; //用定時器設置串口波特率 9600
- TH1=0xfd;
- TL1=0xfd;
- TR1=1;
- TI=1;
- REN=1; //串口初始化
- SM0=0;
- SM1=1;
- EA=1; //開啟總中斷
- ES=1;
- }
- //校準參數
- //因為不同的傳感器特性曲線不是很一致,因此,每一個傳感器需要矯正這里這個參數才能使測量值很準確。
- //當發現測試出來的重量偏大時,增加該數值。
- //如果測試出來的重量偏小時,減小改數值。
- //該值可以為小數
- #define GapValue 3219.9
- //傳感器最大測量值,單位是g
- #define AlarmValue 20000
- //****************************************************
- //主函數
- //****************************************************
- void main()
- {
- // unsigned char receiveData1,receiveData2,receiveData3,receiveData4;
- double temp,te;
- // chuankouinit(); //串口初始化
- Init_LCD1602(); //初始化LCD1602
- LCD1602_write_com(0x80); //設置LCD1602指針
- LCD1602_write_word("Welcome to use!");
-
- Get_Maopi();
- Get_Maopi();
- Delay_ms(2000); //延時2s
- Get_Maopi();
- Get_Maopi(); //稱毛皮重量 //多次測量有利于HX711穩定
- LCD1602_write_com(0x01); //清屏
- init_main(); //組態王通信程序初始化
- while(1)
- {
- Get_Weight();
- // Weight_Shiwu=39876;
- temp=(Weight_Shiwu/100+(Weight_Shiwu%100)*0.01);
- te=Weight_Shiwu;
- //顯示當前重量
- LCD1602_write_com(0x80);
- LCD1602_write_word("Weight=");
- LCD1602_write_data((Weight_Shiwu/10000)%10 + 0x30);
- LCD1602_write_data((Weight_Shiwu/1000)%10 + 0x30);
- LCD1602_write_data((Weight_Shiwu/100)%10 + 0x30);
- LCD1602_write_data('.');
- LCD1602_write_data((Weight_Shiwu/10)%10 + 0x30);
- LCD1602_write_data(Weight_Shiwu%10 + 0x30);
- LCD1602_write_word("K");
- LCD1602_write_word("g");
- Delay_ms(1000);
- printf("%.3f\r\n", temp);
- printf("%.3f\r\n", te);
- }
-
- }
- //****************************************************
- //稱重
- //****************************************************
- void Get_Weight()
- {
- Weight_Shiwu = HX711_Read();
- Delay_ms(10);
-
- Weight_Shiwu = Weight_Shiwu - Weight_Maopi; //獲取凈重
-
- if(Weight_Shiwu >= 0 )
- {
- Weight_Shiwu = (unsigned long)((float)Weight_Shiwu/GapValue); //計算實物的實際重量
-
- }
- else
- {
- Weight_Shiwu = 0;
- Delay_ms(2);
-
- }
- }
- //****************************************************
- //獲取毛皮重量
- //****************************************************
- void Get_Maopi()
- {
- Weight_Maopi = HX711_Read();
- Delay_ms(2);
- }
- //****************************************************
- //MS延時函數(12M晶振下測試)
- //****************************************************
- void Delay_ms(unsigned int n)
- {
- unsigned int i,j;
- for(i=0;i<n;i++)
- for(j=0;j<123;j++);
- }
- /*********************************************************
- 發送數據函數;目前加了發送數據函數及中斷之后 LCD會顯示 WIGHT = 0.0000KG但是壓稱重傳感器 LCD顯示還是0沒有變化 電機在一直轉。
- *********************************************************/
- //void senddata(uchar dat)
- //{
- // SBUF =dat;
- // while(!TI);
- // TI = 0;
- //}
- /*********************************************************
- 串行中斷服務函數//
- *********************************************************/
- //void serial() interrupt 4
- //{
- // ES = 0; //關閉串行中斷
- // RI = 0; //清除串行接受標志位
- // buf = SBUF; //從串口緩沖區取得數據
- //
- // switch(buf)
- // {
- // case '!': senddata(buf);break; //接受到1,發送字符'T'給計算機
- // case '?': senddata(buf);break;//接受到2,發送字符'X'給計算機
- // default: senddata('X');break; //接受到其它數據,將其發送給計算機
- // }
- // ES = 1; //允許串口中斷
- //}
- //****************************************************
- //推料電機
- //****************************************************
- void zhongliangjiance() /*重量檢測*/
- {
- if(Weight_Shiwu <=3000)/* 需要推料,單位為g*/
- KZW='a';
- if(Weight_Shiwu >3000)/* 無需推料 */
- KZW='b';
- }
- void TL()
- {
- EN4=1; //推料,電機運轉
- }
- void BT()
- {
- EN4=0; //不推料
- }
- void motor4()
- {
- while(1)
- {
- zhongliangjiance();
- TL();
- Delay_ms(5);
- BT();
- Delay_ms(5);
-
- temp =KZW;
- Delay_ms(2);
- if(count<16)
- {
- RXDdata[count]=KZW;
- count++;
- if(count>1)
- playflag=1;
- }
- Delay_ms(5);
- play();
- }
- }
- //void motor4()
- //{
- //// u8 i;
- //// EN4=0;
- // if(Weight_Shiwu >= 1000)
- // {
- //
- // LCD1602_write_com(0x80+0x40+8);//寫在第二行
- // LCD1602_write_word("A");
- // }
- // else
- // {
- //// if(Weight_Shiwu==0)
- //// LCD1602_write_com(0x80+0x40+8);//寫在第二行
- //// LCD1602_write_word("B");
- //// else if(Weight_Shiwu<500)
- // LCD1602_write_com(0x80+0x40+8);//寫在第二行
- // LCD1602_write_word("C");
- // Display_Weight();
- // }
- // EN4 = (3<= Weight_Shiwu) && (Weight_Shiwu < 7);
- //
- //}
- //void tuiliaodianji()
- //{
- // EN4 = 0;
- // Weight_Shiwu = HX711_Read();
- // if (Weight_Shiwu <=50)
- // {
- // u8 i;
- // for (i=0;i<100;i++)
- // {
- // EN4 = 1;
- // //LCD1602_write_word("A");
- // Delay_ms(3000);
- // }
- // if (Weight_Shiwu >=50)
- // EN4 = 0 ;
- // }
- // else
- // {
- // EN4 = 0 ;
- // // LCD1602_write_word("B");
- // }
- //
- //}
- //void ShowChar()
- //{
- // //{static bit Run_Flag;
- // if (Weight_Shiwu <= 1)
- // {
- // // u8 i;
- // // if(!Run_Flag)
- // // {
- // // for (i=0; i<100; i++)
- // // {
- // // EN4 = 1;
- // LCD_Init();
- // LCD1602_ShowChar(2,3,"A");
- // Delay_ms(3000);
- // }
- // // if (Weight_Shiwu >=50)
- // //EN4 = 0 ;
- // // Run_Flag = 1;
- // // }
- // // }
- // else
- // {
- // // EN4 = 0 ;
- // LCD_Init();
- // LCD1602_ShowChar(2,3,"B");
- // // Run_Flag = 0;
- // }
- ////}
復制代碼
|