#include "REG52.h"
#include "oled.h"
#include "Delay.h"
#include "OneWire.h"
#include "DS18B20.h"
#include "Key.h"
#include "Timer0.h"
#include "Buzzer.h"
#include "PCF8591.h"
#include "math.h"
#include "wifi.h"
#include "stdio.h"
#include "string.h"
typedef unsigned char uchar;
typedef unsigned char uint;
unsigned char KeyNum,Mode=0,yuzhiSetSelect,yuzhiSetFlashFlag;
char yuzhi[]={44,-10,13,2,89};
uchar buf[]={'A','+',2,3,4,5,0,5,3,6,3, 4, 5, '+',5, 6, '-',1, 0, 1, 4, 0, 0, 9, 9};
uint TDS=0;
float cv;
float PH=0.0,T=0.0;
double ADC_Voltage=0.0;
void PH_Value_Conversion(){
uchar i=2;
EA=0;
while(i--){
ISendByte(PCF8591,0x40);
ADC_Voltage=IRcvByte(PCF8591)*5.0/255;
}
EA=1;
PH=-5.7541*ADC_Voltage+16.654;
if(PH>=14.00){PH=14.00;}
buf[6]=(int)(PH*100)/1000+'0';
buf[7]=(int)(PH*100)%1000/100+'0';
buf[8]=(int)(PH*100)%100/10+'0';
buf[9]=(int)(PH*100)%10+'0';
OLED_ShowChar(24,2,buf[6],16);
OLED_ShowChar(33,2,buf[7],16);
OLED_ShowChar(42,2,'.',16);
OLED_ShowChar(51,2,buf[8],16);
OLED_ShowChar(60,2,buf[9],16);
}
void TDS_Value_Conversion(){
uchar i=2;
EA=0;
while(i--){
ISendByte(PCF8591,0x41);
ADC_Voltage=IRcvByte(PCF8591)*5.0/255;
}
EA=1;
cv=ADC_Voltage/(1.0+0.02*(T-25.0));
TDS=(int)(13342*cv*cv*cv-25586*cv*cv+85739*cv)/200;
buf[10]=(int)(TDS)/100+'0';
buf[11]=(int)(TDS)%100/10+'0';
buf[12]=(int)(TDS)%10+'0';
OLED_ShowNum(32,6,TDS,3,16);
}
void T_Conversion(){
EA=0;
DS18B20_ConvertT();
T=DS18B20_ReadT();
EA=1;
if(T<0) buf[1]='-';
else buf[1]='+';
buf[2]=(int)(T*100)/1000+'0';
buf[3]=(int)(T*100)%1000/100+'0';
buf[4]=(int)(T*100)%100/10+'0';
buf[5]=(int)(T*100)%10+'0';
OLED_ShowChar(16,4,buf[1],16);
OLED_ShowChar(24,4,buf[2],16);
OLED_ShowChar(32,4,buf[3],16);
OLED_ShowChar(40,4,'.',16);
OLED_ShowChar(48,4,buf[4],16);
OLED_ShowChar(56,4,buf[5],16);
}
void yuzhi_Show(){
if(yuzhi[0]<0){buf[13]='-';}
else {buf[13]='+';}
if(yuzhi[1]<0) {buf[16]='-';}
else{buf[16]='+';}
OLED_ShowChar(94,4,buf[13],12);
OLED_ShowChar(94,5,buf[16],12);
OLED_ShowNum(102,4,abs(yuzhi[0]),2,12);
OLED_ShowNum(102,5,abs(yuzhi[1]),2,12);
OLED_ShowNum(72,2,yuzhi[3],2,12);
OLED_ShowNum(96,2,yuzhi[2],2,12);
OLED_ShowNum(96,7,yuzhi[4]*10,3,12);
}
void yuzhi_Set(void){
OLED_ShowNum(120,3,yuzhiSetSelect,1,12);
if(KeyNum==2){
yuzhiSetSelect++;
OLED_ShowNum(120,2,yuzhiSetSelect,1,12);
yuzhiSetSelect%=5;
}
if(KeyNum==3){
yuzhi[yuzhiSetSelect]++;
if(yuzhi[0]>=99){yuzhi[0]=99;}
if(yuzhi[1]>yuzhi[0]){yuzhi[1]--;}
if(yuzhi[2]>=14){yuzhi[2]=14;}
if(yuzhi[3]>yuzhi[2]){yuzhi[3]--;}
if(yuzhi[4]>=99){yuzhi[4]=99;}
}
if(KeyNum==4){
yuzhi[yuzhiSetSelect]--;
if(yuzhi[0]<yuzhi[1]){yuzhi[0]++;}
if(yuzhi[1]<=-55){yuzhi[1]=-55;}
if(yuzhi[2]<yuzhi[3]){yuzhi[2]++;}
if(yuzhi[3]<=0){yuzhi[3]=0;}
if(yuzhi[4]<=0){yuzhi[4]=0;}
}
if(yuzhiSetSelect==0&&yuzhiSetFlashFlag==1){OLED_ShowString(102,4, " ",12);}
if(yuzhiSetSelect==1&&yuzhiSetFlashFlag==1){OLED_ShowString(102,5, " ",12);}
if(yuzhiSetSelect==3&&yuzhiSetFlashFlag==1){OLED_ShowString(72,2, " ",12);}
if(yuzhiSetSelect==2&&yuzhiSetFlashFlag==1){OLED_ShowString(96,2, " ",12);}
if(yuzhiSetSelect==4&&yuzhiSetFlashFlag==1){OLED_ShowString(96,7, " ",12);}
else{yuzhi_Show();}
}
void chai(){
if(yuzhi[0]>=0) buf[13]=(int)'+';
else buf[13]=(int)'-';
buf[14]=(int)abs(yuzhi[0])/10+'0';
buf[15]=(int)abs(yuzhi[0])%10+'0';
if(yuzhi[1]>=0) buf[16]=(int)'+';
else buf[16]=(int)'-';
buf[17]=(int)abs(yuzhi[1])/10+'0';
buf[18]=(int)abs(yuzhi[1])%10+'0';
buf[19]=(int)yuzhi[2]/10+'0';
buf[20]=(int)yuzhi[2]%10+'0';
buf[21]=(int)yuzhi[3]/10+'0';
buf[22]=(int)yuzhi[3]%10+'0';
buf[23]=(int)yuzhi[4]/10+'0';
buf[24]=(int)yuzhi[4]%10+'0';
}
void ifover(){
if(T>yuzhi[0]||T<yuzhi[1]||PH>yuzhi[2]||PH<yuzhi[3]||TDS>(yuzhi[4]*10)){
OLED_ShowCHinese(110,0,9);
Buzzer_Time(100);
}
}
int main(void){
buf[0]='A';
OLED_Init();
OLED_Clear();
OLED_ShowCHinese(0,0,0);
OLED_ShowCHinese(18,0,1);
OLED_ShowCHinese(36,0,2);
OLED_ShowCHinese(54,0,3);
OLED_ShowCHinese(72,0,4);
OLED_ShowCHinese(90,0,5);
OLED_ShowCHinese(68,4,8);
OLED_ShowString(0,4,"T:",16);
OLED_ShowString(0,2,"PH:",16);
OLED_ShowString(0,6,"TDS:",16);
OLED_ShowChar(88,2,'-',12);
OLED_ShowChar(88,7,'<',12);
OLED_ShowString(59,7,"PPM",12);
chai();
yuzhi_Show();
Timer0_Init();
SysInit();
while(1)
{
KeyNum=Key();
T_Conversion();
PH_Value_Conversion();
TDS_Value_Conversion();
if(KeyNum==1){
if(Mode==0){Mode=1;yuzhiSetSelect=0;}
else if(Mode==1){Mode=0;}
OLED_ShowNum(120,2,Mode,1,12);
}
switch(Mode){
case 0:yuzhi_Show();break;
case 1:yuzhi_Set();break;
}
ifover();
sendString("AT+CIPSEND=0,25\r\n");
Delay(100);
sendString(buf);
Delay(100);
sendString("\r\n");
}
}
void Timer0_Routine() interrupt 1
{
static unsigned int T0Count;
TL0 = 0x18;
TH0 = 0xFC;
T0Count++;
if(T0Count>=20)
{
T0Count=0;
yuzhiSetFlashFlag=!yuzhiSetFlashFlag;
Key_Loop();
}
}