标题:
蓝桥杯单片机组 第八届省赛-基于单片机的电子钟程序设计
[打印本页]
作者:
Donsuke
时间:
2019-3-25 15:44
标题:
蓝桥杯单片机组 第八届省赛-基于单片机的电子钟程序设计
没有使用DS1302模块,用定时器完全实现了题目要求,发出来让没有思路的同学们参考一下。
本人水平有限,如果有不足之处多多指正。
单片机源程序如下:
#include "intrins.h"
#include "STC15F2K60S2.h"
#include "key.h"
#include "onewire.h"
sbit S7 = P3^0;
sbit S6 = P3^1;
sbit S5 = P3^2;
sbit S4 = P3^3;
unsigned char table[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xff,0xbf};
unsigned char distime = 0;
unsigned char num0[] = {0,0,17,0,0,17,0,0}; //显示时钟
unsigned char num1[] = {0,0,17,0,0,17,0,0}; //显示闹钟设置时间
unsigned char num2[] = {16,16,16,16,16,0,0,12}; //显示温度
int shizhong = 0; //用于时钟的正确运行
unsigned char time_shi = 0 , time_fen = 0 , time_miao = 0; //时钟参数
unsigned char time_set_mode = 0; //时钟设置模式,0为不设置,1为设置时,2为设置分,3为设置秒
unsigned char alarm_shi = 0 , alarm_fen = 0 , alarm_miao = 0; //闹钟参数
unsigned char alarm_set_mode = 0; //闹钟设置模式,对应同上
int alarm_count = 0 ;
bit alarm_start = 0 , alarm_enough = 0;
unsigned char temper = 0;
unsigned char DS_mode = 0; //数码管显示模式,0为时钟,1为闹钟,2为温度
bit flash_start = 0 ;
bit flash0 = 1 , flash1 = 1;
bit LED_mode = 0;
unsigned int LED_count = 0;
void LED_flash()
{
if(LED_mode)
{
P2 = (P2 & 0x1f) | 0x80;
P0 = 0xfe;
P2 &= 0x1f;
P0 = 0xff;
}
else
{
P2 = (P2 & 0x1f) | 0x80;
P0 = 0xff;
P2 &= 0x1f;
P0 = 0xff;
}
if(++LED_count==1000)
LED_mode = ~(LED_mode);
}
void display()
{
P2 |= 0xc0;
P0 = 1<<distime;
P2 &= ~(0xc0);
P0 = 0xff;
switch(DS_mode)
{
case 0:
if(flash0)
{
P2 |= 0xe0;
P0 = table[num0[distime]];
P2 &= ~(0xe0);
P0 = 0x00;
}
else
{
if((distime==(3*(time_set_mode-1)))||(distime==(3*(time_set_mode-1)+1)))
{
P2 |= 0xe0;
P0 = 0xff;
P2 &= ~(0xe0);
P0 = 0x00;
}
else
{
P2 |= 0xe0;
P0 = table[num0[distime]];
P2 &= ~(0xe0);
P0 = 0x00;
}
}
break;
case 1:
if(flash1)
{
P2 |= 0xe0;
P0 = table[num1[distime]];
P2 &= ~(0xe0);
P0 = 0x00;
}
else
{
if((distime==(3*(alarm_set_mode-1)))||(distime==(3*(alarm_set_mode-1)+1)))
{
P2 |= 0xe0;
P0 = 0xff;
P2 &= ~(0xe0);
P0 = 0x00;
}
else
{
P2 |= 0xe0;
P0 = table[num1[distime]];
P2 &= ~(0xe0);
P0 = 0x00;
}
}
break;
case 2:
P2 |= 0xe0;
P0 = table[num2[distime]];
P2 &= ~(0xe0);
P0 = 0x00;
break;
}
if(++distime==8) distime = 0;
}
void Timer0Init() //2ms
{
AUXR |= 0x80; //?????1T??
TMOD &= 0xF0; //???????
TL0 = 0x40; //??????
TH0 = 0xA2; //??????
TF0 = 0; //??TF0??
TR0 = 1; //???0????
EA = 1;
ET0 = 1;
}
void timer0() interrupt 1
{
//====================================================================================
if(alarm_start) //L1闪烁(闹钟)控制
{
if(++alarm_count==2500) //5s
{
alarm_count = 0;
alarm_start = 0;
alarm_enough = 1;
}
}
//====================================================================================
if(++shizhong==500) //时钟正常计时控制,1s
{
if(time_miao==59)
{
time_miao = 0;
if(time_fen==59)
{
time_fen = 0;
if(time_shi==23)
{
time_shi = 0;
}
else
time_shi++;
}
else
time_fen++;
}
else
time_miao++;
shizhong = 0;
if(flash_start)
{
if(DS_mode==0)
flash0 = ~(flash0);
else
flash0 = 1;
if(DS_mode==1)
flash1 = ~(flash1);
else
flash1 = 1;
}
}
//====================================================================================
display();
}
void main()
{
P2 = (P2 & 0x1f) | 0xa0;
P0 = 0;
P2 &= 0x1f;
P0 = 0xff;
P2 = (P2 & 0x1f) | 0x80;
P0 = 0xff;
P2 &= 0x1f;
P0 = 0x00;
time_shi = 23;
time_fen = 59;
time_miao = 50;
alarm_shi = 0;
alarm_fen = 0;
alarm_miao = 0;
Timer0Init();
//////////////////////初始化
//=========================================================================================
while(1)
{
num0[0] = time_shi/10;
num0[1] = time_shi%10;
num0[3] = time_fen/10;
num0[4] = time_fen%10;
num0[6] = time_miao/10;
num0[7] = time_miao%10;
num1[0] = alarm_shi/10;
num1[1] = alarm_shi%10;
num1[3] = alarm_fen/10;
num1[4] = alarm_fen%10;
num1[6] = alarm_miao/10;
num1[7] = alarm_miao%10;
//=========================================================================================
if(S7==0) //时钟设定
{
Delay10ms();
if(S7==0)
{
DS_mode = 0;
alarm_set_mode = 0;
switch(time_set_mode)
{
case 0:
time_set_mode = 1;
break;
case 1:
time_set_mode = 2;
break;
case 2:
time_set_mode = 3;
break;
case 3:
time_set_mode = 0;
break;
default:
break;
}
while(S7==0);
}
}
//=========================================================================================
if(S6==0) //闹钟设定
{
Delay10ms();
if(S6==0)
{
DS_mode = 1;
time_set_mode = 0;
switch(alarm_set_mode)
{
case 0:
alarm_set_mode = 1;
break;
case 1:
alarm_set_mode = 2;
break;
case 2:
alarm_set_mode = 3;
break;
case 3:
alarm_set_mode = 0;
DS_mode = 0;
break;
default:
break;
}
while(S6==0);
}
}
//=========================================================================================
if(S5==0) //加
{
Delay10ms();
if(S5==0)
{
if(time_set_mode!=0&&alarm_set_mode==0)
{
switch(time_set_mode)
{
case 1:
if(time_shi==23)
time_shi = 0;
else
time_shi++;
break;
case 2:
if(time_fen==59)
time_fen = 0;
else
time_fen++;
break;
case 3:
if(time_miao==59)
time_miao = 0;
else
time_miao++;
break;
default:
break;
}
}
if(time_set_mode==0&&alarm_set_mode!=0)
{
switch(alarm_set_mode)
{
case 1:
if(alarm_shi==23)
alarm_shi = 0;
else
alarm_shi++;
break;
case 2:
if(alarm_fen==59)
alarm_fen = 0;
else
alarm_fen++;
break;
case 3:
if(alarm_miao==59)
alarm_miao = 0;
else
alarm_miao++;
break;
default:
break;
}
}
while(S5==0);
}
}
//=========================================================================================
if(S4==0) //减;显示温度
{
Delay10ms();
if(S4==0)
{
if(time_set_mode!=0&&alarm_set_mode==0)
{
switch(time_set_mode)
{
case 1:
if(time_shi==0)
time_shi = 23;
else
time_shi--;
break;
case 2:
if(time_fen==0)
time_fen = 59;
else
time_fen--;
break;
case 3:
if(time_miao==0)
time_miao = 59;
else
time_miao--;
break;
default:
break;
}
}
if(time_set_mode==0&&alarm_set_mode!=0)
{
switch(alarm_set_mode)
{
case 1:
if(alarm_shi==0)
alarm_shi = 23;
else
alarm_shi--;
break;
case 2:
if(alarm_fen==0)
alarm_fen = 59;
else
alarm_fen--;
break;
case 3:
if(alarm_miao==0)
alarm_miao = 59;
else
alarm_miao--;
break;
default:
break;
}
}
if(time_set_mode==0&&alarm_set_mode==0)
{
temper = temper_read();
num2[5] = temper/10;
num2[6] = temper%10;
DS_mode = 2;
}
while(S4==0);
DS_mode = 0;
}
……………………
…………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
电子钟(自)2018.zip
(53.58 KB, 下载次数: 30)
2019-3-25 15:43 上传
点击文件名下载附件
下载积分: 黑币 -5
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1