sbit RST=P1^3;
sbit SCK=P1^7;
sbit SDA=P2^3;
#define DELAY_TIME 5
sbit sda=P2^1;
sbit scl=P2^0;
sbit DQ=P1^4;
#include <stdio.h>
#include <stc15f2k60s2.h>
#include <intrins.h>
#include "ds1302.h"
#include "onewire.h"
#include "iic.h"
#define u8 unsigned char
#define u16 unsigned int
u8 dis,flag,c=0;
u8 keynum=0,flagkey=0,num=0;
u16 count_t=0,count_f=0,dat_f=0;
float a,b;
int aaa=0;
u8 write_ds1302[8]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};//дµØÖ·
u8 read_ds1302[8]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};//¶ÁµØÖ·
u8 time_ds1302[8]={0x55,0x59,0x11,0x07,0x04,0x09,0x24};//³õʼʱ¼ä
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,//Ãð16
0xff,//Ãð17
0xff,//Ãð18
0xff,//Ãð19
0x40, //20
0x79, //1
0x34, //2
0x40, //3
0x19, //4
0x12, //5
0x02, //6
0x78, //7
0x00, //8
0x10, //9
};
void int_ds1302()
{
u8 i=0;
Write_Ds1302_Byte(0x8e,0x00);
for(i=0;i<7;i++)
{
Write_Ds1302_Byte(write_ds1302[i],time_ds1302[i]);
}
Write_Ds1302_Byte(0x8e,0x80);
}
void rread_ds1302()
{
u8 i=0;
for(i=0;i<7;i++)
{
time_ds1302[i]=Read_Ds1302_Byte(read_ds1302[i]);
}
}
void Write_24C02_Byte(unsigned char addr, unsigned char dat)
{
I2CStart(); //????
I2CSendByte(0xa0); //?????
I2CWaitAck(); //??????
I2CSendByte(addr); //??????
I2CWaitAck(); //??????
I2CSendByte(dat); //????
I2CWaitAck(); //??????
I2CStop(); //????
}
//==================24C02?????====================
unsigned char Read_24C02_Byte(unsigned char addr)
{
unsigned char tmp;
I2CStart(); //????
I2CSendByte(0xa0); //?????
I2CWaitAck(); //??????
I2CSendByte(addr); //??????
I2CWaitAck();
//??????
I2CStart(); //????
I2CSendByte(0xa1); //?????
I2CWaitAck(); //??????
tmp = I2CReceiveByte(); //????
I2CSendAck(1); //???????
I2CStop(); //????
return tmp;
}
void Read_Save_OpenData()
{
num = Read_24C02_Byte(0x00);
num++;
if(num > 99)
{
num = 1;
}
Write_24C02_Byte(0x00, num);
}
void display2()
{
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 display3()
{
discode[0]=discode[1]=discode[2]=discode[3]=discode[4]=16;
b=(a-(int)a)*10000;
discode[5]=(int)a%100/10;
discode[6]=((int)a%10)+20;
discode[7]=(int)b/1000;
}
void display4()
{
discode[0]=discode[1]=discode[2]=16;
if(dat_f>9999)
discode[3]=dat_f/10000;
if(dat_f>999)
discode[4]=dat_f/1000%10;
if(dat_f>99)
discode[5]=dat_f/100%10;
if(dat_f>9)
discode[6]=dat_f/10%10;
discode[7]=dat_f%10;
}
void display7()
{
discode[0]=discode[1]=discode[2]=discode[3]=discode[4]=discode[5]=16;
discode[6]=num/10;
discode[7]=num%10;
}
void Delay5ms() //@12.000MHz
{
unsigned char i, j;
i = 59;
j = 90;
do
{
while (--j);
} while (--i);
}
void Delay1000ms() //@12.000MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 46;
j = 153;
k = 245;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void choosep2(u8 a)
{
switch(a)
{case 4:
P2=P2&0x1f|0x8f;
break;
case 5:
P2=P2&0x1f|0xaf;
break;
case 6:
P2=P2&0x1f|0xcf;
break;
case 7:
P2=P2&0x1f|0xef;
break;
case 0:
P2=P2&0x1f|0x1f;
break;
}
}
void display()
{
choosep2(7);
P0=0xff;
choosep2(6);
P0=0x01<<dis;
choosep2(7);
P0=tab[discode[dis++]];
//Delay1000ms();
if(dis==8)
dis=0;
}
void key()
{
P30=0;P31=P32=P33=1;
if(P44==0)
{Delay5ms();if(P44==0){keynum=7;flagkey=1;}while(P44==0);
}if(P42==0)
{Delay5ms();if(P42==0){keynum=11;flagkey=1;}while(P42==0);
}if(P35==0)
{Delay5ms();if(P35==0){keynum=15;flagkey=1;}while(P35==0);
}
// if(P34==0)
// {Delay5ms();if(P34==0){keynum=19;flagkey=1;}while(P34==0);
// }
P31=0;P30=P32=P33=1;
if(P44==0)
{Delay5ms();if(P44==0){keynum=6;flagkey=1;}while(P44==0);
}if(P42==0)
{Delay5ms();if(P42==0){keynum=10;flagkey=1;}while(P42==0);
}if(P35==0)
{Delay5ms();if(P35==0){keynum=14;flagkey=1;}while(P35==0);
}
// if(P34==0)
// {Delay5ms();if(P34==0){keynum=18;flagkey=1;}while(P34==0);
// }
P32=0;P30=P31=P33=1;
if(P44==0)
{Delay5ms();if(P44==0){keynum=5;flagkey=1;}while(P44==0);
}if(P42==0)
{Delay5ms();if(P42==0){keynum=9;flagkey=1;}while(P42==0);
}if(P35==0)
{Delay5ms();if(P35==0){keynum=13;flagkey=1;}while(P35==0);
}
// if(P34==0)
// {Delay5ms();if(P34==0){keynum=17;flagkey=1;}while(P34==0);
// }
P33=0;P30=P32=P31=1;
if(P44==0)
{Delay5ms();if(P44==0){keynum=4;flagkey=1;}while(P44==0);
}if(P42==0)
{Delay5ms();if(P42==0){keynum=8;flagkey=1;}while(P42==0);
}if(P35==0)
{Delay5ms();if(P35==0){keynum=12;flagkey=1;}while(P35==0);
}
// if(P34==0)
// {Delay5ms();if(P34==0){keynum=16;flagkey=1;}while(P34==0);
// }
}
float rd_temperature(void)
{
unsigned int temp;
float temperature;
unsigned char low,high;
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 Timer0Init(void) //2??@12.000MHz
{
// AUXR |= 0x80; //?????1T??
// TMOD &= 0xF0; //???????
TMOD = 0x15; //ÅäÖö¨Ê±Æ÷¹¤×÷ģʽ
TH1= (65536-1000)/256;
TL1= (65536-1000)%256;
TH0=TL0=0;
//TF0 = 0; //??TF0??
TR0 = 1; //???0????
ET0=1;
TR1=1;
ET1=1;
EA=1;
}void Timer1Init(void) //2??@12.000MHz
{
TMOD |= 0x10;
TL1 = 0x40; //??????
TH1 = 0xA2; //??????
TF1 = 0; //??TF1??
TR1 = 1; //???1????
ET1=1;
EA=1;
}
void main()
{Timer0Init() ;
int_ds1302();Read_Save_OpenData();
while(1)
{ key();rread_ds1302();
if(flag==1)
{
choosep2(4);
P0=0x01<<c++;
if(c==8)c=0;
flag=0;
}if(flagkey==1&&keynum==14)
{flagkey=0;
choosep2(5);
P0=0x50;
}
if(flagkey==1&&keynum==12)
{//flagkey=0;
a=rd_temperature();
display3();
}
if(flagkey==1&&keynum==4)
{//flagkey=0;
display4();
}
if(flagkey==1&&keynum==7)
{flagkey=0;
display7();
}
if(flagkey==1&&keynum==15)
{//flagkey=0;
display2();
}if(flagkey==1 && keynum==13)
{flagkey=0;
choosep2(5);
P0=0x00;
}
}
}
void time1() interrupt 3
{ TH1= (65536-1000)/256;
TL1= (65536-1000)%256;
display();
aaa++;
if(aaa==1000)
{flag=1;
aaa=0;}
count_t++;
if(count_t==1000)
{ count_t=0;
dat_f=(TH0 << 8) | TL0;
TH0 = 0;
TL0 = 0;
}
}
蓝桥杯直接背
于 2024-04-12 23:59:39 首次发布