切换到宽版
爱科技/爱创意/爱折腾;电子/数码爱好者的家!欢迎访问新版数码之家网站
  • 3026阅读
  • 18回复

[C51]请大师帮忙看看这个是什么问题 [复制链接]

上一主题 下一主题
离线数码家园
 

发帖
5228
M币
16486
专家
31
粉丝
540
只看楼主 倒序阅读 我要置顶 楼主  发表于: 2018-08-16
一次在坛里无意中找到的一个ds1302多组定时开关程序,原程序只有一路24组定时时间,想改成四路6组定时时间,折腾了好久,经过砍砍补补下,四路显示和控制是弄出来了,当然对于不懂编程的我肯定是瞎弄的了,也许都不是按规则来的。现在黔驴技穷了,特来请教一下大师。
有两个问题:一是:第一路能断电保存,其他3路不能,请问是什么原因?二是:设置定时时间,第一路6组都没问题,第二路第五组六组时间就不对了,第三路 第四路都有问题,设置的时间和状态会乱掉,请问这个又是什么原因呢?请大师帮忙指点一下,不胜感激!!!

上传百度网盘,里面有仿真文件:链接: https://pan.baidu.com/s/1EaR1a_MfVNf52QJsK2MGcQ 密码: 7h9g

不喜欢下载的请看下面代码,不过有点长
主程序#include <REG52.H>
#include <intrins.h>#include "main.h"
#include "delay.h"#include "iic.h"
#include "DS1302.h"#include "1602.h"
void Read_Time()        //读取DS1302时间{        
    sec = Read(DS1302_SECOND_READ);     min = Read(DS1302_MINUTE_READ);
    hour= Read(DS1302_HOUR_READ);     week= Read(DS1302_WEEK_READ);
    day = Read(DS1302_DAY_READ);    mon = Read(DS1302_MONTH_READ);
    year= Read(DS1302_YEAR_READ);}

void write_sfm(uchar x,add , dat)  //1602写数据{
    uchar a,shi,ge;    
    shi=dat/10;    ge =dat%10;
        if(x)            //0是第二行,1则是第一行
        a=0x80;    else
        a=0xc0;        //0x80+0x40=0xc0    
    write_cmd(a+add);          //写地址    write_dat(0x30+shi);    //0x30是1602内部字符(驱动HD44780)
    write_dat(0x30+ge);}

void write_fuhao(uchar x,add , uchar *dat)    //1602写字符串{                                                                            //单个字符用''字符串请用""号!!!
    uchar a;    
    if(x)    //0是第二行,1则是第一行        a=0x80;
    else        a=0xc0;
        write_cmd(a+add);          //写地址
        while(*dat != '\0')            
    {        write_dat(*dat);
        dat++;    }
}

void Disp()  //1602显示程序{
    write_sfm(0,0,hour);   //时    write_fuhao(0,2,":");
    write_sfm(0,3,min);         //分    write_fuhao(0,5,":");
    write_sfm(0,6,sec);         //秒        write_sfm(1,0,20);
    write_sfm(1,2,year);   //年    write_fuhao(1,4,"/");
    write_sfm(1,5,mon);         //月    write_fuhao(1,7,"/");
    write_sfm(1,8,day);         //天

    if(kzs==1)        write_fuhao(1,12,"ON ");
    if(kzs==2)        write_fuhao(1,12,"AUTO");
    if(kzs==3)        write_fuhao(1,12,"OFF ");

}

void ddms()            //掉电模式{
    if(di==1)            //1:进入掉电模式,2:退出掉电模式    {
        PCON=2;        //进入掉电模式    }
}

uchar get_key(){        
    uchar k;    
    if(set==0)            //菜单 兼 设置 键    {
        delay_ms(10);        if(set==0)
        {            while(!set);
            k=1;        }
    }    
    if(up==0)            //确认 兼加 键    {
        delay_ms(10);        if(up==0)
        {            while(!up);
            k=2;        }
    }        
    if(down==0)            //退出 兼 减 键    {
        delay_ms(10);        if(down==0)
        {            while(!down);
            k=3;        }
    }    return k;
}

void bibi(uchar c)            //报警 、蜂鸣器{
    uchar a,j;    for(a=10;a>0;a--)
    {        Sound=!Sound;
        BJ=!BJ;        delay_ms(8);
    }    for(j=c;j>0;j--);

    Sound=1;    BJ=1;
}

void bs()        // 继电器控制{
    uchar z,x=4,j=0;    
    if(hour>=6 && hour<18)    {
        BL=1;            //关闭1602背光灯    }

    if(hour<6 && hour>18)    {
        BL=0;            //开启1602背光灯    }
        if(kzs==1)            //控制总开关   关闭
        SW=0;                        if(kzs==2)            //自动控制
    {        for(z=1;z<25;z++)            
        {            if(kz[z][0]==hour && kz[z][1]==min)    //如果时分都相同则。。。
            {                if(kz[z][2]==1)            //判断每组控制是否开启,如果是则。。
                {                    SW=0;        //继电器开
                }                else
                    if(kz[z][2]==0)                    {            
                        SW=1;                    }
            }        }
    }    if(kzs==3)
        SW=1;            //控制总开关   关闭}

void bs1()        {
    uchar z,x=4,j=0;    if(kzs==1)            //控制总开关   关闭
        SW1=0;                        if(kzs==2)            //自动控制
    {        for(z=1;z<25;z++)            
        {            if(kz1[z][0]==hour && kz1[z][1]==min)    //如果时分都相同则。。。
            {                if(kz1[z][2]==1)            //判断每组控制是否开启,如果是则。。
                {                    SW1=0;        //继电器开
                }                else
                    if(kz1[z][2]==0)                    {            
                        SW1=1;                    }
            }        }
    }    if(kzs==3)
        SW1=1;            //控制总开关   关闭}

void bs2()        {
    uchar z,x=4,j=0;    if(kzs==1)            //控制总开关   关闭
        SW2=0;                        if(kzs==2)            //自动控制
    {        for(z=1;z<25;z++)            
        {            if(kz2[z][0]==hour && kz2[z][1]==min)    //如果时分都相同则。。。
            {                if(kz2[z][2]==1)            //判断每组控制是否开启,如果是则。。
                {                    SW2=0;        //继电器开
                }                else
                    if(kz1[z][2]==0)                    {            
                        SW2=1;                    }
            }        }
    }    if(kzs==3)
        SW2=1;            //控制总开关   关闭}

void bs3()        {
    uchar z,x=4,j=0;    if(kzs==1)            //控制总开关   关闭
        SW3=0;                        if(kzs==2)            //自动控制
    {        for(z=1;z<25;z++)            
        {            if(kz3[z][0]==hour && kz3[z][1]==min)    //如果时分都相同则。。。
            {                if(kz3[z][2]==1)            //判断每组控制是否开启,如果是则。。
                {                    SW3=0;        //继电器开
                }                else
                    if(kz1[z][2]==0)                    {            
                        SW3=1;                    }
            }        }
    }    if(kzs==3)
        SW3=1;            //控制总开关   关闭}

//////////////////时间调整////////////////////////////////////////////////

void keyscan()         //按键扫描   时间调整{    
    uchar tday;    
    switch(mon)                    //闰年/平年  处理,这个必须在按键扫描上面,否则无效    {
        case 1:tday=31;        break;            case 2:if((year%4)==0)tday=29;
        else tday=28;        break;        case 3:tday=31;        break;
        case 4:tday=30;        break;        case 5:tday=31;        break;
        case 6:tday=30;        break;        case 7:tday=31;        break;
        case 8:tday=31;        break;        case 9:tday=30;        break;
        case 10:tday=31;    break;        case 11:tday=30;    break;
        case 12:tday=31;    break;    }

    if(get_key()==1 || dd==1)        //满足其中一个条件即可    {
        dd=0;        f++;
        bibi(4);    }
        switch(f)            //f 为调整 标志
    {        case 1:    write_cmd(0xc0+7);   //秒
                        write_cmd(0x0f);     //光标闪烁                        Write(0x8e,0x00);         //写保护关        
                        Write(DS1302_SECOND_WRITE,DEC2BCD(sec)|0x80);    //秒最高位 1则 时钟停振        break;

        case 2: write_cmd(0xc0+4);     //分        break;

        case 3:    write_cmd(0xc0+1);     //时        break;
                case 4:    write_cmd(0x80+15);     //控制总开关
        break;        
        case 5:    write_cmd(0x80+9);     //日        break;
                case 6:    write_cmd(0x80+6);     //月
        break;        
        case 7:    write_cmd(0x80+3);     //年        break;        
                case 8:    f=0;                //注意了读出的数是十进制,写到1302要转BCD,切记切记
                        cd1=0;                        write_cmd(0x01);
                        write_cmd(0x0c);   //关光标                        Write(DS1302_SECOND_WRITE,DEC2BCD(sec)&0x7f);    //秒最高位 0则 时钟起振
                        Write(0x8e,0x80);   //写保护开        break;
    }                    
    if(get_key()==2 || dd==2)            //加    {
        dd=0;        bibi(4);
                switch(f)
        {            case 1:    sec++;
                            if(sec==60)                                sec=0;
                            write_sfm(0,6,sec);                            Write(DS1302_SECOND_WRITE,DEC2BCD(sec));
                            write_cmd(0xc0+7);        //光标会自动加一,So... 将光标闪烁调回来            break;
            case 2: min++;                            if(min==60)
                                min=0;                            write_sfm(0,3,min);
                            Write(DS1302_MINUTE_WRITE,DEC2BCD(min));                            write_cmd(0xc0+4);
            break;            case 3: hour++;
                            if(hour==24)                                hour=0;
                            write_sfm(0,0,hour);                            Write(DS1302_HOUR_WRITE,DEC2BCD(hour));
                            write_cmd(0xc0+1);

            break;            case 4: kzs++;            //控制总开关
                            if(kzs==4)                                kzs=1;
                            if(kzs==1)                                write_fuhao(1,12,"ON ");
                            if(kzs==2)                                write_fuhao(1,12,"AUTO");
                            if(kzs==3)                                write_fuhao(1,12,"OFF ");
                            write_iic(73,kzs);                            write_cmd(0x80+15);
            break;            case 5: day++;
                            if(day>tday)                                day=1;
                            write_sfm(1,8,day);                            Write(DS1302_DAY_WRITE,DEC2BCD(day));
                            write_cmd(0x80+9);            break;
            case 6: mon++;                            if(mon==13)
                                mon=1;                            write_sfm(1,5,mon);
                            Write(DS1302_MONTH_WRITE,DEC2BCD(mon));                            write_cmd(0x80+6);
            break;            case 7: year++;
                            if(year==100)                                year=0;
                            write_sfm(1,2,year);                            Write(DS1302_YEAR_WRITE,DEC2BCD(year));
                            write_cmd(0x80+3);            break;
        }    }
            if(get_key()==3 || dd==3)            //减
    {        dd=0;
        bibi(4);        
        switch(f)        {
            case 1: sec--;                            if(sec==-1)
                                sec=59;                            write_sfm(0,6,sec);
                            Write(DS1302_SECOND_WRITE,DEC2BCD(sec));                            write_cmd(0xc0+7);
            break;            case 2: min--;
                            if(min==-1)                                min=59;
                            write_sfm(0,3,min);                            Write(DS1302_MINUTE_WRITE,DEC2BCD(min));
                            write_cmd(0xc0+4);            break;
            case 3: hour--;                            if(hour==-1)
                                hour=23;                            write_sfm(0,0,hour);
                            Write(DS1302_HOUR_WRITE,DEC2BCD(hour));                            write_cmd(0xc0+1);

            break;            case 4: kzs--;            //控制总开关
                            if(kzs==0)                                kzs=3;
                            if(kzs==1)                                write_fuhao(1,12,"ON ");
                            if(kzs==2)                                write_fuhao(1,12,"AUTO");
                            if(kzs==3)                                write_fuhao(1,12,"OFF ");
                            write_iic(73,kzs);                            write_cmd(0x80+15);
            break;            case 5: day--;
                            if(day==0)                                day=tday;
                            write_sfm(1,8,day);                            Write(DS1302_DAY_WRITE,DEC2BCD(day));
                            write_cmd(0x80+9);            break;
            case 6: mon--;                            if(mon==0)
                                mon=12;                            write_sfm(1,5,mon);
                            Write(DS1302_MONTH_WRITE,DEC2BCD(mon));                            write_cmd(0x80+6);
            break;            case 7: year--;
                            if(year==-1)                                year=99;
                            write_sfm(1,2,year);                            Write(DS1302_YEAR_WRITE,DEC2BCD(year));
                            write_cmd(0x80+3);            break;
        }    }
}

////////////////////////////控制调整///////////////////////////////////void kzdisp()
{        write_fuhao(1,0,"set govern1");
    write_fuhao(0,5,":");                    //写入1602 固定字符        write_sfm(0,0,s);                        //当前控制数
    write_sfm(0,3,kz[s][0]);   //控制  时    write_sfm(0,6,kz[s][1]);   //控制  分
        if(kz[s][2]==0)
        write_fuhao(1,13,"OFF");    //控制开关标志    if(kz[s][2]==1)
        write_fuhao(1,13,"ON ");

    if(get_key()==1 || dd==1)         //选择控制    {
        dd=0;        s++;
        bibi(4);        
        if(s==7)        //6组时间        {
            s=0;            cd1=0;
            write_cmd(0x01);            write_cmd(0x0c);    //关光标
        }    }
            if(get_key()==2 || dd==2)         //确认修改
    {        dd=0;
        n=1;        bibi(4);

        while(n!=0)        {                
            if(get_key()==1 || dd==1)            {
                dd=0;                n++;
                bibi(4);            }

            switch(n)            {
                case 1:    write_cmd(0x0f);     //光标闪烁                                write_cmd(0xc0+7);    //定位到控制  分
                break;                
                case 2:    write_cmd(0xc0+4);    //定位到控制  时                break;
                    case 3:    write_cmd(0x80+15);    //定位到控制  开关
                break;                
                case 4:    n=0;                                write_fuhao(1,0,"set govern1");
                                write_cmd(0x0c);    //关光标                break;
            }            
            if(get_key()==2 || dd==2)    //控制加             {
                dd=0;                bibi(4);
                                switch(n)
                {                    case 1:    kz[s][1]++;
                                    if(kz[s][1]>=60)                                        kz[s][1]=0;
                                    write_iic(s*3-1,kz[s][1]);//存入iic,(s*3)-2+1     ,+1表示此组第二个数据                                    write_sfm(0,6,kz[s][1]);        //分
                    break;                    
                    case 2:    kz[s][0]++;                                    if(kz[s][0]>=24)
                                        kz[s][0]=0;                                    write_iic(s*3-2,kz[s][0]);//存入iic,(s*3)-2+0  ,+0表示此组第一个数据
                                    write_sfm(0,3,kz[s][0]);        //时                    break;

                    case 3:    kz[s][2]++;                                    if(kz[s][2]>1)
                                        kz[s][2]=0;                                    write_iic(s*3,kz[s][2]);//存入iic,s*3  
                                                                        if(kz[s][2]==0)
                                    write_fuhao(1,13,"OFF");                                    if(kz[s][2]==1)
                                        write_fuhao(1,13,"ON ");        //开关                    break;
                }            }
                        if(get_key()==3 || dd==3)    //控制减
            {                dd=0;
                bibi(4);                
                switch(n)                {
                    case 1:    kz[s][1]--;                                    if(kz[s][1]==-1)
                                        kz[s][1]=59;                                    write_iic((s*3)-1,kz[s][1]);//存入iic,(s*4)-3+1
                                    write_sfm(0,6,kz[s][1]);                    break;
                                        case 2:    kz[s][0]--;
                                    if(kz[s][0]==-1)                                        kz[s][0]=23;
                                    write_iic((s*3)-2,kz[s][0]);//存入iic,(s*4)-3                                    write_sfm(0,3,kz[s][0]);
                    break;                                    
                                                        case 3:    kz[s][2]--;
                                    if(kz[s][2]==-1)                                        kz[s][2]=1;
                                    write_iic(s*3,kz[s][2]);//存入iic,s*4                                    
                                    if(kz[s][2]==0)                                        write_fuhao(1,13,"OFF");
                                    if(kz[s][2]==1)                                        write_fuhao(1,13,"ON ");        //开关
                    break;                }
            }            }
    }

    if(get_key()==3 || dd==3)        //退出修改    {
        dd=0;        cd1=0;
        s=0;        write_cmd(0x01);
        bibi(4);    }
}

