//porta trigger 6 pieces 8 segments LED.
//portc act as the data port of LEDs,portb1 act as control key
//portb2 act as sensor communication port
//g display - or 100
//f display 10
//e display 1
//d display 0.1
//c display 0.01
//b display the letter of c,means degress
#include
const char table0[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xc6,0xff,0x90,0x90,0x90};
const char table1[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};//including point
unsigned long mul_value;
unsigned long rate;
int pass1,pass2;
char a,b,c,d,e,f,g,i,j,k,l,m,high,low,temp;//a,b,c,d,e,f,g is set for display and data conversion
const char *p;
char *o;
void delay(void)
{for(j=18;--j;)
{for(i=18;--i;);
}
}
void display(void)
{b=12;
c=11;
if(temp)g=10;//display the symbol of negative
if(g==0)g=12;//if the bit of hundred is 0,don't display it
o=&g;
PORTA=0x3e;
for(l=6;l>0;l--)
{PORTC=0xff;
m=*o;
if(l==4)p=&table1[m];
else p=&table0[m];
PORTC=*p;
delay();
o-=1;
if(RA5){PORTA<<=1;PORTA+=1;}
}
PORTA=0xff;
}
void bcd_con2(void)//to adjust the converted BCD code,if is more than 5,subtract it with 3
{if(a>5)a-=3; //otherwise do not change it.
if(b>5)b-=3;
if(c>5)c-=3;
if(d>5)d-=3;
if(e>5)e-=3;
if(f>5)f-=3;
if(g>5)g-=3;
}
void bcd_con(void)//to adjust the binary code after moving to left one time,if low four bits add 3 and is more than
{l=k; //seven,let it add 3, otherwise do not change it,than do the same work for high four bits.
l+=0x03;
l&=0x08;
if(l)k+=0x3;
l=k;
l+=0x30;
l&=0x80;
if(l)k+=0x30;
}
void ready_con()//convert the binary code to BCD code
{for(i=32;i>0;--i)
{mul_value<<=1;
asm("rlf _a,f");
asm("rlf _b,f");
asm("rlf _c,f");
asm("rlf _d,f");
k=a;
bcd_con();
a=k;
k=b;
bcd_con();
b=k;
k=c;
bcd_con();
c=k;
k=d;
bcd_con();
d=k;
}
}
void bcd_spe(void)
{d&=0x0f;
g=d;
l=c;
asm("swapf _l,f");
l&=0x0f;
c&=0x0f;
f=l;
e=c;
l=b;
asm("swapf _l,f");
l&=0x0f;
b&=0x0f;
d=l;
c=b;
l=a;
asm("swapf _a,f");
a&=0x0f;
b=a;
l&=0x0f;
a=l;
}
void initialize(void){
TRISA=0x00; //set porta as port of output and trigger LEDs
ADCON1=0x06; //set porta as I/O port
TRISC=0x00; //set portc as data port of output
TRISB=0x0ff; //set portb as port of key and sensor input port
OPTION=0x00;
}
void write_18b20(void)//the register j is set for temporary R to pass the data
{for(m=8;m>0;m--)
{RB2=0;
TRISB2=0;//low level
asm("nop");
asm("nop");
j>>=1;
if(CARRY==1)TRISB2=1;
for(k=26;--k;);
TRISB2=1;
asm("nop");
asm("nop");
}
}
void read_18b20(void)//high,low will save the 16bits data
{high=0;
low=0;
for(k=16;k>0;--k)
{RB2=0;
TRISB2=0;
asm("nop");
asm("nop");
asm("nop");
asm("nop");
TRISB2=1;
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
if(RB2==1)CARRY=1;
else CARRY=0;
asm("rrf _high,f");
asm("rrf _low,f");
for(m=20;m>0;m--);
}
}
void initial_18b20(void)//using register l to flag whether any response occurs from 18b20
{l=0;
RB2=0;
TRISB2=0; //reset 18b20
for(j=250;--j;); //delay 600us
TRISB2=1; //release the bus
for(i=20;--i;); //delay60us
if(RB2==1)l=0; //no response
else l=1; //responded
for(m=160;--m;); //delay480us to meet the time slot
}
void main(void)
{initialize();
rate=625;
while(1)
{display();
initial_18b20();
if(l==0)continue;
j=0x0cc; //skip the rom
write_18b20();
j=0x44; //release the command of conversion
write_18b20();
for(k=250;k>0;--k)
{display();
}
initial_18b20();
if(l==0)continue;
j=0x0cc;
write_18b20();
j=0x0be; //release the command of reading
write_18b20();
read_18b20(); //read the converted data
initial_18b20();
if(l==0)continue;
temp=0;
pass1=0;
mul_value=0;
for(i=16;i>0;--i)
{asm("rlf _low,f");
asm("rlf _high,f");
if(CARRY)
{pass1<<=1;
pass1+=1;
}
else pass1<<=1;
}
pass2=pass1;
pass2<<=1;
if(CARRY)
{pass1=~pass1;
pass1=pass1+1;
temp=1; // negative temperature
}
mul_value=pass1;
mul_value*=rate;
a=b=c=d=e=f=g=0;
ready_con();
bcd_spe();
bcd_con2();
}
}