目录
1.模块接口
1.138译码器
2. LED模块接口
3.蜂鸣器接口
4.数码管接口
2.使用的模块
1)蜂鸣器模块。
2)LED模块。
3)DS1302。
4)独立按键模块。
5)数码管模块。
2.蜂鸣器模块功能
1)负责闹钟时间达到时候发生警报,一分钟后主动停止警报。
2)开始调试闹钟时间的时候产生声音,两秒后停下。
3.LED模块功能
1)处于没有闹钟界面为LED1亮,其他熄灭。
2)处于调试闹钟界面LED2亮,其他熄灭。
3)处于有闹钟界面LED3亮,其他熄灭。
4.DS1302模块功能
1)负责时间采集。
5.独立按键功能
1.处于时钟界面
1)S4按下小时+1。
2)S5按下分钟+1。
3)S6按下秒钟清零。
4)S7按下转化为闹钟调试界面,按下两次就取消闹钟。
2.处于闹钟调节界面
1)S4按下小时+1。
2)S5按下分钟+1。
3)S6按下开始有闹钟,显示时钟。
3)S7按下返回显示时钟。
6.数码管模块功能。
1)时钟界面显示该时间。
2)闹钟调试界面显示闹钟的时间。
7.初始化
1)默认没有开启闹钟。
2)数码管显示时钟界面。
3)闹钟默认值为08:00:00。
4)初始化时间为08:00:00。
8.代码
1.mian
#include <STC15F2K60S2.H>
#include "ds1302.h"
#include "smg_system.h"
#include "P0_device.h"
#include "Delay.h"
void Timer2Init(void) //1毫秒@12.000MHz
{
AUXR &= 0xFB; //定时器时钟12T模式
T2L = 0x18; //设置定时初始值
T2H = 0xFC; //设置定时初始值
AUXR |= 0x10; //定时器2开始计时
EA=1; //打开总中断
IE2=0x04; // 打定时器2中断开
}
int hour=0,min=0,s=0;//接收时间的变量
void system()//初始化条件
{
P0_Device(0xa0,0);//关闭继电器与蜂鸣器
P0_Device(0x80,0xff);//关闭灯
}
void read_ds1302() //接收数据
{
hour= BCDToDec(Read_Ds1302_Byte(0x85));//接收小时数据
min= BCDToDec(Read_Ds1302_Byte(0x83));//接收分钟数据
s= BCDToDec(Read_Ds1302_Byte(0x81));//接收秒钟数据
}
void ds1302_show()//显示实时时钟
{
smg_system(0,hour/10);Delay(1);//显示小时十位24
smg_system(1,hour%10);Delay(1);//显示小时个位
smg_system(2,12);Delay(1);
smg_system(3,min/10);Delay(1);//显示分钟十位
smg_system(4,min%10);Delay(1);//显示分钟个位
smg_system(5,12);Delay(1);
smg_system(6,s/10);Delay(1);//显示秒钟十位
smg_system(7,s%10); Delay(1);//显示秒钟个位
}
int b=0;//报警变量
int h=8,m=0;//报警条件变量
int a=0;//显示页面参数变量
void bjq()//报警功能
{
if((hour==h)&&(min==m)&&(b==1))//报警条件
{P0_Device(0xa0,0x40);}
if((hour==h)&&(min==(m+1))&&(b==1))//取消报警
{P0_Device(0xa0,0);b=0;}
//LED显示条件
if(b==1)
{P0_Device(0x80,~0x04);}
if((a==0)&&(b==0))
{P0_Device(0x80,~0x01);}
if((a==1)&&(b==0))
{P0_Device(0x80,~0x02);}
}
void nz_show()//闹钟调试界面
{
smg_system(0,h/10);Delay(1);//显示小时十位24
smg_system(1,h%10);Delay(1);//显示小时个位
smg_system(2,12);
smg_system(3,m/10);Delay(1);//显示分钟十位
smg_system(4,m%10);Delay(1);//显示分钟个位
smg_system(5,12);
smg_system(6,s/10);Delay(1);//显示秒钟十位
smg_system(7,s%10);Delay(1); //显示秒钟个位
}
void read_kby()//按键功能
{
if(P30==0)//s7
{//消抖 判断下一个
Delay(1);if(P30==0){a++;a=a%2;while(P30==0){}}//控制小时加
}
}
void main()
{
Timer2Init();
system();
vClock_Set(8,00,00); //初始化
while(1)
{
if(a==1)//显示调试界面条件
{
nz_show();
//按键功能区
if(P33==0)//按下S4
{//消抖 判断下一个 按下一次加一 限制范围 存入该数据 让数码管闪说明在调整
Delay(1);if(P33==0){h++;if(h>23){hour=0;}while(P33==0){}}//控制小时加
}
if(P32==0)//按下s5
{//消抖 判断下一个 按下一次加一 限制范围 存入该数据 让数码管闪说明在调整
Delay(1);if(P32==0){m++;if(m>59){min=0;}while(P32==0){}}//控制分钟加
}
if(P31==0)//按下s6
{//消抖 判断下一个
Delay(1);if(P31==0){b++;b=b%2;a++;a=a%2;while(P31==0){}}
}
}
if(a==0)//显示实时时钟的功能条件
{
ds1302_show();
//按键功能区
if(P33==0)//按下S4
{//消抖 判断下一个 按下一次加一 限制范围 存入该数据 让数码管闪说明在调整
Delay(1);if(P33==0){hour++;if(hour>23){hour=0;}vClock_Set(hour,min,s);while(P33==0){}}//控制小时加
}
if(P32==0)//按下s5
{//消抖 判断下一个 按下一次加一 限制范围 存入该数据 让数码管闪说明在调整
Delay(1);if(P32==0){min++;if(min>59){min=0;}vClock_Set(hour,min,s);while(P32==0){}}//控制分钟加
}
if(P31==0)//按下s6
{//消抖 判断下一个 按下 s=0 存入该数据 让数码管闪说明在调整
Delay(1);if(P31==0){s=0;vClock_Set(hour,min,s);while(P31==0){}}//控制分钟减
}
}
}
}
void t2() interrupt 12 //中断入口
{
read_kby();
bjq();
read_ds1302();
}
2.ds1302 c
#include <intrins.h>
#include "ds1302.h"
#include <STC15F2K60S2.H>
sbit SCK=P1^7;
sbit IO=P2^3;
sbit CE = P1^3; // DS1302复位
void Write_Ds1302(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK=0;
IO=temp&0x01;
temp>>=1;
SCK=1;
}
}
void Write_Ds1302_Byte( unsigned char address,unsigned char dat )
{
CE=0; _nop_();
SCK=0; _nop_();
CE=1; _nop_();
Write_Ds1302(address);
Write_Ds1302(dat);
CE=0;
}
unsigned char Read_Ds1302_Byte ( unsigned char address )
{
unsigned char i,temp=0x00;
CE=0; _nop_();
SCK=0; _nop_();
CE=1; _nop_();
Write_Ds1302(address);
for (i=0;i<8;i++)
{
SCK=0;
temp>>=1;
if(IO)
temp|=0x80;
SCK=1;
}
CE=0; _nop_();
SCK=0; _nop_();
SCK=1; _nop_();
IO=0; _nop_();
IO=1; _nop_();
return (temp);
}
void vClock_Set(unsigned char hour,unsigned char minute,unsigned char second)
{
Write_Ds1302_Byte(0x8e,0x00); //关闭写保护
Write_Ds1302_Byte(0x80,DecToBCD(second)); //写入小时数据到对应的地址
Write_Ds1302_Byte(0x82,DecToBCD(minute)); //写入分钟数据到对应的地址
Write_Ds1302_Byte(0x84,DecToBCD(hour)); //写入秒钟数据到对应的地址
Write_Ds1302_Byte(0x8e,0x80); //打开写保护
}
ds302.h
#ifndef __DS1302_H
#define __DS1302_H
#define DecToBCD(dec) (dec/10*16)+(dec%10)
#define BCDToDec(bcd) (bcd/16*10)+(bcd%16)
unsigned char Read_Ds1302_Byte( unsigned char address );
void vClock_Set(unsigned char hour,unsigned char minute,unsigned char second);
#endif
3.Delay c
#include "Delay.h"
void Delay(unsigned int xms)
{
unsigned char i, j;
while(xms--)
{
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
}
delay h
#ifndef _Delay_H_
#define _Delay_H_
void Delay(unsigned int xms);
#endif
4.P0_Device
#include "P0_device.h"
/**
* @brief p2=0xa0为蜂鸣器和继电器 ,P2=0x80为LED;数码管的为0XE0和0xc0;
* @param
* @retval
* @author
*/
void P0_Device(unsigned char p2,unsigned char p0)
{
P0=p0; //保持恒定的输入值
P2=p2;//打开锁存器
P2=0;//关闭锁存器
}
P0_Device h
#ifndef __P0_DEVICE_H_
#define __P0_DEVICE_H_
#include <STC15F2K60S2.H>
void P0_Device(unsigned char p2,unsigned char p0);
#endif
5.smg
#include <STC15F2K60S2.H>
//这是共阴极接法,蓝桥杯的为共阳极所以用的时候要取反
unsigned char Nixie[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x80,0x39,0x40}; //显示数字0123456789顺序10为.11为C12为-
void smg_system(unsigned n,unsigned m)
{
static int a;
a=0x01 << n;
P0_Device(0xc0,0);
P0_Device(0xe0,~Nixie[m]);//显示的数字
P0_Device(0xc0,a); //显示的位置为1+n
}
smg
#ifndef __SMG_SYSTEM_H_
#define __SMG_SYSTEM_H_
#include <STC15F2K60S2.H>
#include "P0_device.h"
void smg_system(unsigned n,unsigned m);
#endif