main.c
#include "sys.h"
#include "ds1302.h"
void main()
{
InitSystem();
Timer0Init();
Set_DS1302();
while(1)
{
KeyScans();
Get_DS1302();
DisplaySMG();
if(led_k)
{
if(led)
OperateLed(1,0);
else
OperateLed(1,1);
}
else
OperateLed(1,1);
}
}
sys.c
#include "sys.h"
#include "onewire.h"
#include "ds1302.h"
void Select_74HC138(uchar channel)
{
switch(channel)
{
case 0: P2 = P2 & 0X1F; break;
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;
}
}
void InitSystem()
{
uchar i;
Select_74HC138(4);
P0 = 0XFF;
Select_74HC138(5);
P0 = 0X00;
Select_74HC138(0);
for(i = 0; i < 50; i++)
Temper_Get();
}
void Delay(uint ms)
{
uint i;
for(ms; ms > 0; ms--)
for(i = 921; i > 0; i--);
}
void OperateLed(uchar who,status)
{
static uchar temp;
uchar zt;
switch(status)
{
case 0:
temp = temp | _crol_(0x01,who-1);
P0 = ~temp;
break;
case 1:
if(who == 0xff)
{
P0 = 0xff;
temp = 0;
}
else
{
zt = (~temp) | _crol_(0x01,who-1);
if(temp != (~zt))
temp = ~zt;
P0 = ~temp;
}
break;
case 2:
P0 = ~who;
temp = who;
break;
default: break;
}
Select_74HC138(4);
Select_74HC138(0);
}
uchar t0;
uchar smg = 1;
uchar count;
uchar led_k = 0;
bit led = 0;
void Timer0Init() //50毫秒@12.000MHz
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xB0; //设置定时初值
TH0 = 0x3C; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 1;
EA = 1;
}
void ServiceTimer0() interrupt 1
{
if((k7 != 0) || (k6 != 0))
{
t0++;
if(t0 == 20)
{
t0 = 0;
smg = !smg;
}
}
if((Time[2] == clock[2]) && (Time[1] == clock[1]) && (Time[0] == clock[0]))
led_k = 1;
if(led_k == 1)
{
count++;
}
if(count % 4 == 0)
led = ~led;
if(count == 100) //else if(count == 100)
{
count = 0;
led_k = 0;
}
}
nixie.c
#include "sys.h"
#include "ds1302.h"
#include "onewire.h"
uchar code nixie[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xc6,0xbf,0xff};
uchar clock[3] = {0,0,0};
uchar interface = 0;
void DisplayBit(uchar pos,value)
{
P0 = 0x00;
Select_74HC138(6);
Select_74HC138(0);
P0 = nixie[value];
Select_74HC138(7);
Select_74HC138(0);
P0 = _crol_(0x01,pos);
Select_74HC138(6);
Select_74HC138(0);
Delay(1);
}
void DisplayTime()
{
if(k7 == 0)
{
DisplayBit(0,Time[2] / 10);
DisplayBit(1,Time[2] % 10);
DisplayBit(2,11);
DisplayBit(3,Time[1] / 10);
DisplayBit(4,Time[1] % 10);
DisplayBit(5,11);
DisplayBit(6,Time[0] / 10);
DisplayBit(7,Time[0] % 10);
}
else if(k7 == 1)
{
if(smg)
{
DisplayBit(0,Time[2] / 10);
DisplayBit(1,Time[2] % 10);
}
else
{
DisplayBit(0,12);
DisplayBit(1,12);
}
DisplayBit(2,11);
DisplayBit(3,Time[1] / 10);
DisplayBit(4,Time[1] % 10);
DisplayBit(5,11);
DisplayBit(6,Time[0] / 10);
DisplayBit(7,Time[0] % 10);
}
else if(k7 == 2)
{
if(smg)
{
DisplayBit(3,Time[1] / 10);
DisplayBit(4,Time[1] % 10);
}
else
{
DisplayBit(3,12);
DisplayBit(4,12);
}
DisplayBit(0,Time[2] / 10);
DisplayBit(1,Time[2] % 10);
DisplayBit(2,11);
DisplayBit(5,11);
DisplayBit(6,Time[0] / 10);
DisplayBit(7,Time[0] % 10);
}
else if(k7 == 3)
{
if(smg)
{
DisplayBit(6,Time[0] / 10);
DisplayBit(7,Time[0] % 10);
}
else
{
DisplayBit(6,12);
DisplayBit(7,12);
}
DisplayBit(0,Time[2] / 10);
DisplayBit(1,Time[2] % 10);
DisplayBit(2,11);
DisplayBit(3,Time[1] / 10);
DisplayBit(4,Time[1] % 10);
DisplayBit(5,11);
}
}
void DisplayClock()
{
if(k6 == 1)
{
if(smg)
{
DisplayBit(0,clock[2] / 10);
DisplayBit(1,clock[2] % 10);
}
else
{
DisplayBit(0,12);
DisplayBit(1,12);
}
DisplayBit(3,clock[1] / 10);
DisplayBit(4,clock[1] % 10);
DisplayBit(2,11);
DisplayBit(5,11);
DisplayBit(6,clock[0] / 10);
DisplayBit(7,clock[0] % 10);
}
else if(k6 == 2)
{
if(smg)
{
DisplayBit(3,clock[1] / 10);
DisplayBit(4,clock[1] % 10);
}
else
{
DisplayBit(3,12);
DisplayBit(4,12);
}
DisplayBit(0,clock[2] / 10);
DisplayBit(1,clock[2] % 10);
DisplayBit(2,11);
DisplayBit(5,11);
DisplayBit(6,clock[0] / 10);
DisplayBit(7,clock[0] % 10);
}
else if(k6 == 3)
{
if(smg)
{
DisplayBit(6,clock[0] / 10);
DisplayBit(7,clock[0] % 10);
}
else
{
DisplayBit(6,12);
DisplayBit(7,12);
}
DisplayBit(0,clock[2] / 10);
DisplayBit(1,clock[2] % 10);
DisplayBit(2,11);
DisplayBit(3,clock[1] / 10);
DisplayBit(4,clock[1] % 10);
DisplayBit(5,11);
}
}
void DisplaySMG()
{
switch(interface)
{
case 0: DisplayTime(); break;
case 1: DisplayClock(); break;
}
}
void DisplayTemp()
{
Temper_Get();
DisplayBit(5,temp / 10);
DisplayBit(6,temp % 10);
DisplayBit(7,10);
P0 = 0x00;
Select_74HC138(6);
Select_74HC138(0);
}
key.c
#include "sys.h"
#include "ds1302.h"
#include "onewire.h"
uchar k7 = 0;
uchar k6 = 0;
void KeyScans()
{
if(s4 == 0)
{
Delay(10);
led_k = 0;
if(interface == 0)
{
if(k7 == 1)
{
Time[2]--;
if(Time[2] == -1)
Time[2] = 23;
}
else if(k7 == 2)
{
Time[1]--;
if(Time[1] == -1)
Time[1] = 59;
}
else if(k7 == 3)
{
Time[0]--;
if(Time[0] == -1)
Time[0] = 59;
}
Set_DS1302();
}
else if(interface == 1)
{
k7 = 0;
if(k6 == 1)
{
clock[2]--;
if(clock[2] == -1)
clock[2] = 23;
}
else if(k6 == 2)
{
clock[1]--;
if(clock[1] == -1)
clock[1] = 59;
}
else if(k6 == 3)
{
clock[0]--;
if(clock[0] == -1)
clock[0] = 59;
}
}
while(s4 == 0)
{
if(interface == 0)
DisplayTemp();
else
DisplaySMG();
}
}
if(s5 == 0)
{
Delay(10);
led_k = 0;
if(interface == 0)
{
if(k7 == 1)
{
Time[2]++;
if(Time[2] == 24)
Time[2] = 0;
}
else if(k7 == 2)
{
Time[1]++;
if(Time[1] == 60)
Time[1] = 0;
}
else if(k7 == 3)
{
Time[0]++;
if(Time[0] == 60)
Time[0] = 0;
}
Set_DS1302();
}
else if(interface == 1)
{
k7 = 0;
if(k6 == 1)
{
clock[2]++;
if(clock[2] == 24)
clock[2] = 0;
}
else if(k6 == 2)
{
clock[1]++;
if(clock[1] == 60)
clock[1] = 0;
}
else if(k6 == 3)
{
clock[0]++;
if(clock[0] == 60)
clock[0] = 0;
}
}
while(s5 == 0)
{
DisplaySMG();
}
}
if(s6 == 0)
{
Delay(10);
k7 = -1;
led_k = 0;
if(interface == 0)
interface = 1;
k6++;
if(k6 == 4)
k6 = 0;
if(k6 == 0)
{
interface = 0;
k7 = 0;
}
while(s6 == 0)
{
DisplaySMG();
}
}
if(s7 == 0)
{
Delay(10);
k6 = 0;
led_k = 0;
interface = 0;
k7++;
if(k7 == 4)
k7 = 0;
while(s7 == 0)
{
DisplaySMG();
}
}
}
sys.h
#ifndef _SYS_H_
#define _SYS_H_
#include <STC15F2K60S2.H>
#include <intrins.h>
sbit s4 = P3^3;
sbit s5 = P3^2;
sbit s6 = P3^1;
sbit s7 = P3^0;
#define uchar unsigned char
#define uint unsigned int
extern unsigned char k7;
extern unsigned char k6;
extern unsigned char smg;
extern unsigned char interface;
extern unsigned char clock[3];
extern unsigned char led_k;
extern bit led;
extern unsigned char count;
//sys.c
void Select_74HC138(unsigned char channel);
void InitSystem();
void Delay(unsigned int ms);
void OperateLed(unsigned char who,status);
void Timer0Init();
//nixie.c
void DisplayBit(unsigned char pos,value);
void DisplayTime();
void DisplayClock();
void DisplaySMG();
void DisplayTemp();
//key.c
void KeyScans();
#endif