蓝桥杯--第八届省赛试题-电子钟程序设计 【TCP回声服务器】一篇文章帮助你看懂TCP! 关于国标GB28181流媒体协议视频平台EasyGBS设备无法接入平台的问题排查 机器学习16 -- Lifelong Learning 终生学习 Vuex中发送mock请求返回数据 JS 正则表达式详解 学习笔记 花了30分钟,给女朋友们写了个最近抖音很火的3D立体动态相册 这 16 个 CSS 伪类,助你提升布局效率! Mysql系列第八讲 详解排序和分页(order by & limit)及存在的坑 SQL语句的一些应用语句 介绍三个开发技术小知识点 数据库系统概论(1) Mysql备份和恢复的类型,备份恢复的几种方法! --个人整理 MySql数据库基本操作(一) 前八月融合创新提升服务,大数据搭建信息网 长三角税收一体化按下“快进键” 一些建议:给当年刚做程序员的我 谁是物联网江湖明日之星?答案是新CIoT 企业信用有效“变现” 江苏银行物联网金融规模达185亿 2020年中国5G物联网行业:中国通信运营商5G投资达1.23万亿元-可下载 搭建 GFS 分布式文件系统-----实操 排障集锦:九九八十一难之第十五难!-------------史上最全rsync报错详解 docker系列1---docker隔离和限制技术 Linux系统安全及应用加固———最适合新手学,新手都能看懂!超详细的理论+超详细的实验!呕心沥血之作完成! LAMP架构集成ownCloud搭建私有云盘 Shell脚本正则表达式(一) Linux第一人称侵入进程的好地方 Windows DOS常用命令(持续更新完善) 大数据驱动新材料研发 变革从内而发,一站式大数据平台和多模数据库雄起 雕佛像、打棒球、开飞机、去北极,这个90后程序员有点不一样 12项特色功能构建施工现场“物联网” 400亿美元!英伟达收购Arm 软银套现离场物联网梦醒 卫星物联网再次引发热议,原因在这里 信用飞:航旅行业数字化前景可期,大数据预防砍头息乱象 大数据加持自动驾驶产业发展 上海智能网联汽车公共数据中心揭牌 恒久科技(002808.SZ):子公司闽保信息与鸿达兴业签署智能制造及大数据平台综合应用项目战略合作框架协议 工业平板电脑如何在物联网中发挥作用? 400亿美元!英伟达收购Arm 软银套现离场物联网梦醒 “数字心脏”动态解析消费密码——国家级消费市场大数据联合实验 重庆:大数据智能化牵引产业结构调整质效提升 分析物联网技术在智能家居中的应用场景 国家级消费市场大数据联合实验室在上海先行先试 山西吕梁培训搬迁户成为大数据标注员 昔日贫困户,玩转高科技 知乎热议:2020年程序员是否过剩? 柯建东:宁波抢抓工业物联网新机遇,要做好四个平台建设 旷视:城市物联网的实现,是从超级应用到操作系统的沉淀过程 排障集合———忘记原密码,如何重新修改MySQL登入密码 【Redis】主从复制 网易2021批笔试题解 msf工具之木马程序制作以及伪装
您的位置:首页 >物联网 >

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

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

题目:

在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述

思路

1.显示温度使用大赛提供的onewire.c,只需自己添加读取函数。 2.设计时钟程序,或者使用DS1302.c(这里使用的是自己写的函数) 3.键扫描函数 4.进行逻辑判断,都是if语句。哪个按键和模式同时满足,执行相应的操作。 代码中注释很清楚相信大家能看懂

main.c

