📄 temperature.c
字号:
Test_Ack();
if(flag==0) return(0);
for(i=0;i<n-1;i++)//读取字节数据
{
*(p+i)=ReceiveData();//读取数据
ACK();
}
*(p+n-1)=ReceiveData();
NoAck();
I2C_Stop();
return(1);
}
/**************************************************************************************************
**函数名:Led()
**输 入:
**输 出:
**说 明:显示数值
***************************************************************************************************/
void led()
{
P2=0xef;
P0=seg7code[hour/10];
delay2();
P0=0xff;
P2=0xdf;
P0=seg7code[hour%10]&0x7f; //加小数点
delay2();
P0=0xff;
P2=0xbf;
P0=seg7code[min/10];
delay2();
P0=0xff;
P2=0x7f;
P0=seg7code[min%10];
delay2();
P0=0xff;
if(fg==1)
{
P2=0xfe;
P0=seg7code[sdata/10];
delay2();
P0=0xff;
P2=0xfd;
P0=seg7code[sdata%10]&0x7f; //加小数点
delay2();
P0=0xff;
P2=0xfb;
P0=seg7code[xiaoshu1];
delay2();
P0=0xff;
P2=0xf7;
P0=seg7code[xiaoshu2];
delay2();
P0=0xff;
}
if(fg==0) //温度为负时显示的数据
{
P2=0xfe;
P0=0xbf; //显示负号
delay2();
P0=0xff;
P2=0xfd;
P0=seg7code[sdata/10];
delay2();
P0=0xff;
P2=0xfb;
P0=seg7code[sdata%10]&0x7f; //加小数点
delay2();
P0=0xff;
P2=0xf7;
P0=seg7code[xiaoshu1];
delay2();
P0=0xff;
}
}
void led1()
{
P2=0xef;
P0=seg7code[a/10];
delay2();
P0=0xff;
P2=0xdf;
P0=seg7code[a%10]&0x7f; //加小数点
delay2();
P0=0xff;
P2=0xbf;
P0=seg7code[e/10];
delay2();
P0=0xff;
P2=0x7f;
P0=seg7code[e%10];
delay2();
P0=0xff;
P2=0xfe;
P0=seg7code[b/10];
delay2();
P0=0xff;
P2=0xfd;
P0=seg7code[b%10]&0x7f; //加小数点
delay2();
P0=0xff;
P2=0xfb;
P0=seg7code[c];
delay2();
P0=0xff;
P2=0xf7;
P0=seg7code[d];
delay2();
P0=0xff;
}
/*****************************************************************************************************
**函数名:读取数据
**输 入:
**输 出:ucSendBuffer[0]
******************************************************************************************************/
void recdat()
{
ReadNByte(0xa0,suba,ucSendBuffer,1);
a=ucSendBuffer[0];
suba++;
ReadNByte(0xa0,suba,ucSendBuffer,1);
b=ucSendBuffer[0];
suba++;
ReadNByte(0xa0,suba,ucSendBuffer,1);
c=ucSendBuffer[0];
suba++;
ReadNByte(0xa0,suba,ucSendBuffer,1);
d=ucSendBuffer[0];
suba++;
}
/*****************************************************************************************************
**函数名:串口发送
**输 入:
**输 出:printf()
******************************************************************************************************/
void sendat1()
{
a=min/10;
b=min%10;
printf("It is %d:%d%d now!\n",hour,a,b);
printf("The current temperature: %d.%d%d C.\n",sdata,xiaoshu1,xiaoshu2);
rdat=0x00;
}
void sendat2()
{
uchar n;
printf("Read the date from 24C02...\n");
printf("START!\n");
sum=suba;
suba=0x00;
for(n=0;n<24;n++)
{
recdat();
printf("Temperature at '%d:00' is %d.%d%d C;\n",a,b,c,d);
}
printf("END!\n");
suba=sum;
rdat=0x00;
}
/*****************************************************************************************************
**函数名:存储数据
**输 入:hour,sdata,xiaoshu1,xiaoshu2
**输 出:
******************************************************************************************************/
void wridat()
{
ucSendBuffer[0]=hour;
WriteNByte(0xa0,suba,ucSendBuffer,1);
suba++;
ucSendBuffer[0]=sdata;
WriteNByte(0xa0,suba,ucSendBuffer,1);
suba++;
ucSendBuffer[0]=xiaoshu1;
WriteNByte(0xa0,suba,ucSendBuffer,1);
suba++;
ucSendBuffer[0]=xiaoshu2;
WriteNByte(0xa0,suba,ucSendBuffer,1);
suba++;
if(suba>99) suba=0;
}
/*****************************************************************************************************
**函数名:CLEAR()
**输 入:
**输 出:
**说 明:将24c08的100个存储单元清零
******************************************************************************************************/
void CLEAR()
{
uint x;
suba=0x00;
for(x=0;x<100;x++)
{
ucSendBuffer[0]=0x00;
WriteNByte(0xa0,suba,ucSendBuffer,1);
suba++;
}
suba=0x00;
}
/*****************************************************************************************************
**函数名:主函数
**输 入:
**输 出:
******************************************************************************************************/
void main()
{
uint x,y;
//*************串口初始化
TMOD=0x20;
SCON=0x52;
TH1=0xfd;
TR1=1;
printf("Hello,welcome!\n");
//**************设置时钟
hour=12; //设置时间初值
min=30;
sec=0;
us1=0;
ReadTemperature();//读取温度
suba=0x00;
while(1)
{
if(P3_2==0)//数码管显示各时间段温度
{
if(P1_6==0)
{
recdat();
if(suba>96) suba=0;
e=0;
}
if(P1_7==0) suba=0x00;
led1();
}
else if(P3_3==0)//对24c08一百个单元清零
{
CLEAR();
}
else//显示当前温度,并运行时钟
{
for(x=26;x>0;x--)
{
//串口查询
if(RI==1)
{
RI=0;
rdat=SBUF;
}
if(rdat==0x31)
{
sendat1();
}
if(rdat==0x32)
{
sendat2();
}
if(rdat==0x33)
{
printf("Clear 100 memory lacations of 24C02...\n");
CLEAR();
printf("OK!\n");
sendat2();
rdat=0x00;
}
//时钟
led();
if(us1==10)
{
us1=0;
inc_sec();
}
if(P1_7==0)//设置时针
{
hour++;
if(hour>23) hour=0;
delay3(500);
}
if(P1_6==0)//设置分针
{
min++;
if(min>59) min=0;
delay3(500);
}
_nop_();
_nop_();
_nop_();
_nop_();
for(y=0;y<8;y++);
}
us1++;
}
}
}
/********************************************************************************************
**函数名:inc_sec()
**输 入:
**输 出:
*********************************************************************************************/
void inc_sec()
{
sec++;
P1_0=~P1_0;
if(sec>59)
{
sec=0;
inc_min();
ReadTemperature();
}
}
/********************************************************************************************
**函数名:inc_min()
**输 入:
**输 出:
**********************************************************************************************/
void inc_min()
{
min++;
if(min>59)
{
min=0;
inc_hour();
wridat();
}
}
/*******************************************************************************************
**函数名:inc_hour()
**输 入:
**输 出:
*********************************************************************************************/
void inc_hour()
{
hour++;
if(hour>23) hour=0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -