Sunteți pe pagina 1din 10

# include <16F877A.

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

{ output_low(MOTOR0); // MOTOR0 OFF


line2[4]="O";
line2[5]="F";
line2[6]="F";
}
if (thermal[1]>=30)
{ output_high(MOTOR1); // MOTOR1 ON
line2[11]=" ";
line2[12]="O";
line2[13]="N";
}
else
{ output_low(MOTOR1); // MOTOR1 OFF
line2[11]="O";
line2[12]="F";
line2[13]="F";
}
}
void display_lcd()
{ int8 i;
LCD_Write_Cmd(CURSOR_HOME); // vi tri dau dong 1
for(i=0;i<=15;i++) // 16 ky tu
{
LCD_Write_Chr(line1[i]);
}
LCD_Write_Cmd(CURSOR_LINE2); // vi tri dau dong 2
for(i=0;i<=15;i++) // 16 ky tu
{
LCD_Write_Chr(line2[i]);
}
}
void main(void)
{
int8 i;
signed int16 temperature;
trisd=0x00; // output portd
trisa=0x00; // output portd
porta=0x00;
LCD_Init();
output_float(DQ);

// Set as input. 4k7 pullup on bus.

//FindDevices();

// goi lenh tim thiet bi

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

LCD_data_out(data) (PORTD = (PORTD&0xF0)|(data&0x0F)) // 4BIT cao


MODE_4_BIT
0x28
CLR_SCR
0x01
DISP_ON
0x0C
CURSOR_ON
0x0E
CURSOR_HOME 0x80
CURSOR_LINE2 0xC0

void LCD_Write_Nibble(int8 data)


{
LCD_E = 1;
LCD_data_out(data);
LCD_E = 0;
}
void LCD_Wait_Busy(){
int8 temp;
PORTD |= 0x0F;
LCD_RS = 0;
LCD_RW = 1;
TRISD=0X0F; // 4 bit thap input
do {
LCD_E = 1;
//che do 4 bit phai doc 2 lan
temp = PORTD;
//chi luu 4 bit dau de kiem tra
LCD_E = 0;
//busy_flag
LCD_E = 1;
LCD_E = 0;
}while (temp&0x08);
TRISD=0X00; // 4 bit thap output
}
void LCD_Write_Cmd(int8 cmd){
LCD_Wait_Busy();
LCD_RS = 0;
LCD_RW = 0;
LCD_Write_Nibble(cmd>>4); //goi 4bit cao truoc

LCD_Write_Nibble(cmd); // goi 4bit thap sau


}
void LCD_Write_Chr(int8 chr){
LCD_Wait_Busy();
LCD_RS = 1;
LCD_RW = 0;
LCD_Write_Nibble(chr>>4);
LCD_Write_Nibble(chr);
}
void LCD_Init(){
LCD_RS = 0;
LCD_RW = 0;
LCD_Write_Nibble(MODE_4_BIT>>4); //lan dau tien truyen du lieu, LCD mac dinh la` 8 bit
LCD_Write_Cmd(MODE_4_BIT);
//nen phai set function 2 lan de dieu chinh theo che do
LCD_Write_Cmd(DISP_ON);
//mong muon: 4 bit, 2 line, 5x7 dot
LCD_Write_Cmd(CURSOR_ON);
LCD_Write_Cmd(CLR_SCR);
}
// One Wire bus functions - from Dallas publication AN162
// "Interfacing DS18x20/DS1822 1-wire Temperature Sensor in a Microcontroller
// Environment". Delays calculated from values given for 8051.
// Changed variable name ROM[] to RomBytes[] because ROM is a reserved word
// in version 4 of the CCS compiler.
// Global variables
int8 RomBytes[8];
int8 lastDiscrep = 0;
short doneFlag = 0;
//int8 FoundROM[9][8]; // Table of found ROM codes, 8 bytes for each
int8 FoundROM[2][8]= { {0x28,0x69,0x5b,0xf8,0x01,0x00,0x00,0x68},
// Sensor1 ROM
{0x28,0x03,0x19,0x10,0x03,0x00,0x00,0x14} }; // Sensor2 ROM
int8 numROMs;
int8 dowcrc;
// crc is accumulated in this variable
// crc lookup table
int8 const dscrc_table[] = {
0,94,188,226,97,63,221,131,194,156,126,32,163,253,31,65,
157,195,33,127,252,162,64,30,95,1,227,189,62,96,130,220,
35,125,159,193,66,28,254,160,225,191,93,3,128,222,60,98,
190,224,2,92,223,129,99,61,124,34,192,158,29,67,161,255,
70,24,250,164,39,121,155,197,132,218,56,102,229,187,89,7,
219,133,103,57,186,228,6,88,25,71,165,251,120,38,196,154,
101,59,217,135,4,90,184,230,167,249,27,69,198,152,122,36,
248,166,68,26,153,199,37,123,58,100,134,216,91,5,231,185,
140,210,48,110,237,179,81,15,78,16,242,172,47,113,147,205,
17,79,173,243,112,46,204,146,211,141,111,49,178,236,14,80,
175,241,19,77,206,144,114,44,109,51,209,143,12,82,176,238,
50,108,142,208,83,13,239,177,240,174,76,18,145,207,45,115,

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;

// Reset next flag to false


// Reset the dowcrc

flag = ow_reset();
if (flag||doneFlag)
{
lastDiscrep = 0;

// If no parts return false


// Reset the search

return FALSE;
}
write_byte(0xF0);

// Send SearchROM command

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;

// There are no devices on the one wire bus

// All devices coupled have 0 or 1


// Bit write value for search

// If this discrepancy is before the last discrepancy on a previous


// Next then pick the same as last time.
else
{
if (m < lastDiscrep)
g = ((RomBytes[n] & k) > 0);
// If equal to last pick 1
else
g = (m == lastDiscrep); // If not then pick 0
// If 0 was picked then record position with mask k
if (g == 0) discrepMarker = m;
}
// Isolate bit in ROM[n] with mask k
if (g == 1) RomBytes[n] |= k;
else RomBytes[n] &= ~k;
write_bit(g); // ROM search write
m++;
// Increment bit counter m
k = k << 1; // and shift the bit mask k
// If the mask is 0 then go to new ROM
if (k == 0)
{ // Byte n and reset mask
ow_crc(RomBytes[n]); // Accumulate the crc
n++;
k++;
}
}
} while (n < 8); // Loop through until through all ROM bytes 0-7

