直接复制粘贴然后运行

然后打开stc烧录到开发板上面就能用  程序哪里不懂的话问我,我闲的蛋疼!

#include <STC15F2K60S2.H>
#include <intrins.h>

unsigned char tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff,0xc6};
unsigned char yi,er,san,si,wu,liu,qi,ba;
unsigned char shijian[]={50,59,23,0,0,0,0};
unsigned char num[7];
unsigned char shezhi1=0;
unsigned char shezhi2=0;
unsigned char gaibian=0;
unsigned char panding=0;
long a,b;
unsigned char sansuo=0,sansuo1=0,sansuo2=0;
unsigned char naozhong=0;
unsigned char dingshi[]={0,0,0};
unsigned char huan;
unsigned char jia1=0,jia2=0,jia3=0;
unsigned char jian1=0,jian2=0,jian3=0;
unsigned char shi,fen,miao;
unsigned char shi1,fen1,miao1;
unsigned char yanshi=0;
int m;
bit add=0;

bit qiehuan=0;

unsigned char wendu;

sbit DQ = P1^4;  //单总线接口

sbit SCK=P1^7;        
sbit SDA=P2^3;        
sbit RST = P1^3;   // DS1302复位

void Delayms(int ms)
{
    int i,j;
    for(i=0;i<ms;i++)
        for(j=845;j>0;j--);
}

void chushihua()
{
    P2=0XA0;P0=0X00;P2=0X80;P0=0XFF;
    P2=0XC0;P0=0X00;P2=0XFF;P0=0XFF;
}
void shumaguan1(unsigned char yi,unsigned char er)
{
    P2=0XC0;
    P0=0X01;
    P2=0XFF;
    P0=tab[yi];
    Delayms(1);
    
    P2=0XC0;
    P0=0X02;
    P2=0XFF;
    P0=tab[er];
    Delayms(1);
}
void shumaguan2(unsigned char san,unsigned char si)
{
    P2=0XC0;
    P0=0X04;
    P2=0XFF;
    P0=tab[san];
    Delayms(1);
    
    P2=0XC0;
    P0=0X08;
    P2=0XFF;
    P0=tab[si];
    Delayms(1);
}

void shumaguan3(unsigned char wu,unsigned char liu)
{
    P2=0XC0;
    P0=0X10;
    P2=0XFF;
    P0=tab[wu];
    Delayms(1);
    
    P2=0XC0;
    P0=0X20;
    P2=0XFF;
    P0=tab[liu];
    Delayms(1);
}
void shumaguan4(unsigned char qi,unsigned char ba)
{
    P2=0XC0;
    P0=0X40;
    P2=0XFF;
    P0=tab[qi];
    Delayms(1);
    
    P2=0XC0;
    P0=0X80;
    P2=0XFF;
    P0=tab[ba];
    Delayms(1);
}

void Delay500us()        //@11.0592MHz
{
    unsigned char i, j;

_nop_();
    _nop_();
    i = 6;
    j = 93;
    do
    {
        while (--j);
    } while (--i);
}

void Delay80us()        //@11.0592MHz
{
    unsigned char i, j;

_nop_();
    i = 1;
    j = 217;
    do
    {
        while (--j);
    } while (--i);
}

void chushihua2()
{
    DQ=0;
    Delay500us();
    DQ=1;
    Delay500us();
}

void Write_DS18B20(unsigned char dat)
{
    unsigned char i;
    for(i=0;i<8;i++)
    {
        DQ = 0;
        DQ = dat&0x01;
        Delay80us();
        DQ = 1;
        dat >>= 1;
    }
    Delay80us();
}

unsigned char Read_DS18B20(void)
{
    unsigned char i;
    unsigned char dat;
  
    for(i=0;i<8;i++)
    {
        DQ = 0;
        dat >>= 1;
        DQ = 1;
        if(DQ)
        {
            dat |= 0x80;
        }        
        Delay80us();
    }
    return dat;
}

unsigned char zhuchengxu()
{
     unsigned char di,gao,temp;
    chushihua2();
    Write_DS18B20(0xcc);
    Write_DS18B20(0x44);
    Delay500us();
    
    chushihua2();
    Write_DS18B20(0xcc);
    Write_DS18B20(0xbe);
    Delay500us();
    
    di=Read_DS18B20();
    gao=Read_DS18B20();
    
    di=di>>4;
    gao=gao<<4;
    temp=di|gao;
    return temp;
    
}

void Write_Ds1302_Byte(unsigned  char temp) 
{
    unsigned char i;
    for (i=0;i<8;i++)         
    { 
        SCK=0;
        SDA=temp&0x01;
        temp>>=1; 
        SCK=1;
    }
}

void Write_Ds1302( unsigned char address,unsigned char dat )     
{
     RST=0;
    _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();;
     SCK=0;
    _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
     RST=1;    
       _nop_();  _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
     Write_Ds1302_Byte(address);    
     Write_Ds1302_Byte(((dat/10)<<4)|(dat%10));        
     RST=0; 
}

unsigned char Read_Ds1302 ( unsigned char address )
{
     unsigned char i,temp=0x00;
    unsigned char dat1,dat2;
     RST=0;
    _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
     SCK=0;
    _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
     RST=1;
    _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
     Write_Ds1302_Byte(address);
     for (i=0;i<8;i++)     
     {        
        SCK=0;
        temp>>=1;    
         if(SDA)
         temp|=0x80;    
         SCK=1;
    } 
     RST=0;
    _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
     RST=0;
    SCK=0;
    _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
    SCK=1;
    _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
    SDA=0;
    _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
    SDA=1;
    _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
    SDA=0;
    dat1=temp/16;
    dat2=temp%16;
    temp=dat1*10+dat2;
    
    return temp;            
}

void chushihua3()
{
    unsigned char i,add;
    add=0x80;
    Write_Ds1302(0x8e,0x00);
    for(i=0;i<7;i++)
    {
        Write_Ds1302(add,shijian[i]);
        add=add+2;
    }
    Write_Ds1302(0x8e,0x80);
}

void DS1302()
{
    unsigned char i,add;
    add=0x81;
    Write_Ds1302(0x8e,0x00);
    for(i=0;i<7;i++)
    {
         num[i]=Read_Ds1302 (add);
        add=add+2;
    }
    Write_Ds1302(0x8e,0x80);
}
void anjian()
{
    if(P30==0)
    {
        Delayms(5);
        if(P30==0)
        {
            while(!P30);
        }
        
        if(shezhi1==0)
        {
            panding=1;
            shezhi1=1;
            
        }
        else
            if(shezhi1==1)
            {
                shezhi1=2;

}
            else
                if(shezhi1==2)
                {
                    
                    shezhi1=3;
                
                }
                else 
                    if(shezhi1==3)
                    {
                        panding=0;
                        shezhi1=0;
                    
                    }
        
    }
    
    if(P31==0)
    {
        Delayms(5);
        if(P31==0)
        {
            while(!P31);
        }
        if(naozhong==0)
        {
            naozhong=1;
        }
    
        
        
        if(shezhi2==0)
        {
            panding=1;
            shezhi2=1;
            
        }
        else
            if(shezhi2==1)
            {
                shezhi2=2;

}
            else
                if(shezhi2==2)
                {
                    
                    shezhi2=3;
                
                }
                else 
                    if(shezhi2==3)
                    {
                        panding=0;
                        shezhi2=0;
                    naozhong=0;
                    }
        
        
    }
    
    if(P32==0)
    {
        Delayms(10);
        if(P32==0)
        {
            while(!P32);
        }
        
        if(naozhong==1)
        {
            if(shezhi2==1)
            {
                dingshi[2]=dingshi[2]+1;
            
            }
            if(shezhi2==2)
            {
                dingshi[1]=dingshi[1]+1;

}
            if(shezhi2==3)
            {
                dingshi[0]=dingshi[0]+1;

}
        }
        if(naozhong==0)
        {
            if(shezhi1==1)
            {
                jia1=jia1+1;

}
            if(shezhi1==2)
            {
                jia2=jia2+1;
    
            }
            if(shezhi2==3)
            {
                jia3=jia3+1;
    
            }
        }
        
    }

if(P33==0)   //s4按键
    {
        Delayms(5);
        if(P33==0)   //判断按键是否按下
        {
            qiehuan=1;
            if(panding==0)
            {
                while(!P33)   //按下按键后执行死循环,知道松手为止。
                {
                    wendu=zhuchengxu();
                    yi=11;er=11;san=11;si=11;wu=11;
                    liu=wendu/10;qi=wendu%10;ba=12;
                    shumaguan1(yi,er);
                    shumaguan2(san,si);
                    shumaguan3(wu,liu);
                    shumaguan4(qi,ba);
                }
         }
        }
        while(!P33);   //松开后向下执行
        qiehuan=0;
        
                if(naozhong==1)
        {
            if(shezhi2==1)
            {
                dingshi[2]=dingshi[2]-1;

}
            if(shezhi2==2)
            {
                dingshi[1]=dingshi[1]-1;

}
            if(shezhi2==3)
            {
                dingshi[0]=dingshi[0]-1;

}
        }
        if(naozhong==0)
        {
            if(shezhi1==1)
            {
                jian1=jian1+1;

}
            if(shezhi1==2)
            {
                jian2=jian2+1;

}
            if(shezhi2==3)
            {
                jian3=jian3+1;

}
        }
        
        
    }
    
    
    
}

void Timer0Init(void)        //5毫秒@11.0592MHz
{
    AUXR |= 0x80;        //定时器时钟1T模式
    TMOD &= 0xF0;        //设置定时器模式
    TL0 = 0x00;        //设置定时初值
    TH0 = 0x28;        //设置定时初值
    TF0 = 0;        //清除TF0标志
    TR0 = 1;        //定时器0开始计时
    EA=1;
    ET0=1;
}

void tm0_isr() interrupt 1
{
    TL0 = 0x00;        //设置定时初值
    TH0 = 0x28;        //设置定时初值
    
    a++;
    
    
    if(a==100)
    {
        a=0;
        if(naozhong==0)
        {
        if(shezhi1==1)
        {
            if(sansuo==0)
            {
                sansuo=1;
                yi=11;er=11;
            }
            else
                if(sansuo==1)
                {
                    sansuo=0;
                    
                }
        }
        if(shezhi1==2)
        {
            if(sansuo==0)
            {
                sansuo=1;
                si=11;wu=11;
            }
            else
                if(sansuo==1)
                {
                    sansuo=0;
                    
                }
        }
        
        if(shezhi1==3)
        {
            if(sansuo==0)
            {
                sansuo=1;
                qi=11;ba=11;
            }
            else
                if(sansuo==1)
                {
                    sansuo=0;
                    
                }
        }
        
    }
        
        if(naozhong==1)
        {
        if(shezhi2==1)
        {
            if(sansuo==0)
            {
                sansuo=1;
                yi=11;er=11;
            }
            else
                if(sansuo==1)
                {
                    sansuo=0;
                    
                }
        }
        if(shezhi2==2)
        {
            if(sansuo==0)
            {
                sansuo=1;
                si=11;wu=11;
            }
            else
                if(sansuo==1)
                {
                    sansuo=0;
                    
                }
        }
        
        if(shezhi2==3)
        {
            if(sansuo==0)
            {
                sansuo=1;
                qi=11;ba=11;
            }
            else
                if(sansuo==1)
                {
                    sansuo=0;
                    
                }
        }
        
    }
        
        
    }
}

void Timer1Init(void)        //5毫秒@11.0592MHz
{
    AUXR |= 0x40;        //定时器时钟1T模式
    TMOD &= 0x0F;        //设置定时器模式
    TL1 = 0x00;        //设置定时初值
    TH1 = 0x28;        //设置定时初值
    TF1 = 0;        //清除TF1标志
    TR1 = 1;        //定时器1开始计时
}

