蓝桥杯第八届省赛 电子钟 by YYC

onewire.c

/*程序说明: 单总线驱动程序软件环境: Keil uVision 4.10 硬件环境: CT107单片机综合实训平台(外部晶振12MHz) STC89C52RC单片机日    期: 2011-8-9
*/
#include "reg52.h"
#include <onewire.h>sbit DQ = P1^4;  //单总线接口//单总线延时函数
void Delay_OneWire(unsigned int t);  //STC89C52RC//通过单总线向DS18B20写一个字节
void Write_DS18B20(unsigned char dat)
{unsigned char i;for(i=0;i<8;i++){DQ = 0;DQ = dat&0x01;Delay_OneWire(5);DQ = 1;dat >>= 1;}Delay_OneWire(5);
}//从DS18B20读取一个字节
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;}        Delay_OneWire(5);}return dat;
}//DS18B20设备初始化
bit init_ds18b20(void)
{bit initflag = 0;DQ = 1;Delay_OneWire(12);DQ = 0;Delay_OneWire(80);DQ = 1;Delay_OneWire(10); initflag = DQ;     Delay_OneWire(5);return initflag;
}void Delay_OneWire(unsigned int t)  //STC89C52RC
{t*=11;while(t--);
}float read_temp(void)
{float temp;unsigned char low,high;init_ds18b20();Write_DS18B20(0xcc); Write_DS18B20(0x44); Delay_OneWire(200);init_ds18b20();Write_DS18B20(0xcc); Write_DS18B20(0xbe); low=Read_DS18B20();high=Read_DS18B20();temp=(high<<8|low)*0.065;return temp;
}

ds1302.c

/*程序说明: DS1302驱动程序软件环境: Keil uVision 4.10 硬件环境: CT107单片机综合实训平台 8051,12MHz日    期: 2011-8-9
*/#include <reg52.h>
#include <intrins.h>sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST = P1^3;   // DS1302复位                                             void Write_Ds1302(unsigned  char temp)
{unsigned char i;for (i=0;i<8;i++)        { SCK=0;SDA=temp&0x01;temp>>=1; SCK=1;}
}   void Write_Ds1302_Byte( unsigned char address,unsigned char dat )
{RST=0;    _nop_();SCK=0; _nop_();RST=1;     _nop_();  Write_Ds1302(address);    Write_Ds1302(dat);      RST=0;
}unsigned char Read_Ds1302_Byte ( unsigned char address )
{unsigned char i,temp=0x00;RST=0; _nop_();SCK=0; _nop_();RST=1; _nop_();Write_Ds1302(address);for (i=0;i<8;i++)   {       SCK=0;temp>>=1; if(SDA)temp|=0x80; SCK=1;} RST=0;    _nop_();SCK=0; _nop_();SCK=1; _nop_();SDA=0; _nop_();SDA=1; _nop_();return (temp);
}void set_time(char hour,char min,char sec)
{Write_Ds1302_Byte(0x8e,0x00);Write_Ds1302_Byte(0x84,hour/10*16+hour%10);Write_Ds1302_Byte(0x82,min/10*16+min%10);Write_Ds1302_Byte(0x80,sec/10*16+sec%10);Write_Ds1302_Byte(0x8e,0x80);
}

common.c