void kzdisp1(){    
    write_fuhao(1,0,"set govern2");    write_fuhao(0,5,":");                    //写入1602 固定字符    
    write_sfm(0,0,s);                        //当前控制数    write_sfm(0,3,kz1[s][0]);   //控制  时
    write_sfm(0,6,kz1[s][1]);   //控制  分    
    if(kz1[s][2]==0)        write_fuhao(1,13,"OFF");    //控制开关标志
    if(kz1[s][2]==1)        write_fuhao(1,13,"ON ");

    if(get_key()==1 || dd==1)         //选择控制    {
        dd=0;        s++;
        bibi(4);        
        if(s==7)        //6组时间        {
            s=0;            cd1=0;
            write_cmd(0x01);            write_cmd(0x0c);    //关光标
        }    }
            if(get_key()==2 || dd==2)         //确认修改
    {        dd=0;
        n=1;        bibi(4);

        while(n!=0)        {                
            if(get_key()==1 || dd==1)            {
                dd=0;                n++;
                bibi(4);            }

            switch(n)            {
                case 1:    write_cmd(0x0f);     //光标闪烁                                write_cmd(0xc0+7);    //定位到控制  分
                break;                
                case 2:    write_cmd(0xc0+4);    //定位到控制  时                break;
                    case 3:    write_cmd(0x80+15);    //定位到控制  开关
                break;                
                case 4:    n=0;                                write_fuhao(1,0,"set govern2");
                                write_cmd(0x0c);    //关光标                break;
            }            
            if(get_key()==2 || dd==2)    //控制加             {
                dd=0;                bibi(4);
                                switch(n)
                {                    case 1:    kz1[s][1]++;
                                    if(kz1[s][1]>=60)                                        kz1[s][1]=0;
                                    write_iic(s*3-1,kz1[s][1]);//存入iic,(s*3)-2+1     ,+1表示此组第二个数据                                    write_sfm(0,6,kz1[s][1]);        //分
                    break;                                        case 2:    kz1[s][0]++;
                                    if(kz1[s][0]>=24)                                        kz1[s][0]=0;
                                    write_iic(s*3-2,kz1[s][0]);//存入iic,(s*3)-2+0  ,+0表示此组第一个数据                                    write_sfm(0,3,kz1[s][0]);        //时
                    break;                                                                                            case 3:    kz1[s][2]++;
                                    if(kz1[s][2]>1)                                        kz1[s][2]=0;
                                    write_iic(s*3,kz1[s][2]);//存入iic,s*3                                      
                                    if(kz1[s][2]==0)                                    write_fuhao(1,13,"OFF");
                                    if(kz1[s][2]==1)                                        write_fuhao(1,13,"ON ");        //开关
                    break;                }
            }            
            if(get_key()==3 || dd==3)    //控制减            {
                dd=0;                bibi(4);
                                switch(n)
                {                    case 1:    kz1[s][1]--;
                                    if(kz1[s][1]==-1)                                        kz1[s][1]=59;
                                    write_iic((s*3)-1,kz1[s][1]);//存入iic,(s*4)-3+1                                    write_sfm(0,6,kz1[s][1]);
                    break;                                        case 2:    kz1[s][0]--;
                                    if(kz1[s][0]==-1)                                        kz1[s][0]=23;
                                    write_iic((s*3)-2,kz1[s][0]);//存入iic,(s*4)-3                                    write_sfm(0,3,kz1[s][0]);
                    break;                                                        case 3:    kz1[s][2]--;
                                    if(kz1[s][2]==-1)                                        kz1[s][2]=1;
                                    write_iic(s*3,kz1[s][2]);//存入iic,s*4                                                                        if(kz1[s][2]==0)
                                        write_fuhao(1,13,"OFF");                                    if(kz1[s][2]==1)
                                        write_fuhao(1,13,"ON ");        //开关                    break;
                }            }    
        }    }

    if(get_key()==3 || dd==3)        //退出修改    {
        dd=0;        cd1=0;
        s=0;        write_cmd(0x01);
        bibi(4);    }
}