void tm1_isr() interrupt 3
{
    TL1 = 0x00;        //设置定时初值
    TH1 = 0x28;        //设置定时初值
    m++;

b++;

if(m<=1000)
        {
            if(b==40)
            {
                b=0;
                if(sansuo1==0)
                {
                    P2=0X80;P0=0XFE;
                    sansuo1=1;
                    sansuo2=1;
                }
                else 
                    if(sansuo1==1)
                    {
                        P2=0X80;
                        P0=0XFF;
                        sansuo1=0;
                    }
            }
  
        }
        else
        {
        P2=0X80;
        P0=0XFF;
        }
    
}

void jjj()
{
    if(miao==1)
    {
        yanshi=1;
    }
    
    
        if(yanshi==1)
        {
        if(shi>dingshi[2])
            {
                EA=1;
                ET1=1;
            }    
                else
                    if(fen>dingshi[1])
                    {
                        EA=1;
                        ET1=1;
                    }
                    else
                        if(miao>dingshi[0])
                    {
                        EA=1;
                        ET1=1;
                    }
        }
}

void main()
{
    chushihua();
    chushihua3();
    Timer0Init();
    Timer1Init();

while(1)
    {

jjj();
    
    
        shi=num[2]+jia1-jian1;    //用于记录1302时钟跑的时间
        fen=num[1]+jia2-jian2;
        miao=num[0]+jia3-jian3;

if(dingshi[2]>=24)
        {
            dingshi[2]=dingshi[2]-23;    //将自己定的时间转化为新的一天
        }
        if(dingshi[1]>=60)
        {
        dingshi[1]=dingshi[1]-59;
            dingshi[2]=dingshi[2]+1;
        }
        if(dingshi[0]>=60)
        {
            dingshi[0]=dingshi[0]-59;
            dingshi[1]=dingshi[1]+1;
        }    
        
        
        anjian();
        DS1302();
        if(naozhong==0)
        {
            if(qiehuan==0)
            {
                if(sansuo==0)
                {
                    yi=shi/10;er=shi%10;
                }
                san=10;
                if(sansuo==0)
                {
                    si=fen/10;wu=fen%10;
                }
                liu=10;
                if(sansuo==0)
                {
                    qi=miao/10;ba=miao%10;
                }
            }
      }
        
        
            if(naozhong==1)
            {
                if(sansuo==0)
                {
                    yi=dingshi[2]/10;er=dingshi[2]%10;
                }
                san=10;
                if(sansuo==0)
                {
                    si=dingshi[1]/10;wu=dingshi[1]%10;
                }
                liu=10;
                if(sansuo==0)
                {
                    qi=dingshi[0]/10;ba=dingshi[0]%10;
                }
                
            }
            
        shumaguan1(yi,er);
        shumaguan2(san,si);
        shumaguan3(wu,liu);
        shumaguan4(qi,ba);
    }
}

