#include <16f877a.h>
#use delay(clock=20M)
#fuses HS, NOWDT, NOLVP, NOBROWNOUT, NOPROTECT, NOPUT
#use rs232(baud=9600,parity=n,xmit=pin_c6,rcv=pin_c7)
#define DS PIN_A3
int8 led1,led2;
int8 const seg7[10] ={0xC0,0x8F9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
int8 const char9[10] ={'a','b','c','d','e','f','g','h','j','k'};
void reset()
{
output_low(DS);
delay_us( 500 );
output_float(DS);
delay_us( 500 );
output_float(DS);
}
void write(int data)
{
int count;
for (count=0; count<8; ++count)
{
output_low(DS);
delay_us( 2 );
output_bit(DS, shift_right(&data,1,0));
delay_us( 60 );
output_float(DS);
delay_us( 2 );
}
}
unsigned read()
{
int count, data;
for (count=0; count<8; ++count)
{
output_low(DS);
delay_us( 2 );
output_float(DS);
delay_us( 8 );
shift_right(&data,1,input(DS));
delay_us( 120 );
}
return( data );
}
unsigned int CHUYEN(float data)
{
data = data* 0.625;
return(data);
}
void DS18B20()
{
int8 byte2,led1,led2,byte1,dec,decimal,led3,i,j,busy=0;
int16 data;
unsigned char nhiet_do,so_le;
reset();
write(0xCC);
write(0x44);
while (busy == 0)
busy = read();
reset();
write(0xCC);
write(0xBE);
byte1 = read();
byte2 = read();
data=make16(byte2,byte1);
data=data>>4;
data=data&0x0ff;
led2=data/10;
led1=data%10;
decimal=byte1&0x0f;
dec=CHUYEN(decimal);
for(i=0;i<20;i++)
{
[COLOR="red"]printf("D=%u U=%u L=%u",led1, led2,dec); [/COLOR]
}
for(i=0;i<20;i++)
{
for(J=0;J<50;J++)
{
output_low(pin_d0);
output_b(seg7[led2]);
delay_ms(1);
output_high(pin_d0);
output_low(pin_d1);
output_b(seg7[led1]);
output_low(pin_b7);
delay_ms(1);
output_high(pin_d1);
output_low(pin_d2);
output_b(seg7[dec]);
delay_ms(1);
output_high(pin_d2);
output_low(pin_d3);
output_b(0b10011100);// hien thi do
delay_ms(1);
output_high(pin_d3);
output_low(pin_d4);
output_b(0b11000110);// hien thi c
delay_ms(1);
output_high(pin_d4);
}
}
}
void main()
{
SET_TRIS_A(0b00001000);
SET_TRIS_B(0);
OUTPUT_A(0XFF);
OUTPUT_LOW(PIN_A0);
while (1)
{
DS18B20();
}
}