答案仅供参考,有错误欢迎指正,非常感谢!!!
文件较多,代码较长,感谢查看!!!
目录
显示当前设定的闹钟时间。我的理解是,当我按下S6按键后,直接将时钟显示的时间作为当前进行操作。
注意:不管是时钟设置还是闹钟设置,从秒切换回时钟显示后,设置的数据才开始生效,虽然题目没有讲清楚,但是从一个产品的角度出发,以及用户的实际使用角度出发,这样才符合正常的使用。
main.h
#ifndef __MAIN_H_
#define __MAIN_H_
#define uchar unsigned char
#define uint unsigned int
#include <STC15F2K60S2.H>
#include "Init.h"
#include "SMG.h"
#include "YanShi.h"
#include "DLKey.h"
#include "DSQ.h"
#include "onewire.h"
#include "ds1302.h"
unsigned char DLKey_GetKeynum();
void LED_Show();
#endif
main.c
#include "main.h"
uchar ShiZhong_SheZhi=0;//ʱÖÓÉèÖá£0£ºÊ±ÖÓÏÔʾ£»1£ºÊ±£»2£º·Ö£»3£ºÃë
uchar NaoZhong_SheZhi=0;//ÄÖÖÓÉèÖá£0£ºÊ±ÖÓÏÔʾ£»1£ºÊ±£»2£º·Ö£»3£ºÃë
uchar key_num;
char ShiZhong_Shi,ShiZhong_Fen,ShiZhong_Miao;
char NaoZhong_Shi=-1;
char NaoZhong_Fen=-1;
char NaoZhong_Miao=-1;
char NaoZhong_Shi_temp,NaoZhong_Fen_temp,NaoZhong_Miao_temp;
uint DanYuan_time=0;
char wen_du;
uchar TiShi_flag=0;//Ìáʾ¡£0£ºÌáʾ¹Ø±Õ£»1£ºÌáʾ¿ªÆô¡£
uint TiShi_time=0;
unsigned char DLKey_GetKeynum(){
unsigned char Key_num=0;
if(P30==0){
Delay10ms();
if(P30==0){
Key_num=7;
}
while(P30==0);
}
else if(P31==0){
Delay10ms();
if(P31==0){
Key_num=6;
}
while(P31==0);
}
else if(P32==0){
Delay10ms();
if(P32==0){
Key_num=5;
}
while(P32==0);
}
else if(P33==0){
Delay10ms();
if(P33==0){
Key_num=4;
}
if(ShiZhong_SheZhi>0||NaoZhong_SheZhi>0){
while(P33==0);
}
}
DLKey_Init();
return Key_num;
}
void main(){
Init_BZ();
SMG_Init();
DLKey_Init();
DSQ_2_Init();
Ds1302_XieData(23,59,50);
DS18B20_GetWenDu();
while(1){
key_num=DLKey_GetKeynum();
if(key_num==7){
TiShi_flag=0;
if(NaoZhong_SheZhi==0){
ShiZhong_SheZhi++;
ShiZhong_SheZhi=ShiZhong_SheZhi%4;
if(ShiZhong_SheZhi==0){
Ds1302_XieData(ShiZhong_Shi,ShiZhong_Fen,ShiZhong_Miao);
}
}
}
else if(key_num==6){
TiShi_flag=0;
if(ShiZhong_SheZhi==0){
NaoZhong_SheZhi++;
NaoZhong_SheZhi=NaoZhong_SheZhi%4;
if(NaoZhong_SheZhi==0){
NaoZhong_Shi=NaoZhong_Shi_temp;
NaoZhong_Fen=NaoZhong_Fen_temp;
NaoZhong_Miao=NaoZhong_Miao_temp;
}
else if(NaoZhong_SheZhi==1){
NaoZhong_Shi_temp=ShiZhong_Shi;
NaoZhong_Fen_temp=ShiZhong_Fen;
NaoZhong_Miao_temp=ShiZhong_Miao;
}
}
}
else if(key_num==5){
TiShi_flag=0;
if(ShiZhong_SheZhi>0){
switch(ShiZhong_SheZhi){
case 1:{
ShiZhong_Shi++;
if(ShiZhong_Shi>=23){
ShiZhong_Shi=23;
}
break;
}
case 2:{
ShiZhong_Fen++;
if(ShiZhong_Fen>=59){
ShiZhong_Fen=59;
}
break;
}
case 3:{
ShiZhong_Miao++;
if(ShiZhong_Miao>=59){
ShiZhong_Miao=59;
}
break;
}
}
}
else if(NaoZhong_SheZhi>0){
switch(NaoZhong_SheZhi){
case 1:{
NaoZhong_Shi_temp++;
if(NaoZhong_Shi_temp>=23){
NaoZhong_Shi_temp=23;
}
break;
}
case 2:{
NaoZhong_Fen_temp++;
if(NaoZhong_Fen_temp>=59){
NaoZhong_Fen_temp=59;
}
break;
}
case 3:{
NaoZhong_Miao_temp++;
if(NaoZhong_Miao_temp>=59){
NaoZhong_Miao_temp=59;
}
break;
}
}
}
}
else if(key_num==4){
TiShi_flag=0;
if(ShiZhong_SheZhi>0){
switch(ShiZhong_SheZhi){
case 1:{
ShiZhong_Shi--;
if(ShiZhong_Shi<=0){
ShiZhong_Shi=0;
}
break;
}
case 2:{
ShiZhong_Fen--;
if(ShiZhong_Fen<=0){
ShiZhong_Fen=0;
}
break;
}
case 3:{
ShiZhong_Miao--;
if(ShiZhong_Miao<=0){
ShiZhong_Miao=0;
}
break;
}
}
}
else if(NaoZhong_SheZhi>0){
switch(NaoZhong_SheZhi){
case 1:{
NaoZhong_Shi_temp--;
if(NaoZhong_Shi_temp<=0){
NaoZhong_Shi_temp=0;
}
break;
}
case 2:{
NaoZhong_Fen_temp--;
if(NaoZhong_Fen_temp<=0){
NaoZhong_Fen_temp=0;
}
break;
}
case 3:{
NaoZhong_Miao_temp--;
if(NaoZhong_Miao_temp<=0){
NaoZhong_Miao_temp=0;
}
break;
}
}
}
else{
wen_du=(char)DS18B20_GetWenDu();
}
}
if(ShiZhong_SheZhi==0&&NaoZhong_SheZhi==0){
ShiZhong_Shi=Ds1302_GetShi();
ShiZhong_Fen=Ds1302_GetFen();
ShiZhong_Miao=Ds1302_GetMiao();
}
if(ShiZhong_Shi==NaoZhong_Shi&&ShiZhong_Fen==NaoZhong_Fen&&ShiZhong_Miao==NaoZhong_Miao){
TiShi_flag=1;
TiShi_time=0;
}
}
}
void DSQ_2_ZD() interrupt 12{
if(key_num!=4&&ShiZhong_SheZhi==0&&NaoZhong_SheZhi==0){
SMG_Show(ShiZhong_Shi/10,ShiZhong_Shi%10,17,ShiZhong_Fen/10,ShiZhong_Fen%10,17,ShiZhong_Miao/10,ShiZhong_Miao%10);
}
else if(ShiZhong_SheZhi>0||NaoZhong_SheZhi>0){
DanYuan_time++;
if(DanYuan_time>=2000){
DanYuan_time=0;
}
if(DanYuan_time>=0&&DanYuan_time<=1000){
if(ShiZhong_SheZhi>0){
SMG_Show(ShiZhong_Shi/10,ShiZhong_Shi%10,17,ShiZhong_Fen/10,ShiZhong_Fen%10,17,ShiZhong_Miao/10,ShiZhong_Miao%10);
}
if(NaoZhong_SheZhi>0){
SMG_Show(NaoZhong_Shi_temp/10,NaoZhong_Shi_temp%10,17,NaoZhong_Fen_temp/10,NaoZhong_Fen_temp%10,17,NaoZhong_Miao_temp/10,NaoZhong_Miao_temp%10);
}
}
else if(DanYuan_time>1000){
switch(ShiZhong_SheZhi){
case 1:{
SMG_Show(16,16,17,ShiZhong_Fen/10,ShiZhong_Fen%10,17,ShiZhong_Miao/10,ShiZhong_Miao%10);
break;
}
case 2:{
SMG_Show(ShiZhong_Shi/10,ShiZhong_Shi%10,17,16,16,17,ShiZhong_Miao/10,ShiZhong_Miao%10);
break;
}
case 3:{
SMG_Show(ShiZhong_Shi/10,ShiZhong_Shi%10,17,ShiZhong_Fen/10,ShiZhong_Fen%10,17,16,16);
break;
}
}
switch(NaoZhong_SheZhi){
case 1:{
SMG_Show(16,16,17,NaoZhong_Fen_temp/10,NaoZhong_Fen_temp%10,17,NaoZhong_Miao_temp/10,NaoZhong_Miao_temp%10);
break;
}
case 2:{
SMG_Show(NaoZhong_Shi_temp/10,NaoZhong_Shi_temp%10,17,16,16,17,NaoZhong_Miao_temp/10,NaoZhong_Miao_temp%10);
break;
}
case 3:{
SMG_Show(NaoZhong_Shi_temp/10,NaoZhong_Shi_temp%10,17,NaoZhong_Fen_temp/10,NaoZhong_Fen_temp%10,17,16,16);
break;
}
}
}
}
else if(key_num==4&&ShiZhong_SheZhi==0&&NaoZhong_SheZhi==0){
if(wen_du>=10){
SMG_Show(16,16,16,16,16,wen_du/10,wen_du%10,12);
}
else if(wen_du>=0){
SMG_Show(16,16,16,16,16,16,wen_du,12);
}
else if(wen_du<0){
SMG_Show(16,16,16,16,16,17,wen_du,12);
}
}
if(TiShi_flag){
TiShi_time++;
if(TiShi_time>=5000){
TiShi_time=0;
TiShi_flag=0;
}
}
LED_Show();
}
void LED_Show(){
uchar L1=0xff;
if((TiShi_time%400)<200){
L1=0xff;
}
else{
L1=0xfe;
}
P0=L1;
P2=(P2&0x1f)|0x80;
P2=(P2&0x1f)|0x00;
}
Init.h
#ifndef __INIT_H_
#define __INIT_H_
#include <STC15F2K60S2.H>
void Init_BZ();
#endif
Init.c
#include "Init.h"
void Init_BZ(){
P2=(P2&0x1f)|0xa0;
P0=0x00;
P2=(P2&0x1f)|0x80;
P0=0xff;
}
SMG.h
#ifndef __SMG_H_
#define __SMG_H_
#include <STC15F2K60S2.H>
void SMG_Init();
void SMG_Show(unsigned char n1,n2,n3,n4,n5,n6,n7,n8);
#endif
SMG.c
#include "SMG.h"
unsigned char code t_display[]={ //????
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
//black - H J K L N o P U t G Q r M y
0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e,
0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46}; //0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1
void SMG_Init(){
P2=(P2&0x1f)|0xc0;
P0=0xff;
P2=(P2&0x1f)|0xe0;
P0=0xff;
}
void SMG_Show(unsigned char n1,n2,n3,n4,n5,n6,n7,n8){
static unsigned char i=0;
i++;
i=i%8;
P2=(P2&0x1f)|0xc0;
switch(i){
case 1:{
P0=0x01;
P2=(P2&0x1f)|0xe0;
P0=~t_display[n1];
break;
}
case 2:{
P0=0x02;
P2=(P2&0x1f)|0xe0;
P0=~t_display[n2];
break;
}
case 3:{
P0=0x04;
P2=(P2&0x1f)|0xe0;
P0=~t_display[n3];
break;
}
case 4:{
P0=0x08;
P2=(P2&0x1f)|0xe0;
P0=~t_display[n4];
break;
}
case 5:{
P0=0x10;
P2=(P2&0x1f)|0xe0;
P0=~t_display[n5];
break;
}
case 6:{
P0=0x20;
P2=(P2&0x1f)|0xe0;
P0=~t_display[n6];
break;
}
case 7:{
P0=0x40;
P2=(P2&0x1f)|0xe0;
P0=~t_display[n7];
break;
}
case 0:{
P0=0x80;
P2=(P2&0x1f)|0xe0;
P0=~t_display[n8];
break;
}
}
P2=(P2&0x1f)|0x00;
}
DLKey.h
#ifndef __DLKEY_H_
#define __DLKEY_H_
#include "YanShi.h"
void DLKey_Init();
#endif
DLKey.c
#include "DLKey.h"
void DLKey_Init(){
P30=1;
P31=1;
P32=1;
P33=1;
}
DSQ.h
#ifndef __DSQ_H_
#define __DSQ_H_
#include <STC15F2K60S2.H>
void DSQ_2_Init();
#endif
DSQ.c
#include "DSQ.h"
void DSQ_2_Init(){
AUXR |= 0x04; //定时器时钟1T模式
T2L = 0x20; //设置定时初值
T2H = 0xD1; //设置定时初值
AUXR |= 0x10; //定时器2开始计时
AUXR=AUXR&0xf7;
IE2=IE2|0x04;
EA=1;
}
ds1302.h
#ifndef __DS1302_H
#define __DS1302_H
#include <STC15F2K60S2.H>
#include <intrins.h>
void Write_Ds1302(unsigned char temp);
void Write_Ds1302_Byte( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302_Byte( unsigned char address );
void Ds1302_XieData(unsigned char shi,fen,miao);
unsigned char Ds1302_GetShi();
unsigned char Ds1302_GetFen();
unsigned char Ds1302_GetMiao();
#endif
ds1302.c
/*
³ÌÐò˵Ã÷: DS1302Çý¶¯³ÌÐò
Èí¼þ»·¾³: Keil uVision 4.10
Ó²¼þ»·¾³: CT107µ¥Æ¬»ú×ÛºÏʵѵƽ̨ 8051£¬12MHz
ÈÕ ÆÚ: 2011-8-9
*/
#include "ds1302.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 Ds1302_XieData(unsigned char shi,fen,miao){
shi=(shi/10)*16+(shi%10);
fen=(fen/10)*16+(fen%10);
miao=(miao/10)*16+(miao%10);
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(0x84,shi);
Write_Ds1302_Byte(0x82,fen);
Write_Ds1302_Byte(0x80,miao);
Write_Ds1302_Byte(0x8e,0x80);
}
unsigned char Ds1302_GetShi(){
unsigned char date;
date=Read_Ds1302_Byte(0x85);
date=(date/16)*10+(date%16);
return date;
}
unsigned char Ds1302_GetFen(){
unsigned char date;
date=Read_Ds1302_Byte(0x83);
date=(date/16)*10+(date%16);
return date;
}
unsigned char Ds1302_GetMiao(){
unsigned char date;
date=Read_Ds1302_Byte(0x81);
date=(date/16)*10+(date%16);
return date;
}
onewire.h
#ifndef __ONEWIRE_H
#define __ONEWIRE_H
#include <STC15F2K60S2.H>
float DS18B20_GetWenDu();
#endif
onewire.c
/*
³ÌÐò˵Ã÷: µ¥×ÜÏßÇý¶¯³ÌÐò
Èí¼þ»·¾³: Keil uVision 4.10
Ó²¼þ»·¾³: CT107µ¥Æ¬»ú×ÛºÏʵѵƽ̨(Íⲿ¾§Õñ12MHz) STC89C52RCµ¥Æ¬»ú
ÈÕ ÆÚ: 2011-8-9
*/
#include "onewire.h"
sbit DQ = P1^4; //µ¥×ÜÏß½Ó¿Ú
//µ¥×ÜÏßÑÓʱº¯Êý
void Delay_OneWire(unsigned int t) //STC89C52RC
{
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;
}
float DS18B20_GetWenDu(){
float wen_du,zs,xs;
unsigned char di,gao;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay_OneWire(200);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
EA=0;
di=Read_DS18B20();
gao=Read_DS18B20();
EA=1;
if(gao>=240){
di=~di;
gao=~gao;
if(di==0xff){
di=0;
gao++;
}
else{
di++;
}
zs=((gao<<4)|(di>>4))*1.0;
xs=(di&0x0f)*0.0625;
wen_du=-(zs+xs);
}
else{
zs=((gao<<4)|(di>>4))*1.0;
xs=(di&0x0f)*0.0625;
wen_du=(zs+xs);
}
return wen_du;
}
YanShi.h
#ifndef __YANSHI_H_
#define __YANSHI_H_
#include <STC15F2K60S2.H>
#include "intrins.h"
void Delay10ms();
#endif
YanShi.c
#include "YanShi.h"
void Delay10ms() //@12.000MHz
{
unsigned char i, j;
i = 117;
j = 184;
do
{
while (--j);
} while (--i);
}