PH值检测C语言程序,单片机PH值检测鱼缸自动换水系统程序
/******************** (C) COPYRIGHT 2015 Silicone shell electronic studio **************************
* 文件名??:main.c
* 描述? ? :
* 硬件連接:
*
* 實(shí)驗(yàn)平臺(tái):STC12C5A60S2開(kāi)發(fā)板
*https://www.bilibili.com/video/BV1rK411V7xa/
**********************************************************************************/
#include
#include
#include
#include
#define? ?? ???uchar? ?? ???unsigned char
#define uint? ?? ???unsigned int
#define MAIN_Fosc? ?? ?? ?? ?? ? 11059200L? ?? ???//定義主時(shí)鐘, 模擬串口和和延時(shí)會(huì)自動(dòng)適應(yīng)。5~35MHZ??22118400L
#define ADC_OFF()? ?? ???ADC_CONTR = 0
#define ADC_ON? ?? ?? ?? ?? ? (1 << 7)
#define ADC_90T? ?? ?? ?? ?? ? (3 << 5)
#define ADC_180T? ?? ???(2 << 5)
#define ADC_360T? ?? ???(1 << 5)
#define ADC_540T? ?? ???0
#define ADC_FLAG? ?? ???(1 << 4)? ?? ???//軟件清0
#define ADC_START? ?? ???(1 << 3)? ?? ???//自動(dòng)清0
#define ADC_CH0? ?? ?? ?? ?? ? 0
#define ADC_CH1? ?? ?? ?? ?? ? 1
#define ADC_CH2? ?? ?? ?? ?? ? 2
#define ADC_CH3? ?? ?? ?? ?? ? 3
#define ADC_CH4? ?? ?? ?? ?? ? 4
#define ADC_CH5? ?? ?? ?? ?? ? 5
#define ADC_CH6? ?? ?? ?? ?? ? 6
#define ADC_CH7? ?? ?? ?? ?? ? 7
sbit k1 = P2^0;
sbit k2 = P2^1;
sbit k3 = P2^2;
sbit fs_led = P3^3;
sbit ss_led = P3^4;
sbit yw_up = P3^6;
sbit yw_down = P3^7;
/**************變量定義***************************/
float PH=0.0;
float PH_value=0.0;
float ph1=0.0,ph2=0.0;
float ph_h=0.0, ph_h_2=0.0;
unsigned int temp_data=0;
unsigned char disbuff[14]={0};
void DelayUs(unsigned int time);? ?//延時(shí)子函數(shù)
void Delay1(unsigned int time);? ?? ?? ?? ?? ???//延時(shí)主程序
void??delay_ms_AD(unsigned char ms);
void??press_k1();
void??press_k2();
void??press_k3();
void Display_PH1();
void Display_PH2();
void Display_time_set();
uint? ?? ???adc10_start(uchar channel);? ?? ???//channel = 0~7
uint? ?? ???key_flag = 0,turnwater=0,flag1=0;
uint count1 = 0;
uint i;
void PH_Value_Conversion()
{
PH=(PH*5)/1024;
PH_value=-5.8887*PH+21.677;
if((PH_value<=0)){PH_value=0;}
if((PH_value>14)){PH_value=14.1;}
/*顯示酸堿度*/
disbuff[0]=(int)(PH_value*100)/1000+'0';
disbuff[1]=(int)(PH_value*100)%1000/100+'0';
disbuff[2]='.';
disbuff[3]=(int)(PH_value*100)%100/10+'0';
disbuff[4]=(int)(PH_value*100)%10+'0';
DisplaySingleChar(0x03,1,disbuff[0]);
DisplaySingleChar(0x04,1,disbuff[1]);
DisplaySingleChar(0x05,1,disbuff[2]);
DisplaySingleChar(0x06,1,disbuff[3]);
DisplaySingleChar(0x07,1,disbuff[4]);
}
void TEMP_Value_Conversion()? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //溫度
{
disbuff[9]=(int)(temp_data)/1000+'0';
disbuff[10]=(int)(temp_data)%1000/100+'0';
disbuff[11]=(int)(temp_data)%100/10+'0';
disbuff[12]='.';
disbuff[13]=(int)(temp_data)%10+'0';
DisplaySingleChar(0x0b,1,disbuff[10]);
DisplaySingleChar(0x0c,1,disbuff[11]);
DisplaySingleChar(0x0d,1,disbuff[12]);
DisplaySingleChar(0x0e,1,disbuff[13]);
}
/*
* 函數(shù)名:main
* 描述??:主函數(shù)
* 輸入??:無(wú)
* 輸出??:無(wú)
*/
void main(void)
{
ss_led = 0;
fs_led = 0;
Init_Ds18b20();
LCDInit();? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?//顯示屏初始化
DisplayString(0x0,0,"??Welcome...??");
DisplayString(0x0,1,"water quality");
Delay1(2500);
WriteCmd(LCD_CLS);
P1ASF = 0x07;? ?? ?? ?? ?? ?? ?? ?? ?//12C5A60AD/S2系列模擬輸入(AD)選擇
ADC_CONTR = ADC_360T | ADC_ON;
ph1 = 5.0;
ph2 = 6.0;
while(1)
{
press_k1();
press_k2();
press_k3();
if (count1 == 0)
{
DisplayString(0x02,0,"*");
DisplayString(0x09,0," ");
}
if (count1 == 1)
{
DisplayString(0x02,0," ");
DisplayString(0x09,0,"*");
}
DisplayString(0x0,1,"PH:");
DisplayString(0x09,1,"T:");
Display_time_set();
for(i=0; i<2; i++)
{
Delay1(500);
if(i==0)
{
PH = adc10_start(0);
PH_Value_Conversion();
}
if(i==1)
{
temp_data=Get_temp();
TEMP_Value_Conversion();
}
}
if (PH_value < ph1 || PH_value > ph2)
{
turnwater = 1;
}
if( turnwater == 1 )
{
if(yw_down == 0 && flag1 == 0)? ?? ? //放水過(guò)程
{
ss_led = 0;
fs_led = 1;
}
if(yw_down == 1 && flag1 == 0)? ?? ? //放水完畢,開(kāi)始上水
{
ss_led = 1;
fs_led = 0;
flag1 = 1;
}
if(yw_up == 0 && flag1 == 1)? ?? ?//上水完畢,關(guān)繼電器
{
ss_led = 0;
fs_led = 0;
turnwater = 0;
flag1=0;
}
}
}
}
///********************* 做一次ADC轉(zhuǎn)換 *******************/
uint? ?? ???adc10_start(uchar channel)? ?? ???//channel = 0~7
{
uint? ?? ???adc;
uchar? ?? ???i;
ADC_RES = 0;
ADC_RESL = 0;
ADC_CONTR = (ADC_CONTR & 0xe0) | ADC_START | channel;
i = 250;
do{
if(ADC_CONTR & ADC_FLAG)
{
ADC_CONTR &= ~ADC_FLAG;
adc = (uint)ADC_RES;
adc = (adc << 2) | (ADC_RESL & 3);
return? ?? ???adc;
}
}while(--i);
return? ?? ???1024;
}
//========================================================================
// 函數(shù): void??delay_ms(unsigned char ms)
// 描述: 延時(shí)函數(shù)。
// 參數(shù): ms,要延時(shí)的ms數(shù).
// 返回: none.
// 版本: VER1.0
// 日期: 2010-12-15
// 備注:
//========================================================================
void??delay_ms_AD(unsigned char ms)
{
unsigned int i;
do{
i = MAIN_Fosc / 14000;
while(--i)? ?? ???;? ?//14T per loop
}while(--ms);
}
//========================================================================
void Display_time_set()
{
Display_PH1();
Display_PH2();
}
void Display_PH1()
{
uint ph_h1, ph_h2,ph_h3,ph_h4;
ph_h = ph1 * 100;
ph_h1 = (int)ph_h/1000;
ph_h2 = (int)ph_h%1000/100;
ph_h3 = (int)ph_h%100/10;
ph_h4 = (int)ph_h%10;
DisplaySingleChar(0x03,0,ph_h1+0x30);
DisplaySingleChar(0x04,0,ph_h2+0x30);
DisplaySingleChar(0x05,0,'.');
DisplaySingleChar(0x06,0,ph_h3+0x30);
DisplaySingleChar(0x07,0,ph_h4+0x30);
}
void Display_PH2()
{
uint ph_h1, ph_h2,ph_h3,ph_h4;
ph_h_2 = ph2 * 100;
ph_h1 = (int)ph_h_2/1000;
ph_h2 = (int)ph_h_2%1000/100;
ph_h3 = (int)ph_h_2%100/10;
ph_h4 = (int)ph_h_2%10;
DisplaySingleChar(0x0a,0,ph_h1+0x30);
DisplaySingleChar(0x0b,0,ph_h2+0x30);
DisplaySingleChar(0x0c,0,'.');
DisplaySingleChar(0x0d,0,ph_h3+0x30);
DisplaySingleChar(0x0e,0,ph_h4+0x30);
}
//========================================================================
// 描述: 鍵盤(pán)掃描。
void press_k1()
{
if (k1 == 0)
{
Delay1(80);
if (k1 == 0)
{
//key_flag = 1;
count1++;
if (count1 ==2)
{
count1 = 0 ;
}
}
}
}
void press_k2()
{
if (k2 == 0)
{
Delay1(100);
if (k2 == 0)
{
if (count1 == 0)
{
ph1+=0.1;
if(ph1>=14)
{
ph1 = 0;
}
}
if (count1 == 1)
{
ph2+=0.1;
if(ph2>=14)
{
ph2 = 0;
}
}
}
}
}
void press_k3()
{
if (k3 == 0)
{
Delay1(100);
if (k3 == 0)
{
if (count1 == 0)
{
ph1-=0.1;
if(ph1<=0)
{
ph1 = 14;
}
}
if (count1 == 1)
{
ph2-=0.1;
if(ph2<=0)
{
ph2 = 14;
}
}
}
}
}
//延時(shí)函數(shù)//
void Delay1(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_();
}
//void? ?? ???BitTime(void)? ?? ???//位時(shí)間函數(shù)
//{
//? ?? ???uint i;
//? ?? ???i = ((MAIN_Fosc / 100) * 104) / 140000L - 1;? ?? ?? ?? ?? ? //根據(jù)主時(shí)鐘來(lái)計(jì)算位時(shí)間
//? ?? ???while(--i);
//}
//模擬串口發(fā)送
//void? ?? ???Tx1Send(uchar dat)? ?? ?? ?? ?? ? //9600,N,8,1? ?? ?? ?? ?? ? 發(fā)送一個(gè)字節(jié)
//{
//? ?? ???uchar? ?? ???i;
//? ?? ???EA = 0;
//? ?? ???P_TXD1 = 0;
//? ?? ???BitTime();
//? ?? ???for(i=0; i<8; i++)
//? ?? ???{
//? ?? ?? ?? ?? ? if(dat & 1)? ?? ?? ?? ?? ? P_TXD1 = 1;
//? ?? ?? ?? ?? ? else? ?? ?? ?? ?? ?? ?? ?? ?P_TXD1 = 0;
//? ?? ?? ?? ?? ? dat >>= 1;
//? ?? ?? ?? ?? ? BitTime();
//? ?? ???}
//? ?? ???P_TXD1 = 1;
//? ?? ???EA = 1;
//? ?? ???BitTime();
//? ?? ???BitTime();
//}
總結(jié)
以上是生活随笔為你收集整理的PH值检测C语言程序,单片机PH值检测鱼缸自动换水系统程序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 有哪位大神提供免费的AQWA分析软件的资
- 下一篇: CAD图层文件(DWG)转矢量图层处理