|
水的渾濁度是指水中含有的泥沙,粘土,有機(jī)物,浮游生物和微生物等懸浮
物質(zhì),造成的渾濁程度。工業(yè)級的濁度傳感器或濁度儀價(jià)格昂貴,在電子產(chǎn)品設(shè)
計(jì)中成本太高不適合選用;因此我們選取了一款在家用電器洗衣機(jī)、洗碗機(jī)上廣
泛應(yīng)用的渾濁度傳感器,這款濁度傳感器利用光學(xué)原理,通過溶液中的透光率和
散射率來綜合判斷濁度情況。傳感器內(nèi)部是一個(gè)紅外線對管,當(dāng)光線穿過一定量
的水時(shí),光線的透過量取決于該水的污濁程度,水越污濁,透過的光就越少。光
接收端把透過的光強(qiáng)度轉(zhuǎn)換為對應(yīng)的電流大小,透過的光多,電流大,反之透過
的光少,電流小。
濁度傳感器模塊將傳感器輸出的電流信號轉(zhuǎn)換為電壓信號,通過單片機(jī)進(jìn)行
AD 轉(zhuǎn)換數(shù)據(jù)處理。改款模塊具有模擬量和數(shù)字量輸出接口。模擬量可通過單片
機(jī) A/D 轉(zhuǎn)換器進(jìn)行采樣處理,以獲知當(dāng)前水的污濁度。數(shù)字量可通過模塊上的
電位器調(diào)節(jié)觸發(fā)閾值,當(dāng)濁度達(dá)到設(shè)置好的閾值后,D1 指示燈會被點(diǎn)亮,傳感
器模塊輸出由高電平變成低電平,單片機(jī)通過監(jiān)測電平的變化,判斷水的濁度是
否超標(biāo),從而預(yù)警或者聯(lián)動其他設(shè)備。該模塊價(jià)格低廉、使用方便、測量精度高
可以用于洗衣機(jī)、洗碗機(jī)等產(chǎn)品的水污濁程度的測量;也可以用于工業(yè)現(xiàn)場控制,
環(huán)境污水采集等需要濁度檢測控制的場合。
* 硬件連接:
* ADC0809:OE -> P1.0; EOC -> P1.1; ST -> P1.2;CLK -> P1.3; A -> P1.4; B -> P1.5; C->P1.6;
* OUT1~OUT8 -> P3口
* LCD1602:RS -> P0.0; RW -> P0.1; E -> P0.2;
* D0~D7 -> P2口
* PH模塊: VCC -> 5V; GND -> GND; PO -> IN3;
/************************************************************************************
* 文件名 :main.c
* 描述 :
* 硬件連接:
* ADC0809:OE -> P1.0; EOC -> P1.1; ST -> P1.2;CLK -> P1.3; A -> P1.4; B -> P1.5; C->P1.6;
* OUT1~OUT8 -> P3口
* LCD1602:RS -> P0.0; RW -> P0.1; E -> P0.2;
* D0~D7 -> P2口
* 濁度傳感器模塊: VCC -> 5V; GND -> GND; PO -> IN3;
*
* 實(shí)驗(yàn)平臺:
*
**********************************************************************************/
#include <reg52.h>
#include <adc0809.h>
#include <1602.h>
#include <intrins.h>
#include <math.h>
float TU=0.0;
float TU_value=0.0;
float TU_calibration=0.0;
unsigned char disbuff[13]={0};
float temp_data=25.0;
float K_Value=3047.19;
uint num;
/**************延時(shí)1s函數(shù)***************/
void Delay_1s()
{
unsigned char a,b,c;
for(c=167;c>0;c--)
for(b=171;b>0;b--)
for(a=16;a>0;a--);
}
/**************PH值采集函數(shù)***************/
void TU_Collection()
{
Red_0809 () ;
TU =num * 5.0 / 256;
TU_calibration=-0.0192*(temp_data/10-25)+TU;
TU_value=-865.68*TU_calibration + K_Value;
if(TU_value<=0){TU_value=0;}
if(TU_value>=3000){TU_value=3000;}
}
/**************PH值顯示函數(shù)***************/
void TU_Display()
{
disbuff[0]=(int)(TU_value)/1000+'0';
disbuff[1]=(int)(TU_value)%1000/100+'0';
disbuff[2]=(int)(TU_value)%100/10+'0';
disbuff[3]=(int)(TU_value)%10+'0';
DisplaySingleChar(0x3,1,disbuff[0]);
DisplaySingleChar(0x4,1,disbuff[1]);
DisplaySingleChar(0x5,1,disbuff[2]);
DisplaySingleChar(0x6,1,disbuff[3]);
DisplaySingleChar(0x7,1,disbuff[4]);
}
/**************主函數(shù)***************/
void main()
{
TMOD = 0x02; //中斷初始化,adc0809產(chǎn)生時(shí)鐘信號
TH0 = 0x14;
TL0 = 0x00;
IE = 0x82;
TR0 = 1;
P1 = 0x3f;
LCDInit(); //LCD初始化
DisplayString(0x0,0," Welcome TU ");
DisplayString(0x0,1,"Detection system");
Delay_1s();
WriteCmd(LCD_CLS); //清屏
DisplaySingleChar(0x0,1,'T');
DisplaySingleChar(0x1,1,'U');
DisplaySingleChar(0x2,1,'=');
DisplaySingleChar(0x8,1,'p');
DisplaySingleChar(0x9,1,'p');
DisplaySingleChar(0x0A,1,'m');
while(1)
{
TU_Collection();
TU_Display();
}
}
void Timer0_INT() interrupt 1
{
CLK = !CLK;
}
#ifndef ADC0809_H
#define ADC0809_H
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
extern uint num;
sbit OE = P1^0;
sbit EOC = P1^1;
sbit ST = P1^2;
sbit CLK = P1^3;
void Red_0809 ();
#endif
#include "adc0809.h"
void Red_0809 ()
{
ST = 0;
ST = 1;
ST = 0;
while(EOC == 0);
OE = 1;
num = P3;
OE = 0;
}
#ifndef _1602_H
#define _1602_H
#include <reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
#define Busy 0x80 //忙信號
#define LCD_Data P2
#define Write 0x00 //低電平寫入
#define Read 0x01 //高電平讀出
#define Data 0x01 //高電平選擇數(shù)據(jù)
#define Cmd 0x00 //低電平選擇命令
#define Enable 0x00 //躍變到低電平時(shí)執(zhí)行命令
#define Disable 0x01
#define True 0x01
#define False 0x00
#define LCD_Init 0x38 //初始化模式
#define LCD_DispCtr 0x0C //開顯示及光標(biāo)設(shè)置
#define LCD_CloseCtr 0x08 //關(guān)顯示
#define LCD_CLS 0x01 //清屏幕
#define LCD_EnterSet 0x06 //顯示光標(biāo)
sbit LCD_RS =P0^0; //數(shù)據(jù)命令選擇端
sbit LCD_RW =P0^1; //讀寫控制端
sbit LCD_E =P0^2; //執(zhí)行使能端
/*------------------------------- LCD模塊 -------------------------------------*/
void LCDInit(void); //LCD初始化
void SetWriteCmd(void); //設(shè)置寫命令模式
void SetReadCmd(void); //設(shè)置讀命令模式
void SetWriteData(void); //設(shè)置寫數(shù)據(jù)模式
void WriteCmd(char cmd); //寫命令
void WriteData(char ddata); //寫數(shù)據(jù)
void ExecuteCmd(void); //執(zhí)行命令
void SetXY(char x,char y); //定位顯示地址
void DisplaySingleChar(char x,char y,char cchar); //顯示單個(gè)字符
void DisplayString(char x,char y,char *str); //顯示一段字符串
void Delay(unsigned int time); //延時(shí)主程序
void DelayUs(unsigned int time); //延時(shí)子程序
//void delay500ms(void) ;
bit IsBusy(void); //判斷忙標(biāo)志函數(shù)
void DisplayTime(void); //顯示時(shí)間
void DisplayAVGSpeed(void); //顯示平均速度
void DisplayDistance(void); //顯示路程
void DelayUs(unsigned int time); //延時(shí)函數(shù)
#endif
#include "1602.h"
void Delay(unsigned int time)
{
unsigned int timeCounter = 0;
for (timeCounter = time;timeCounter > 0 ;timeCounter --)
DelayUs(255);
}
void DelayUs(unsigned int time)
{
unsigned int timeCounter = 0;
for (timeCounter = 0;timeCounter < time;timeCounter ++)
_nop_();
}
/*--------------------------------- LCD初始化函數(shù)--------------------------------------*/
void LCDInit(void)
{
//三次顯示模式設(shè)置
LCD_Data=0;
LCD_E=Disable;
Delay(5);
WriteCmd(LCD_Init);
Delay(5);
WriteCmd(LCD_Init);
Delay(5);
WriteCmd(LCD_Init);
WriteCmd(LCD_Init); //初始化
WriteCmd(LCD_CloseCtr); //關(guān)顯示
WriteCmd(LCD_CLS); //清屏幕
WriteCmd(LCD_EnterSet); //光標(biāo)移動設(shè)置
WriteCmd(LCD_DispCtr); //顯示開以及光標(biāo)設(shè)置
}
/*--------------------------------- LCD模式設(shè)置函數(shù)--------------------------------------*\
SetWriteCmd() 設(shè)置LCD為寫命令模式
SetReadCmd() 設(shè)置LCD為讀命令模式
SetWriteData() 設(shè)置LCD為寫數(shù)據(jù)模式
\*----------------------------------------------------------------------------------------*/
void SetWriteCmd(void)
{
LCD_RW=Write;
LCD_RS=Cmd;
}
void SetReadCmd(void)
{
LCD_RW=Read;
LCD_RS=Cmd;
}
void SetWriteData(void)
{
LCD_RW=Write;
LCD_RS=Data;
}
/*--------------------------------- LCD功能執(zhí)行函數(shù)--------------------------------------*\
WriteCmd() 寫命令
WriteData() 寫數(shù)據(jù)
ExecuteCmd() 執(zhí)行命令
SetXY() 顯示定位
DisplaySingleChar() 顯示單個(gè)字符
DisplayString() 顯示一串字符
IsBusy() 忙標(biāo)志檢測
\*----------------------------------------------------------------------------------------*/
void WriteCmd(char cmd)
{
while(IsBusy());
LCD_Data=cmd;
SetWriteCmd();
ExecuteCmd();
}
void WriteData(char ddata)
{
while(IsBusy());
LCD_Data=ddata;
SetWriteData();
ExecuteCmd();
}
void ExecuteCmd(void)
{
LCD_E=Enable;
LCD_E=Disable;
}
void SetXY(char x,char y)
{
if (y)
x|=0x40;
x|=0x80;
Delay(5);
WriteCmd(x);
}
void DisplaySingleChar(char x,char y,char cchar)
{
Delay(5);
SetXY(x,y);
WriteData(cchar);
}
void DisplayString(char x,char y,char *str)
{
while(*str)
{
Delay(5);
DisplaySingleChar(x++,y,*str);
str++;
}
}
bit IsBusy(void)
{
LCD_Data=0xFF;
SetReadCmd();
ExecuteCmd();
return (bit)(LCD_Data & 0x80);
}
|
|