Documente Academic
Documente Profesional
Documente Cultură
h>
# include<define_16F877A.h>
#fuses NOWDT,PUT,HS,NOPROTECT,NOLVP
# use delay(clock=4Mhz)
#define DQ PIN_B0
// One Wire Bus pin assignment
#define MOTOR0 PIN_A0
// One Wire Bus pin assignment
#define MOTOR1 PIN_A1
// One Wire Bus pin assignment
#include "lcd_library.c"
#include "onewire.c"
#use fast_io(d)
#use fast_io(a)
char line1[17]={"T1: T2: "},line2[17]={"M1: M2: "}, bcd_ascii[]={"0123456789"};
int8 tram,chuc,donvi;
int8 scratch[9];
// 9 data byte trong bo nho
signed int8 thermal[2]; // luu tru nhiet do
void hex_bcd_line1()
{
int8 i,value;
for(i=0;i<=1;i++)
{
value=abs(thermal[i]);
tram=value/100;
value=value%100;
chuc=value/10;
donvi=value%10;
if (tram>0)
line1[4+i*7]=bcd_ascii[tram];
else
line1[4+i*7]=' ';
if(thermal[i]<0) line1[4+i*7]='-';
line1[5+i*7]=bcd_ascii[chuc];
line1[6+i*7]=bcd_ascii[donvi];
}
}
void motor_status_line2()
{
if (thermal[0]>=30)
{ output_high(MOTOR0); // MOTOR0 ON
line2[4]=" ";
line2[5]="O";
line2[6]="N";
}
else
//FindDevices();
while(1)
{
if (!ow_reset())
// If a device is present
{
for (numROMs=0;numROMs<=1;numROMs++)
{
Send_MatchRom();
write_byte(0x44); // Temperature convert command
output_float(DQ);
delay_ms(750); // Max. conv. time is 750mS for 12 bit
}
ow_reset();
// Now get the device raw temperature data using Match ROM with the
// addresses obtained with FindDevices().
// If the received crc is same as calculated then data is valid.
// Scale and round to nearest degree C.
// Scaling is 0.0625 (1/16) deg.C/bit with default 12 bit resolution.
// Round by adding half denominator for positive temperatures and
// subtracting half denominator for negative temperatures.
for (numROMs=0;numROMs<=1;numROMs++)
{
if (Send_MatchRom())
{
write_byte(0xBE); // Read scratch pad command
dowcrc = 0;
// Get the data bytes
for (i=0;i<=7;i++)
{
scratch[i] = read_byte();
ow_crc(scratch[i]);
}
scratch[8] = read_byte(); // Get crc byte
ow_reset();
// If calculated crc from incoming bytes equal to crc byte
// then data is valid.
if (scratch[8] == dowcrc)
{
temperature = (signed int16) make16(scratch[1],scratch[0]);
temperature = temperature/16; // xoay xuong 4 bit
thermal[numROMs]= make8(temperature,0); // lay byte 0
}
}
}
}
hex_bcd_line1();
motor_status_line2();
display_lcd();
}
}
/// thu vien lcd 14 che do 4 bit
/// dung portd nhu khoi tao khai bao bien
#define LCD_D4 RD0
#define LCD_D5 RD1
#define LCD_D6 RD2
#define LCD_D7 RD3
#define LCD_E RD5
#define LCD_RW RD6
#define LCD_RS RD7
#define
#define
#define
#define
#define
#define
#define
202,148,118,40,171,245,23,73,8,86,180,234,105,55,213,139,
87,9,235,181,54,104,138,212,149,203,41,119,244,170,72,22,
233,183,85,11,136,214,52,106,43,117,151,201,74,20,246,168,
116,42,200,150,21,75,169,247,182,232,10,84,215,137,107,53
};
// Returns 0 for one wire device presence, 1 for none
int8 ow_reset(void)
{
int8 presence;
output_low(DQ);
delay_us(488);
// Min. 480uS
output_float(DQ);
delay_us(72);
// Takes 15 to 60uS for devices to respond
presence = input(DQ);
delay_us(424);
// Wait for end of timeslot
return(presence);
}
//******************************************************************************
// Read bit on one wire bus
int8 read_bit(void)
{
output_low(DQ);
delay_us(1);
// Added, 1uS min. Code relied on 8051 being slow.
output_float(DQ);
delay_us(12);
// Read within 15uS from start of time slot
return(input(DQ));
}
//******************************************************************************
void write_bit(int8 bitval)
{
output_low(DQ);
if(bitval == 1) {
delay_us(1);
// 1uS min. Code relied on 8051 being slow.
output_float(DQ);
}
delay_us(105);
// Wait for end of timeslot
output_float(DQ);
}
//******************************************************************************
int8 read_byte(void)
{
int8 i;
int8 val = 0;
for(i=0;i<8;i++)
{
if(read_bit()) val |= (0x01 << i);
delay_us(120); // To finish time slot
}
return val;
}
//******************************************************************************
void write_byte(int8 val)
{
int8 i;
int8 temp;
for (i=0;i<8;i++)
{
temp = val >> i;
temp &= 0x01;
write_bit(temp);
}
delay_us(105);
}
//******************************************************************************
// One wire crc
int8 ow_crc(int8 x)
{
dowcrc = dscrc_table[dowcrc^x];
return dowcrc;
}
//******************************************************************************
// Searches for the next device on the one wire bus. If there are no more
// devices on the bus then false is returned.
//
int8 Next(void)
{
int8 m = 1;
// ROM Bit index
int8 n = 0;
// ROM Byte index
int8 k = 1;
// Bit mask
int8 x = 0;
int8 discrepMarker = 0;
int8 g;
// Output bit
int8 nxt;
// Return value
short flag;
nxt = FALSE;
dowcrc = 0;
flag = ow_reset();
if (flag||doneFlag)
{
lastDiscrep = 0;
return FALSE;
}
write_byte(0xF0);
do
{
x = 0;
if (read_bit() == 1) x = 2;
delay_us(120);
if (read_bit() == 1) x |= 1; // And it's complement
if (x == 3)
break;
else
{
if (x > 0)
g = x >> 1;
//
do
{
numROMs++;
for (m=0;m<8;m++)
{
FoundROM[numROMs][m] = RomBytes[m];
}
numROMs++;
/* lcd_putc("\f"); // them vaoclearn man hinh
printf(lcd_putc,"Device No.%u address ",numROMs);
lcd_putc("\n"); // hien dong 2
printf(lcd_putc, "%X%X%X%X%X%X%X%X\n\r",
FoundROM[numROMs][7],FoundROM[numROMs][6],FoundROM[numROMs][5],
FoundROM[numROMs][4],FoundROM[numROMs][3],FoundROM[numROMs][2],
FoundROM[numROMs][1],FoundROM[numROMs][0]); */