void kzdisp2(){    
    write_fuhao(1,0,"set govern3");    write_fuhao(0,5,":");                    //写入1602 固定字符    
    write_sfm(0,0,s);                        //当前控制数    write_sfm(0,3,kz2[s][0]);   //控制  时
    write_sfm(0,6,kz2[s][1]);   //控制  分    
    if(kz2[s][2]==0)        write_fuhao(1,13,"OFF");    //控制开关标志
    if(kz2[s][2]==1)        write_fuhao(1,13,"ON ");

    if(get_key()==1 || dd==1)         //选择控制    {
        dd=0;        s++;
        bibi(4);        
        if(s==7)        //6组时间        {
            s=0;            cd1=0;
            write_cmd(0x01);            write_cmd(0x0c);    //关光标
        }    }
            if(get_key()==2 || dd==2)         //确认修改
    {        dd=0;
        n=1;        bibi(4);

        while(n!=0)        {                
            if(get_key()==1 || dd==1)            {
                dd=0;                n++;
                bibi(4);            }

            switch(n)            {
                case 1:    write_cmd(0x0f);     //光标闪烁                                write_cmd(0xc0+7);    //定位到控制  分
                break;                
                case 2:    write_cmd(0xc0+4);    //定位到控制  时                break;
                    case 3:    write_cmd(0x80+15);    //定位到控制  开关
                break;                
                case 4:    n=0;                                write_fuhao(1,0,"set govern3");
                                write_cmd(0x0c);    //关光标                break;
            }            
            if(get_key()==2 || dd==2)    //控制加             {
                dd=0;                bibi(4);
                                switch(n)
                {                    case 1:    kz2[s][1]++;
                                    if(kz2[s][1]>=60)                                        kz2[s][1]=0;
                                    write_iic(s*3-1,kz2[s][1]);//存入iic,(s*3)-2+1     ,+1表示此组第二个数据                                    write_sfm(0,6,kz2[s][1]);        //分
                    break;                                        case 2:    kz2[s][0]++;
                                    if(kz2[s][0]>=24)                                        kz2[s][0]=0;
                                    write_iic(s*3-2,kz2[s][0]);//存入iic,(s*3)-2+0  ,+0表示此组第一个数据                                    write_sfm(0,3,kz2[s][0]);        //时
                    break;                                                                                            case 3:    kz2[s][2]++;
                                    if(kz2[s][2]>1)                                        kz2[s][2]=0;
                                    write_iic(s*3,kz2[s][2]);//存入iic,s*3                                      
                                    if(kz2[s][2]==0)                                    write_fuhao(1,13,"OFF");
                                    if(kz2[s][2]==1)                                        write_fuhao(1,13,"ON ");        //开关
                    break;                }
            }            
            if(get_key()==3 || dd==3)    //控制减            {
                dd=0;                bibi(4);
                                switch(n)
                {                    case 1:    kz2[s][1]--;
                                    if(kz2[s][1]==-1)                                        kz2[s][1]=59;
                                    write_iic((s*3)-1,kz2[s][1]);//存入iic,(s*4)-3+1                                    write_sfm(0,6,kz2[s][1]);
                    break;                                        case 2:    kz2[s][0]--;
                                    if(kz2[s][0]==-1)                                        kz2[s][0]=23;
                                    write_iic((s*3)-2,kz2[s][0]);//存入iic,(s*4)-3                                    write_sfm(0,3,kz2[s][0]);
                    break;                                                        case 3:    kz2[s][2]--;
                                    if(kz2[s][2]==-1)                                        kz2[s][2]=1;
                                    write_iic(s*3,kz2[s][2]);//存入iic,s*4                                                                        if(kz2[s][2]==0)
                                        write_fuhao(1,13,"OFF");                                    if(kz2[s][2]==1)
                                        write_fuhao(1,13,"ON ");        //开关                    break;
                }            }    
        }    }

    if(get_key()==3 || dd==3)        //退出修改    {
        dd=0;        cd1=0;
        s=0;        write_cmd(0x01);
        bibi(4);    }
}

