#include <stdio.h>
#include <stc15f2k60s2.h>
#include <intrins.h>
#include "iic.h"
#include "onewire.h"
#include "ds1302.h"
#define u8 unsigned char
#define u16 unsigned int
float aa=0,bb=0;
u8 dis=0,keynum=0,keyflag=0;
u16 f=0,count_t=0,count_f=0,adca=0,adcc=0;
u8 time_ds1302[8]={0x55,0x59,0x11,0x07,0x04,0x09,0x24};//³õʼʱ¼ä
u8 write_ds1302[8]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};//дµØÖ·
u8 read_ds1302[8]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};//¶ÁµØÖ·
u8 discode[8]={16,16,16,16,16,16,16,16};
code unsigned char tab[] =
{
0xc0, //0
0xf9, //1
0xa4, //2
0xb0, //3
0x99, //4
0x92, //5
0x82, //6
0xf8, //7
0x80, //8
0x90, //9
0x88, //A10
0x83, //b11
0xc6, //C12
0xa1, //d13
0x86, //E14
0x8e, //F15
0xff, //F16
0xff, //F17
0xff, //F18
0xff, //F19
0x40, //0
0x79, //1
0x24, //2
0x30, //3
0x19, //4
0x12, //5
0x02, //6
0x78, //7
0x00, //8
0x10, //9
0x08, //A10
0x03, //b11
0x46, //C12
0x21, //d13
0x06, //E14
0x0e //F15
};
void choosep2(u8 a)
{
switch(a)
{
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 Delay5ms() //@12.000MHz
{
unsigned char i, j;
i = 59;
j = 90;
do
{
while (--j);
} while (--i);
}
u8 readiic(u8 a)
{ u8 tep;
I2CStart();
I2CSendByte(0x90);
I2CWaitAck();
if(a==1)
I2CSendByte(0x01);
if(a==3)
I2CSendByte(0x03);
I2CWaitAck();
I2CStop();
I2CStart();
I2CSendByte(0x91);
I2CWaitAck();
tep=I2CReceiveByte();
I2CSendAck(1);
I2CStop();
return tep;
}
void readad()
{adca=readiic(1);
adcc=readiic(3);
}
void int_ds1302()
{
u8 i;
Write_Ds1302_Byte(0x8e,0x00);
for(i=0;i<8;i++)
{
Write_Ds1302_Byte(write_ds1302[i],time_ds1302[i]);
} Write_Ds1302_Byte(0x8e,0x80);
}
void re_ds1302()
{
u8 i;
for(i=0;i<8;i++)
{
time_ds1302[i]=Read_Ds1302_Byte(read_ds1302[i]);
}
}
float re_temp()
{
unsigned int temp;
float temperature;
unsigned char high,low;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay_OneWire(200);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
low=Read_DS18B20();
high=Read_DS18B20();
temp = (high&0x0f);
temp <<= 8;
temp |= low;
temperature = temp*0.0625;
return temperature;
}
void display1()
{
discode[0]=15;
discode[1]=16;
if(f>99999) discode[2]=f/100000;else discode[2]=16;
if(f>9999) discode[3]=f%100000/10000;else discode[3]=16;
if(f>999)discode[4]=f%10000/1000;else discode[4]=16;
if(f>99)discode[5]=f%1000/100;else discode[5]=16;
if(f>9)discode[6]=f%100/10;else discode[6]=16;
discode[7]=f%10;
}
void display2()
{
if(adca>99) discode[0]=adca/100; else discode[0]=16;
if(adca>9) discode[1]=adca%100/10; else discode[1]=16;
discode[2]=adca%10;
discode[3]=16;
discode[4]=16;
discode[5]=adcc/100;
discode[6]=adcc%100/10;
discode[7]=adcc%10;
}
void display3()
{bb=(aa-(int)aa)*10000;
discode[0]=discode[1]=discode[2]=discode[3]=discode[4]=16;
discode[5]=(int)aa%100/10;
discode[6]=(int)aa%10+20;
discode[7]=(int)bb/1000;
}
void display4()
{ discode[0]=time_ds1302[2]/16;
discode[1]=time_ds1302[2]%16;
discode[3]=time_ds1302[1]/16;
discode[4]=time_ds1302[1]%16;
discode[5]=16;
discode[6]=time_ds1302[0]/16;
discode[7]=time_ds1302[0]%16;
}
void key()
{P30=0;P31=P32=P33=1;
if(P44==0)
{Delay5ms();
if(P44==0)
{keyflag=1;keynum=7;}while(P44==0);
}if(P42==0)
{Delay5ms();
if(P42==0)
{keyflag=1;keynum=11;}while(P42==0);
}if(P35==0)
{Delay5ms();
if(P35==0)
{keyflag=1;keynum=15;}while(P35==0);
}
P31=0;P30=P32=P33=1;
if(P44==0)
{Delay5ms();
if(P44==0)
{keyflag=1;keynum=6;}while(P44==0);
}if(P42==0)
{Delay5ms();
if(P42==0)
{keyflag=1;keynum=10;}while(P42==0);
}if(P35==0)
{Delay5ms();
if(P35==0)
{keyflag=1;keynum=14;}while(P35==0);
}
P32=0;P30=P31=P33=1;
if(P44==0)
{Delay5ms();
if(P44==0)
{keyflag=1;keynum=5;}while(P44==0);
}if(P42==0)
{Delay5ms();
if(P42==0)
{keyflag=1;keynum=9;}while(P42==0);
}if(P35==0)
{Delay5ms();
if(P35==0)
{keyflag=1;keynum=13;}while(P35==0);
}
P33=0;P30=P31=P30=1;
if(P44==0)
{Delay5ms();
if(P44==0)
{keyflag=1;keynum=4;}while(P44==0);
}if(P42==0)
{Delay5ms();
if(P42==0)
{keyflag=1;keynum=8;}while(P42==0);
}if(P35==0)
{Delay5ms();
if(P35==0)
{keyflag=1;keynum=12;}while(P35==0);
}
}
void display()
{
choosep2(7);
P0=0xff;
choosep2(6);
P0=0x01<<dis;
choosep2(7);
P0=tab[discode[dis++]];
if(dis==8)dis=0;
}
void Timer1Init(void) //2??@12.000MHz
{
TMOD |= 0x15;
TL1 = (65536-2000)%256;
TH1 = (65536-2000)/256;
TR1 = 1;
ET1=1;
TR0 = 1;
ET0=1;
EA=1;
}
void main()
{Timer1Init();int_ds1302();
while(1)
{key();
//display();
if(keyflag==1&&keynum==7)
{
display1();
}
if(keyflag==1&&keynum==4)
{readad();
display2();
}
if(keyflag==1&&keynum==5)
{aa=re_temp();
display3();
}
if(keyflag==1&&keynum==6)
{re_ds1302();
display4();
}
}
}
void time3() interrupt 3
{
TL1 = (65536-1000)%256;
TH1 = (65536-1000)/256;
display();
count_t++;
if(count_t==1000)
{
count_t=0;
f=(TH0<<8)|TL0;
TH0=TL0=0;
}
}
蓝桥杯adda
最新推荐文章于 2024-06-16 21:24:39 发布