功能介绍:
0.本系统采用STC89C52作为单片机
1.系统实时显示当前的日期/时间/温度
2.系统支持三个闹钟设定
3.系统具备0点提醒功能
4.系统支持设定温度阈值,超过温度阈值,蜂鸣器报警
5.采用DC002作为电源接口可直接输入5V给整个系统供电
主程序:
#include //调用单片机头文件
#include "ds1302.h"
#include "lcd1602.h"
#define uchar unsigned char //无符号字符型 宏定义 变量范围0~255
#define uint unsigned int //无符号整型 宏定义 变量范围0~65535
sbit DQ = P1 ^ 0; // 18b20 IO口的定义
bit flag_200ms;
bit flag_100ms;
sbit beep = P2 ^ 0; //蜂鸣器定义
uchar menu_1, menu_2;
unsigned char rev = 2;
uchar Count;
bit flag_moshi;
bit flag_alarm, flag_alarm_control;
uint temperature; //温度变量
bit open1 = 1, open2 = 1, open3 = 1, open4 = 1, open5 = 1, open6 = 1;
uchar fen1 = 0x0, shi1 = 0x08; //第1个开关的时间
uchar fen2 = 0x1, shi2 = 0x08; //第2个开关的时间
uchar fen3 = 0x0, shi3 = 0x10; //第3个开关的时间
uchar fen4 = 0x1, shi4 = 0x10; //第4个开关的时间
uchar fen5 = 0x0, shi5 = 0x12; //第5个开关的时间
uchar fen6 = 0x1, shi6 = 0x12; //第6个开关的时间
uint t_high = 400; //温度上限
/******************1ms 延时函数*******************/
void delay_1ms(uint q)
{
uint i, j;
for (i = 0; i < q; i++)
for (j = 0; j < 120; j++)
;
}
/*************时间温度显示程序***************/
void init_1602_ds1302()
{
write_sfm2_ds1302(1, 1, shi); //显示时
write_sfm2_ds1302(1, 4, fen); //显示分
write_sfm2_ds1302(1, 7, miao); //显示秒
write_sfm3_18B20(1, 11, temperature); //显示温度
write_sfm2_ds1302(2, 2, nian); //显示年
write_sfm2_ds1302(2, 5, yue); //显示月
write_sfm2_ds1302(2, 8, ri); //显示日
write_sfm1(2, 14, week);
}
/*************定时器0初始化程序***************/
void init_time0()
{
EA = 1; //开总中断
TMOD = 0X01; //定时器0、工作方式1
ET0 = 1; //开定时器0中断
TR0 = 1; //允许定时器0定时
}
/*************开关函数***************/
void menu_dis()
{
if (((fen == fen1) && (shi == shi1) && (open1 == 1)) ||
((fen == fen3) && (shi == shi3) && (open3 == 1)) ||
((fen == fen5) && (shi == shi5) && (open5 == 1)))
{ //打开开关
flag_alarm_control = 1;
if (flag_alarm == 0)
beep = 0;
}
if (((fen == fen2) && (shi == shi2) && (open2 == 1)) ||
((fen == fen4) && (shi == shi4) && (open4 == 1)) ||
((fen == fen6) && (shi == shi6) && (open6 == 1)))
{ //关闭开关
flag_alarm = 0;
flag_alarm_control = 0;
beep = 1;
}
if ((shi == 0) && (fen == 0) && (miao == 0))
{
beep = 0;
delay_1ms(300);
beep = 1;
}
}
/********************独立按键程序*****************/
uchar key_can; //按键值
void key() //独立按键程序
{
static uchar key_new;
key_can = 0; //按键值还原
P3 |= 0x78; //对应的按键IO口输出为1
if ((P3 & 0xF0) != 0xF0) //按键按下
{
delay_1ms(1); //按键消抖动
if (((P3 & 0xF0) != 0xF0) && (key_new == 1))
{ //确认是按键按下
key_new = 0;
switch (P3 & 0xF0)
{
case 0x70:
key_can = 2;
break; //得到按键值//选择键
case 0xB0:
key_can = 3;
break; //得到按键值//加
case 0xD0:
key_can = 1;
break; //得到按键值//设置键
case 0xE0:
key_can = 4;
break; //得到按键值//减
}
}
}
else
key_new = 1;
}
/**********************设置函数************************/
void key_with()
{
if ((menu_1 == 0) && (flag_alarm_control == 1)) //正常状态,取消闹钟报警
{
if ((key_can == 2) || (key_can == 3) || (key_can == 4))
{
flag_alarm = 1;
beep = 1;
}
}
if (key_can == 1) //设置键
{
menu_1++;
if (menu_1 == 1) //设置时间
{
menu_2 = 1;
write_string(1, 0, " : : W: ");
write_string(2, 0, " 20 - - ");
}
menu_2 = 1;
if (menu_1 > 5) //回到正常显示
{
menu_1 = 0;
write_guanbiao(1, 2, 0); //关闭光标
init_1602_dis_csf(); //初始化液晶显示
}
}
if (key_can == 2) //选择键
{
flag_200ms = 1;
if (menu_1 == 1) //设置时间
{
menu_2++;
if (menu_2 > 7)
menu_2 = 1;
}
if (menu_1 != 1) //设置
{
menu_2++;
if (menu_2 > 6)
menu_2 = 1;
}
}
if (menu_1 == 1)
{
if (menu_2 == 1) //设置时
{
if (key_can == 3) //加
set_ds1302time(1, &shi, 0x24);
if (key_can == 4) //减
set_ds1302time(0, &shi, 0x24);
}
if (menu_2 == 2) //设置分
{
if (key_can == 3) //加
set_ds1302time(1, &fen, 0x60);
if (key_can == 4) //减
set_ds1302time(0, &fen, 0x60);
}
if (menu_2 == 3) //设置秒
{
if (key_can == 3) //加
set_ds1302time(1, &miao, 0x60);
if (key_can == 4) //减
set_ds1302time(0, &miao, 0x60);
}
if (menu_2 == 4) //设置星期
{
if (key_can == 3) //加
set_ds1302week(1, &week, 8);
if (key_can == 4) //减
set_ds1302week(0, &week, 8);
}
if (menu_2 == 5) //设置年
{
if (key_can == 3) //加
set_ds1302time(1, &nian, 0x9A);
if (key_can == 4) //减
set_ds1302time(0, &nian, 0x9A);
}
if (menu_2 == 6) //设置月
{
if (key_can == 3) //加
set_ds1302time(1, &yue, 0x13);
if (key_can == 4) //减
set_ds1302time(0, &yue, 0x13);
}
if (menu_2 == 7) //设置日
{
if (key_can == 3) //加
set_ds1302time(1, &ri, 0x32);
if (key_can == 4) //减
set_ds1302time(0, &ri, 0x32);
}
write_sfm2_ds1302(1, 2, shi); //显示时
write_sfm2_ds1302(1, 5, fen); //显示分
write_sfm2_ds1302(1, 8, miao); //显示秒
write_sfm1(1, 14, week); //显示星期
write_sfm2_ds1302(2, 3, nian); //显示年
write_sfm2_ds1302(2, 6, yue); //显示月
write_sfm2_ds1302(2, 9, ri); //显示日
switch (menu_2) // 光标显示
{
case 1:
write_guanbiao(1, 2, 1);
break;
case 2:
write_guanbiao(1, 5, 1);
break;
case 3:
write_guanbiao(1, 8, 1);
break;
case 4:
write_guanbiao(1, 14, 1);
break;
case 5:
write_guanbiao(2, 3, 1);
break;
case 6:
write_guanbiao(2, 6, 1);
break;
case 7:
write_guanbiao(2, 9, 1);
break;
}
write_time(); //把时间写进去
}
仿真演示视频:
https://www.bilibili.com/video/BV1Km4y1P7ZF/
实物演示视频:
https://www.bilibili.com/video/BV1be411L7Y1/