蓝桥杯之单片机学习(二十七)——电子钟(附题目和完整代码)
博主写题时,两个纰漏
- 时钟显示顺序错误
- 再对8位进行定义时,没包装成子函数,显的很啰嗦
一、题目展示
二、代码展示
2.1 main.c
#include <STC15F2K60S2.h>
#include "ds1302.h"
#include "onewire.h"
#define uchar unsigned char
#define uint unsigned int
uchar code SMG_duanma[19] =
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
0x88,0x80,0xc6,0xc0,0x86,0x8e,
0xbf,0x7f,0XFF};//分别是0-9(对应下标),A-F,“-”,“.”,“灭”
//分别是“0.-9.”
uchar code SMG_Dot_AC[10] =
{0X40,0X79,0X24,0X30,0X19,0X12,0X02,0X78,0X00,0X10};
//对应数码管8位位码
uchar yi,er,san,si,wu,liu,qi,ba;
//秒、分、时、日、月、周、年
uchar shijian[] = {0X50,0X59,0X23,0X00,0X00,0X00,0X00};
//闹钟
uchar naozhong[] = {0X00,0X00,0X00,0X00,0X00,0X00,0X00};
//DS1302写和读地址
uchar Write_DS1302_adrr[7] = {0X80, 0X82, 0X84, 0X86, 0X88, 0X8A, 0X8C};
uchar Read_DS1302_adrr[7] = {0X81, 0X83, 0X85, 0X87, 0X89, 0X8B, 0X8D};
//时钟显示与设置(0为显示状态、1为设置状态)
uchar Mode_shizhong = 0;
//判断是亮还是灭(0是亮,1是灭)
uchar Mode_light = 0;
//闹钟设置
uchar Mode_naozhong = 0;
uchar tt = 0;
uchar t = 0;
uchar miao = 0;
//在加减时,判断S4还是S5\S7按下,一般置0
uchar Key_S4_S5_S7_Flag = 0;
//在闹钟加减时
uchar Key_S4_S5_S6_Flag = 0;
//判断L1是亮还是灭(0是亮,1是灭)
uchar Mode_L1_light = 0;
//闹钟提示标志位
uchar Check_naozhong = 0;
//系统初始化
void Initsys();
//配置HC138
void SelectHC138(uchar channel);
//在pos位码上,显示value段码
void DisplaySMG_Bit(uchar pos, uchar value);
//数码管8位码显示
void SMG_Display();
//数码管的延时
void Delay_one_ms_SMG();
//独立按键(BTN)
void Alone_Key();
//按键消抖延时
void Delay_five_ms_Key();
//读出时间
void Read_DS1302();
//时间初始化
void Init_DS1302();
//时钟设置函数
void Setting_shizhong();
//中断初始化
void Timer0Init(void);
//闹钟设置函数
void Setting_naozhong();
void main()
{
yi = er = san = si = wu = liu = qi = ba = 18;
Init_DS1302();
Initsys();
Timer0Init();
while(1)
{
Read_DS1302();
Alone_Key();
yi = shijian[0] / 16;
er = shijian[0] % 16;
san = 16;
si = shijian[1] / 16;
wu = shijian[1] % 16;
liu = 16;
qi = shijian[2] / 16;
ba = shijian[2] % 16;
SMG_Display();
if(Mode_shizhong == 1)
{
Setting_shizhong();
}
if(Mode_naozhong == 1)
{
Setting_naozhong();
}
if(naozhong[0] == shijian[0] && naozhong[1] == shijian[1] && naozhong[2] == shijian[2])
{
Check_naozhong = 1;
miao = 0;
}
if(Check_naozhong == 0)
{
SelectHC138(4);P0 = 0XFF;
}
}
}
//闹钟设置函数
void Setting_naozhong()
{
//1
while(1)
{
Key_S4_S5_S6_Flag = 0;
Alone_Key();
if(Mode_light == 0)yi = 18;
else if(Mode_light == 1)yi = naozhong[0] / 16;;
er = naozhong[0] % 16;
san = 16;
si = naozhong[1] / 16;
wu = naozhong[1] % 16;
liu = 16;
qi = naozhong[2] / 16;
ba = naozhong[2] % 16;
SMG_Display();
if(Key_S4_S5_S6_Flag == 5 && yi != 9 && yi != 18){naozhong[0] = naozhong[0] + 16;Key_S4_S5_S6_Flag = 0;}
if(Key_S4_S5_S6_Flag == 4 && yi != 0 && yi != 18){naozhong[0] = naozhong[0] - 16;Key_S4_S5_S6_Flag = 0;}
if(Key_S4_S5_S6_Flag == 6)break;
}
//2
while(1)
{
Key_S4_S5_S6_Flag = 0;
Alone_Key();
if(Mode_light == 0)er = 18;
else if(Mode_light == 1)er = naozhong[0] % 16;;
yi = naozhong[0] / 16;
san = 16;
si = naozhong[1] / 16;
wu = naozhong[1] % 16;
liu = 16;
qi = naozhong[2] / 16;
ba = naozhong[2] % 16;
SMG_Display();
if(Key_S4_S5_S6_Flag == 5 && er != 9 && er != 18){naozhong[0] = naozhong[0] + 1;Key_S4_S5_S6_Flag = 0;}
if(Key_S4_S5_S6_Flag == 4 && er != 0 && er != 18){naozhong[0] = naozhong[0] - 1;Key_S4_S5_S6_Flag = 0;}
if(Key_S4_S5_S6_Flag == 6)break;
}
//4
while(1)
{
Key_S4_S5_S6_Flag = 0;
Alone_Key();
if(Mode_light == 0)si = 18;
else if(Mode_light == 1)si = naozhong[1] / 16;;
yi = naozhong[0] / 16;
er = naozhong[0] % 16;
san = 16;
wu = naozhong[1] % 16;
liu = 16;
qi = naozhong[2] / 16;
ba = naozhong[2] % 16;
SMG_Display();
if(Key_S4_S5_S6_Flag == 5 && si != 9 && si != 18){naozhong[1] = naozhong[1] + 16;Key_S4_S5_S6_Flag = 0;}
if(Key_S4_S5_S6_Flag == 4 && si != 0 && si != 18){naozhong[1] = naozhong[1] - 16;Key_S4_S5_S6_Flag = 0;}
if(Key_S4_S5_S6_Flag == 6)break;
}
//5
while(1)
{
Key_S4_S5_S6_Flag = 0;
Alone_Key();
if(Mode_light == 0)wu = 18;
else if(Mode_light == 1)wu = naozhong[1] % 16;;
yi = naozhong[0] / 16;
er = naozhong[0] % 16;
san = 16;
si = naozhong[1] / 16;
liu = 16;
qi = naozhong[2] / 16;
ba = naozhong[2] % 16;
SMG_Display();
if(Key_S4_S5_S6_Flag == 5 && wu != 9 && wu != 18){naozhong[1] = naozhong[1] + 1;Key_S4_S5_S6_Flag = 0;}
if(Key_S4_S5_S6_Flag == 4 && wu != 0 && wu != 18){naozhong[1] = naozhong[1] - 1;Key_S4_S5_S6_Flag = 0;}
if(Key_S4_S5_S6_Flag == 6)break;
}
//7
while(1)
{
Key_S4_S5_S6_Flag = 0;
Alone_Key();
if(Mode_light == 0)qi = 18;
else if(Mode_light == 1)qi = naozhong[2] / 16;;
yi = naozhong[0] / 16;
er = naozhong[0] % 16;
san = 16;
si = naozhong[1] / 16;
wu = naozhong[1] % 16;
liu = 16;
ba = naozhong[2] % 16;
SMG_Display();
if(Key_S4_S5_S6_Flag == 5 && qi != 9 && qi != 18){naozhong[2] = naozhong[2] + 16;Key_S4_S5_S6_Flag = 0;}
if(Key_S4_S5_S6_Flag == 4 && qi != 0 && qi != 18){naozhong[2] = naozhong[2] - 16;Key_S4_S5_S6_Flag = 0;}
if(Key_S4_S5_S6_Flag == 6)break;
}
//8
while(1)
{
Key_S4_S5_S6_Flag = 0;
Alone_Key();
if(Mode_light == 0)ba = 18;
else if(Mode_light == 1)ba = naozhong[2] % 16;;
yi = naozhong[0] / 16;
er = naozhong[0] % 16;
san = 16;
si = naozhong[1] / 16;
wu = naozhong[1] % 16;
liu = 16;
qi = naozhong[2] / 16;
SMG_Display();
if(Key_S4_S5_S6_Flag == 5 && ba != 9 && ba != 18){naozhong[2] = naozhong[2] + 1;Key_S4_S5_S6_Flag = 0;}
if(Key_S4_S5_S6_Flag == 4 && ba != 0 && ba != 18){naozhong[2] = naozhong[2] - 1;Key_S4_S5_S6_Flag = 0;}
if(Key_S4_S5_S6_Flag == 6)break;
}
Key_S4_S5_S6_Flag = 0;
Mode_naozhong = 0;
}
//时钟设置函数
void Setting_shizhong()
{
//1
while(1)
{
Key_S4_S5_S7_Flag = 0;
Alone_Key();
if(Mode_light == 0)yi = 18;
else if(Mode_light == 1)yi = shijian[0] / 16;;
er = shijian[0] % 16;
san = 16;
si = shijian[1] / 16;
wu = shijian[1] % 16;
liu = 16;
qi = shijian[2] / 16;
ba = shijian[2] % 16;
SMG_Display();
if(Key_S4_S5_S7_Flag == 5 && yi != 9 && yi != 18){shijian[0] = shijian[0] + 16;Key_S4_S5_S7_Flag = 0;}
if(Key_S4_S5_S7_Flag == 4 && yi != 0 && yi != 18){shijian[0] = shijian[0] - 16;Key_S4_S5_S7_Flag = 0;}
if(Key_S4_S5_S7_Flag == 7)break;
}
//2
while(1)
{
Key_S4_S5_S7_Flag = 0;
Alone_Key();
if(Mode_light == 0)er = 18;
else if(Mode_light == 1)er = shijian[0] % 16;;
yi = shijian[0] / 16;
san = 16;
si = shijian[1] / 16;
wu = shijian[1] % 16;
liu = 16;
qi = shijian[2] / 16;
ba = shijian[2] % 16;
SMG_Display();
if(Key_S4_S5_S7_Flag == 5 && er != 9 && er != 18){shijian[0] = shijian[0] + 1;Key_S4_S5_S7_Flag = 0;}
if(Key_S4_S5_S7_Flag == 4 && er != 0 && er != 18){shijian[0] = shijian[0] - 1;Key_S4_S5_S7_Flag = 0;}
if(Key_S4_S5_S7_Flag == 7)break;
}
//4
while(1)
{
Key_S4_S5_S7_Flag = 0;
Alone_Key();
if(Mode_light == 0)si = 18;
else if(Mode_light == 1)si = shijian[1] / 16;;
yi = shijian[0] / 16;
er = shijian[0] % 16;
san = 16;
wu = shijian[1] % 16;
liu = 16;
qi = shijian[2] / 16;
ba = shijian[2] % 16;
SMG_Display();
if(Key_S4_S5_S7_Flag == 5 && si != 9 && si != 18){shijian[1] = shijian[1] + 16;Key_S4_S5_S7_Flag = 0;}
if(Key_S4_S5_S7_Flag == 4 && si != 0 && si != 18){shijian[1] = shijian[1] - 16;Key_S4_S5_S7_Flag = 0;}
if(Key_S4_S5_S7_Flag == 7)break;
}
//5
while(1)
{
Key_S4_S5_S7_Flag = 0;
Alone_Key();
if(Mode_light == 0)wu = 18;
else if(Mode_light == 1)wu = shijian[1] % 16;;
yi = shijian[0] / 16;
er = shijian[0] % 16;
san = 16;
si = shijian[1] / 16;
liu = 16;
qi = shijian[2] / 16;
ba = shijian[2] % 16;
SMG_Display();
if(Key_S4_S5_S7_Flag == 5 && wu != 9 && wu != 18){shijian[1] = shijian[1] + 1;Key_S4_S5_S7_Flag = 0;}
if(Key_S4_S5_S7_Flag == 4 && wu != 0 && wu != 18){shijian[1] = shijian[1] - 1;Key_S4_S5_S7_Flag = 0;}
if(Key_S4_S5_S7_Flag == 7)break;
}
//7
while(1)
{
Key_S4_S5_S7_Flag = 0;
Alone_Key();
if(Mode_light == 0)qi = 18;
else if(Mode_light == 1)qi = shijian[2] / 16;;
yi = shijian[0] / 16;
er = shijian[0] % 16;
san = 16;
si = shijian[1] / 16;
wu = shijian[1] % 16;
liu = 16;
ba = shijian[2] % 16;
SMG_Display();
if(Key_S4_S5_S7_Flag == 5 && qi != 9 && qi != 18){shijian[2] = shijian[2] + 16;Key_S4_S5_S7_Flag = 0;}
if(Key_S4_S5_S7_Flag == 4 && qi != 0 && qi != 18){shijian[2] = shijian[2] - 16;Key_S4_S5_S7_Flag = 0;}
if(Key_S4_S5_S7_Flag == 7)break;
}
//8
while(1)
{
Key_S4_S5_S7_Flag = 0;
Alone_Key();
if(Mode_light == 0)ba = 18;
else if(Mode_light == 1)ba = shijian[2] % 16;;
yi = shijian[0] / 16;
er = shijian[0] % 16;
san = 16;
si = shijian[1] / 16;
wu = shijian[1] % 16;
liu = 16;
qi = shijian[2] / 16;
SMG_Display();
if(Key_S4_S5_S7_Flag == 5 && ba != 9 && ba != 18){shijian[2] = shijian[2] + 1;Key_S4_S5_S7_Flag = 0;}
if(Key_S4_S5_S7_Flag == 4 && ba != 0 && ba != 18){shijian[2] = shijian[2] - 1;Key_S4_S5_S7_Flag = 0;}
if(Key_S4_S5_S7_Flag == 7)break;
}
//情况按键标志位
Key_S4_S5_S7_Flag = 0;
//初始化时钟
Init_DS1302();
//变成显示模式
Mode_shizhong = 0;
}
//按键消抖延时
void Delay_five_ms_Key()
{
uint i,j;
for(i = 0; i < 5; i++)
for(j = 845; j > 0; j--);
}
//独立按键(BTN)
void Alone_Key()
{
//S7按键
if(P30 == 0)
{
Delay_five_ms_Key();
if(P30 == 0)
{
Check_naozhong = 0;
if(Mode_shizhong == 0 && Mode_naozhong != 1)Mode_shizhong = 1;
else if(Mode_shizhong == 1)
{
Key_S4_S5_S7_Flag = 7;
}
}
while(!P30);
}
//S6按键
else if(P31 == 0)
{
Delay_five_ms_Key();
if(P31 == 0)
{
Check_naozhong = 0;
if(Mode_shizhong != 1)
{
Mode_naozhong = 1;
Key_S4_S5_S6_Flag = 6;
}
}
while(!P31);
}
//S5按键
else if(P32 == 0)
{
Delay_five_ms_Key();
if(P32 == 0)
{
Key_S4_S5_S7_Flag = 5;
Key_S4_S5_S6_Flag = 5;
Check_naozhong = 0;
}
while(!P32);
}
//S4按键
else if(P33 == 0)
{
Delay_five_ms_Key();
if(P33 == 0)
{
Key_S4_S5_S7_Flag = 4;
Key_S4_S5_S6_Flag = 4;
Check_naozhong = 0;
}
if(Mode_shizhong == 0)
{
while(!P33)
{
uchar temp;
temp = Temper();
yi = 18; er = 18; san = 18; si = 18; wu = 18;
liu = temp / 10;
qi = temp % 10;
ba = 12;
SMG_Display();
}
}
else
{
while(!P33);
}
}
}
//配置HC138
void SelectHC138(uchar channel)
{
switch(channel)
{
case 4: //LED
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;
case 0: //锁住所有寄存器
P2 = (P2 & 0X1F) | 0X00;
break;
}
}
//系统初始化
void Initsys()
{
SelectHC138(5);
P0 = 0X00;//关闭蜂鸣器和继电器
SelectHC138(4);
P0 = 0XFF;//关闭LED
SelectHC138(6);
P0 = 0XFF; //选择所有数码管
SelectHC138(7);
P0 = 0XFF; //关闭所有数码管
}
//在pos位码上,显示value段码
void DisplaySMG_Bit(uchar pos, uchar value)
{
SelectHC138(6);
P0 = 0X01 << pos;
SelectHC138(7);
P0 = value;
}
//数码管8位码显示
void SMG_Display()
{
DisplaySMG_Bit(0, SMG_duanma[yi]);
Delay_one_ms_SMG();
DisplaySMG_Bit(1, SMG_duanma[er]);
Delay_one_ms_SMG();
DisplaySMG_Bit(2, SMG_duanma[san]);
Delay_one_ms_SMG();
DisplaySMG_Bit(3, SMG_duanma[si]);
Delay_one_ms_SMG();
DisplaySMG_Bit(4, SMG_duanma[wu]);
Delay_one_ms_SMG();
DisplaySMG_Bit(5, SMG_duanma[liu]);
Delay_one_ms_SMG();
DisplaySMG_Bit(6, SMG_duanma[qi]);
Delay_one_ms_SMG();
DisplaySMG_Bit(7, SMG_duanma[ba]);
Delay_one_ms_SMG();
}
//数码管的延时
void Delay_one_ms_SMG()
{
uint j;
for(j = 845; j > 0; j--);
}
//时间初始化
void Init_DS1302()
{
uchar i;
Write_Ds1302(0X81, 0X00);
for(i = 0; i < 7; i++)
{
Write_Ds1302(Write_DS1302_adrr[i], shijian[i]);
}
}
//读出时间
void Read_DS1302()
{
uchar i;
for(i = 0; i < 7; i++)
{
shijian[i] = Read_Ds1302(Read_DS1302_adrr[i]);
}
}
void Timer0Init(void) //5毫秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x00; //设置定时初值
TH0 = 0x28; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 1;
EA = 1;
}
void Timer0Server() interrupt 1
{
tt++;
t++;
if(tt == 200)
{
if(Mode_light == 0)Mode_light = 1;
else if(Mode_light == 1)Mode_light = 0;
miao++;
tt = 0;
}
if(miao == 5){Check_naozhong = 0;}
if(Check_naozhong == 1 && t == 40)
{
SelectHC138(4);
if(Mode_L1_light == 0){Mode_L1_light = 1;P0 = 0XFF;}
else if(Mode_L1_light == 1){Mode_L1_light = 0;P0 = 0XFE;}
t = 0;
}
}
2.2 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_Byte(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK=0;
SDA=temp&0x01;
temp>>=1;
SCK=1;
}
}
void Write_Ds1302( unsigned char address,unsigned char dat )
{
RST=0;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
SCK=0;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
RST=1;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
Write_Ds1302_Byte(address);
Write_Ds1302_Byte(dat);
RST=0;
}
unsigned char Read_Ds1302 ( unsigned char address )
{
unsigned char i,temp=0x00;
RST=0;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
SCK=0;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
RST=1;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
Write_Ds1302_Byte(address);
for (i=0;i<8;i++)
{
SCK=0;
temp>>=1;
if(SDA)
temp|=0x80;
SCK=1;
}
RST=0;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
RST=0;
SCK=0;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
SCK=1;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
SDA=0;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
SDA=1;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
return (temp);
}
2.3 onewire.c
/*
程序说明: 单总线驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台(外部晶振12MHz) STC89C52RC单片机
日 期: 2011-8-9
*/
#include "reg52.h"
sbit DQ = P1^4; //单总线接口
//单总线延时函数
void Delay_OneWire(unsigned int t) //STC89C52RC
{
while(t--);
}
//通过单总线向DS18B20写一个字节
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(50);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(50);
}
//从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(50);
}
return dat;
}
//DS18B20设备初始化
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(120);
DQ = 0;
Delay_OneWire(800);
DQ = 1;
Delay_OneWire(100);
initflag = DQ;
Delay_OneWire(50);
return initflag;
}
unsigned char Temper()
{
unsigned char di8,gao8,temp;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay_OneWire(200);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
di8=Read_DS18B20();
gao8=Read_DS18B20();
temp=gao8<<4;
temp=temp|(di8>>4);
return temp;
}
附
对于DS1302写入时间时,怎么可以输入61产生0X61的效果
因为这样修改DS1302的初始化时,就很好重置了
解决:
(dat/10<<4)|(dat%10)
dat如果是61,那么经过上式就变成0X61了
之后,我在写时,又决定改成16进制了,如果改变6的话,其实加上16就行了