|
#include <stc15f2k60s2.h>
sbit S7 = P3^0;
sbit S6 = P3^1;
sbit S5 = P3^2;
sbit S4 = P3^3;
unsigned char code LedChar[] = {
0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,
0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xBF,0x7F
};
unsigned char LedBuff[8] = {
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
};
unsigned char InitTime[7] = {
0x50,0x59,0x23,0x07,0x03,0x06,0x20
};
unsigned char clock[7] = {
0x00,0x00,0x00,0x07,0x03,0x06,0x20
};
unsigned char set_time[7] = {0,0,0,0,0,0,0};
unsigned int temp;
unsigned char time[7];
unsigned char mode_time = 0; //设置时间标志位
unsigned char mode_clock = 0; //设置闹钟标志位
unsigned char mode_SMG = 1; // 1 显示时间 2 设置时间 3 设置闹钟 4 显示温度
bit mode_T = 0; //温度显示标志位
bit flag5s = 0;
bit share_Led = 0;
void InitSystem();
void Timer0Init(void); //1毫秒@12.000MHz
void SMGWroking();
void ScanKeyS();
void InitDS1302();
void SetClock();
extern unsigned char Get18B20Temp();
extern bit Start18B20();
extern void DS1302SingWrite(unsigned char addr,unsigned char dat);
extern unsigned char DS1302ByteRead(unsigned char addr);
void main()
{
InitSystem();
Timer0Init();
InitDS1302();
while(1)
{
SMGWroking();
ScanKeyS();
SetClock();
}
}
/******HC573选择函数*******/
void Select74HC573(unsigned char 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;
default:P2 = (P2&0x1F)|0x00;break;
}
}
/*******关闭LED和蜂鸣器、继电器******/
void InitSystem()
{
Select74HC573(5);
P0 = 0x00;
Select74HC573(4);
P0 = 0xFF;
Select74HC573(0);
}
/*********DS1302********/
void DS1302SingRead()
{
unsigned char i,addr = 0x81;
DS1302SingWrite(0x8E,0x00);
for(i=0;i<7;i++)
{
time[i] = DS1302ByteRead(addr);
addr = addr + 2;
}
DS1302SingWrite(0x8E,0x80);
}
void InitDS1302() //初始化时钟芯片
{
unsigned char i,addr = 0x80;
DS1302SingWrite(0x8E,0x00);
for(i=0;i<7;i++)
{
DS1302SingWrite(addr,InitTime[i]);
addr = addr + 2;
}
DS1302SingWrite(0x8E,0x80);
}
/*******DS18B20*********/
void Config18B20()
{
Start18B20();
temp = Get18B20Temp();
}
/********数码管********/
void SMGWroking()
{
switch(mode_SMG)
{
case 1:DS1302SingRead(); //显示时间
LedBuff[0] = LedChar[time[2]>>4];
LedBuff[1] = LedChar[time[2]&0x0F];
LedBuff[2] = LedChar[16];
LedBuff[3] = LedChar[time[1]>>4];
LedBuff[4] = LedChar[time[1]&0x0F];
LedBuff[5] = LedChar[16];
LedBuff[6] = LedChar[time[0]>>4];
LedBuff[7] = LedChar[time[0]&0x0F];
break;
case 2:LedBuff[0] = LedChar[set_time[2]/10]; //设置时间
LedBuff[1] = LedChar[set_time[2]%10];
LedBuff[2] = LedChar[16];
LedBuff[3] = LedChar[set_time[1]/10];
LedBuff[4] = LedChar[set_time[1]%10];
LedBuff[5] = LedChar[16];
LedBuff[6] = LedChar[set_time[0]/10];
LedBuff[7] = LedChar[set_time[0]%10];
break;
case 3:LedBuff[0] = LedChar[clock[2]/10];
LedBuff[1] = LedChar[clock[2]%10];
LedBuff[2] = LedChar[16];
LedBuff[3] = LedChar[clock[1]/10];
LedBuff[4] = LedChar[clock[1]%10];
LedBuff[5] = LedChar[16];
LedBuff[6] = LedChar[clock[0]/10];
LedBuff[7] = LedChar[clock[0]%10];
break;
case 4:Config18B20(); //显示温度
LedBuff[0] = 0xFF;
LedBuff[1] = 0xFF;
LedBuff[2] = 0xFF;
LedBuff[3] = 0xFF;
LedBuff[4] = 0xFF;
LedBuff[5] = LedChar[temp/10];
LedBuff[6] = LedChar[temp%10];
LedBuff[7] = LedChar[12];
break;
}
}
/*****十进制转BCD码*****/
unsigned char NumToBCD(unsigned char num)
{
unsigned char low,high,bcd;
low = (num%10)&0x0F;
high = ((num/10)<<4)&0xF0;
bcd = low|high;
return bcd;
}
/*****传递设置时间数值*******/
void Setnum_time()
{
unsigned char i;
for(i=0;i<3;i++)
{
InitTime[i] = NumToBCD(set_time[i]);
}
}
/*******闹钟提示功能******/
void SetClock()
{
if((clock[0]==time[0])&&(clock[1]==time[1])&&(clock[2]==time[2]))
{
flag5s = 1;
}
if(flag5s)
{
if(share_Led)
{
Select74HC573(4);
P0 = 0xFE;
Select74HC573(0);
P0 = 0xFF;
}
else
{
Select74HC573(4);
P0 = 0xFF;
Select74HC573(0);
P0 = 0xFF;
}
}
else
{
Select74HC573(4);
P0 = 0xFF;
Select74HC573(0);
}
}
/******设置闹钟*******/
void SetClock_jia()
{
if(mode_clock==1)
{
clock[0]++;
if(clock[0]>=59)
clock[0] = 59;
}
else if(mode_clock==2)
{
clock[1]++;
if(clock[1]>=59)
clock[1] = 59;
}
else if(mode_clock==3)
{
clock[2]++;
if(clock[2]>=23)
clock[2] = 23;
}
}
void SetClock_jian()
{
if(mode_clock==1)
{
if(clock[0]>0)
clock[0]--;
else
clock[0] = 0;
}
else if(mode_clock==2)
{
if(clock[1]>0)
clock[1]--;
else
clock[1] = 0;
}
else if(mode_clock==3)
{
if(clock[2]>0)
clock[2]--;
else
clock[2] = 0;
}
}
/******设置时间*******/
void SetTime_jia()
{
if(mode_time==1)
{
set_time[0]++;
if(set_time[0]>=59)
set_time[0] = 59;
}
else if(mode_time==2)
{
set_time[1]++;
if(set_time[1]>=59)
set_time[1] = 59;
}
else if(mode_time==3)
{
set_time[2]++;
if(set_time[2]>=23)
set_time[2] = 23;
}
}
void SetTime_jian()
{
if(mode_time==1)
{
if(set_time[0]>0)
set_time[0]--;
else
set_time[0] = 0;
}
else if(mode_time==2)
{
if(set_time[1]>0)
set_time[1]--;
else
set_time[1] = 0;
}
else if(mode_time==3)
{
if(set_time[2]>0)
set_time[2]--;
else
set_time[2] = 0;
}
}
/*******按键处理******/
void Delay1ms() //@12.000MHz
{
unsigned char i, j;
i = 12;
j = 169;
do
{
while (--j);
} while (--i);
}
void ScanKeyS()
{
if(S7==0) //时钟设置
{
Delay1ms();
if(S7==0)
{
flag5s = 0;
if((mode_clock==0)&&(mode_T==0))
{
switch(mode_time)
{
case 0:mode_time = 1;mode_SMG = 2;
DS1302SingWrite(0x8E,0x00);break;
case 1:mode_time = 2;mode_SMG = 2;break;
case 2:mode_time = 3;mode_SMG = 2;break;
case 3:mode_time = 0;mode_SMG = 1;
DS1302SingWrite(0x8E,0x80);
Setnum_time();
InitDS1302();break;
}
}
while(S7==0)
{
SMGWroking();
}
}
}
if(S6==0) //闹钟设置
{
Delay1ms();
if(S6==0)
{
flag5s = 0;
if((mode_time==0)&&(mode_T==0))
{
switch(mode_clock)
{
case 0:mode_clock = 1;mode_SMG = 3;break;
case 1:mode_clock = 2;mode_SMG = 3;break;
case 2:mode_clock = 3;mode_SMG = 3;break;
case 3:mode_clock = 0;mode_SMG = 1;break;
}
}
while(S6==0)
{
SMGWroking();
}
}
}
if(S5==0) //“加”按键,在“时钟设置”或“闹钟设置”状态下,
{ //每次按下该按键当前选择的单元(时、分或秒)增加 1 个单位。
Delay1ms();
if(S5==0)
{
flag5s = 0;
SetTime_jia();
SetClock_jia();
while(S5==0)
{
SMGWroking();
}
}
}
if(S4==0) //“减”按键,在“时钟设置”或“闹钟设置”状态下,
{ //每次按下该按键当前选择的单元(时、分或秒)减少 1 个单位。
Delay1ms();
if(S4==0)
{
flag5s = 0;
SetTime_jian();
SetClock_jian();
if((mode_time==0)&&(mode_clock==0)) //在 “时钟显示”状态下,按下 S4 按键,
{ //显示温度数据,松开按键,返回“时钟显示”界面。
mode_T = ~mode_T;
if(mode_T)
mode_SMG = 4; //显示温度
else
mode_SMG = 1; //显示时间
}
while(S4==0)
{
SMGWroking();
}
}
}
}
/******数码管刷新*****/
void ScanSMG()
{
static unsigned char i = 0;
P0 = 0xFF;
Select74HC573(6);
P0 = 0x01<<i;
Select74HC573(7);
P0 = LedBuff[i++];
if(i>=8)
i = 0;
Select74HC573(0);
}
void Timer0Init(void) //1毫秒@12.000MHz
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x18; //设置定时初值
TH0 = 0xFC; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 1;
EA = 1;
}
void IterruptTime0() interrupt 1
{
static unsigned int cnt = 0;
static unsigned char share = 0;
ScanSMG();
if(flag5s)
{
share++;
if(share>=200)
{
share_Led = ~share_Led;
cnt++;
share = 0;
}
if(cnt>=25)
{
flag5s = 0;
}
}
}
|
|