Mình viết chương trình sử dụng pic16f887 + ds18b20 xuất nhiệt độ ra lcd. (có gửi file đính kèm..)
link : http://www.mediafire.com/?z421gvz8h7k4b01
http://www.mediafire.com/?h85o48ee0yw5b2d
còn đây là có mình viết trên hitech C.
Do mình mới làm quen với pic nên gặp khó khăn nhờ các bạn xem giúp .Cám ơn!!
//
#include <htc.h>
#include<stdio.h>
#include "lcd.h"
__CONFIG(INTIO&WDTDIS&PWRTEN&MCLREN&UNPROTECT&DUNP ROTECT&BORDIS&IESODIS&FCMDIS&LVPDIS);
__CONFIG(BORV21);
#define _XTAL_FREQ 4000000
#define DQ RC0
//PORTC 0X07
unsigned char ow_reset(void)
// bit ow_reset(void)
{
unsigned char presence;
presence=1;
while(presence)
{
TRISC=0;
DQ = 0; //pull DQ line low
__delay_us(500); // leave it low for about 490us
DQ = 1; // allow line to return high
__delay_us(40); // wait for presence 55 uS
TRISC=1;
presence = DQ; // get presence signal
__delay_us(25); // wait for end of timeslot 316 uS
}
return(presence); // presence signal returned
} // 0=presence, 1 = no part
unsigned char read_bit(void)
{
//unsigned char i=0;
TRISC=0;
DQ = 0;
__delay_us(1);
TRISC=1;
__delay_us(15); // pull DQ low to start timeslot
// DQ=1;
// for (i=0; i<3; i++); // delay 17 us from start of timeslot
return(DQ); // return value of DQ line
}
void write_bit(char bitval)
{
TRISC=0;
DQ=0;
//DQ = Dbit;
__delay_us(15); // delay about 39 uS
if(bitval==1) TRISC=1;
__delay_us(50);
// DQ = 1;
}
// READ_BYTE - reads a byte from the one-wire bus.
//
unsigned char read_byte(void)
{
unsigned char i;
unsigned char value = 0;
for (i=0;i<8;i++)
{
if(read_bit()) value|=0x01<<i; // reads byte in, one byte at a time and then
// shifts it left
__delay_us(150); // wait for rest of timeslot
}
return(value);
}
void write_byte(char val)
{
unsigned char i;
unsigned char temp;
for (i=0; i<8; i++) // writes byte, one bit at a time
{
temp = val>>i; // shifts val right 'i' spaces
temp &= 0x01; // copy that bit to temp
write_bit(temp); // write bit in temp into
}
__delay_us(100);
}
void main (void)
{
unsigned char x;
TRISC=0;
RC0=0;
ANSEL=ANSELH=0;
ow_reset();
write_byte(0xCC); // skip ROM
write_byte(0x44); // Start conversion
__delay_us(100);
ow_reset();
write_byte(0xCC);
x = read_byte();
lcd_init();
lcd_gotoxy(2,1);
printf("temp = %u",x);
__delay_us(100000);
}
void putch(char c)
{
lcd_putc(c);
}
link : http://www.mediafire.com/?z421gvz8h7k4b01
http://www.mediafire.com/?h85o48ee0yw5b2d
còn đây là có mình viết trên hitech C.
Do mình mới làm quen với pic nên gặp khó khăn nhờ các bạn xem giúp .Cám ơn!!
//
#include <htc.h>
#include<stdio.h>
#include "lcd.h"
__CONFIG(INTIO&WDTDIS&PWRTEN&MCLREN&UNPROTECT&DUNP ROTECT&BORDIS&IESODIS&FCMDIS&LVPDIS);
__CONFIG(BORV21);
#define _XTAL_FREQ 4000000
#define DQ RC0
//PORTC 0X07
unsigned char ow_reset(void)
// bit ow_reset(void)
{
unsigned char presence;
presence=1;
while(presence)
{
TRISC=0;
DQ = 0; //pull DQ line low
__delay_us(500); // leave it low for about 490us
DQ = 1; // allow line to return high
__delay_us(40); // wait for presence 55 uS
TRISC=1;
presence = DQ; // get presence signal
__delay_us(25); // wait for end of timeslot 316 uS
}
return(presence); // presence signal returned
} // 0=presence, 1 = no part
unsigned char read_bit(void)
{
//unsigned char i=0;
TRISC=0;
DQ = 0;
__delay_us(1);
TRISC=1;
__delay_us(15); // pull DQ low to start timeslot
// DQ=1;
// for (i=0; i<3; i++); // delay 17 us from start of timeslot
return(DQ); // return value of DQ line
}
void write_bit(char bitval)
{
TRISC=0;
DQ=0;
//DQ = Dbit;
__delay_us(15); // delay about 39 uS
if(bitval==1) TRISC=1;
__delay_us(50);
// DQ = 1;
}
// READ_BYTE - reads a byte from the one-wire bus.
//
unsigned char read_byte(void)
{
unsigned char i;
unsigned char value = 0;
for (i=0;i<8;i++)
{
if(read_bit()) value|=0x01<<i; // reads byte in, one byte at a time and then
// shifts it left
__delay_us(150); // wait for rest of timeslot
}
return(value);
}
void write_byte(char val)
{
unsigned char i;
unsigned char temp;
for (i=0; i<8; i++) // writes byte, one bit at a time
{
temp = val>>i; // shifts val right 'i' spaces
temp &= 0x01; // copy that bit to temp
write_bit(temp); // write bit in temp into
}
__delay_us(100);
}
void main (void)
{
unsigned char x;
TRISC=0;
RC0=0;
ANSEL=ANSELH=0;
ow_reset();
write_byte(0xCC); // skip ROM
write_byte(0x44); // Start conversion
__delay_us(100);
ow_reset();
write_byte(0xCC);
x = read_byte();
lcd_init();
lcd_gotoxy(2,1);
printf("temp = %u",x);
__delay_us(100000);
}
void putch(char c)
{
lcd_putc(c);
}
Comment