if (m < (65||dowcrc)) // If search was unsuccessful then


lastDiscrep = 0; // reset the last Discrepancy to zero
else // Search was successful, so set lastDiscrep, lastOne, nxt
{
lastDiscrep = discrepMarker;
doneFlag = (lastDiscrep == 0);
nxt = TRUE; // Indicates search not yet complete, more parts remain
}
return nxt;
}
//******************************************************************************
// Resets current state of a ROM search and calls Next to find the first device
// on the one wire bus.
int8 First(void)
{
lastDiscrep = 0;
doneFlag = FALSE;
return Next(); // Call Next and return it's return value;
}
//******************************************************************************
void FindDevices(void)
{
int8 m;
if(!ow_reset())
{
if(First()) // Begins when at least one part found
{
numROMs = 0;

//

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]); */

} while (Next() && (numROMs<3)); // Continues until no additional


// devices found.
// numROMs< 2 so luong khong qua 2
}
}
// printf(lcd_putc, "\n");
}
//******************************************************************************
// Sends Match ROM command to bus then device address
// tim ROMs hoat cho phep rom hoat dong
int8 Send_MatchRom(void)
{
int8 i;
if (ow_reset()) return FALSE;
// 0 if device present
write_byte(0x55);
// Match ROM
for (i=0;i<8;i++)
{
write_byte(FoundRom[numROMs][i]); // Send ROM code
}
return TRUE;
}

S-ar putea să vă placă și