找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1729|回复: 0
打印 上一主题 下一主题
收起左侧

单片机PH值检测鱼缸自动换水系统程序

[复制链接]
跳转到指定楼层
楼主
ID:429540 发表于 2020-11-27 19:01 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
/******************** (C) COPYRIGHT 2015 Silicone shell electronic studio **************************
* 文件名  :main.c
* 描述    :        
* 硬件连接:
*
* 实验平台:STC12C5A60S2开发板
*https://www.bilibili.com/video/BV1rK411V7xa/
**********************************************************************************/
#include <STC12C5A60S2.h>
#include <intrins.h>
#include <lcd.h>
#include <DS18B20.h>


#define        uchar        unsigned char
#define uint        unsigned int


#define MAIN_Fosc                11059200L        //定义主时钟, 模拟串口和和延时会自动适应。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)        //自动清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);   //延时子函数
void Delay1(unsigned int time);                 //延时主程序
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]);        




}


/*
* 函数名:main
* 描述  :主函数
* 输入  :无
* 输出  :无
*/
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)       //放水过程
                        {
                                 ss_led = 0;
                                 fs_led = 1;
                        }
                        if(yw_down == 1 && flag1 == 0)       //放水完毕,开始上水
                        {
                                ss_led = 1;
                                 fs_led = 0;
                                flag1 = 1;
                                
                        }
                        if(yw_up == 0 && flag1 == 1)      //上水完毕,关继电器
                        {
                                ss_led = 0;
                                 fs_led = 0;
                                 turnwater = 0;        
                                  flag1=0;
                                
                        }
                                
                }








                }


        


        }


///********************* 做一次ADC转换 *******************/
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;
}


//========================================================================
// 函数: void  delay_ms(unsigned char ms)
// 描述: 延时函数。
// 参数: ms,要延时的ms数.
// 返回: 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);        
}


//========================================================================
// 描述: 键盘扫描。
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;
                                }
                        }        
                  


                        }
                }
        }


//延时函数//
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)        //位时间函数
//{
//        uint i;
//        i = ((MAIN_Fosc / 100) * 104) / 140000L - 1;                //根据主时钟来计算位时间
//        while(--i);
//}


//模拟串口发送
//void        Tx1Send(uchar dat)                //9600,N,8,1                发送一个字节
//{
//        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();
//}


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表