#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit TSOR = P3^7;
static uchar temp1,temp2;
uchar code tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//7段数码管段码表共阳
uchar data tab3[4]=0;
int bai,shi,ge;
////////////////////////////////////////////////////////////////////////
void delay(uchar a);
void Delay15();
void Delay60();
void Delay100ms();
void Write0TS();
void Write1TS();
bit ReadTS();
void ResetTS();
void WriteByteTS(uchar byte);
uchar ReadByteTS();
void InitTS();
void GetTempTS();
void display(void);
////////////////////////////////////////////////////////////////////////
/////////////////////////////
sbit lcdrs=P2^2;//数据命令选择端H/L
sbit dxxz=P2^1;//读写选择端H/L
sbit lcden=P2^0;//使能信号
uchar code table1[]="0123456789";
uchar code table2[]="0123456789";
yanshi(uint i)
{
uint j;
for(i;i>0;i--)
for(j=100;j>0;j--);//i*0.0006秒
}
//延时Tms
void mingling(uchar ml)
{
lcdrs=0;// 选择写命令模式
P0=ml; // 送入命令
yanshi(1); //延时等数据稳定
lcden=1; // 使能端一个高脉冲,初始时为0
yanshi(1);
lcden=0; // 完成高脉冲
}
void shuju(uchar sj)
{
lcdrs=1;
P0=sj;
yanshi(1);
lcden=1;
yanshi(1);
lcden=0;
}
void csh()
{
dxxz=0;
lcden=0;
mingling(0x38);// 设置16*2显示,5*7点阵,8位数据接口
mingling(0x0c);// 设置开显示,不显示光标
mingling(0x06); // 写一个字符后地址指针加1
mingling(0x01);// 显示清0,数据指针清0
}
void display(void)
{
temp1=temp1;
tab3[1]=temp1/100;
tab3[2]=(temp1%100)/10;
tab3[3]=temp1%10;
tab3[0]=0;
bai=temp1/100;shi=temp1%100/10;ge=temp1%100%10;
csh();
mingling(0x80);
shuju(table1[ tab3[1]]);_nop_();
shuju(table1[ tab3[2]]);_nop_();
shuju(table1[ tab3[3]]);_nop_();
P1=tab[tab3[3]]; P3=0x01;
}
/////////////////////////////////////////////////////////////////////////
void main(void)
{
while(1)
{
InitTS();
GetTempTS();
display();
}
}
///////////////////////////////////////////////////////////////////////
void delay(uchar a)
{
unsigned i,j;
for(i=0;i<a;i++)
for(j=0;j<200;j++);
}
////////////////////////////////////////////////////////////////////////
void Delay100ms()
{
uchar i,j,k;
for(i=0;i<8;i++)
for(j=0;j<25;j++)
for(k=0;k<250;k++);
}
////////////////////////////////////////////////////////////////////////
void Delay15()
{
uchar i;
for(i=0;i<8;i++);
}
////////////////////////////////////////////////////////////////////////
void Delay60()
{
uchar i;
for(i=0;i<30;i++);
}
////////////////////////////////////////////////////////////////////////
void Write0TS()
{
TSOR=1;
TSOR=0;
Delay15();
Delay15();
Delay15();
Delay15();
TSOR=1;
_nop_();
_nop_();
}
////////////////////////////////////////////////////////////////////////
void Write1TS()
{
TSOR=1;
TSOR=0;
_nop_();
_nop_();
_nop_();
TSOR=1;
Delay15();
Delay15();
Delay15();
Delay15();
}
////////////////////////////////////////////////////////////////////////
bit ReadTS()
{
bit b;
TSOR=1;
TSOR=0;
_nop_();
_nop_();
_nop_();
_nop_();
TSOR=1;
Delay15();
b=TSOR;
Delay15();
Delay15();
Delay15();
_nop_();
_nop_();
return b;
}
/////////////////////////////////////////////////////////////////////////
void ResetTS()
{
uchar i;
TSOR=1;
TSOR=0;
for(i=0;i<8;i++)
Delay60();
TSOR=1;
while(TSOR);
for(i=0;i<5;i++)
Delay60();
}
/////////////////////////////////////////////////////////////////////////
void WriteByteTS(uchar byte)
{
uchar i;
for(i=0;i<8;i++)
{
if(byte&0x01)
Write1TS();
else
Write0TS();
byte=byte>>1;
}
}
/////////////////////////////////////////////////////////////////////////
uchar ReadByteTS()
{
uchar i,j;
bit b;
j=0;
for(i=0;i<8;i++)
{
b=ReadTS();
if(b)
j+=1;
j=_cror_(j,1);
}
return j;
}
////////////////////////////////////////////////////////////////////////
void InitTS()
{
ResetTS();
WriteByteTS(0xCC);
WriteByteTS(0x4E);
WriteByteTS(0x64);
WriteByteTS(0x8A);
WriteByteTS(0x1F);
}
////////////////////////////////////////////////////////////////////////
void GetTempTS()
{
ResetTS();
WriteByteTS(0xCC);
WriteByteTS(0x44);
Delay100ms();
ResetTS();
WriteByteTS(0xCC);
WriteByteTS(0xBE);
temp2=ReadByteTS();
temp1=ReadByteTS();
temp1=temp1<<4;
temp1+=(temp2&0xF0)>>4;
temp2=(temp2&0x0F)?5:0;
}
////////////////////////////////////////////////////////////////////////
/*
void display(void)
{
uchar i;
tab3[1]=temp1/100;
tab3[2]=temp1%100/10;
tab3[3]=temp1%10;
tab3[0]=0;
// for(i=0;i<4;i++)
// {
P1=tab0[tab3[3]];
// P2=tab2[i];
delay(20);
// }
}
*/