#include <stc15f2k60s2.h>#include <onewire.h>#define uchar unsigned char#define uint unsigned intcode unsigned char tab[] = { 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0xff, 0xbf };uchar dspbuf[8] = { 10, 10, 10, 10, 10, 10, 10, 10 };uchar temper_flag = 0;//温度扫描标志位uchar hour, minute, sec, alarmHour, alarmMinute, alarmSec; //时分秒 闹钟的时分秒uint count = 0, miao = 0;//count == 25则为5秒, miao == 200 0.2秒uchar temperature, key_value;//温度数值 ;键扫描返回值uchar mode = 0;//模式uchar tt = 0;//显示转变温度的标志uchar tn = 0;//时钟和闹钟转换的标志位uchar ledshan = 0, ledflag = 0; //时钟 == 闹钟,led闪烁void display();void init();unsigned char read_key();void load();void tempera();//关闭蜂鸣器和LED灯void cls_buzz(){P2 = (P2 & 0x1f) | 0xa0;P0 = 0x00;P2 = 0x1f;}void cls_led(){P2 = (P2 & 0x1f) | 0x80;P0 = 0xff;P2 = 0x1f;}//主函数void main(){cls_buzz();cls_led();AUXR = 0x80;TMOD = 0xf0;TL0 = 0xcd;TH0 = 0xd4;TF0 = 0;TR0 = 1;ET0 = 1;EA = 1;init();while (1){if (temper_flag){temper_flag = 0;temperature = rd_temperature();//读温度 }key_value = read_key();}}//温度赋值函数void tempera(){uchar i;for (i = 0; i < 6; i++)dspbuf[i] = 10;dspbuf[7] = temperature % 10;dspbuf[6] = (temperature - dspbuf[7]) / 10;}void time0(void) interrupt 1{static unsigned char intr;if (++intr == 10)//1ms执行一次{intr = 0;temper_flag = 1;//10ms温度读取标志位置1}if (count == 1000)//1s{sec++;count = 0;miao = (miao + 1) % 2;}count += 1;//时钟时间限制条件if (sec == 60) {sec = 0;minute += 1;}if (minute == 60) {minute = 0;hour += 1;}if (hour == 24) {hour = 0;}//闹钟时间限制条件if (alarmSec == 60) {alarmSec = 0;alarmMinute += 1;}if (alarmMinute == 60) {alarmMinute = 0;alarmHour += 1;}if (alarmHour == 24) {alarmHour = 0;}if (ledshan == 200){ledshan = 0;ledflag = (ledflag + 1) % 2;}ledshan += 1;display();}void load(){if (key_value == 7) {mode += 1;}if (tt == 0) { if (tn == 0){//模式1段码值赋值if (mode == 1){dspbuf[2] = dspbuf[5] = 11;dspbuf[1] = hour % 10;dspbuf[0] = hour / 10;dspbuf[4] = minute % 10;dspbuf[3] = minute / 10;if (miao == 1)dspbuf[6] = dspbuf[7] = 10;else{dspbuf[7] = sec % 10;dspbuf[6] = sec / 10;}}if (mode == 2){dspbuf[2] = dspbuf[5] = 11;dspbuf[1] = hour % 10;dspbuf[0] = hour / 10;dspbuf[7] = sec % 10;dspbuf[6] = sec / 10;if (miao == 1)dspbuf[3] = dspbuf[4] = 10;else{dspbuf[4] = minute % 10;dspbuf[3] = minute / 10;}}if (mode == 3){dspbuf[2] = dspbuf[5] = 11;dspbuf[4] = minute % 10;dspbuf[3] = minute / 10;dspbuf[7] = sec % 10;dspbuf[6] = sec / 10;if (miao == 1)dspbuf[1] = dspbuf[0] = 10;else{dspbuf[1] = hour % 10;dspbuf[0] = hour / 10;}}//时钟+设置if (key_value == 4 && mode == 1){key_value = 0;sec += 1;}if (key_value == 4 && mode == 2){key_value = 0;minute++;}if (key_value == 4 && mode == 3){key_value = 0;hour += 1;}//时钟-设置if (key_value == 5 && mode == 1){key_value = 0;if (sec == 0)sec = 59;elsesec -= 1;}if (key_value == 5 && mode == 2){key_value = 0;if (minute == 0)minute = 59;elseminute--;}if (key_value == 5 && mode == 3){key_value = 0;if (hour == 0)hour = 23;elsehour -= 1;}if (mode == 0) {dspbuf[2] = dspbuf[5] = 11;dspbuf[1] = hour % 10;dspbuf[0] = hour / 10;dspbuf[4] = minute % 10;dspbuf[3] = minute / 10;dspbuf[7] = sec % 10;dspbuf[6] = sec/ 10;}}if (tn == 1)//时钟转闹钟标志位{if (mode == 1){dspbuf[2] = dspbuf[5] = 11;dspbuf[1] = alarmHour % 10;dspbuf[0] = alarmHour / 10;dspbuf[4] = alarmMinute % 10;dspbuf[3] = alarmMinute / 10;if (miao == 1)dspbuf[6] = dspbuf[7] = 10;else{dspbuf[7] = alarmSec % 10;dspbuf[6] = alarmSec / 10;}}if (mode == 2){dspbuf[2] = dspbuf[5] = 11;dspbuf[1] = alarmHour % 10;dspbuf[0] = alarmHour / 10;dspbuf[7] = alarmSec % 10;dspbuf[6] = alarmSec / 10;if (miao == 1)dspbuf[3] = dspbuf[4] = 10;else{dspbuf[4] = alarmMinute % 10;dspbuf[3] = alarmMinute / 10;}}if (mode == 3){dspbuf[2] = dspbuf[5] = 11;dspbuf[4] = alarmMinute % 10;dspbuf[3] = alarmMinute / 10;dspbuf[7] = alarmSec % 10;dspbuf[6] = alarmSec / 10;if (miao == 1)dspbuf[1] = dspbuf[0] = 10;else{dspbuf[1] = alarmHour % 10;dspbuf[0] = alarmHour / 10;}}if (key_value == 4 && mode == 1){key_value = 0;alarmSec += 1;}if (key_value == 4 && mode == 2){key_value = 0;alarmMinute++;}if (key_value == 4 && mode == 3){key_value = 0;alarmHour += 1;}if (key_value == 5 && mode == 1){key_value = 0;if (alarmSec == 0)alarmSec = 59;elsealarmSec -= 1;}if (key_value == 5 && mode == 2){key_value = 0;if (alarmMinute == 0)alarmMinute = 59;elsealarmMinute--;}if (key_value == 5 && mode == 3){key_value = 0;if (alarmHour == 0)alarmHour = 23;elsealarmHour -= 1;}if (mode == 0) {dspbuf[2] = dspbuf[5] = 11;dspbuf[1] = alarmHour % 10;dspbuf[0] = alarmHour / 10;dspbuf[4] = alarmMinute % 10;dspbuf[3] = alarmMinute/ 10;dspbuf[7] = alarmSec % 10;dspbuf[6] = alarmSec / 10;}}if (hour == alarmHour && minute == alarmMinute && sec <= (alarmSec + 4))//到达闹钟时间led闪烁五秒{if (ledflag == 1)// == 1 L1亮{P2 = (P2 & 0x1f) | 0x80;P0 = 0xfe;P2 = 0x1f;}else{P2 = (P2 & 0x1f) | 0x80;P0 = 0xff;P2 = 0x1f;}}else{P2 = (P2 & 0x1f) | 0x80;P0 = 0xff;P2 = 0x1f;}}else if (mode == 0 && key_value == 4 && tt == 1)//条件满足显示温度{tempera();}if (mode > 3) mode = 0; //模式转换}void init()//初始赋值 时分秒 闹钟时分秒{hour = 23;minute = 59;sec = 50;alarmHour = 0;alarmMinute = 0;alarmSec = 0;}//键扫描unsigned char read_key(void){//state0:充当消抖 state1:有按键按下 state2:检查按键是否松开uchar temp;static unsigned char state = 0;uchar key_value = 0;temp = P3 & 0x0f;switch (state){case 0:{if (P3 != 0x0f) //有按键按下state = 1;}break;case 1:{if (P3 != 0x0f) {switch (temp){case 0x0e:{key_value = 7; state = 2;//S7}break;case 0x0d:{key_value = 6; state = 2;//S6tn = (tn + 1) % 2;}break;case 0x0b:{key_value = 5; state = 2;//S5}break;case 0x07:{key_value = 4; state = 2; //S4 if(mode == 0)tt = 1;}break;}}elsestate = 0;}break;case 2:{if (P3 == 0xff){tt = 0;state = 0;key_value = 0;}}break;}return key_value;}//显示函数void display(){static unsigned char dspcom = 0;load();P2 = (P2 & 0x1f) | 0xe0;//消影P0 = 0xff;P2 = 0x1f;P2 = (P2 & 0x1f) | 0xc0;//位选P0 = 1 << dspcom;P2 = 0x1f;P2 = (P2 & 0x1f) | 0xe0;//显示P0 = tab[dspbuf[dspcom]];P2 = 0x1f;if (++dspcom == 8) dspcom = 0;}

onewire.h

#include "reg52.h"sbit DQ = P1^4;//单总线接口//单总线延时函数void Delay_OneWire(unsigned int t){unsigned char i;while(t--){for(i=0;i<12;i++);}}//通过单总线向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;}unsigned char rd_temperature(void){unsigned char high,low;char temp;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<<4; //只取了整数部分和符号位temp |= (low>>4);return temp;}

感谢丁莉老师的指导!!!

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。