本帖最后由 eehome 于 2013-1-5 09:54 编辑
#include
#define uchar unsigned char
#define uint unsigned int
__CONFIG(0X3B31);
#define DQ RA0
#define DQ_HIGH TRISA0=1
#define DQ_LOW TRISA0=0;DQ=0
#define duan RA1
#define wei RA2
const uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f};
const uchar table1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};
uint a1=0,a2=1,a3=2,a4=3;
void display_seg();
void delay(uint m)//2.2M毫秒
{
uint i;
for(i=0;i
}
void init()
{
TRISA=0;
TRISD=0;
PORTA=0;
PORTD=0;
}
void t_init()
{
uchar flag=1;
DQ_HIGH;
NOP();NOP();
while(flag)
{
DQ_LOW;
delay(325);
DQ_HIGH;
delay(35);
if(DQ==1)
flag=1;
else flag=0;
delay(250);
}
}
void write_byte(uchar date)
{
uint i;
uchar temp;
for(i=0;i<7;i++)
{
DQ_HIGH;
NOP();NOP();
DQ_LOW;
delay(8);
temp=date&0x01;
DQ_LOW;
delay(4);
if(temp==1)
DQ_HIGH;
delay(23);
DQ_HIGH;
date>>=1;
}
}
uchar read_byte()
{
static bit tem;
uchar temp;
uint i;
for(i=0;i<8;i++)
{
DQ_HIGH;
delay(2);
DQ_LOW;
delay(4);
DQ_HIGH;
delay(2);
tem=DQ;
if(tem==1)
temp|=0x80;
temp>>=1;
}
return (temp);
}
void get_tempture()
{
uint temp,i;
uchar tem1,tem2;
float num;
t_init();
write_byte(0xcc);
write_byte(0x44);
for(i=1000;i>0;i--)
display_seg();
t_init();
write_byte(0xcc);
write_byte(0xBE);
tem1=read_byte();
tem2=read_byte();
num=(tem2*256+tem1)*6.25;
temp=(int)num;
a1=temp/1000;//最高位
a2=temp%1000/100;
a3=temp%100/10;
a4=temp%10;
}
void display_seg()
{
PORTD=0xfe;wei=1;wei=0;
PORTD=table[a1];duan=1;duan=0;
PORTD=0;delay(5);
PORTD=0xfd;wei=1;wei=0;
PORTD=table1[a2];duan=1;duan=0;
PORTD=0;delay(5);
PORTD=0xfb;wei=1;wei=0;
PORTD=table[a3];duan=1;duan=0;
PORTD=0;delay(5);
PORTD=0xf7; wei=1;wei=0;
PORTD=table[a4];duan=1;duan=0;
PORTD=0;delay(5);
PORTD=0xff;wei=1;wei=0;
PORTD=0x00;duan=1;duan=0;
}
void main()
{
init();
while(1)
{
get_tempture();
}
}
|