void kzdisp3(){    
    write_fuhao(1,0,"set govern4");    write_fuhao(0,5,":");                    //写入1602 固定字符    
    write_sfm(0,0,s);                        //当前控制数    write_sfm(0,3,kz3[s][0]);   //控制  时
    write_sfm(0,6,kz3[s][1]);   //控制  分    
    if(kz3[s][2]==0)        write_fuhao(1,13,"OFF");    //控制开关标志
    if(kz3[s][2]==1)        write_fuhao(1,13,"ON ");

    if(get_key()==1 || dd==1)         //选择控制    {
        dd=0;        s++;
        bibi(4);        
        if(s==7)        //6组时间        {
            s=0;            cd1=0;
            write_cmd(0x01);            write_cmd(0x0c);    //关光标
        }    }
            if(get_key()==2 || dd==2)         //确认修改
    {        dd=0;
        n=1;        bibi(4);

        while(n!=0)        {                
            if(get_key()==1 || dd==1)            {
                dd=0;                n++;
                bibi(4);            }

            switch(n)            {
                case 1:    write_cmd(0x0f);     //光标闪烁                                write_cmd(0xc0+7);    //定位到控制  分
                break;                
                case 2:    write_cmd(0xc0+4);    //定位到控制  时                break;
                    case 3:    write_cmd(0x80+15);    //定位到控制  开关
                break;                
                case 4:    n=0;                                write_fuhao(1,0,"set govern4");
                                write_cmd(0x0c);    //关光标                break;
            }            
            if(get_key()==2 || dd==2)    //控制加             {
                dd=0;                bibi(4);
                                switch(n)
                {                    case 1:    kz3[s][1]++;
                                    if(kz3[s][1]>=60)                                        kz3[s][1]=0;
                                    write_iic(s*3-1,kz3[s][1]);//存入iic,(s*3)-2+1     ,+1表示此组第二个数据                                    write_sfm(0,6,kz3[s][1]);        //分
                    break;                    
                    case 2:    kz3[s][0]++;                                    if(kz3[s][0]>=24)
                                        kz3[s][0]=0;                                    write_iic(s*3-2,kz3[s][0]);//存入iic,(s*3)-2+0  ,+0表示此组第一个数据
                                    write_sfm(0,3,kz3[s][0]);        //时                    break;

                    case 3:    kz3[s][2]++;                                    if(kz3[s][2]>1)
                                        kz3[s][2]=0;                                    write_iic(s*3,kz3[s][2]);//存入iic,s*3  
                                                                        if(kz3[s][2]==0)
                                    write_fuhao(1,13,"OFF");                                    if(kz3[s][2]==1)
                                        write_fuhao(1,13,"ON ");        //开关                    break;
                }            }
                        if(get_key()==3 || dd==3)    //控制减
            {                dd=0;
                bibi(4);                
                switch(n)                {
                    case 1:    kz3[s][1]--;                                    if(kz3[s][1]==-1)
                                        kz3[s][1]=59;                                    write_iic((s*3)-1,kz3[s][1]);//存入iic,(s*4)-3+1
                                    write_sfm(0,6,kz3[s][1]);                    break;
                                        case 2:    kz3[s][0]--;
                                    if(kz3[s][0]==-1)                                        kz3[s][0]=23;
                                    write_iic((s*3)-2,kz3[s][0]);//存入iic,(s*4)-3                                    write_sfm(0,3,kz3[s][0]);
                    break;                                    
                                                        case 3:    kz3[s][2]--;
                                    if(kz3[s][2]==-1)                                        kz3[s][2]=1;
                                    write_iic(s*3,kz3[s][2]);//存入iic,s*4                                    
                                    if(kz3[s][2]==0)                                        write_fuhao(1,13,"OFF");
                                    if(kz3[s][2]==1)                                        write_fuhao(1,13,"ON ");        //开关
                    break;                }
            }            }
    }

    if(get_key()==3 || dd==3)        //退出修改    {
        dd=0;        cd1=0;
        s=0;        write_cmd(0x01);
        bibi(4);    }
}


