自己做的,不太完整。超聲波很準
所有資料51hei提供下載:
1號水情板.rar
(321.52 KB, 下載次數: 26)
2017-11-8 03:23 上傳
點擊文件名下載附件
單片機源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "sys.h"
- #include "key.h"
- #include "usart.h"
- #include "exti.h"
- #include "timer.h"
- #include "UltrasonicWave.h"
- #include "lcd12864.h"
- #include "adc.h"
- extern float temp2;
- float temp3;
- int num1,num2,num3;
- ///////////////////////////////////***************************************************//////////////////////////////////////////////////////////
- u16 Wtemp,Wtemp1;
- float AD,AD1,ad1,PH,fPH,fU;
- u16 ph,U,u;
- /////////////////////////////////****************************************************///////////////////////////////////////////////////////////
- u16 i,j,n;
- void PHnum(void);
- int main(void)
- {
- // SystemInit();
- delay_init(72); //延時初始化
- NVIC_Configuration();
- uart_init(9600); //串口初始化
- LED_Init(); //LED端口初始化
- KEY_Init(); //按鍵端口初始化
- Timerx_Init(0xffff,71); //10Khz的計數頻率,計數到5000為500ms
- UltrasonicWave_Configuration(); //對超聲波模塊初始化
- Adc_Init();
- LCD_init();
- LCD_Putstring(1,2,"水情檢測系統");
- LCD_Putstring(3,1,"PH :");
- LCD_Putstring(3,2,"值");
- LCD_Putstring(2,1,"水位:");
- LCD_Putstring(4,1,"輸出電壓:");
- LCD_Putstring(4,8,"V");
- LCD_Putstring(2,6,"mm");
- while(1)
- {
-
- for(n=0;n<10;n++)
- {
- Wtemp1=Get_Adc_Average(ADC_Channel_1,10);
- AD1=Wtemp1*3.3/4096;
- ad1=AD1*2;
- U=ad1;
- fU=ad1-U;
- u=(int)(fU*100);
- LCD_Setaddress(4,6);
- LCD_write_dat(0x30+U);
- LCD_write_dat(0x20+14);
- LCD_write_dat(0x30+u/10);
- LCD_write_dat(0x30+u%10);
- }
- for(i=0;i<100;i++)
- {
- UltrasonicWave_StartMeasure(); //開始測距,發送一個>10us的脈沖,然后測量返回的高電平時間
- temp3=temp2;
- temp3=2540-temp3;
- // if((temp3>395)&&(temp3<448))
- // {
- // temp3=temp3+50;
- // }
- // if((temp3>1288)&&(temp3<1680))
- // {
- // temp3=temp3+43;
- // }
- num1=temp3;
- if(num1<0)num1=0;
- // num2=(temp3-num1)*100;
- printf("JU %d\n",num1);
- LCD_Setaddress(2,4);
- //delay_ms(1000);
- LCD_write_dat(0x30+(int)num1/1000);
- LCD_write_dat(0x30+(int)num1/100%10);
- // LCD_write_dat(0x20+14);
- LCD_write_dat(0x30+(int)num1/10%10);
- // LCD_write_dat(0x30+(int)num1%10);
- }
-
- for(j=0;j<10;j++)
- {
- Wtemp=Get_Adc_Average(ADC_Channel_0,40);
- AD=Wtemp*3.3/4096+0.165;
- printf("PHad:%f\n",AD*2);
- AD=(int)(AD*1000);
- PHnum(); //PH值修正
- // temp2=2121-AD;
- // PH=temp2*0.0112721417096;
- ph=PH;
- fPH=(PH-ph)*100;
- printf("-----------PH:%f\n",PH);
- LCD_Setaddress(3,4);
- LCD_write_dat(0x30+ph);
- LCD_write_dat(0x20+14);
- LCD_write_dat(0x30+(int)fPH/10);
- }
-
-
- }
- }
-
-
- void PHnum()
- {
- if((2032.5<AD)&&(AD<2121)){ PH=(2121-AD)*0.0112994350282;}
- if((1942.5<AD)&&(AD<=2032.5)){ PH=1+(2032.5-AD)*0.0111111111111;}
- if((1854.9<AD)&&(AD<=1942.5)){ PH=2+(1942.5-AD)*0.0114155251141-0.128;}
- if((1766.25<AD)&&(AD<=1854.9)){ PH=3+(1854.9-AD)*0.0112803158488-0.151;}
- if((1677<AD)&&(AD<=1766.25)){ PH=4+(1766.25-AD)*0.0112044817927-0.05;}
- if((1588.75<AD)&&(AD<=1677)){ PH=5+(1677-AD)*0.0113314447592068+0.135;}
- if((1500<AD)&&(AD<=1588.75)){ PH=6+(1588.75-AD)*0.0112676056338028+0.278;}
- if((1411.25<AD)&&(AD<=1500)){ PH=7+(1500-AD)*0.0112676056338028-0.10;}
- if((1323<AD)&&(AD<=1411.25)){ PH=8+(1411.25-AD)*0.0113314447592068;}
- if((1233.75<AD)&&(AD<=1323)){ PH=9+(1323-AD)*0.0112044817927;}
- }
復制代碼
|