一、题目要求

二、代码操作
2.1 main.c
#include <STC15F2K60S2.h>
#include <intrins.h>
#include "ds1302.h"
sbit DQ=P1^4;
#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};
uchar code SMG_Dot_AC[10] =
{0X40,0X79,0X24,0X30,0X19,0X12,0X02,0X78,0X00,0X10};
uchar Write_DS1302_adrr[7] = {0X80, 0X82, 0X84, 0X86, 0X88, 0X8A, 0X8C};
uchar Read_DS1302_adrr[7] = {0X81, 0X83, 0X85, 0X87, 0X89, 0X8B, 0X8D};
uchar shijian[7] = {0X50, 0X59, 0X23, 0X18, 0X04, 0X06, 0X20};
uchar Flashing = -1;
bit Save_Temp_FULL = -1;
uchar L1_Check = 1;
bit Check_T_s = 0;
uchar count = 0;
uchar Save_Temp[10] = {0};
uchar T_5ms = 0;
uchar T_Interval_i = 0;
uchar T_Interval_s[4]= {1, 5, 30, 60};
uchar yi=18;
uchar er=18;
uchar san=18;
uchar si=18;
uchar wu=18;
uchar liu=18;
uchar qi=18;
uchar ba=18;
void Initsys();
void SelectHC138(uchar channel);
void DisplaySMG_Bit(uchar pos, uchar value);
void SMG_Display();
void Delay_one_ms_SMG();
void Delay_five_ms_Key();
void Init_DS1302();
void Read_DS1302();
uchar temget();
void Delay500us();
void Delay100us();
void dsinit();
void write(uchar dat);
uchar read();
void Delays(uint b);
void Timer0Init(void);
uchar Setting_Alone_Key();
void Setting_Display_Mode();
void Chock_Display_Mode();
void GetTemp_Display_Mode();
uchar GetTemp_Alone_Key();
void main()
{
Init_DS1302();
Initsys();
Timer0Init();
while(1)
{
yi = 18;
er = 18;
san = 18;
EA = 0;
Setting_Display_Mode();
Chock_Display_Mode();
GetTemp_Display_Mode();
}
}
void GetTemp_Display_Mode()
{
uchar temp = 0;
while(1)
{
uchar i;
yi = 16;er = 0;
san = 0;si = 18;
wu = 18; liu = 16;
qi = Save_Temp[0] / 10;
ba = Save_Temp[0] % 10;
SMG_Display();
temp = GetTemp_Alone_Key();
if(temp == 1)
{
L1_Check = -1;
Flashing = -1;
SelectHC138(4);
P0 = 0XFF;
for(i = 0; i < 10; i++)
{
san = i;
qi = Save_Temp[i] / 10;
ba = Save_Temp[i] % 10;
SMG_Display();
Delays(200);
}
temp = GetTemp_Alone_Key();
while(1)
{
SMG_Display();
temp = GetTemp_Alone_Key();
if(temp == 2)
{
break;
}
}
if(temp == 2)
{
break;
}
}
}
}
void Delays(uint b)
{
while(b--)
{
SMG_Display();
}
}
uchar GetTemp_Alone_Key()
{
if(P31 == 0)
{
Delay_five_ms_Key();
if(P31 == 0)
{
return 1;
}
while(!P31)
{
SMG_Display();
}
}
else if(P30 == 0)
{
Delay_five_ms_Key();
if(P30 == 0)
{
return 2;
}
while(!P30);
}
return 3;
}
void Chock_Display_Mode()
{
EA = 1;
Flashing = 0;
Init_DS1302();
while(1)
{
if(Check_T_s == 1)
{
Save_Temp[count] = temget();
count++;
Check_T_s = 0;
if(count == 10)
{
count = 0;
Save_Temp_FULL = 1;
break;
}
}
Read_DS1302();
yi = shijian[2] / 16; er = shijian[2] % 16;
si = shijian[1] / 16; wu = shijian[1] % 16;
qi = shijian[0] / 16; ba = shijian[0] % 16;
SMG_Display();
}
}
void Setting_Display_Mode()
{
uchar check = -2;
Save_Temp_FULL = 0;
T_Interval_i = 0;
while(1)
{
liu = 16;
check = Setting_Alone_Key();
qi = T_Interval_s[T_Interval_i] / 10;
ba = T_Interval_s[T_Interval_i] % 10;
if(check == 0)
{
break;
}
SMG_Display();
}
liu = 18;
}
uchar Setting_Alone_Key()
{
uchar i = -1;
if(P32 == 0)
{
Delay_five_ms_Key();
if(P32 == 0)
{
while(1)
{
return 0;
}
}
while(!P32)
{
SMG_Display();
}
}
else if(P33 == 0)
{
Delay_five_ms_Key();
if(P33 == 0)
{
T_Interval_i++;
if(T_Interval_i == 4)
{
T_Interval_i = 0;
}
i = 1;
}
while(!P33)
{
SMG_Display();
}
}
return i;
}
void Delay_five_ms_Key()
{
uint i,j;
for(i = 0; i < 5; i++)
for(j = 845; j > 0; j--);
}
void SelectHC138(uchar channel)
{
switch(channel)
{
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;
case 0:
P2 = (P2 & 0X1F) | 0X00;
break;
}
}
void Initsys()
{
SelectHC138(5);
P0 = 0X00;
SelectHC138(4);
P0 = 0XFF;
SelectHC138(6);
P0 = 0XFF;
SelectHC138(7);
P0 = 0XFF;
}
void DisplaySMG_Bit(uchar pos, uchar value)
{
SelectHC138(6);
P0 = 0X01 << pos;
SelectHC138(7);
P0 = value;
}
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 Timer0Init(void)
{
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0x00;
TH0 = 0x28;
TF0 = 0;
TR0 = 1;
ET0 = 1;
EA = 1;
}
void ServiceTimer0() interrupt 1 using 1
{
static uchar T_s = 0;
T_5ms++;
if(T_5ms == 200)
{
if((T_s == T_Interval_s[T_Interval_i]) && (Save_Temp_FULL == 0))
{
Check_T_s = 1;
T_s = 0;
}
if(Flashing == 0)
{
Flashing = 1;
san = 18;
liu = 18;
if(Save_Temp_FULL == 1 && L1_Check == 1)
{
P0 = 0XFF;
SelectHC138(4);
P00 = 0;
}
}
else if(Flashing == 1)
{
Flashing = 0;
san = 16;
liu = 16;
if(Save_Temp_FULL == 1 && L1_Check == 1)
{
P0 = 0XFF;
SelectHC138(4);
P00 = 1;
}
}
T_5ms = 0;
T_s++;
}
}
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]);
}
}
uchar temget()
{
unsigned char di8,gao8,temp;
dsinit();
write(0xcc);
write(0x44);
Delay500us();
Delay500us();
dsinit();
write(0xcc);
write(0xbe);
di8=read();
gao8=read();
temp=gao8<<4;
temp=temp|(di8>>4);
return temp;
}
uchar read()
{
uchar i;
uchar dat;
for(i=0;i<8;i++)
{
DQ=0;
_nop_();
dat>>=1;
DQ=1;
if(DQ==1)
{
dat|=0x80;
}
Delay100us();
}
return dat;
}
void write(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{DQ=0;
DQ=dat&0x01;
Delay100us();
DQ=1;
dat>>=1;
}}
void dsinit()
{
DQ=0;
Delay500us();
DQ=1;
Delay500us();
}
void Delay100us()
{
unsigned char i, j;
_nop_();
_nop_();
i = 2;
j = 15;
do
{
while (--j);
} while (--i);
}
void Delay500us()
{
unsigned char i,j;
_nop_();
_nop_();
i=6;
j=93;
do
{
while(j--);
}while(i--);
}
2.2 ds1302.c
#include <reg52.h>
#include <intrins.h>
sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST = P1^3;
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_();
SCK=0;
_nop_();
RST=1;
_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_();
SCK=0;
_nop_();
RST=1;
_nop_();
Write_Ds1302_Byte(address);
for (i=0;i<8;i++)
{
SCK=0;
temp>>=1;
if(SDA)
temp|=0x80;
SCK=1;
}
RST=0;
_nop_();
RST=0;
SCK=0;
_nop_();
SCK=1;
_nop_();
SDA=0;
_nop_();
SDA=1;
_nop_();
return (temp);
}
三、一些心得
- bit型慎用
- 按键最好return出来,出来后再做处理
- 大程序,在最后写延时时,写短一点,因为可能还有其他程序在运行,拖慢时间
- DS1302用16进制写入,16进制读出,转换时用/16和%16
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理