void cdqh()                //菜单切换{
    if(get_key()==1 || dd==1)    {
        dd=0;        cd1++;            //菜单切换
        bibi(4);        
        switch(cd1)        {            
            case 1: write_cmd(0x80+15);                            write_dat(0x7f);            //<-  箭头符号  1602内部字符集
                            write_fuhao(1,0," 0: Set Time   ");        //注意这俩个留的空格不同哦,                            write_fuhao(0,0," 1: set govern1");    //那是因为我在下面不想用清屏命令了
            break;            case 2:    write_cmd(0xc0+15);
                            write_dat(0x7f);                            write_fuhao(1,0," 0: Set Time    ");        
                            write_fuhao(0,0," 1: set govern1");        //空格留格 <- 箭头的            break;            
            case 3:    write_cmd(0xc0+15);                            write_dat(0x7f);
                            write_fuhao(1,0," 1: set govern1");                            write_fuhao(0,0," 2: set govern2");
            break;                        case 4:    write_cmd(0xc0+15);
                            write_dat(0x7f);                            write_fuhao(1,0," 2: set govern2");
                            write_fuhao(0,0," 3: set govern3");            break;            
            case 5:    write_cmd(0xc0+15);                            write_dat(0x7f);
                            write_fuhao(1,0," 3: set govern3");                            write_fuhao(0,0," 4: set govern4");
            break;            case 6: cd1=0;
                            write_cmd(0x01);         //清屏            break;
        }    }
        if(cd1!=0)
    {        if(get_key()==2 || dd==2)        //确认键
        {            dd=0;
            bibi(4);            
            switch(cd1)            {
                case 1: f=1;        //这样以便一进去就可以调整   秒                                write_cmd(0x01);        //清屏
                                Disp();                                while(cd1==1)keyscan();        //只运行  按键扫描
                break;                
                case 2: s=1;                                write_cmd(0x01);        //清屏
                                while(cd1==2)                                    kzdisp();
                break;                
                case 3: s=1;                                write_cmd(0x01);        //清屏
                                while(cd1==3)                                    kzdisp1();
                break;                
                case 4: s=1;                                write_cmd(0x01);        //清屏
                                while(cd1==4)                                    kzdisp2();
                break;                
                case 5: s=1;                                write_cmd(0x01);        //清屏
                                while(cd1==5)                                    kzdisp3();
                break;

            }        }
                    if(get_key()==3 || dd==3)        //加键
        {            dd=0;
            bibi(4);            write_cmd(0x0c);     //关光标闪烁
            write_cmd(0x01);         //清屏            cd1=0;        //显示时间
        }    }
}

void Init()        //系统初始化{
    EX0=1;        //红外遥控专用    IT0=1;        //下降沿有效,默认是0即低电平
    EA=1;    BL=0;
        SW=1;    //开机关闭继电器
    SW1=1;    //开机关闭继电器        SW2=1;    //开机关闭继电器
    SW3=1;    //开机关闭继电器        }

void main(){    
    uchar a,b,t;    
    Init();                    //系统初始化    lcd_init();            //LCD16020初始化
    ClockInit();        //DS1302初始化    init_iic();            //IIC初始化
///////////////////iic初始化//////////////////////////     t=read_iic(1);
    if(t==255)            //iic未初始化内部数据全是0xff,    {
        for(a=1;a<73;a++)        {
            write_iic(a,1);                //未初始化则全写1            delay_ms(1);
        }        write_iic(73,3);    //总开关 关闭
    }////////////////////读取控制数据///////////////////////    
    t=1;    for(b=1;b<25;b++)                    //共24组数据                从1开始
    {        for(a=0;a<3;a++)                    //每组3个数据,从 0开始
        {            kz[a]=read_iic(t);
            if(t++==73)            //24*3                t=1;
        }    }
        kzs    = read_iic(73);
    while(1)    {    
        ddms();                //掉电模式        cdqh();                    //菜单切换
        bs();                    //        bs1();                    
        bs2();        bs3();                    

        if(cd1==0 && f==0)    //cd1 菜单  ,f为时间调整标志        {
            Read_Time();    //读取时间            Disp();                
        }    }
} void y() interrupt 1
{    TH0=(65536-1000)/256;            //1ms
    TL0=(65536-1000)%256; }

-------------------------------------------------下面是main程序-----------------------------------------------

#ifndef _MAIN_H#define _MAIN_H

#define uchar unsigned char#define uint unsigned int

sbit set =P1^5;            //调整键sbit up  =P1^6;            //按键加
sbit down=P1^7;            //按键减

sbit IRIN=P3^2;            //红外遥控

sbit BJ        = P2^5;        //报警灯sbit Sound= P2^4;        //蜂鸣器
sbit SW        = P2^3;        //继电器开关sbit SW1    = P3^4;
sbit SW2    = P3^5;sbit SW3    = P3^6;
sbit BL        = P2^6;        //1602背光灯

uchar code xq[8][5] =     //字符串数组    ,因为后面有字符结束标志 "/0" ,所以 是5 {
    {"        "},   //0  注意前面有0哦!!!!!!    {"Mon1"},        //星期一
    {"Tue2"},        //星期二    {"Wed3"},        //星期三
    {"Thu4"},        //星期四    {"Fri5"},        //星期五
    {"Sat6"},        //星期六    {"Sun7"}        //星期日
};

