#include "stc15.h"
#include <intrins.h>
#include <stdio.h>
#include <ds1302.h>
#include <onewire.h>
#define FOSC 11059200L
#define T1MS (65536-FOSC/1000)
#define LED 4
#define ULN 5
#define COM 6
#define ABC 7
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned long u32;
u8 LEDbuf=0xFF;
u8 ULNbuf=0x00;
u8 SMGbuf[8];
u8 SMGtemp[20];
u32 t1mscnt=0;
u16 LEDdly=0;
u16 ULNdly=0;
u16 SMGABCdly=0;
u16 SMGdly=0;
u16 KEYdly=0;
u16 ReadDly =0;
float wendu=0;
float wenduset=23;
char page=0;
char pagexx=0;
char ctrlmode=0;
u8 zhengdianflag=0;
u16 zhengdiantime=0;
u8 code t_display[]={
0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e,
0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46};
u8 code T_COM[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
void Delay1ms();
void delayms(u16 xms);
void sel573(u8 x);
void closeall();
void LEDctrl();
void ULNctrl();
void LEDout();
void ULNout();
void SMGconv();
void SMGdisp();
void KeyRead();
void ReadData();
void SMGnow();
void main()
{
u8 i=0;
closeall();
wendu = read18b20();
delayms(800);
AUXR |= 0x40;
TMOD = 0x00;
TL1 = T1MS;
TH1 = T1MS >> 8;
TR1 = 1;
ET1 = 1;
EA = 1;
second = 50;
minute = 59;
hour =5;
set1302();
wendu = read18b20();
while(1)
{
if(LEDdly>100)
{
LEDdly=0;
LEDctrl();
}
if(ULNdly>100)
{
ULNdly=0;
ULNctrl();
}
if(ReadDly>500)
{
ReadDly=0;
ReadData();
}
if(SMGABCdly>300)
{
SMGABCdly =0;
SMGnow();
}
KeyRead();
}
}
void tm1_isr() interrupt 3 using 1
{
t1mscnt++;
LEDdly++;
ULNdly++;
ReadDly++;
SMGABCdly++;
SMGdly++;
SMGdisp();
if(zhengdianflag==1)
{
zhengdiantime++;
if(zhengdiantime>=5000)
{
zhengdiantime=0;
zhengdianflag=0;
}
}
}
void Delay1ms()
{
unsigned char i, j;
_nop_();
_nop_();
_nop_();
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
void delayms(u16 xms)
{
u16 i;
for(i=0;i<xms;i++)
{
Delay1ms();
}
}
void sel573(u8 x)
{
switch(x)
{
case 0: P2=P2&0x1F;break;
case LED: P2=P2&0x1F|0x80;break;
case ULN: P2=P2&0x1F|0xA0;break;
case COM: P2=P2&0x1F|0xC0;break;
case ABC: P2=P2&0x1F|0xE0;break;
}
}
void closeall()
{
sel573(LED);LEDbuf=0x00;P0=~LEDbuf;sel573(0);
sel573(ULN);ULNbuf=0x00;P0=ULNbuf;sel573(0);
sel573(COM);P0=0x00;sel573(0);
sel573(ABC);P0=0xFF;sel573(0);
}
void LEDout()
{
sel573(LED);P0=~LEDbuf;sel573(0);
}
void LEDctrl()
{
if(zhengdianflag==1)
{
LEDbuf = LEDbuf | 0x01 ;
}
else if(zhengdianflag==0)
{
LEDbuf = LEDbuf& (~0x01) ;
}
if(ctrlmode==0)
{
LEDbuf = LEDbuf | 0x02 ;
}
else if(ctrlmode==1)
{
LEDbuf = LEDbuf & (~0x02) ;
}
if((ULNbuf>>4)&0x01 ==1)
{
LEDbuf = LEDbuf ^ 0x04 ;
}
else
{
LEDbuf = LEDbuf &(~0x04) ;
}
LEDout();
}
void ULNout()
{
sel573(ULN);P0=ULNbuf;sel573(0);
}
void ULNctrl()
{
if(ctrlmode==0)
{
if(wendu>=wenduset)
{
ULNbuf = ULNbuf |0x10;
}
else if(wendu<wenduset)
{
ULNbuf = ULNbuf & (~0x10);
}
}
else if(ctrlmode==1)
{
if(zhengdianflag==1)
{
ULNbuf = ULNbuf |0x10;
}
else if(zhengdianflag==0)
{
ULNbuf = ULNbuf & (~0x10);
}
}
ULNout();
}
void SMGconv()
{
u8 i,j,temp;
while(SMGtemp[i]!=0x00)
{
switch(SMGtemp[i])
{
case '0':temp =0x3F;break;
case '1':temp =0x06;break;
case '2':temp =0x5B;break;
case '3':temp =0x4F;break;
case '4':temp =0x66;break;
case '5':temp =0x6D;break;
case '6':temp =0x7D;break;
case '7':temp =0x07;break;
case '8':temp =0x7F;break;
case '9':temp =0x6F;break;
case '-':temp =0x40;break;
case 'U':temp =0x3E;break;
default : temp =0x00;break;
}
if(SMGtemp[i+1]=='.')
{
temp =temp |0x80;
i=i+2;
}
else
i=i+1;
SMGbuf[j]= temp;
j=j+1;
}
}
void SMGdisp()
{
static u8 i=0;
sel573(COM);P0=0x00;sel573(0);
sel573(ABC);P0=0xFF;sel573(0);
sel573(COM);P0=T_COM[i];sel573(0);
sel573(ABC);P0=~SMGbuf[i];sel573(0);
i=i+1;
if(i>=8)i=0;
}
void KeyRead()
{
P30=0;P31=1;P32=1;P33=1;
P34=1;P35=1;P42=1;P44=1;
if(P34==0)
{
delayms(10);
if(P34==0)
{
}
while(P34==0);
}
else if(P35==0)
{
delayms(10);
if(P35==0)
{
}
while(P35==0);
}
else if(P42==0)
{
delayms(10);
if(P42==0)
{
}
while(P42==0);
}
else if(P44==0)
{
delayms(10);
if(P44==0)
{
}
while(P44==0);
}
P30=1;P31=0;P32=1;P33=1;
P34=1;P35=1;P42=1;P44=1;
if(P34==0)
{
delayms(10);
if(P34==0)
{
}
while(P34==0);
}
else if(P35==0)
{
delayms(10);
if(P35==0)
{
}
while(P35==0);
}
else if(P42==0)
{
delayms(10);
if(P42==0)
{
}
while(P42==0);
}
else if(P44==0)
{
delayms(10);
if(P44==0)
{
}
while(P44==0);
}
P30=1;P31=1;P32=0;P33=1;
P34=1;P35=1;P42=1;P44=1;
if(P34==0)
{
delayms(10);
if(P34==0)
{
if(page==2)
{
wenduset=wenduset-1;
if(wenduset<10)wenduset=99;
}
}
while(P34==0)
{
if(page==1)
{
pagexx=1;
read1302();
SMGnow();
delayms(1);
}
}
pagexx=0;
}
else if(P35==0)
{
delayms(10);
if(P35==0)
{
if(ctrlmode==0)ctrlmode=1;
else if(ctrlmode==1)ctrlmode=0;
}
while(P35==0);
}
else if(P42==0)
{
delayms(10);
if(P42==0)
{
}
while(P42==0);
}
else if(P44==0)
{
delayms(10);
if(P44==0)
{
}
while(P44==0)
{
}
}
P30=1;P31=1;P32=1;P33=0;
P34=1;P35=1;P42=1;P44=1;
if(P34==0)
{
delayms(10);
if(P34==0)
{
if(page==2)
{
wenduset=wenduset+1;
if(wenduset>100)wenduset=10;
}
}
while(P34==0);
}
else if(P35==0)
{
delayms(10);
if(P35==0)
{
if(page==0)page=1;
else if(page==1)page=2;
else if(page==2)page=0;
}
while(P35==0);
}
else if(P42==0)
{
delayms(10);
if(P42==0)
{
}
while(P42==0);
}
else if(P44==0)
{
delayms(10);
if(P44==0)
{
}
while(P44==0);
}
}
void ReadData()
{
read1302();
if(minute==0 && second ==0 )
{
zhengdianflag=1;
}
wendu = read18b20();
}
void SMGnow()
{
if(page==0)
{
sprintf(SMGtemp,"U1 %4.1f",wendu);
}
else if(page==1)
{
if(pagexx==0)
{
sprintf(SMGtemp,"U2 %02d-%02d",(int)hour,(int)minute);
}
else if(pagexx==1)
{
sprintf(SMGtemp,"U2 %02d-%02d",(int)minute,(int)second);
}
}
else if(page==2)
{
sprintf(SMGtemp,"U3xxxx%2d",(int)wenduset);
}
SMGconv();
}
DS1302.h
#ifndef __DS1302_H
#define __DS1302_H
extern unsigned char second;
extern unsigned char minute;
extern unsigned char hour;
void Write_Ds1302(unsigned char temp);
void Write_Ds1302_Byte( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302_Byte( unsigned char address );
void init1302();
void set1302();
void read1302();
#endif
DS1302.c
#include <stc15.h>
#include <intrins.h>
#include <ds1302.h>
unsigned char second;
unsigned char minute;
unsigned char hour;
sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST = P1^3;
void Write_Ds1302(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK=0;
SDA=temp&0x01;
temp>>=1;
SCK=1;
}
}
void Write_Ds1302_Byte( unsigned char address,unsigned char dat )
{
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
Write_Ds1302(dat);
RST=0;
}
unsigned char Read_Ds1302_Byte ( unsigned char address )
{
unsigned char i,temp=0x00;
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
for (i=0;i<8;i++)
{
SCK=0;
temp>>=1;
if(SDA)
temp|=0x80;
SCK=1;
}
RST=0; _nop_();
SCK=0; _nop_();
SCK=1; _nop_();
SDA=0; _nop_();
SDA=1; _nop_();
return (temp);
}
void init1302()
{
Write_Ds1302_Byte(0x8E,0x00);
Write_Ds1302_Byte(0x80,0x50);
Write_Ds1302_Byte(0x82,0x20);
Write_Ds1302_Byte(0x84,0x10);
Write_Ds1302_Byte(0x8E,0x80);
}
void set1302()
{
unsigned char miaotemp;
unsigned char fentemp;
unsigned char shitemp;
miaotemp= ((second / 10) << 4) + (second % 10);
fentemp = ((minute / 10) << 4) + (minute % 10);
shitemp= ((hour / 10) << 4) + (hour % 10);
Write_Ds1302_Byte(0x8E,0x00);
Write_Ds1302_Byte(0x80,miaotemp);
Write_Ds1302_Byte(0x82,fentemp);
Write_Ds1302_Byte(0x84,shitemp);
Write_Ds1302_Byte(0x8E,0x80);
}
void read1302()
{
unsigned char miaotemp;
unsigned char fentemp;
unsigned char shitemp;
Write_Ds1302_Byte(0x8E,0x00);
miaotemp=Read_Ds1302_Byte(0x81);
fentemp=Read_Ds1302_Byte(0x83);
shitemp= Read_Ds1302_Byte(0x85);
Write_Ds1302_Byte(0x8E,0x80);
second = ((miaotemp >> 4) & 0x07) * 10 + (miaotemp& 0x0f);
minute = ((fentemp >> 4) & 0x07) * 10 + (fentemp & 0x0f);
hour = ((shitemp >> 4) & 0x03) * 10 + (shitemp& 0x0f);
}
onewire.h
#ifndef __ONEWIRE_H
#define __ONEWIRE_H
unsigned char rd_temperature(void);
float read18b20();
#endif
onewire.c
#include "stc15.h"
#include "onewire.h"
sbit DQ = P1^4;
void Delay_OneWire(unsigned int t)
{
t=t*10;
while(t--);
}
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(5);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(5);
}
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;
for(i=0;i<8;i++)
{
DQ = 0;
dat >>= 1;
DQ = 1;
if(DQ)
{
dat |= 0x80;
}
Delay_OneWire(5);
}
return dat;
}
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(12);
DQ = 0;
Delay_OneWire(80);
DQ = 1;
Delay_OneWire(10);
initflag = DQ;
Delay_OneWire(5);
return initflag;
}
float read18b20()
{
unsigned char high,low;
unsigned int temp;
float xx;
init_ds18b20();
Write_DS18B20(0xCC);
Write_DS18B20(0x44);
Delay_OneWire(100);
init_ds18b20();
Write_DS18B20(0xCC);
Write_DS18B20(0xBE);
Delay_OneWire(100);
low= Read_DS18B20();
high= Read_DS18B20();
temp = ((high &0x0F)<<8) | low ;
xx=temp*0.0625;
return xx;
}