<>蓝桥杯单片机第8届省赛_电子钟
修改版本
基础知识学习来自小蜜蜂老师 yyds
下面代码功能没有完全实现 有误还望大佬指正

总结分析:
第八届题目难度从我个人来说明显会比第七届难了很多,当然也可能是我自己这方面相关程序编程没有掌握好 从我个人在编程中遇到的问题进行分析。
首先在开始编程的时候一定要先把题目读清楚搞懂 让自己大脑里面有一个基础框架,然后分模块化编程 在进行相应的逻辑组合 ,切记一定不要全部写完了再来调式
写部分功能就调试一下 这样即使出错也可能快速定位错误并解决 ,其次在自己进行练习时 对程序的调试一定要有耐心,这一点也是成功至关重要的一个因素
,从我自己来说解决Bug的时间花了很久时间即使是一个小错误 有时候都反复调试了好多遍 好在没有放弃最终还是成功解决。
在第八届题目练习过程一开始就遇到了一些问题,然后就想去找一下网上其他大佬写的代码,从内心来说他们写的是真的好
但是可能我自己功力太浅很多看不懂,然后就只能靠自己一点一点慢慢实现 所花时间将近比赛时间2倍说到底还是自己练习的不够 嗯 只能说继续努力了。
下面程序的功能在我开发板上测试 进行按键各种模式切换显示都没有问题 当时不知道为什么进行修改时钟和设置闹钟时 显示的数字就有点问题了
然后Led操作的功能还没有写 ,后面会继续完善 。
路漫漫其修远兮 加油