#include <common.h>uchar dsp_show[8];
uchar dsp_code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf,0xc6}; //0~9.0xff,-,C
uchar cnt,trg;
uchar interface;  //0为当前时间,1设置时间,2设置闹钟,3显示温度
uchar bit_set;
uchar hour,min,sec;
uchar hour1,min1,sec1;
uchar hour_alarm,min_alarm,sec_alarm;
uchar temp;
uint count_temp;  //注意计数的变量要用uint
uint count_time;
uint count_led;
uint time_limit;
uint flag_ledblink;
uint count_button;void system_init(void)
{uchar i;P0=0xff;P2=0x80;P2=0;P0=0x00;P2=0xa0;P2=0;for(i=0;i<79;i++)temp=read_temp()+0.5;set_time(23,59,50);Timer0Init();}void Timer0Init(void)       //1毫秒@11.0592MHz
{AUXR |= 0x80;     //定时器时钟1T模式TMOD &= 0xF0;       //设置定时器模式TL0 = 0xCD;       //设置定时初值TH0 = 0xD4;        //设置定时初值TF0 = 0;       //清除TF0标志TR0 = 1;      //定时器0开始计时ET0=1;EA=1;
}void tm0() interrupt 1
{static uchar bit_com=0;P0=0;P2=0xc0;P2=0;P0=dsp_show[bit_com];P2=0xe0;P2=0;P0=1<<bit_com;P2=0xc0;P2=0;if(bit_com==7)bit_com=0;elsebit_com++;if(flag_ledblink){count_led++;time_limit++;if(count_led<200)  //0.2s闪烁一次{P0=~0x01;P2=0x80;P2=0;}else{P0=0xff;P2=0x80;P2=0;if(count_led==400)count_led=0;}if(time_limit==5000){flag_ledblink=0;time_limit=0;}}if(count_time==2000)count_time=0;count_temp++;count_time++;count_button++;
}void pause_ds1302()    //DS1302时钟暂停函数
{Write_Ds1302_Byte(0x8e,0);    //写入允许Write_Ds1302_Byte(0x80,0x80);  //置秒位最高位为1,DS1302时钟就会暂停Write_Ds1302_Byte(0x8e,0x80);  //禁止写入
}void scan_button(void)
{//三行按键uchar readdat=P3^0xff,i;trg=readdat&(readdat^cnt);cnt=readdat;if(flag_ledblink)  //按下任意键停止闪烁{if(trg)flag_ledblink=0;P0=0xff;P2=0x80;P2=0;goto loop;}if(trg&0x01)   //S7,时间设定{if(interface==0){interface=1;    //1为设置时间,0为显示时间pause_ds1302(); //暂停时间}if(interface==1){bit_set++;    //bit_set控制选中的位if(bit_set==4){bit_set=0;set_time(hour,min,sec);  //修改DS1302中的时间interface=0;}}}else if(trg&0x02)  //S6,闹钟设定{if(interface==0){interface=2;hour=hour_alarm;min=min_alarm;sec=sec_alarm;}if(interface==2){bit_set++;if(bit_set==4){bit_set=0;interface=0;hour_alarm=hour;min_alarm=min;sec_alarm=sec;}}}else if(trg&0x04)  //S5,加按键{if(interface==1||interface==2){if(bit_set==1){if(hour<23)hour++;}if(bit_set==2){if(min<11)min++;}if(bit_set==3){if(sec<59)sec++;}}}else if(trg&0x08)  //S4,减按键{if(interface==1||interface==2){if(bit_set==1){if(hour>0)hour--;}if(bit_set==2){if(min>0)min--;}if(bit_set==3){if(sec>0)sec--;}}}if((trg&0x08)&&cnt)  //连续按下S4{if(interface==0){for(i=0;i<5;i++)dsp_show[i]=dsp_code[10];interface=3;}}if(!cnt)  //松开S4{if(interface==3)interface=0;}loop: ;
}

main.c

#include <main.h>extern uchar dsp_code[]; //0~9.0xff,-,C
extern uchar dsp_show[8];
extern uchar cnt,trg;
extern uchar interface;  //0为当前时间,1设置时间,2设置闹钟,3显示温度
extern uchar bit_set;
extern uchar hour,min,sec;
extern uchar hour1,min1,sec1;
extern uchar hour_alarm,min_alarm,sec_alarm;
extern uchar temp;
extern uint count_temp;
extern uint count_time;
extern uint count_led;
extern uint time_limit;
extern uint flag_ledblink;
extern uint count_button;void main()
{system_init();while(1){if(interface==0)   //显示时间{if(TH0<0xf0){hour1=Read_Ds1302_Byte(0x85);min1=Read_Ds1302_Byte(0x83);sec1=Read_Ds1302_Byte(0x81);hour=hour1/16*10+hour1%16;min=min1/16*10+min1%16;sec=sec1/16*10+sec1%16;}dsp_show[0]=dsp_code[hour/10];dsp_show[1]=dsp_code[hour%10];dsp_show[2]=dsp_code[11];    //符号“-”dsp_show[3]=dsp_code[min/10];dsp_show[4]=dsp_code[min%10];dsp_show[5]=dsp_code[11];dsp_show[6]=dsp_code[sec/10];dsp_show[7]=dsp_code[sec%10];}else if(interface==3)  //显示温度{if(count_temp>799) //待温度稳定后显示{count_temp=0;temp=read_temp()+0.5;}dsp_show[5]=dsp_code[temp/10];dsp_show[6]=dsp_code[temp%10];dsp_show[7]=dsp_code[12];}else  //设定时间与设定闹钟界面{if(count_time<1000)  //count_time每2秒清零,即前1s亮,后1s灭达到闪烁效果{dsp_show[0]=dsp_code[hour/10];dsp_show[1]=dsp_code[hour%10];dsp_show[2]=dsp_code[11];    //符号“-”dsp_show[3]=dsp_code[min/10];dsp_show[4]=dsp_code[min%10];dsp_show[5]=dsp_code[11];dsp_show[6]=dsp_code[sec/10];dsp_show[7]=dsp_code[sec%10];}else{if(bit_set==1)dsp_show[0]=dsp_show[1]=dsp_code[10];else{dsp_show[0]=dsp_code[hour/10];dsp_show[1]=dsp_code[hour%10];}if(bit_set==2)dsp_show[3]=dsp_show[4]=dsp_code[10];else{dsp_show[3]=dsp_code[min/10];dsp_show[4]=dsp_code[min%10];}if(bit_set==3)dsp_show[6]=dsp_show[7]=dsp_code[10];else{dsp_show[6]=dsp_code[sec/10];dsp_show[7]=dsp_code[sec%10];}}}if(interface==0||interface==3)  //只有在这2个界面下,闹钟才生效{if(sec==sec_alarm)  //先判断sec,最后判断hour{if(min==min_alarm){if(hour==hour_alarm)flag_ledblink=1;}}}if(count_button>9)  //10ms刷新一次按键函数,相当于消抖10ms{count_button=0;scan_button();}}
}

蓝桥杯第八届省赛 电子钟 by YYC相关推荐

  1. 蓝桥杯--第八届省赛试题-电子钟程序设计

    蓝桥杯-第八届省赛试题-电子钟程序设计 题目: 思路 1.显示温度使用大赛提供的onewire.c,只需自己添加读取函数. 2.设计时钟程序,或者使用DS1302.c(这里使用的是自己写的函数) 3. ...

  2. 【蓝桥杯第八届省赛_电子钟】

    蓝桥杯第八届省赛_电子钟 修改版本 基础知识学习来自小蜜蜂老师 yyds 下面代码功能没有完全实现 有误还望大佬指正 总结分析: 第八届题目难度从我个人来说明显会比第七届难了很多,当然也可能是我自己这 ...

  3. 蓝桥杯第八届省赛JAVA真题----k倍区间

    标题: k倍区间 给定一个长度为N的数列,A1, A2, ... AN,如果其中一段连续的子序列Ai, Ai+1, ... Aj(i <= j)之和是K的倍数,我们就称这个区间[i, j]是K倍 ...

  4. 蓝桥杯第八届省赛JAVA真题----油漆面积

    标题:油漆面积 X星球的一批考古机器人正在一片废墟上考古. 该区域的地面坚硬如石.平整如镜. 管理人员为方便,建立了标准的直角坐标系. 每个机器人都各有特长.身怀绝技.它们感兴趣的内容也不相同. 经过 ...

  5. 蓝桥杯——第八届省赛_基于单片机的电子钟程序设计与调试

    一.使用到的模块 数码管,独立按键,LED,DS1302,ds18b20 二.代码部分 实验平台:CT107D 实验芯片:stc15f2k60s2 实验现象:完成题目要求 代码如下 onewire.c ...

  6. 蓝桥杯第八届省赛JAVA真题----9数算式

    标题:9数算式 观察如下的算式: 9213 x 85674 = 789314562 左边的乘数和被乘数正好用到了1~9的所有数字,每个1次. 而乘积恰好也是用到了1~9的所有数字,并且每个1次. 请你 ...

  7. 蓝桥杯第八届省赛JAVA真题----字母组串

    标题:字母组串 由 A,B,C 这3个字母就可以组成许多串. 比如:"A","AB","ABC","ABA"," ...

  8. 蓝桥杯第八届省赛JAVA真题----包子凑数

    标题:包子凑数 小明几乎每天早晨都会在一家包子铺吃早餐.他发现这家包子铺有N种蒸笼,其中第i种蒸笼恰好能放Ai个包子.每种蒸笼都有非常多笼,可以认为是无限笼. 每当有顾客想买X个包子,卖包子的大叔就会 ...

  9. 蓝桥杯第八届省赛JAVA真题----分巧克力

    标题: 分巧克力 儿童节那天有K位小朋友到小明家做客.小明拿出了珍藏的巧克力招待小朋友们. 小明一共有N块巧克力,其中第i块是Hi x Wi的方格组成的长方形. 为了公平起见,小明需要从这 N 块巧克 ...

  10. 蓝桥杯第八届省赛JAVA真题----承压计算

    标题:承压计算 X星球的高科技实验室中整齐地堆放着某批珍贵金属原料. 每块金属原料的外形.尺寸完全一致,但重量不同. 金属材料被严格地堆放成金字塔形. 7 5 8 7 8 8 9 2 7 2 8 1 ...

最新文章

  1. Java项目:在线旅游系统(java+jsp+SSM+Spring+mysql+maven)
  2. 合并两个有序链表分析
  3. linux jsp配置mysql_Linux下搭建Tomcat+MySQL+JSP
  4. 1.10 throws和throw:声明和抛出异常
  5. 计算机删除百度云盘图标,win10系统百度云盘图标删除的操作方法
  6. redis——缓存击穿/穿透/雪崩
  7. python爬取暖享图片
  8. C#例子 易懂故事 接口 委托 事件 异步通知 好玩.
  9. CCF虚拟现实与可视化技术专委会丨面向增强现实的可视计算技术研究进展概述
  10. 80sec被黑原因分析
  11. 五笔字根表识别码图_86版五笔字根表及口诀(最新图文版)
  12. 封装多帧dicm图像
  13. 等比数列和等差数列求和公式
  14. 如何在万网注册域名及域名解析?
  15. 摩拜与ofo, 你们这是为了共享单车还是共享经济?
  16. 同位素标记其他的研究方法和技术
  17. 开源规则引擎比较_Lively Wallpaper - 免费开源的动态壁纸桌面软件 (Wallpaper Engine 替代品)...
  18. 五个经典漏斗模型,看漏斗思维穿透流程化的本质
  19. JD商城经典爬楼梯布局,看到就是赚到
  20. 【Docker】搭建Docker私库Harbor

热门文章

  1. 使用MD5加密的登陆demo
  2. 一位“技术宅”自制的自行车码表在B站火了,稚晖君点赞,网友催量产
  3. Acm - 隔壁老王买酒问题
  4. CSS之display用法
  5. 1.2 Genetic and Evolutionary Algorithms
  6. 桌面管理 | Windows自带工具
  7. 想要做网页游戏怎么办 ?PixiJs 篇(四)
  8. [洛谷P4118][Ynoi2016]炸脖龙I([洛谷P3934]Nephren Ruq Insania)
  9. 设置计算机用户权限,计算机管理员权限在哪里设置_电脑系统如何设置管理员权限-win7之家...
  10. XL4001 典型应用电路