uchar kz[25][3];uchar kz1[3][3];
uchar kz2[2][3];uchar kz3[2][3];
// ={                //7组闹钟,第一组不用//     {0,    0, 0,0},
//     {6 ,30,1,0},                    //每组分别是时,分,闹钟开关0关,1开//     {7 , 0,2,0},
//     {7 ,20,3,0},//     {8 , 0,4,0},
//     {8 ,20,5,0},//     {9 ,30,6,0},                    //注意前面下标 有0  
//     {12, 0,7,0}                    //譬如要此组第一个数据,应这样  nz[8][0]// };

uchar year;uchar mon;
uchar day;uchar week;
uchar hour;uchar min;
uchar sec;uchar kzs;    //闹钟总开关

uchar dd=0,di=0;        //掉电标志

uchar q,cd1=0;        //菜单uchar g1s=0;        //1s标志

uchar s,n,f;                        //调整标志  , 闹钟选择 && 闹钟修改 & 时间修改

#endif


本帖最近打赏记录:共1条打赏M币+10
人艰不拆了 M币 +10 2018-09-15
离线lc418

发帖
495
M币
518
专家
3
粉丝
10
只看该作者 1楼 发表于: 2018-08-16
哎呀妈呀,这么长!我可能走错门了。
离线bg4rff