#include "reg52.h" #include "onewire.h" #include "absacc.h"
/*=================================================================
Date:2022-1-14 Author:小殷同学 Version:1.1 IO方式
==================================================================*/ sbit S7 =
P3^0; sbit S6 = P3^1; sbit S5 = P3^2; sbit S4 = P3^3; sbit Led1 = P0^0; unsigned
char T_Hour = 23,T_Min= 59,T_Sec = 50; unsigned char C_Hour = 0,C_Min = 0,C_Sec
= 0; unsigned char Temperature = 0;//温度存储 unsigned char time_set = 0,clock_set =
0,mode_set = 0;//时间设置和闹钟时钟标志 unsigned char t_count0 = 0,t_flag = 0,t_count1,
c_count= 0;//计时和闪烁标志 unsigned char t_led = 0,led_flag = 0;//分别标记0.2s 和5s
unsigned char code smg_duan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90
,0xbf,0xff}; //------------------------------简单延时----------------------------
void Delay(unsigned int t) { while(t--); }
//--------------------------74CHC138初始化------------------------- void
Init_74HC138(unsigned char n) { switch(n) { case 0:P2 = (P2 & 0x1f) | 0x00;break
; case 4:P2 = (P2 & 0x1f) | 0x80;break; case 5:P2 = (P2 & 0x1f) | 0xa0;break;
case 6:P2 = (P2 & 0x1f) | 0xc0;break; case 7:P2 = (P2 & 0x1f) | 0xe0;break; } }
//-------------------------系统初始化----------------------------- void InitSyetem(
void) { Init_74HC138(4); P0 = 0xff; //关闭LED Init_74HC138(5); P0 = 0x00;
//关闭继电器和蜂鸣器 } //-------------------------数码管安位显示------------------------- void
SMG_DisplayBit(unsigned char pos,unsigned char dat) { Init_74HC138(6); P0 = (
0x01 << pos); Init_74HC138(7); P0 = dat; } void SMG_Close(void) { Init_74HC138(6
); P0 = 0xff; Init_74HC138(7); P0 = 0xff; }
//-----------------------数码管显示------------------------------ void SMG_Display(
unsigned char hour,unsigned char min,unsigned char sec) { SMG_DisplayBit(0,
smg_duan[hour/10]); Delay(200); SMG_DisplayBit(1,smg_duan[hour%10]); Delay(200);
SMG_DisplayBit(2,smg_duan[10]); Delay(200); SMG_DisplayBit(3,smg_duan[min/10]);
Delay(200); SMG_DisplayBit(4,smg_duan[min%10]); Delay(200); SMG_DisplayBit(5,
smg_duan[10]); Delay(200); SMG_DisplayBit(6,smg_duan[sec/10]); Delay(200);
SMG_DisplayBit(7,smg_duan[sec%10]); Delay(200); SMG_Close(); } void
SMG_Hour_Flash(unsigned char hour,unsigned char min,unsigned char sec) {
SMG_DisplayBit(2,smg_duan[10]); Delay(200); SMG_DisplayBit(3,smg_duan[min/10]);
Delay(200); SMG_DisplayBit(4,smg_duan[min%10]); Delay(200); SMG_DisplayBit(5,
smg_duan[10]); Delay(200); SMG_DisplayBit(6,smg_duan[sec/10]); Delay(200);
SMG_DisplayBit(7,smg_duan[sec%10]); Delay(200); if(t_flag == 0) { SMG_DisplayBit
(0,smg_duan[hour/10]); Delay(200); SMG_DisplayBit(1,smg_duan[hour%10]); Delay(
200); } else { SMG_DisplayBit(0,smg_duan[11]); Delay(200); SMG_DisplayBit(1,
smg_duan[11]); Delay(200); } SMG_Close(); } void SMG_Min_Flash(unsigned char
hour,unsigned char min,unsigned char sec) { SMG_DisplayBit(0,smg_duan[hour/10]);
Delay(200); SMG_DisplayBit(1,smg_duan[hour%10]); Delay(200); SMG_DisplayBit(2,
smg_duan[10]); Delay(200); SMG_DisplayBit(5,smg_duan[10]); Delay(200);
SMG_DisplayBit(6,smg_duan[sec/10]); Delay(200); SMG_DisplayBit(7,smg_duan[sec%10
]); Delay(200); if(t_flag == 0) { SMG_DisplayBit(3,smg_duan[min/10]); Delay(200)
; SMG_DisplayBit(4,smg_duan[min%10]); Delay(200); } else { SMG_DisplayBit(3,
smg_duan[11]); Delay(200); SMG_DisplayBit(4,smg_duan[11]); Delay(200); }
SMG_Close(); } void SMG_Sec_Flash(unsigned char hour,unsigned char min,unsigned
char sec) { SMG_DisplayBit(0,smg_duan[hour/10]); Delay(200); SMG_DisplayBit(1,
smg_duan[hour%10]); Delay(200); SMG_DisplayBit(2,smg_duan[10]); Delay(200);
SMG_DisplayBit(3,smg_duan[min/10]); Delay(200); SMG_DisplayBit(4,smg_duan[min%10
]); Delay(200); SMG_DisplayBit(5,smg_duan[10]); Delay(200); if(t_flag == 0) {
SMG_DisplayBit(6,smg_duan[sec/10]); Delay(200); SMG_DisplayBit(7,smg_duan[sec%10
]); Delay(200); } else { SMG_DisplayBit(6,smg_duan[11]); Delay(200);
SMG_DisplayBit(7,smg_duan[11]); Delay(200); } SMG_Close(); }
//---------------------------------温度读取------------------------ void
Read_Temperature(void) { unsigned char LSB = 0,MSB = 0; init_ds18b20();
Write_DS18B20(0xcc);//跳过Rom Write_DS18B20(0x44);//开始转换 Delay(500); init_ds18b20(
); Write_DS18B20(0xcc);//跳过Rom Write_DS18B20(0xbe);//读取温度到暂存区 LSB = Read_DS18B20
();//读取第0字节 MSB = Read_DS18B20();//读取第1字节 Temperature = (MSB << 8)| LSB;
Temperature= Temperature >> 4;//保留整数 /* 小数处理 if((Temperature & 0x1f) != 0x0000)
//高五位为符号 全零为+ { Temperature = Temperature >> 4; Temperature = Temperature * 10;
Temperature = Temperature + (LSB & 0x0f) *0.625; } */ }
//-------------------------------温度显示--------------------------- void
Temperature_Display(void) { SMG_DisplayBit(0,smg_duan[11]); Delay(200);
SMG_DisplayBit(1,smg_duan[11]); Delay(200); SMG_DisplayBit(2,smg_duan[11]);
Delay(200); SMG_DisplayBit(3,smg_duan[11]); Delay(200); SMG_DisplayBit(4,
smg_duan[11]); Delay(200); SMG_DisplayBit(5,smg_duan[Temperature/10]); Delay(200
); SMG_DisplayBit(6,smg_duan[Temperature%10]); Delay(200); SMG_DisplayBit(7,0xC6
); // C Delay(200); SMG_Close(); }
//----------------------------------按键处理------------------------ void KeyScans(
void) { if(S7 == 0) { Delay(20); if(S7 == 0) { mode_set = 1; while(S7 == 0);
time_set++; ET0 = 0,TR0 = 0; if(time_set == 1) { //时设置 SMG_Hour_Flash(T_Hour,
T_Min,T_Sec); } else if(time_set == 2) { //分设置 SMG_Min_Flash(T_Hour,T_Min,T_Sec)
; } else if(time_set > 3) { //秒 SMG_Sec_Flash(T_Hour,T_Min,T_Sec); time_set = 0;
ET0= 1,TR0 = 1; } } } // s6 闹钟时分秒选择 if(S6 == 0) { Delay(20); if(S6 == 0) {
clock_set++; mode_set = 2; ET0 = 0,TR0 = 0; while(S6 == 0); //等待按键松开 if(
clock_set== 1) //选择时 { SMG_Hour_Flash(C_Hour,C_Min,C_Sec); } else if(clock_set
== 2) //选择分 { SMG_Min_Flash(C_Hour,C_Min,C_Sec); } else if(clock_set == 3) {
SMG_Sec_Flash(C_Hour,C_Min,C_Sec); } else if(clock_set == 4) { SMG_Display(
T_Hour,T_Min,T_Sec); //时间实时显示 clock_set = 0; ET0 = 1,TR0 = 1; } } } if(S5 == 0)
{ Delay(20); if(S5 == 0) { while(S5 == 0); if(time_set == 1) //小时进行加 { T_Hour++;
if(T_Hour >= 24) //边界处理 { T_Hour = 0; } } else if(time_set == 2) //分钟进行加 { T_Min
++; if(T_Min >= 60) //边界处理 { T_Min = 0; } } else if(time_set == 3)//秒进行加 { T_Sec
++; if(T_Sec >= 60) //边界处理 { T_Sec = 0; } } else if(clock_set == 1) { C_Hour++;
if(C_Hour == 24) //边界处理 { C_Hour = 0; } } else if(clock_set == 2) { C_Min++; if(
C_Min== 60) { C_Min = 0; } } else if(clock_set == 3) { C_Sec++; if(C_Sec == 60)
{ C_Sec = 0; } } } } if(S4 == 0) { Delay(20); if(S4 == 0) { while(S4 == 0); if(
time_set== 1)//时进行减 { T_Hour--; if(T_Hour == 0)//边界处理 { T_Hour = 24; } } else if
(time_set == 2) //分钟减 { T_Min--; if(T_Min == 0) //边界处理 { T_Min = 60; } } else if
(time_set == 3) //秒进行减 { T_Sec--; if(T_Sec == 0)//边界处理 { T_Sec = 60; } } else if
(clock_set == 1) { C_Hour--; if(C_Hour == 0) //边界处理 { C_Hour = 24; } } else if(
clock_set== 2) { C_Min--; if(C_Min == 0) { C_Min = 60; } } else if(clock_set ==
3) { C_Sec--; if(C_Sec == 0) { C_Sec = 60; } } } } }
//-------------------------------------定时器初始化------------------- void
Init_Timer0(void) { TMOD = 0x11; //定时器0 定时器1 方式一16 TH0 = (65535-50000)/256;
//50ms TL0 = (65535-50000)%256; TH1 = (65535-50000)/256;//50ms TL1 = (65535-
50000)%256; ET1 = 1; TR1 = 1; ET0 = 1; TR0 = 1; EA = 1; }
//-----------------------------主函数------------------------------- void main(void
) { InitSyetem(); Init_Timer0(); while(1) { //读取温度 Read_Temperature(); if((
time_set== 0) && (clock_set == 0)) { if(S4 == 0) { Delay(20); if(S4 == 0) {
while(S4 == 0) { Delay(200); //显示温度 Temperature_Display(); } } } SMG_Display(
T_Hour,T_Min,T_Sec); //时间实时显示 // SMG_Display(C_Hour,C_Min,C_Sec); //闹钟实时显示 }
KeyScans(); if(mode_set == 1) { switch(time_set) { case 1:SMG_Hour_Flash(T_Hour,
T_Min,T_Sec);break; case 2:SMG_Min_Flash(T_Hour,T_Min,T_Sec);break; case 3:
SMG_Sec_Flash(T_Hour,T_Min,T_Sec);break; } } else if(mode_set == 2) { switch(
clock_set) { case 1:SMG_Hour_Flash(C_Hour,C_Min,C_Sec);break; case 2:
SMG_Min_Flash(C_Hour,C_Min,C_Sec);break; case 3:SMG_Sec_Flash(C_Hour,C_Min,C_Sec
);break; } } //闹钟到时响应 led1 0.2s闪烁 5s else if(((T_Hour == 0) && (T_Min == 0) && (
T_Sec== 0))|| ((T_Hour == C_Hour) && (T_Min == C_Min) && (T_Sec == C_Sec))) {
Init_74HC138(4); if(led_flag == 0) { P0 = 0xfe; } else if(led_flag == 1) { P0 =
0xff; } Delay(50000); Delay(50000); P0 = 0xff; } } }
//-----------------------------定时器中断服务------------------------- void
Server_Timero() interrupt 1 { TH0 = (65535-50000)/256; TL0 = (65535-50000)%256;
t_count0++; c_count++; if(t_count0 == 4) // 0.2s { led_flag = ~led_flag; } else
if(t_count0 == 20) // 1s 时间计数标志 { t_count0 = 0; if(T_Sec++ == 60) { T_Sec = 0;
T_Min++; if(T_Min == 60) { T_Min = 0; T_Hour++; if(T_Hour == 24) { T_Hour = 0; }
} } } if(c_count == 20) //闹钟计时1s { c_count = 0; C_Sec++; if(C_Sec == 60) { C_Sec
= 0; C_Min++; if(C_Min == 60) { C_Min = 0; C_Hour++; if(C_Hour == 24) { C_Hour =
0; } } } } } void Server_Timer1() interrupt 3 { TH1 = (65535-50000)/256;//50ms
TL1= (65535-50000)%256; t_count1++; if(t_count1 == 20) //1s { t_count1 = 0;
t_flag= ~t_flag; } }

技术
今日推荐
PPT
阅读数 126
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信