第八届蓝桥杯单片机省赛相关推荐

  1. 第八届蓝桥杯单片机省赛----程序题

    main.c程序 #include<stc15f2k60s2.h> #include "ds18b20.h" #include <ds1302.h>#def ...

  2. 第八届蓝桥杯单片机省赛题目解析

    今天终于有空可以写第八届省赛的题目解析了,第八届(2017)总体也不算难,我们一起来看看吧. 题目 这次题目还是一样用到了三大模块(按键.LED.数码管),还用了实时时钟(DS1302)和DS18B2 ...

  3. 蓝桥杯单片机省赛——第五届(模拟智能灌溉系统)

    蓝桥杯单片机省赛--第五届(模拟智能灌溉系统) 一.题目内容 二.程序源代码 1.主函数 2.ds1302驱动函数 3.iic驱动函数 注意:驱动代码需要改动一下,可以将原驱动代码按照下面的代码修改 ...

  4. 【第十三届蓝桥杯单片机省赛模拟冲刺01】

    第十三届蓝桥杯单片机省赛模拟冲刺 赛前一两周一定要多练习,把之前写过的程序在好好看看,把每个模块的代码都多敲几遍,把常考的每个模块部分都吃透,如果理解不了的趁时间还比较充足该背的背牢.同时在准备编程题 ...

  5. 第四届蓝桥杯单片机省赛 自动灌溉系统

    第四届蓝桥杯单片机省赛 自动灌溉系统 新手.所用驱动为大赛提供驱动 #include<stc15f2k60s2.h> #include "intrins.h"#defi ...

  6. 【蓝桥杯】第十三届蓝桥杯单片机国赛 代码程序

    第十三届蓝桥杯单片机国赛 程序 题目 hex文件 代码 工程文件 B站视频 更多资料 题目 历届的省赛和国赛的题目我已经在前面的文章(点击查看)里给大家分享了(网盘资源),需要的话,直接去下载,我在这 ...

  7. 关于第八届蓝桥杯单片机初赛----电子钟设计的题目分析和编程思路分享

    关于第八届蓝桥杯单片机初赛----电子钟设计的题目分析和编程思路分享 1.前言 蓝桥杯单片机比赛,对于初试者,比如那些没有接触51系列单片机,或者很少编程训练的人而言,确实很有挑战力.而即使对于有基础 ...

  8. 第十三届“蓝桥杯”单片机省赛——程序设计题

    第十三届"蓝桥杯"单片机省赛--程序设计题 在比赛时候编写,代码仅供参考 如有不足,多多指教 1.题目 2.代码 main.c程序 #include "reg52.h&q ...

  9. 【蓝桥杯】第九届蓝桥杯单片机国赛 代码程序

    第九届蓝桥杯单片机国赛 程序 题目 hex文件 代码 工程文件 B站视频 更多资料 题目 历届的省赛和国赛的题目我已经在前面的文章(点击查看)里给大家分享了(网盘资源),需要的话,直接去下载,我在这里 ...

最新文章

  1. OpenCV代码提取:rotate函数的实现
  2. ECCV2020超快车道线检测算法:Ultra Fast Structure-aware Deep Lane Detection
  3. 外网无法访问hdfs文件系统
  4. 【音频处理】Melodyne 选择工具使用 ( 主工具简介 | 修改音高 | 自动吸附 | 音符长度修改 | 长度自动吸附 | 设置音符分离线 | 设置片段分离线 )
  5. 【Flutter】Image 组件 ( 配置本地 gif 图片资源 | 本地资源加载 placeholder )
  6. 判断两个字符串是否为旋转词
  7. ArcGIS Server 部署与配置
  8. cmake的使用--从零建立一个小cmake工程
  9. Python 3基础教程32-正则
  10. BIEE连接数据库的方法
  11. 2010-2011年中国嵌入式开发从业人员调查报告隆重推
  12. C语言文件的随机读写
  13. php curl iis,解决IIS运行PHP出现Call to undefined function curl_init()的问题
  14. 数字电子技术基础(二):原码、反码、补码
  15. 缺少软件测试项目?最好试试这套方法
  16. matlab coder 4.0,利用MATLAB Coder将MATLAB代码生成C/C++代码
  17. 来,给产品狗起个正儿八经的名字!
  18. 灵思科电子科技—室内定位技术有哪些_七大室内定位技术详解
  19. 未来几年学什么设计更有前途?
  20. 解决spring JdbcTemplate更新操作成功但数据库未插入值的方案

热门文章

  1. worldpress 新浪微博同步插件0.1版
  2. 收银漏洞要这样“找”!
  3. Math_Calculus_07_向量代数与空间解析几何
  4. c 语言,pcm 数据增大音量
  5. 蚂蚁分类信息系统原生微信小程序百度小程序
  6. springboot 默认日志配置以及更换其他日志配置
  7. android Pernission的所有权限说明
  8. 春校赛--求索溪边树下的小瓜
  9. Navicat 连接远程Mysql
  10. Synopsys coreConsultant