发帖
1518
M币
1237
专家
8
粉丝
32
只看该作者 2楼 发表于: 2018-08-16
uchar kz[6][3];
uchar kz1[6][3];
uchar kz2[6][3];
uchar kz3[6][3];
改成这样就是4路6组

    {        for(z=1;z<25;z++)            
        {            if(kz[z][0]==hour && kz[z][1]==min)    //如果时分都相同则。。。

这个改成
    {        for(z=0;z<6;z++)    多处        
        {            if(kz[z][0]==hour && kz[z][1]==min)    //如果时分都相同则。。。
本帖最近打赏记录:共1条打赏M币+20
数码家园 M币 +20 熱心助人 2018-08-16
离线数码家园

发帖
5228
M币
16486
专家
31
粉丝
540
只看该作者 3楼 发表于: 2018-08-16
回 bg4rff 的帖子
非常感谢!!!

我测试一下。
[ 此帖被数码家园在2018-08-16 15:03重新编辑 ]
离线数码家园

发帖
5228
M币
16486
专家
31
粉丝
540
只看该作者 4楼 发表于: 2018-08-17
回 bg4rff 的帖子
bg4rff:uchar kz[6][3];
uchar kz1[6][3];
uchar kz2[6][3];
uchar kz3[6][3];
改成这样就是4路6组
....... (2018-08-16 12:38) 回 bg4rff 的帖子

大师:按指点改之后定时时间不会错乱了,非常感谢啊!我折腾了好长时间都没搞定。另外2  3   4路的定时时间不能掉电保存是什么原因呢?望再指点指点。
离线bg4rff

发帖
1518
M币
1237
专家
8
粉丝
32
只看该作者 5楼 发表于: 2018-08-17
void kzdisp1()
{    这个函数下的
    //write_iic(s*3.....这样的函数;
    //改成write_iic(s*3+18.....,这样存储地址就不重复
}
void kzdisp2()
{    这个函数下的
    //write_iic(s*3.....这样的函数;
    //改成write_iic(s*3+36.....,这样存储地址就不重复
}
void kzdisp3()
{    这个函数下的
    //write_iic(s*3.....这样的函数;
    //改成write_iic(s*3+54.....,这样存储地址就不重复
}
    其实这四个函数功能结构是完全一样的,只要把不同的部分使用一个参数传入就可以了
    比如把路数当做参数,不同的路用的不同的内存,不同的EPROM,数据结构是完全一样的
本帖最近打赏记录:共1条打赏M币+20
数码家园 M币 +20 非常感谢! 2018-08-18
离线bg4rff

发帖
1518
M币
1237
专家
8
粉丝
32
只看该作者 6楼 发表于: 2018-08-17
上面解决了存的问题,再来解决掉电后取的问题
    ////////////////////读取控制数据///////////////////////    
    t=1;
    for(b=0;b<6;b++)                    
    {
        for(a=0;a<3;a++)                    
        {
            kz[a]=read_iic(t);
            kz1[a]=read_iic(18+t);
            kz2[a]=read_iic(36+t);
            kz3[a]=read_iic(54+t++);//注意这个不同的
        }
    }
本帖最近打赏记录:共2条打赏M币+20专家+1
数码家园 专家 +1 非常感谢! 2018-08-18
数码家园 M币 +20 非常感谢! 2018-08-18
离线bg4rff

发帖
1518
M币
1237
专家
8
粉丝
32
只看该作者 7楼 发表于: 2018-08-19
回 数码家园 的帖子
数码家园:大师:按指点改之后定时时间不会错乱了,非常感谢啊!我折腾了好长时间都没搞定。另外2  3   4路的定时时间不能掉电保存是什么原因呢?望再指点指点。 (2018-08-17 06:27) 回 数码家园 的帖子

搞定没有啊
本帖最近打赏记录:共1条打赏M币+20
数码家园 M币 +20 熱心助人 2018-08-20
离线数码家园

发帖
5228
M币
16486
专家
31
粉丝
540
只看该作者 8楼 发表于: 2018-08-20
回 bg4rff 的帖子
没搞定呢,都不好意思问了
我是这样改的:


                                    if(kz1[s][1]>=60)                                        kz1[s][1]=0;
                                    write_iic(s*3+18,kz1[s][1]);//存入iic,(s*3)-2+1     ,+1表示此组第二个数据                                    write_sfm(0,6,kz1[s][1]);        //分
                    break;                                        case 2:    kz1[s][0]++;
                                    if(kz1[s][0]>=24)                                        kz1[s][0]=0;
                                    write_iic(s*3+18,kz1[s][0]);//存入iic,(s*3)-2+0  ,+0表示此组第一个数据                                    write_sfm(0,3,kz1[s][0]);        //时
                    break;                                                                                            case 3:    kz1[s][2]++;
                                    if(kz1[s][2]>1)                                        kz1[s][2]=0;
                                    write_iic(s*3+18,kz1[s][2]);//存入iic,s*3                                      
                                    if(kz1[s][2]==0)                                    write_fuhao(1,13,"OFF");
                                    if(kz1[s][2]==1)                                        write_fuhao(1,13,"ON ");        //开关
                    break;                }
            }            
            if(get_key()==3 || dd==3)    //控制减            {
                dd=0;                bibi(4);
                                switch(n)
                {                    case 1:    kz1[s][1]--;
                                    if(kz1[s][1]==-1)                                        kz1[s][1]=59;
                                    write_iic((s*3+18)-1,kz1[s][1]);//存入iic,(s*4)-3+1                                    write_sfm(0,6,kz1[s][1]);
                    break;                                        case 2:    kz1[s][0]--;
                                    if(kz1[s][0]==-1)                                        kz1[s][0]=23;
                                    write_iic((s*3+18)-2,kz1[s][0]);//存入iic,(s*4)-3                                    write_sfm(0,3,kz1[s][0]);
                    break;                                                        case 3:    kz1[s][2]--;
                                    if(kz1[s][2]==-1)                                        kz1[s][2]=1;
                                    write_iic(s*3+18,kz1[s][2]);//存入iic,s*4                                                                        if(kz1[s][2]==0)
                                        write_fuhao(1,13,"OFF");                                    if(kz1[s][2]==1)
                                        write_fuhao(1,13,"ON ");        //开关
接下去的参数也都是按指点的改,
------------------------------------------------------------------------------------------------------------------------
这个参数是这样:

////////////////////读取控制数据///////////////////////    
     t=1;
    for(b=0;b<6;b++)                    
    {
        for(a=0;a<3;a++)                    
        {
            kz[a]=read_iic(t);
            kz1[a]=read_iic(18+t);
            kz2[a]=read_iic(36+t);
            kz3[a]=read_iic(54+t++);//注意这个不同的
        }
    }
这样修改之后,反而4路都乱了。
---------------------------------------------------------------------------------------------------------
请问IIC.H是不是也要修改呢?
文件如下:

#ifndef _IIC_H
#define _IIC_H

sbit SDA=P3^4;
sbit SCL=P3^3;

void init_iic()   //初始化
{
    SCL=1;
    delay_us(1);
    SDA=1;
    delay_us(1);
}

void start()   //开始
{
    SDA=1;
    SCL=1;
    delay_us(1);
    SDA=0;
    delay_us(1);
}

void stop()   //停止
{
    SDA=0;
    SCL=1;
    delay_us(1);
    SDA=1;
    delay_us(1);
}

void ack()   //应答
{
    uchar i=0;
    SCL=1;
    delay_us(1);
    while((SDA==1)&&(i<250))i++;
    SCL=0;
}

void write_byte_iic(uchar dat)   //按字节写数据
{
    uchar i,temp;
    temp=dat;
    for(i=0;i<8;i++)
    {
        SCL=0;
        delay_us(1);
        temp=temp<<1;
        SDA=CY;
        delay_us(1);
        SCL=1;
        delay_us(1);
    }
    SCL=0;
    delay_us(1);
    SDA=1;
    delay_us(1);
}

uchar read_byte_iic()   //读字节数据   带返回值
{
    uchar i,k;
    SCL=0;
    delay_us(1);
    SDA=1;
    delay_us(1);
    for(i=0;i<8;i++)
    {
        SCL=1;
        delay_us(1);
        k=(k<<1)|SDA;
        SCL=0;
        delay_us(1);
    }
    return k;
}

void write_iic(uchar add,uchar dat)
{
    start();
    write_byte_iic(0xa0);   //a2 a是设备地址a0,a0=2     最低位0写/1读
    ack();
    write_byte_iic(add);    //数据地址
    ack();
    write_byte_iic(dat);    //数据
    ack();
    stop();
}

uchar read_iic(uchar add)
{
    uchar dat;
    start();
    write_byte_iic(0xa0);
    ack();
    write_byte_iic(add);   //数据地址
    ack();
    start();             //写完数据地址重新开始
    write_byte_iic(0xa1);   //读数据
    ack();
    dat=read_byte_iic();   //把读到的数据给dat
    stop();
    
    return dat;
}

#endif
本帖最近打赏记录:共1条打赏M币+30
bg4rff M币 +30 您用我的那个去编译,中间也有改动的,就是那个+18等等的地方 2018-08-21
离线bg4rff

发帖
1518
M币
1237
专家
8
粉丝
32
只看该作者 9楼 发表于: 2018-08-20
不好意思,我没注意原来的程序这里也是有问题的
上面解决了存的问题,再来解决掉电后取的问题
    ////////////////////读取控制数据///////////////////////    
    t=1;
    for(bbb=0;bbb<6;b++)                    
    {
        for(a=0;a<3;a++)                    
        {
            kz[bbb][a]=read_iic(t);
            kz1[bbb][a]=read_iic(18+t);
            kz2[bbb][a]=read_iic(36+t);
            kz3[bbb][a]=read_iic(54+t++);//注意这个不同的
        }
    }

注意中间有[bbb]的改动
我说为啥黑体原来网页吧方括号b换成了显示,所以就不对了
本帖最近打赏记录:共1条打赏M币+20
数码家园 M币 +20 熱心助人 2018-08-21
快速回复
限80 字节
温馨提示:欢迎交流讨论,请勿发布纯表情、纯引用等灌水帖子;以免被删除
 
上一个 下一个