制作出来的实物图如下:
DS3231时钟仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
/****************************************************************************************************************
2021年12月4日
共阳一芯数码管,1.2寸白色和和1寸翠绿色,制作的时钟和倒计时日期。
型号:21-12-1
****************************************************************************************************************/
#include "STC15F2K.h"
#include "I2C.h"
#include <intrins.h>
#include <hc595.h>
#include "epprom1.h"
#include "count_hanshu.h"
#define uchar unsigned char
#define uint unsigned int
/*************
epprom 读取的变量
******************/
uchar B1,B2,B3,B4,key_nmb;
uint jishu,DJS,chuzhi=0x0000,fanhui;
sbit led=P1^3;
sbit Kadd =P3^4;
sbit Kfun=P3^5;
//sbit Ksub=P1^6;
sbit shuangdian=P3^7;
uint day_count(uchar Years,uchar Months,uchar Days);
//--定义全局变量--//
uchar fen1,fen10,shi1,shi10,ri1,ri10,yue1,yue10,zhou,jishi,miao1,miao10,djs100,djs10,djs1;
unsigned char code DIG_CODE[19]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x54,0x6e,0x50};
//0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,n,y,r
uchar ridian,yuedian;
uchar nnn[7]={0x00,0x01,0x02,0x03,0x04,0x05,0x06};
uchar aa[7];
uchar low[7]={0,0,1,1,1,1,21};
uchar hi[7]={59,59,24,7,31,12,30};
void chushihua();
uchar read_time,shanshuo,KKEEYY,xish;
uchar read_byte(uchar random_addr);
void anjian();
void TIME_read()
{
uchar djsks;
//TIME(key_nmb)=0;
// if(key_nmb!=0)Ds1302ReadTime();
if(read_time==1){read_time=0;
aa[0]=BCD2HEX( read_byte(0x00));
aa[1]=BCD2HEX( read_byte(0x01));
aa[2]=BCD2HEX( read_byte(0x02));
aa[3]=BCD2HEX( read_byte(0x03));
aa[4]=BCD2HEX( read_byte(0x04));
aa[5]=BCD2HEX( read_byte(0x05));
aa[6]=BCD2HEX( read_byte(0x06));
if(djsks!=aa[4]){DJS= day_count(B1,B2,B3);djsks=aa[4];}
miao1 =DIG_CODE[aa[0]%10];
miao10 =DIG_CODE[aa[0]/10];
fen1 =DIG_CODE[aa[1]%10];
fen10 =DIG_CODE[aa[1]/10];
shi1 =DIG_CODE[aa[2]%10];
shi10 =DIG_CODE[aa[2]/10];
zhou =DIG_CODE[aa[3]%10];
ri1 =DIG_CODE[(aa[4]%10)]|ridian;
ri10 =DIG_CODE[aa[4]/10];
yue1 =DIG_CODE[(aa[5]%10)]|yuedian;
yue10 =DIG_CODE[aa[5]/10];
djs1 =DIG_CODE[DJS%10];
djs10 =DIG_CODE[DJS/10%10];
djs100 =DIG_CODE[DJS/100%10];
//miao1 =DIG_CODE[TIME[0]%16];
//miao10 =DIG_CODE[TIME[0]/16];
//fen1 =DIG_CODE[TIME[1]%16];
//fen10 =DIG_CODE[TIME[1]/16];
//shi1 =DIG_CODE[TIME[2]%16];
//shi10 =DIG_CODE[TIME[2]/16];
//ri1 =DIG_CODE[TIME[3]%16]|ridian;
//ri10 =DIG_CODE[TIME[3]/16];
//yue1 =DIG_CODE[TIME[4]%16]|yuedian;
//yue10 =DIG_CODE[TIME[4]/16];
//zhou =DIG_CODE[TIME[5]%16];
//djs1 =DIG_CODE[DJS%10];
//djs10 =DIG_CODE[DJS/10%10];
//djs100 =DIG_CODE[DJS/100%10];
if(key_nmb==6)
{
shi1 =DIG_CODE[aa[6]%10];
shi10 =DIG_CODE[aa[6]/10];
}
if(key_nmb==7)
{
djs100 =DIG_CODE[16];
djs1 =DIG_CODE[B1%10];
djs10 =DIG_CODE[B1/10%10];
}
if(key_nmb==8)
{
djs100 =DIG_CODE[17];
djs1 =DIG_CODE[B2%10];
djs10 =DIG_CODE[B2/10%10];
}
if(key_nmb==9)
{
djs100 =DIG_CODE[18];
djs1 =DIG_CODE[B3%10];
djs10 =DIG_CODE[B3/10%10];
}
}
if(xish)
{
if (key_nmb==0) {miao1=0x00;miao10=0x00;}
if (key_nmb==1) {fen1=0x00;fen10=0x00;}
if (key_nmb==2) {shi1=0x00;shi10=0x00;}
if (key_nmb==4) {ri1=0x00;ri10=0x00;}
if (key_nmb==5) {yue1=0x00;yue10=0x00;}
if (key_nmb==3) {zhou=0x00;}
if (key_nmb==6) {fen1=0x00;fen10=0x00;miao1=0x00;miao10=0x00;ri1=0x00;ri10=0x00;yue1=0x00;yue10=0x00;zhou=0x00;}
if (key_nmb>6 && key_nmb<10){djs1=0x00;djs10=0x00;}
}
if(led){miao1=miao10=fen1=fen10=shi1=shi10=ri1=ri10=yue1=yue10=zhou=djs100=djs10=djs1=0x00;}
}
/****************************************************************************************************************
* 函 数 名 : key_sm
* 函数功能 : 扫描三个按键
****************************************************************************************************************/
void key_sm()
{
uchar temp;
if (KKEEYY==3){KKEEYY=0;
key_nmb=key_nmb+1;
fanhui=0;
}
if (key_nmb>10) key_nmb=0;
if (KKEEYY==6){KKEEYY=0;
if(key_nmb<7) {
temp= aa[key_nmb]+1 ;
if(key_nmb==0) temp=0;
if (temp >hi[key_nmb])temp=low[key_nmb];
if (temp <low[key_nmb])temp=hi[key_nmb];
write_byte(nnn[key_nmb], HEX2BCD(temp));
read_time=1;
// bbb(miao1,miao10);
// HC595SendData(~djs100,~djs10,~djs1,~/*DIG_CODE[key_nmb]*/zhou, ~ri1,~ri10,~yue1,~yue10,~shi10,~shi1,~fen10,~fen1);
}
else
{
if (key_nmb==7) B1++;
if (key_nmb==8) B2++;
if (key_nmb==9) B3++;
if (B1>30)B1=21;
if (B2>12)B2=1;
if (B3>31)B3=1;
shanchu(chuzhi);
xieru(chuzhi+1,B1);
xieru(chuzhi+2,B2);
xieru(chuzhi+3,B3);
B1=duqu(chuzhi+1);
B2=duqu(chuzhi+2);
B3=duqu(chuzhi+3);
read_time=1;
}
DJS= day_count(B1,B2,B3);
fanhui=0;
}
if (KKEEYY>0){};
}
//按钮扫描 需在定时中断中调用
void anjian()
{
static uint JiShu=0;
static uint JiShu1=0;
static uchar keybuf = 0xff;
static uchar keybuf1 = 0xff;
uchar nm=28;
//
keybuf = (keybuf <<1) |Kfun;
if(keybuf == 0x00)
{ JiShu++; if(JiShu>=nm) {JiShu=0;KKEEYY=3/*下键*/;} }
else keybuf=0;
// keybuf1 = 0xff;
keybuf1 = (keybuf1 <<1) |Kadd;
if(keybuf1 == 0x00)
{ JiShu1++; if(JiShu1>=nm) {JiShu1=0;KKEEYY=6/*上键*/;} }
else keybuf1=0;
}
/****************************************************************************************************************
* 函 数 名 : main
* 函数功能 : 主函数
****************************************************************************************************************/
//uint BB;
void main()
{
key_nmb=10;
EA = 1; //开总中断
ConfigTimer0(5); //配置T0定时
write_byte(DS3231_CONTROL, 0x1C); //控制寄存器 DS3231_CONTROL 这两行写入main函数里
write_byte(DS3231_STATUS, 0x00); //状态寄存器 DS3231_STATUS
// write_byte(DS3231_SECOND,HEX2BCD(20)); //修改秒
// write_byte(DS3231_MINUTE,HEX2BCD(58)); //修改分
// write_byte(DS3231_HOUR,HEX2BCD(8)); //修改时
// write_byte(DS3231_WEEK,HEX2BCD(6)); //修改周
// write_byte(DS3231_DAY,HEX2BCD(18)); //修改日
// write_byte(DS3231_MONTH,HEX2BCD(12)); //修改月
// write_byte(DS3231_YEAR,HEX2BCD(21)); //修改
B1=duqu(chuzhi+1) ;
B2=duqu(chuzhi+2) ;
B3=duqu(chuzhi+3) ;
DJS= day_count(B1,B2,B3);
chushihua();
while(1)
{ if(fanhui==20){key_nmb=10;}
key_sm();
TIME_read();
bbb(miao1,miao10);
// if(!shanshuo) { //
HC595SendData(~djs100,~djs10,~djs1,~/*DIG_CODE[key_nmb]*/zhou, ~ri1,~ri10,~yue1,~yue10,~shi10,~shi1,~fen10,~fen1);
// }
}
}
//中断定义为5毫秒
void InterruptTimer0() interrupt 1
{
static uchar Di;
//非编辑模式进入
// if(BianJi==0)
// { anjian();
Di++;
shanshuo++ ;
if(shanshuo==30)shanshuo=0;
if(Di%100<90) {xish=0;}else {xish=1;}
if(Di==100)
{
read_time=0;
} //秒显示赋值 0.5秒
if(Di==200)
{
Di=0;
read_time=1; //一秒一次读DS3231时间,并写入数码管缓冲区的标记
if(!led)shuangdian=~shuangdian;
if(led) shuangdian=1;
yuedian=yuedian^0x80;
ridian=yuedian^0x80;
fanhui++;
}
// }
anjian(); //按键扫描
WDT_CONTR=0x37; //看门狗刷新 8.4S
}
Keil代码与Proteus仿真下载:
74hc595 共阳数码管20211204 型号:21-12-1 - 改装DS3231 - 副本.rar
(1.4 MB, 下载次数: 129)
|