基于普中科技的单片机开发实验仪编写的数字表
基于普中科技的单片机开发实验仪编写的数字表
1、项目简单说明
一、显示日期,对ds1302芯片进行计时所得的日期用数码管进行显示;
二、显示温度,对18B20芯片测量所得的温度用数码管进行显示;
三、进行计时,从零开始进行计时,用定时器中断对秒进行加一操作,数码管显示计时时间;
四、进行倒计时,运用矩阵键盘进行倒计时时间的设置,用定时器中断对秒进行减一操作,数码管显示倒计时时间,时间为零时蜂鸣器响起,响起后需按下相关按键结束;
五、对时间进行设置,运用矩阵键盘进行当前时间的设置,数码管显示设置时间,设置完成后ds1302芯片会从设置时间开始重新计时;
六、对日期进行设置,即对当前日期进行设置。
使用此数字表时,应先对日期进行设置,再对时间进行设置,这样才能得到当前的正确时间。
基本框图如下:
2、代码编写
main函数
#include<reg52.h>
#include"type.h"
#include"temperature.h"
#include"digital.h"
#include"count.h"
#include"ds1302.h"
#include"key.h"
#include"ring.h"
void main(){
init_ds1302(); //初始化ds1302
init_counter(); //初始化定时器1
digital_flag = close;
EA= 1;
while(1){
if(digital_flag == open){
function(); //功能显示
if(count_up_flag == close)
main_key_check(); //独立按键检测
}
if(ring_flag == open)
player(); //蜂鸣器唱歌
switch_check(); //数码管显示开关
}
}
1、type
type.h
#ifndef _TYPE_H
#define _TYPE_H
typedef unsigned char uchar;
typedef unsigned int uint;
#define open 1
#define close 0
void Delay1ms(uint x); //延时x ms
void delay(uint x); //延时
#endif
type.c
#include"type.h"
void Delay1ms(uint x){
uint i, j;
for(; x > 0; x--)
for(i = 199; i > 0; i--)
for(j = 1; j > 0; j--) ;
}
void delay(uint x){
uint i, j;
for(i = x; i > 0; --i)
for(j = 110; j > 0; --j) ;
}
2、temperature
temperature.h
#ifndef _TEMPERATURE_H
#define _TEMPERATURE_H
#include<reg52.h>
#include"type.h"
extern uint temperature;
sbit temp = P3^7; //数据发收位
void init_temp(); //初始化温感芯片
uchar read_temp(); //读取芯片数据
void write_temp(uchar temp_data); //对芯片写入命令
void temp2digital(); //将读取的数据转换为十进制
#endif
temperature.c
#include"temperature.h"
#include"digital.h"
uint temperature = 10;
void init_temp(){ //初始化芯片
temp = 0;
delay(1);
temp = 1;
show_temp();
}
uchar read_temp(){ //读取数据
uchar temp_byte, temp_bit;
uint i, j;
for(i = 0; i < 8; ++i){
temp = 0;
j++;
temp = 1;
j++; j++;
temp_bit = temp;
temp_byte = (temp_byte >> 1) | (temp_bit << 7);
j = 4;
while(j--);
}
return temp_byte;
}
void write_temp(uchar temp_data){ //写入数据
uint i, j;
for(i = 0; i < 8; ++i){
temp = 0;
j++;
temp = temp_data & 0x01;
j= 6;
while(j--);
temp = 1;
temp_data >>= 1;
}
}
void temp2digital(){ //将获取的温度转化为可用数码管显示的数据
uchar tempL, tempH;
uint i = 10;
init_temp();
delay(1);
write_temp(0xcc);
write_temp(0x44);
while(i--) show_temp();
init_temp();
delay(1);
write_temp(0xcc);
write_temp(0xbe);
tempL = read_temp();
tempH = read_temp();
//将温度的高8位与低8位组合乘以0.0625即为温度值,后面乘以100是为了保留两位小数位, 加上0.5四舍五入
temperature = ((tempH * 256) + tempL) * 0.0625 * 100 + 0.5;
}
3、digital
digital.h
#ifndef _DIGITAL_H
#define _DIGITAL_H
#include<reg52.h>
#include"type.h"
#define SMG7 P0
sbit LS_A = P2^2; //数码管位选
sbit LS_B = P2^3;
sbit LS_C = P2^4;
sbit digital_flag = P1^0; //数码管显示标志
extern uchar time_show[]; //时间显示数组
extern uchar temp_show[]; //温度显示数组
extern uchar time_temp; //时间暂存
extern bit flash_flag; //闪烁标志
void get_time_now(uint hour, uint min, uintsec); //获取当前时间
void get_time_count(uint hour, uint min,uint sec); //获取计时时间
void get_temp(uint temp); //获取显示温度
void flash(uint flash_bit); //选定位闪烁
void show_time(); //显示时间
void show_temp(); //显示温度
void show_func(uchar show); //显示功能序号
#endif
digital.c
#include"digital.h"
#include"count.h"
uchar digital_show[] = {0x3f, 0x06, 0x5b,0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x40, 0x00, 0x39};
// 0 1 2 3 4 5 6 7 8 9 - C
uchar time_show[] = {0, 0, 10, 0, 0, 10, 0,0}; //数码管显示时间数组
uchar temp_show[] = {11, 11, 11, 0, 0, 0,0, 12}; //数码管显示温度数组
bit flash_flag = open;
uchar time_temp = 0;
void get_time_now(uint hour, uint min, uintsec){ //获取现在的时间
time_show[0] = hour / 16;
time_show[1] = hour % 16;
time_show[3] = min / 16;
time_show[4] = min % 16;
time_show[6] = sec / 16;
time_show[7] = sec % 16;
}
void get_time_count(uint hour, uint min,uint sec){ //获取计时的时间
time_show[0] = hour / 10;
time_show[1] = hour % 10;
time_show[3] = min / 10;
time_show[4] = min % 10;
time_show[6] = sec / 10;
time_show[7] = sec % 10;
}
void get_temp(uint temp){ //获取温度
temp_show[3] = temp % 10000 / 1000;
temp_show[4] = temp % 1000 / 100;
temp_show[5] = temp % 100 / 10;
temp_show[6] = temp % 10;
}
void show_time(){ //显示时间
LS_A = 0; LS_B = 0; LS_C = 0;
SMG7 = digital_show[time_show[7]]; delay(1);
LS_A= 1; LS_B = 0; LS_C = 0;
SMG7 = digital_show[time_show[6]]; delay(1);
LS_A = 0; LS_B = 1; LS_C = 0;
SMG7 = digital_show[time_show[5]]; delay(1);
LS_A = 1; LS_B = 1; LS_C = 0;
SMG7 = digital_show[time_show[4]]; delay(1);
LS_A = 0; LS_B = 0; LS_C = 1;
SMG7 = digital_show[time_show[3]]; delay(1);
LS_A = 1; LS_B = 0; LS_C = 1;
SMG7 = digital_show[time_show[2]]; delay(1);
LS_A = 0; LS_B = 1; LS_C = 1;
SMG7 = digital_show[time_show[1]]; delay(1);
LS_A = 1; LS_B = 1; LS_C = 1;
SMG7 = digital_show[time_show[0]]; delay(1);
}
void show_temp(){ //显示温度
LS_A = 0; LS_B = 0; LS_C = 0;
SMG7 = digital_show[temp_show[7]]; delay(1);
LS_A = 1; LS_B = 0; LS_C = 0;
SMG7 = digital_show[temp_show[6]]; delay(1);
LS_A = 0; LS_B = 1; LS_C = 0;
SMG7 = digital_show[temp_show[5]]; delay(1);
LS_A = 1; LS_B = 1; LS_C = 0;
SMG7 = digital_show[temp_show[4]] | 0x80; delay(1);
LS_A = 0; LS_B = 0; LS_C = 1;
SMG7 = digital_show[temp_show[3]]; delay(1);
LS_A = 1; LS_B = 0; LS_C = 1;
SMG7 = digital_show[temp_show[2]]; delay(1);
LS_A = 0; LS_B = 1; LS_C = 1;
SMG7 = digital_show[temp_show[1]]; delay(1);
LS_A = 1; LS_B = 1; LS_C = 1;
SMG7 = digital_show[temp_show[0]]; delay(1);
}
void show_func(uchar show){ //显示功能序号
LS_A = 0; LS_B = 0; LS_C = 0;
SMG7 = digital_show[show]; delay(1);
}
void flash(uint flash_bit){ //选中位闪烁
if(flash_flag)
time_show[flash_bit] = 11;
else
time_show[flash_bit] = time_temp;
}
4、count
count.h
#ifndef _COUNT_H
#define _COUNT_H
#include<reg52.h>
#include"type.h"
extern uint hour_now; //当前时间
extern uint min_now;
extern uint sec_now;
extern uint year_now;
extern uint mon_now;
extern uint day_now;
extern uint hour_count; //计时时间
extern uint min_count;
extern uint sec_count;
extern bit count_up_flag; //计时标志
extern bit count_down_flag; //倒计时标志
extern bit reset_time_flag;
void init_time_now(); //初始化当前时间
void init_day_now(); //初始化当前日期
void init_time_count(); //初始计时时间
void set_time_now(uchar choose_bit, uchartype); //设置当前时间
void set_day_now(uchar choose_bit, uchartype); //设置日期
void set_time_count(uchar choose_bit, uchartype); //设置倒计时时间
void ten2bcd(bit flag); //将十进制的时间转换为BCD码
void count_up(); //计时
void count_down(); //倒计时
void init_counter(); //初始化化计时器1
#endif
count.c
#include"count.h"
#include"digital.h"
#include"ring.h"
#include"ds1302.h"
uint hour_now = 0; //当前时间
uint min_now = 0;
uint sec_now = 0;
uint year_now = 0;
uint mon_now = 0;
uint day_now = 0;
uint hour_count = 0; //计时时间
uint min_count = 0;
uint sec_count = 0;
uint t1 = 0;
uint t2 = 0;
bit count_up_flag = close; //计时标志
bit count_down_flag = close; //倒计时标志
bit reset_time_flag = close;
void init_time_now(){
hour_now = 0;
min_now = 0;
sec_now = 0;
}
void init_day_now(){ //初始化当前日期
year_now = 0;
mon_now = 0;
day_now = 0;
}
void init_time_count(){
hour_count = 0;
min_count = 0;
sec_count = 0;
}
void set_time_now(uchar choose_bit, uchartype){ //设置当前时间
if(type == '+')
switch(choose_bit){ //将数据的个位与十位分离进行操作
case 0 : hour_now = (hour_now / 10 + 1) % 10 * 10 + hour_now % 10;break;
case 1 : hour_now = (hour_now % 10 + 1) % 10 + hour_now / 10 * 10;break;
case 3 : min_now = (min_now / 10 + 1) % 6 * 10 + min_now % 10; break;
case 4 : min_now = (min_now % 10 + 1) % 10 + min_now / 10 * 10; break;
case 6 : sec_now = (sec_now / 10 + 1) % 6 * 10 + sec_now % 10; break;
case 7 : sec_now = (sec_now % 10 + 1) % 10 + sec_now / 10 * 10; break;
}
else if(type == '-'){
if(choose_bit == 0){
if(hour_now / 10 == 0)
hour_now += 90;
else
hour_now = (hour_now / 10 - 1) * 10 + hour_now % 10;
}
else if(choose_bit == 1){
if(hour_now % 10 == 0)
hour_now += 9;
else
hour_now = (hour_now % 10 - 1) + hour_now / 10 * 10;
}
else if(choose_bit == 3){
if(min_count / 10 == 0)
min_now += 50;
else
min_now = (min_now / 10 - 1) * 10 + min_now % 10;
}
else if(choose_bit == 4){
if(min_now % 10 == 0)
min_now += 9;
else
min_now = (min_now % 10 - 1) + min_now / 10 * 10;
}
else if(choose_bit == 6){
if(sec_now / 10 == 0)
sec_now += 50;
else
sec_now = (sec_now / 10 - 1) * 10 + sec_now % 10;
}
else if(choose_bit == 7){
if(sec_now % 10 == 0)
sec_now += 9;
else
sec_now = (sec_now % 10 - 1) + sec_now / 10 * 10;
}
}
}
void set_day_now(uchar choose_bit, uchartype){ //设置日期
if(type == '+')
switch(choose_bit){
case 0 : year_now = (year_now / 10 + 1) % 10 * 10 + year_now % 10;break;
case 1 : year_now = (year_now % 10 + 1) % 10 + year_now / 10 * 10; break;
case 3 : mon_now = (mon_now / 10 + 1) % 2 * 10 + mon_now % 10; break;
case 4 : mon_now = (mon_now % 10 + 1) % 10 + mon_now / 10 * 10; break;
case 6 : day_now = (day_now / 10 + 1) % 4 * 10 + day_now % 10; break;
case 7 : day_now = (day_now % 10 + 1) % 10 + day_now / 10 * 10; break;
}
else if(type == '-'){
if(choose_bit == 0){
if(year_now / 10 == 0)
year_now += 90;
else
year_now = (year_now / 10 - 1) * 10 + year_now % 10;
}
else if(choose_bit == 1){
if(year_now % 10 == 0)
year_now += 9;
else
year_now = (year_now % 10 - 1) + year_now / 10 * 10;
}
else if(choose_bit == 3){
if(mon_now / 10 == 0)
mon_now += 10;
else
mon_now = (mon_now / 10 - 1) * 10 + mon_now % 10;
}
else if(choose_bit == 4){
if(mon_now % 10 == 0)
mon_now += 9;
else
mon_now = (mon_now % 10 - 1) + mon_now / 10 * 10;
}
else if(choose_bit == 6){
if(day_now / 10 == 0)
day_now += 30;
else
day_now = (day_now / 10 - 1) * 10 + day_now % 10;
}
else if(choose_bit == 7){
if(day_now % 10 == 0)
day_now += 9;
else
day_now = (day_now % 10 - 1) + day_now / 10 * 10;
}
}
}
void set_time_count(uchar choose_bit, uchartype){ //设置倒计时时间
if(type == '+')
switch(choose_bit){
case 0 : hour_count = (hour_count / 10 + 1) % 10 * 10 + hour_count % 10;break;
case 1 : hour_count = (hour_count % 10 + 1) % 10 + hour_count / 10 * 10;break;
case 3 : min_count = (min_count / 10 + 1) % 6 * 10 + min_count % 10;break;
case 4 : min_count = (min_count % 10 + 1) % 10 + min_count / 10 * 10;break;
case 6 : sec_count = (sec_count / 10 + 1) % 6 * 10 + sec_count % 10;break;
case 7 : sec_count = (sec_count % 10 + 1) % 10 + sec_count / 10 * 10;break;
}
else if(type == '-'){
if(choose_bit == 0){
if(hour_count / 10 == 0)
hour_count += 90;
else
hour_count = (hour_count / 10 - 1) * 10 + hour_count % 10;
}
else if(choose_bit == 1){
if(hour_count % 10 == 0)
hour_count += 9;
else
hour_count = (hour_count % 10 - 1) + hour_count / 10 * 10;
}
else if(choose_bit == 3){
if(min_count / 10 == 0)
min_count += 50;
else
min_count = (min_count / 10 - 1) * 10 + min_count % 10;
}
else if(choose_bit == 4){
if(min_count % 10 == 0)
min_count += 9;
else
min_count = (min_count % 10 - 1) + min_count / 10 * 10;
}
else if(choose_bit == 6){
if(sec_count / 10 == 0)
sec_count += 50;
else
sec_count = (sec_count / 10 - 1) * 10 + sec_count % 10;
}
else if(choose_bit == 7){
if(sec_count % 10 == 0)
sec_count += 9;
else
sec_count = (sec_count % 10 - 1) +sec_count / 10 * 10;
}
}
}
void ten2bcd(bit flag){ //将十进制的时间转换为BCD码
if(flag == 1){
time[0] = sec_now / 10 * 16 + sec_now % 10;
time[1] = min_now / 10 * 16 + min_now % 10;
time[2] = hour_now / 10 * 16 + hour_now % 10;
}
if(flag == 0){
time[3] = day_now / 10 * 16 + day_now % 10;
time[4] = mon_now / 10 * 16 + mon_now % 10;
time[6] = year_now / 10 * 16 + year_now % 10;
}
}
void count_up(){ //计时
if(count_up_flag == open){
if(sec_count < 60)
sec_count++;
else{
if(min_count < 60)
min_count++;
else{
if(hour_count < 24)
hour_count++;
else
hour_count = 0;
min_count = 0;
}
sec_count = 0;
}
}
}
void count_down(){ //倒计时
if(count_down_flag == open){
if(sec_count == 0){
if(min_count == 0){
if(hour_count == 0){
ring_flag = open; //倒计时结束,蜂鸣器响
}
else{
hour_count--;
min_count = 59;
}
}
else{
min_count--;
sec_count = 59;
}
}
else
sec_count--;
}
}
void init_counter(){ //初始化定时器1
TMOD &= 0xf0;
TMOD |= 0x01;
TH0= 0xd8;
TL0= 0xf0;
TR0= 1;
TF0= 0;
}
void ISR_time0() interrupt 1
{
TH0= 0xd8;
TL0= 0xf0;
t1++;
t2++;
if(t1 == 40){
t1 = 0;
if(count_down_flag == close && count_up_flag == close &&reset_time_flag == close)
flash_flag = ~flash_flag;
}
if(t2 == 100){
t2 = 0;
if(count_down_flag == open){
count_down();
get_time_count(hour_count, min_count, sec_count);
}
if(count_up_flag == open){
count_up();
get_time_count(hour_count, min_count, sec_count);
}
}
}
5、ds1302
ds1302.h
#ifndef _DS1302_H
#define _DS1302_H
#include<reg52.h>
#include"type.h"
extern uchar time[];
sbit DSIO = P3^4; //数据发收位
sbit RST = P3^5; //CE位
sbit SCLK = P3^6; //时钟位
void init_ds1302(); //初始化ds1302
void write_ds1302(uchar temp); //写入一个数据
void write_data(uchar address, uchardat); //对ds1302进行写操作
uchar read_data(uchar address); //对ds1302进行读操作
void read_time(); //读取当前时间
#endif
ds1302.c
#include"ds1302.h"
#include<intrins.h>
uchar write_address[] = {0x80, 0x82, 0x84,0x86, 0x88, 0x8a, 0x8c};
uchar read_address[] = {0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d};
uchar time[] = {0x00, 0x00, 0x00, 0x03,0x01, 0x01, 0x17};
void init_ds1302(){
uchar i;
write_data(0x8e, 0x00);
for(i = 0; i < 7; i++)
write_data(write_address[i], time[i]);
write_data(0x8e, 0x80);
}
void write_ds1302(uchar temp){
uchar i;
for(i = 0; i < 8; i++){ //将8位数据一位一位的传输
DSIO = temp & 0x01;
temp >>= 1;
SCLK = 1;
_nop_();
SCLK = 0;
_nop_();
}
}
void write_data(uchar address, uchar dat){
RST= 0;
_nop_();
SCLK = 0;
_nop_();
RST= 1;
_nop_();
write_ds1302(address);
write_ds1302(dat);
RST= 0;
_nop_();
}
uchar read_data(uchar address){
uchar i, time, temp;
RST= 0;
_nop_();
SCLK = 0;
_nop_();
RST= 1;
_nop_();
write_ds1302(address);
_nop_();
for(i = 0; i < 8; i++){ //将8位数据一位一位的接收
temp = DSIO;
time = (time >> 1) | (temp << 7);
SCLK = 1;
_nop_();
SCLK = 0;
_nop_();
}
RST= 0;
_nop_();
SCLK = 1;
_nop_();
DSIO = 0;
_nop_();
DSIO = 1;
_nop_();
return time;
}
void read_time(){
uchar i;
for(i = 0; i < 7; i++)
time[i] = read_data(read_address[i]);
}
6、key
key.h
#ifndef _KEY_H
#define _KEY_H
#include<reg52.h>
#include"type.h"
extern uchar choose_bit; //闪烁位
extern uchar choose_func; //选择功能序号
extern bit func_flag; //进入选择功能的标志
extern bit choose_func_flag; //确认选择功能的标志
sbit hang1 = P1^7; //部分矩阵键盘
sbit hang2 = P1^6;
sbit lie1 = P1^3;
sbit lie2 = P1^2;
sbit lie3 = P1^1;
sbit K1 = P3^1; //独立按键
sbit K2 = P3^0;
sbit K3 = P3^2;
sbit K4 = P3^3;
void main_key_check(); //主要键盘检测
void function(); //功能
void function0(); //显示时间
void function1(); //显示日期
void function2(); //显示温度
void function3(); //计时
void function4(); //倒计时
void function5(); //设置时间
void function6(); //设置日期
void assi_key_check(); //辅助键盘检测
void move_left(); //左移
void move_right(); //右移
void add_bit(); //加
void dec_bit(); //减
void one_key_check(); //蜂鸣器关闭按键检测
void switch_check(); //开关检测
#endif
key.c
#include"key.h"
#include"temperature.h"
#include"digital.h"
#include"count.h"
#include"ds1302.h"
#include"ring.h"
uchar choose_bit = 0;
uchar choose_func = 1;
bit func_flag = close;
bit choose_func_flag = close;
bit init_flag = close;
void main_key_check(){ //主要键盘检测
if(!K2){
Delay1ms(1);
if(!K2){
while(!K2) function();
if(choose_func_flag == close){
if(func_flag == close)
func_flag = open;
else
func_flag = close;
}
}
}
if(!K3){
Delay1ms(1);
if(!K3){
while(!K3) function();
if(func_flag == open && choose_func_flag == close){
choose_func++;
if(choose_func > 6)
choose_func = 1;
}
}
}
if(!K4){
Delay1ms(1);
if(!K4){
while(!K4) function();
if(func_flag == open){
if(choose_func_flag == close){
choose_func_flag = open;
}
else{
choose_func_flag = close;
init_flag = close;
reset_time_flag = close;
time_temp = time_show[choose_bit];
choose_bit = 0;
ET0 = 0;
}
}
}
}
}
void function(){ //功能
if(func_flag == close)
function0();
if(func_flag == open){
if(choose_func_flag == close)
show_func(choose_func);
if(choose_func_flag == open)
switch(choose_func){
case 1 : function1(); break;
case 2 : function2(); break;
case 3 : function3(); break;
case 4 : function4(); break;
case 5 : function5(); break;
case 6 : function6(); break;
}
}
}
void function0(){ //显示时间
read_time();
get_time_now(time[2], time[1], time[0]);
show_time();
}
void function1(){ //显示日期
read_time();
get_time_now(time[6], time[4], time[3]);
show_time();
}
void function2(){ //显示温度
RST= 1;
temp2digital();
get_temp(temperature);
show_temp();
}
void function3(){ //计时
if(init_flag == close){
init_time_count();
get_time_count(hour_count, min_count, sec_count);
init_flag = open;
}
assi_key_check();
show_time();
}
void function4(){ //倒计时
if(init_flag == close){
init_time_count();
get_time_count(hour_count, min_count, sec_count);
init_flag = open;
}
if(count_down_flag == close)
flash(choose_bit);
if(ring_flag == close){
assi_key_check();
show_time();
}
}
void function5(){ //设置时间
if(init_flag == close){
init_time_count();
get_time_count(hour_count, min_count, sec_count);
init_flag = open;
}
if(count_down_flag == close)
flash(choose_bit);
assi_key_check();
show_time();
}
void function6(){ //设置日期
if(init_flag == close){
init_time_count();
get_time_count(hour_count, min_count, sec_count);
init_flag = open;
}
if(count_down_flag == close)
flash(choose_bit);
assi_key_check();
show_time();
}
void assi_key_check(){ //辅助键盘检测
if(choose_func == 3){
hang1 = 0;
if(hang1 == 0 && count_up_flag == close)
if(!lie3){
delay(1);
if(!lie3){
while(!lie3) show_time();
count_up_flag = open;
ET0 = 1;
get_time_count(hour_count, min_count, sec_count);
}
}
hang1 = 1;
hang2 = 0;
if(hang2 == 0 && count_up_flag == open){
if(!lie3){
delay(1);
if(!lie3){
while(!lie3) show_time();
count_up_flag = close;
ET0 = 0;
}
}
}
hang2 = 1;
}
if(choose_func == 4){
ET0 = 1;
hang1 = 0;
if(hang1 == 0 && count_down_flag == close){
if(!lie1){
delay(1);
if(!lie1){
while(!lie1) show_time();
move_left();
}
}
if(!lie2){
delay(1);
if(!lie2){
while(!lie2) show_time();
move_right();
}
}
if(!lie3){
delay(1);
if(!lie3){
while(!lie3) show_time();
count_down_flag = open;
get_time_count(hour_count, min_count, sec_count);
ET0 = 0;
ET0 = 1;
}
}
}
hang1 = 1;
hang2 = 0;
if(hang2 == 0){
if(!lie1 && count_down_flag == close){
delay(1);
if(!lie1){
while(!lie1) show_time();
add_bit();
}
}
if(!lie2 && count_down_flag == close){
delay(1);
if(!lie2){
while(!lie2) show_time();
dec_bit();
}
}
if(!lie3 && count_down_flag == open){
delay(1);
if(!lie3){
while(!lie3) show_time();
count_down_flag = close;
get_time_count(hour_count, min_count,sec_count);
time_temp = time_show[choose_bit];
ET0 = 0;
}
}
}
hang2 = 1;
}
if(choose_func == 5 || choose_func == 6){
ET0 = 1;
hang1 = 0;
if(hang1 == 0){
if(!lie1){
delay(1);
if(!lie1){
while(!lie1) show_time();
move_left();
}
}
if(!lie2){
delay(1);
if(!lie2){
while(!lie2) show_time();
move_right();
}
}
if(!lie3){
delay(1);
if(!lie3){
while(!lie3) show_time();
if(choose_func == 5)
ten2bcd(1);
else
ten2bcd(0);
init_ds1302();
reset_time_flag = open;
ET0 = 0;
}
}
}
hang1 = 1;
hang2 = 0;
if(hang2 == 0){
if(!lie1){
delay(1);
if(!lie1){
while(!lie1) show_time();
add_bit();
}
}
if(!lie2){
delay(1);
if(!lie2){
while(!lie2) show_time();
dec_bit();
}
}
if(!lie3){
delay(1);
if(!lie3){
while(!lie3) show_time();
if(choose_func == 5){
init_time_now();
get_time_count(hour_now, min_now, sec_now);
}
else{
init_day_now();
get_time_count(year_now, mon_now, day_now);
}
time_temp = time_show[choose_bit];
reset_time_flag = close;
ET0 = 1;
}
}
}
hang2 = 1;
}
}
void move_left(){ //左移
time_show[choose_bit] = time_temp;
if(choose_bit == 0)
choose_bit = 7;
else{
choose_bit--;
if(choose_bit == 2 || choose_bit == 5)
choose_bit--;
}
time_temp = time_show[choose_bit];
}
void move_right(){ //右移
time_show[choose_bit] = time_temp;
choose_bit++;
if(choose_bit > 7)
choose_bit = 0;
if(choose_bit == 2 || choose_bit == 5)
choose_bit++;
time_temp = time_show[choose_bit];
}
void add_bit(){ //加
if(choose_func == 4){
set_time_count(choose_bit, '+');
get_time_count(hour_count, min_count, sec_count);
}
if(choose_func == 5){
set_time_now(choose_bit, '+');
get_time_count(hour_now, min_now, sec_now);
}
if(choose_func == 6){
set_day_now(choose_bit, '+');
get_time_count(year_now, mon_now, day_now);
}
time_temp = time_show[choose_bit];
}
void dec_bit(){ //减
if(choose_func == 4){
set_time_count(choose_bit, '-');
get_time_count(hour_count, min_count, sec_count);
}
if(choose_func == 5){
set_time_now(choose_bit, '-');
get_time_count(hour_now, min_now, sec_now);
}
if(choose_func == 6){
set_day_now(choose_bit, '-');
get_time_count(year_now,mon_now, day_now);
}
time_temp = time_show[choose_bit];
}
void one_key_check(){ //检测一个按键
hang2 = 0;
if(!lie3){
delay(1);
if(!lie3){
while(!lie3) ;
ring_flag = close;
}
}
}
void switch_check(){ //数码管显示开关
if(!K1){
Delay1ms(1);
if(!K1){
while(!K1) function();
if(digital_flag == close)
digital_flag = open;
else
digital_flag = close;
}
}
}
7、ring
ring.h
#ifndef _RING_H
#define _RING_H
#include<reg52.h>
#include"type.h"
sbit beep = P1^5;
extern bit ring_flag; //蜂鸣器标志
void init_player(); //初始化定时器2
void player(); //播放音乐
#endif
ring.c
#include"ring.h"
#include"count.h"
#include"digital.h"
#include"key.h"
uchar code music[] = {0xfe, 0x2a, 0x7f,0xfe, 0x2a, 0x7f, 0xfe, 0x2a, 0xff,
0xfd, 0x81, 0xff, 0xfe,0x84, 0x7f, 0xfe, 0x84, 0x7f,
0xfe, 0x84, 0xff, 0xfe,0x2a, 0xff, 0xfe, 0x2a, 0x7f,
0xfe, 0x84, 0x7f, 0xfe,0xc0, 0xff, 0xfe, 0xc0, 0xff,
0xfe, 0x99, 0x7f, 0xfe,0x84, 0x7f, 0xfe, 0x55, 0xff,
0xff, 0xff, 0xff, 0xfe,0x55, 0x7f, 0xfe, 0x84, 0x7f,
0xfe, 0x99, 0xff, 0xfe,0x99, 0xff, 0xfe, 0x84, 0x7f,
0xfe, 0x55, 0x7f, 0xfe, 0x84, 0xff,0xfe, 0x2a, 0xff,
0xfe, 0x84, 0x7f, 0xfe,0x84, 0x7f, 0xfe, 0x55, 0xff,
0xfd, 0x81, 0xff, 0xfe,0x05, 0x7f, 0xfe, 0x55, 0x7f,
0xfe, 0x2a, 0xff, 0xff,0xff, 0xff, 0x00}; //新年好
uchar high, low;
bit ring_flag = close;
void init_player(){ //初始化定时器2
TMOD &= 0x0f;
TMOD |= 0x10;
TH1= high;
TL1= low;
TF1= 0;
ET1= 1;
}
void player(){ //播放音乐
uchar time, i = 0;
init_player();
while(count_down_flag == open && ring_flag == open){
digital_flag = close;
if(music[i] != 0xff && music[i] != 0x00){
TR1 = 0;
beep = 1;
Delay1ms(5);
TR1 = 1;
high = music[i];
low = music[i + 1];
time = music[i + 2];
Delay1ms(time - 10);
i += 3;
}
else if(music[i] == 0xff){
time = music[i + 2];
Delay1ms(time - 10);
i += 3;
}
else if(music[i] == 0x00){
TR1 = 0;
beep = 1;
Delay1ms(150);
i = 0;
}
one_key_check();
}
digital_flag = open;
count_down_flag = close;
time_temp = 0;
ET1= 0;
}
void ISR_time1() interrupt 3
{
TH1= high;
TL1= low;
beep = ~beep;
}
因为调用了多个模块,所以编写了一段时间,从一个一个模块出发,一块一块进行编写、调试,最后结合起来调试运行,总体运行良好,各模块都正常运行。编写过程虽有遇到一些难题,但通过部分注释进行调试找到了问题的根源,解决了问题,受益颇丰。